This repository has been archived by the owner on Aug 21, 2023. It is now read-only.
forked from faceit/go-steam
/
packet.go
74 lines (56 loc) · 1.52 KB
/
packet.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
package gc
import (
"bytes"
pb "github.com/13k/go-steam-resources/protobuf/steam"
"github.com/13k/go-steam-resources/steamlang"
"github.com/13k/go-steam/protocol"
"google.golang.org/protobuf/proto"
)
type PacketHandler interface {
HandleGCPacket(*Packet)
}
// An incoming, partially unread message from the Game Coordinator.
type Packet struct {
AppID uint32
MsgType uint32
IsProto bool
GCName string
Body []byte
TargetJobID protocol.JobID
}
func NewPacket(wrapper *pb.CMsgGCClient) (*Packet, error) {
packet := &Packet{
AppID: wrapper.GetAppid(),
MsgType: wrapper.GetMsgtype(),
GCName: wrapper.GetGcname(),
}
r := bytes.NewReader(wrapper.GetPayload())
if steamlang.IsProto(wrapper.GetMsgtype()) {
packet.MsgType = packet.MsgType & steamlang.EMsgMask
packet.IsProto = true
header := steamlang.NewMsgGCHdrProtoBuf()
err := header.Deserialize(r)
if err != nil {
return nil, err
}
packet.TargetJobID = protocol.JobID(header.Proto.GetJobidTarget())
} else {
header := steamlang.NewMsgGCHdr()
if err := header.Deserialize(r); err != nil {
return nil, err
}
packet.TargetJobID = protocol.JobID(header.TargetJobID)
}
body := make([]byte, r.Len())
if _, err := r.Read(body); err != nil {
return nil, err
}
packet.Body = body
return packet, nil
}
func (g *Packet) ReadProtoMsg(body proto.Message) error {
return proto.Unmarshal(g.Body, body)
}
func (g *Packet) ReadMsg(body protocol.StructMessageBody) error {
return body.Deserialize(bytes.NewReader(g.Body))
}