/
DispatcherClientProxy.go
81 lines (68 loc) · 2.03 KB
/
DispatcherClientProxy.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
package main
import (
"net"
"fmt"
"github.com/xiaonanln/goworld/engine/consts"
"github.com/xiaonanln/goworld/engine/gwioutil"
"github.com/xiaonanln/goworld/engine/gwlog"
"github.com/xiaonanln/goworld/engine/netutil"
"github.com/xiaonanln/goworld/engine/post"
"github.com/xiaonanln/goworld/engine/proto"
)
type dispatcherClientProxy struct {
*proto.GoWorldConnection
owner *DispatcherService
gameid uint16
gateid uint16
}
func newDispatcherClientProxy(owner *DispatcherService, _conn net.Conn) *dispatcherClientProxy {
conn := netutil.NetConnection{_conn}
gwc := proto.NewGoWorldConnection(netutil.NewBufferedConnection(conn), false, "")
dcp := &dispatcherClientProxy{
GoWorldConnection: gwc,
owner: owner,
}
dcp.SetAutoFlush(consts.DISPATCHER_CLIENT_PROXY_WRITE_FLUSH_INTERVAL)
return dcp
}
func (dcp *dispatcherClientProxy) serve() {
// Serve the dispatcher client from server / gate
defer func() {
dcp.Close()
post.Post(func() {
dcp.owner.handleDispatcherClientDisconnect(dcp)
})
err := recover()
if err != nil && !netutil.IsConnectionError(err) {
gwlog.TraceError("Client %s paniced with error: %v", dcp, err)
}
}()
gwlog.Infof("New dispatcher client: %s", dcp)
for {
var msgtype proto.MsgType
pkt, err := dcp.Recv(&msgtype)
if err != nil {
if gwioutil.IsTimeoutError(err) {
continue
} else if netutil.IsConnectionError(err) {
break
}
gwlog.Panic(err)
}
//
//if consts.DEBUG_PACKETS {
// gwlog.Debugf("%s.RecvPacket: msgtype=%v, payload=%v", dcp, msgtype, pkt.Payload())
//}
// pass the packet to the dispatcher service
dcp.owner.messageQueue <- dispatcherMessage{dcp, proto.Message{msgtype, pkt}}
}
}
func (dcp *dispatcherClientProxy) String() string {
if dcp.gameid > 0 {
return fmt.Sprintf("dispatcherClientProxy<game%d|%s>", dcp.gameid, dcp.RemoteAddr())
} else if dcp.gateid > 0 {
return fmt.Sprintf("dispatcherClientProxy<gate%d|%s>", dcp.gateid, dcp.RemoteAddr())
} else {
return fmt.Sprintf("dispatcherClientProxy<%s>", dcp.RemoteAddr())
}
}