forked from PretendoNetwork/nex-protocols-go
-
Notifications
You must be signed in to change notification settings - Fork 1
/
messaging.go
75 lines (57 loc) · 2.19 KB
/
messaging.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
package nexproto
import (
"log"
nex "github.com/ihatecompvir/nex-go"
)
const (
MessagingProtocolID = 0x17
GetMessageHeaders = 0x3
)
type MessagingProtocol struct {
server *nex.Server
ConnectionIDCounter *nex.Counter
GetMessageHeadersHandler func(err error, client *nex.Client, callID uint32, pid uint32, recipientType uint32, rangeOffset uint32, rangeSize uint32)
}
func (unknownProtocol *MessagingProtocol) Setup() {
nexServer := unknownProtocol.server
nexServer.On("Data", func(packet nex.PacketInterface) {
request := packet.RMCRequest()
if MessagingProtocolID == request.ProtocolID() {
switch request.MethodID() {
case GetMessageHeaders:
go unknownProtocol.handleGetMessageHeaders(packet)
default:
log.Printf("Unsupported Messaging method ID: %#v\n", request.MethodID())
}
}
})
}
func (messagingProtocol *MessagingProtocol) GetMessageHeaders(handler func(err error, client *nex.Client, callID uint32, pid uint32, recipientType uint32, rangeOffset uint32, rangeSize uint32)) {
messagingProtocol.GetMessageHeadersHandler = handler
}
func (messagingProtocol *MessagingProtocol) handleGetMessageHeaders(packet nex.PacketInterface) {
if messagingProtocol.GetMessageHeadersHandler == nil {
log.Println("[Warning] MessagingProtocol::GetMessageHeadersHandler not implemented")
go respondNotImplemented(packet, MessagingProtocolID)
return
}
client := packet.Sender()
request := packet.RMCRequest()
callID := request.CallID()
parameters := request.Parameters()
parametersStream := NewStreamIn(parameters, messagingProtocol.server)
pid := parametersStream.ReadUInt32LE()
recipientType := parametersStream.ReadUInt32LE() // 1 = PID, 2 = gathering ID
rangeOffset := parametersStream.ReadUInt32LE()
rangeSize := parametersStream.ReadUInt32LE()
go messagingProtocol.GetMessageHeadersHandler(nil, client, callID, pid, recipientType, rangeOffset, rangeSize)
}
// NewMessagingProtocol returns a new MessagingProtocol
func NewMessagingProtocol(server *nex.Server) *MessagingProtocol {
messagingProtocol := &MessagingProtocol{
server: server,
ConnectionIDCounter: nex.NewCounter(10),
}
messagingProtocol.Setup()
return messagingProtocol
}