This repository has been archived by the owner on Aug 21, 2023. It is now read-only.
forked from faceit/go-steam
/
message.go
136 lines (110 loc) · 2.75 KB
/
message.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package gc
import (
"io"
"github.com/13k/go-steam-resources/steamlang"
"github.com/13k/go-steam/protocol"
"google.golang.org/protobuf/proto"
)
// Message represents an outgoing message to the Game Coordinator.
type Message interface {
protocol.Serializer
IsProto() bool
GetAppID() uint32
GetMsgType() uint32
GetTargetJobID() protocol.JobID
SetTargetJobID(protocol.JobID)
GetSourceJobID() protocol.JobID
SetSourceJobID(protocol.JobID)
}
type ProtoMessage struct {
AppID uint32
Header *steamlang.MsgGCHdrProtoBuf
Body proto.Message
}
var _ Message = (*ProtoMessage)(nil)
func NewProtoMessage(appID, msgType uint32, body proto.Message) *ProtoMessage {
hdr := steamlang.NewMsgGCHdrProtoBuf()
hdr.Msg = msgType
return &ProtoMessage{
AppID: appID,
Header: hdr,
Body: body,
}
}
func (g *ProtoMessage) IsProto() bool {
return true
}
func (g *ProtoMessage) GetAppID() uint32 {
return g.AppID
}
func (g *ProtoMessage) GetMsgType() uint32 {
return g.Header.Msg
}
func (g *ProtoMessage) GetTargetJobID() protocol.JobID {
return protocol.JobID(g.Header.Proto.GetJobidTarget())
}
func (g *ProtoMessage) SetTargetJobID(job protocol.JobID) {
g.Header.Proto.JobidTarget = proto.Uint64(uint64(job))
}
func (g *ProtoMessage) GetSourceJobID() protocol.JobID {
return protocol.JobID(g.Header.Proto.GetJobidSource())
}
func (g *ProtoMessage) SetSourceJobID(job protocol.JobID) {
g.Header.Proto.JobidSource = proto.Uint64(uint64(job))
}
func (g *ProtoMessage) Serialize(w io.Writer) error {
err := g.Header.Serialize(w)
if err != nil {
return err
}
body, err := proto.Marshal(g.Body)
if err != nil {
return err
}
_, err = w.Write(body)
return err
}
type StructMessage struct {
AppID uint32
MsgType uint32
Header *steamlang.MsgGCHdr
Body protocol.Serializer
}
var _ Message = (*StructMessage)(nil)
func NewStructMessage(appID, msgType uint32, body protocol.Serializer) *StructMessage {
return &StructMessage{
AppID: appID,
MsgType: msgType,
Header: steamlang.NewMsgGCHdr(),
Body: body,
}
}
func (g *StructMessage) GetMsgType() uint32 {
return g.MsgType
}
func (g *StructMessage) GetAppID() uint32 {
return g.AppID
}
func (g *StructMessage) IsProto() bool {
return false
}
func (g *StructMessage) GetTargetJobID() protocol.JobID {
return protocol.JobID(g.Header.TargetJobID)
}
func (g *StructMessage) SetTargetJobID(job protocol.JobID) {
g.Header.TargetJobID = uint64(job)
}
func (g *StructMessage) GetSourceJobID() protocol.JobID {
return protocol.JobID(g.Header.SourceJobID)
}
func (g *StructMessage) SetSourceJobID(job protocol.JobID) {
g.Header.SourceJobID = uint64(job)
}
func (g *StructMessage) Serialize(w io.Writer) error {
err := g.Header.Serialize(w)
if err != nil {
return err
}
err = g.Body.Serialize(w)
return err
}