-
Notifications
You must be signed in to change notification settings - Fork 0
/
packet.go
34 lines (27 loc) · 899 Bytes
/
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
package packet
import (
"github.com/gitfyu/mable/internal/protocol"
)
// Inbound represents a packet sent from the client.
type Inbound interface {
UnmarshalPacket(r protocol.Reader) error
}
// Outbound represents a packet sent from the server.
type Outbound interface {
PacketID() uint
MarshalPacket(w protocol.Writer) error
}
var idToPacket = make(map[uint]func() Inbound)
// RegisterInbound registers a supplier function that creates an Inbound packet for a specific protocol.State and id.
func RegisterInbound(state protocol.State, id uint, supplier func() Inbound) {
idToPacket[id<<2|uint(state)] = supplier
}
// createInbound creates an Inbound packet for the specified protocol.State and ID, or nil in case no instance could be
// created.
func createInbound(state protocol.State, id uint) Inbound {
f, ok := idToPacket[id<<2|uint(state)]
if !ok {
return nil
}
return f()
}