-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
handlers.go
99 lines (82 loc) · 2.46 KB
/
handlers.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
package world
import (
"fmt"
"github.com/paalgyula/summit/pkg/summit/world/packets"
"github.com/paalgyula/summit/pkg/wow"
"github.com/rs/zerolog/log"
)
type (
handlePacket = func(wow.PacketData)
handlePacket2 = func(*wow.Packet)
handleCommand = func()
)
// ExternalPacketFunc register packet for external processing.
type ExternalPacketFunc = func(*WorldSession, wow.OpCode, []byte)
type PacketHandler struct {
Opcode wow.OpCode
Handler any
}
func (gc *WorldSession) RegisterHandlers(handlers ...PacketHandler) {
if len(handlers) < int(wow.NumMsgTypes) {
origSize := len(handlers)
additional := int(wow.NumMsgTypes) - len(handlers)
for i := 0; i < additional; i++ {
handlers = append(handlers, PacketHandler{
Opcode: wow.OpCode(origSize + i + 1),
Handler: "none",
})
}
}
for _, oh := range handlers {
if len(packets.OpcodeTable) <= int(oh.Opcode) {
log.Printf("opcode table too short: 0x%03x", oh.Opcode)
continue
}
packets.OpcodeTable.Handle(oh.Opcode, oh.Handler)
}
packets.OpcodeTable.Handle(wow.ClientPing, gc.PingHandler)
packets.OpcodeTable.Handle(wow.ClientAuthSession, gc.AuthSessionHandler)
packets.OpcodeTable.Handle(wow.ClientCharEnum, gc.SendCharacterEnum)
packets.OpcodeTable.Handle(wow.ClientCharCreate, gc.CreateCharacter)
packets.OpcodeTable.Handle(wow.ClientRealmSplit, gc.HandleRealmSplit)
}
func (gc *WorldSession) Handle(pkt *wow.Packet) {
wow.GetPacketDumper().Write(pkt.Opcode(), pkt.Bytes())
handle := packets.OpcodeTable.Get(pkt.Opcode())
if handle == nil {
// return errors.New("no handler record found")
gc.log.Warn().Msgf("no handler record found: 0x%04x", pkt.OpCode())
return
}
switch t := handle.Handler.(type) {
case string:
gc.log.Warn().
Str("packet", pkt.Opcode().String()).
Str("handler", t).
Msg("handler defined as string")
case handlePacket:
t(pkt.Bytes())
case handlePacket2: // func(*wow.Packet)
t(pkt)
case handleCommand:
t()
case ExternalPacketFunc:
t(gc, pkt.Opcode(), pkt.Bytes())
default:
gc.log.Error().Msgf("handler function is not defined: %s", t)
gc.log.Error().
Type("packet", pkt.Opcode().String()).
Str("opcode", fmt.Sprintf("0x%04x", pkt.OpCode())).
Str("handler", fmt.Sprintf("%+v %T", handle, handle.Handler)).
Msgf("handler type not handled")
}
// switch oc {
// case ClientPing:
// gc.PingHandler()
// case ClientAuthSession:
// gc.AuthSessionHandler(data)
// case ClientCharEnum:
// gc.ListCharacters()
// default:
// }
}