-
Notifications
You must be signed in to change notification settings - Fork 0
/
msg.go
128 lines (106 loc) · 2.65 KB
/
msg.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package cluster
import (
"encoding/gob"
"errors"
"fmt"
"sync/atomic"
"github.com/eric2918/leaf/chanrpc"
"github.com/eric2918/leaf/conf"
"github.com/eric2918/leaf/log"
lgob "github.com/eric2918/leaf/network/gob"
)
var (
Processor = lgob.NewProcessor()
)
const (
callNotForResult = iota
callForResult
)
func init() {
gob.Register([]interface{}{})
}
type S2S_NotifyServerName struct {
ServerName string
}
type S2S_HeartBeat struct {
}
type S2S_RequestMsg struct {
RequestID uint32
MsgID interface{}
CallType uint8
Args []interface{}
}
type S2S_ResponseMsg struct {
RequestID uint32
Ret interface{}
Err string
}
func handleNotifyServerName(args []interface{}) {
msg := args[0].(*S2S_NotifyServerName)
agent := args[1].(*Agent)
addAgent(msg.ServerName, agent)
}
func handleHeartBeat(args []interface{}) {
agent := args[1].(*Agent)
atomic.StoreInt32(&agent.heartBeatWaitTimes, 0)
}
func handleRequestMsg(args []interface{}) {
recvMsg := args[0].(*S2S_RequestMsg)
agent := args[1].(*Agent)
sendMsg := &S2S_ResponseMsg{RequestID: recvMsg.RequestID}
if closing && recvMsg.CallType == callForResult {
sendMsg.Err = fmt.Sprintf("%v server is closing", conf.ServerName)
agent.WriteMsg(sendMsg)
return
}
msgID := recvMsg.MsgID
client, ok := routeMap[msgID]
if !ok {
err := fmt.Sprintf("%v msg is not set route", msgID)
log.Error(err)
if recvMsg.CallType == callForResult {
sendMsg.Err = err
agent.WriteMsg(sendMsg)
}
return
}
args = recvMsg.Args
if recvMsg.CallType == callNotForResult {
args = append(args, nil)
client.RpcCall(msgID, args...)
} else {
sendMsgFunc := func(ret *chanrpc.RetInfo) {
sendMsg.Ret = ret.Ret
if ret.Err != nil {
sendMsg.Err = ret.Err.Error()
}
agent.WriteMsg(sendMsg)
}
args = append(args, sendMsgFunc)
client.RpcCall(msgID, args...)
}
}
func handleResponseMsg(args []interface{}) {
msg := args[0].(*S2S_ResponseMsg)
agent := args[1].(*Agent)
request := agent.popRequest(msg.RequestID)
if request == nil {
log.Error("%v: request id %v is not exist", agent.ServerName, msg.RequestID)
return
}
ret := &chanrpc.RetInfo{Ret: msg.Ret, Cb: request.cb}
if msg.Err != "" {
ret.Err = errors.New(msg.Err)
}
request.chanRet <- ret
}
func init() {
Processor.Register(&S2S_NotifyServerName{})
Processor.Register(&S2S_HeartBeat{})
Processor.Register(&S2S_RequestMsg{})
Processor.Register(&S2S_ResponseMsg{})
Processor.SetHandler(&S2S_NotifyServerName{}, handleNotifyServerName)
Processor.SetHandler(&S2S_HeartBeat{}, handleHeartBeat)
Processor.SetHandler(&S2S_RequestMsg{}, handleRequestMsg)
Processor.SetHandler(&S2S_ResponseMsg{}, handleResponseMsg)
}