-
Notifications
You must be signed in to change notification settings - Fork 2
/
event.go
54 lines (49 loc) · 1.39 KB
/
event.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
package channel
import (
"github.com/juju/errors"
"github.com/ngaut/log"
"github.com/sryanyuan/ForeverMS/core/consts"
"github.com/sryanyuan/ForeverMS/core/maplepacket"
"github.com/sryanyuan/ForeverMS/core/netio"
"github.com/sryanyuan/ForeverMS/core/packet79"
)
func (s *ChannelServer) handleConnEvent(evt *netio.ConnEvent) error {
switch evt.Type {
case netio.CEConnected:
{
return errors.Trace(s.handleEvtConnected(evt))
}
case netio.CEDisconnected:
{
return errors.Trace(s.handleEvtDisconnected(evt))
}
case netio.CERecv:
{
return errors.Trace(s.handleEvtRecv(evt))
}
}
return nil
}
func (s *ChannelServer) handleEvtConnected(evt *netio.ConnEvent) error {
// Once connected, send handshake response
evt.Conn.Send(packet79.NewHello(consts.ServerVersion,
evt.Conn.GetSendCipher().GetKey(),
evt.Conn.GetRecvCipher().GetKey(),
s.config.TestServer))
return nil
}
func (s *ChannelServer) handleEvtDisconnected(evt *netio.ConnEvent) error {
return nil
}
func (s *ChannelServer) handleEvtRecv(evt *netio.ConnEvent) error {
reader := maplepacket.NewReader(&evt.Packet)
opcode := reader.ReadInt16()
// Dispatch packet with opcode
handler, ok := s.packetDispatchMap[opcode]
if ok && nil != handler {
log.Debugf("Handle opcode 0x%04x", opcode)
return errors.Trace(handler(evt.Conn, &reader))
}
log.Warningf("Opcode %d do not setup packet handler", opcode)
return nil
}