Skip to content

Commit 65d522e

Browse files
committed
rpc增加失败回调
1 parent 8c58c95 commit 65d522e

File tree

12 files changed

+52
-25
lines changed

12 files changed

+52
-25
lines changed

core/liFace/iconnect.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ type IConnection interface {
2626
RemoteAddr() net.Addr
2727

2828
//直接将Message数据发送数据给远程的TCP客户端
29-
RpcCall(msgName string, data []byte, f func(rsp IRespond)) error
29+
RpcCall(msgName string, data []byte, success func(rsp IRespond), fail func(rsp IRespond)) error
3030
RpcReply(msgName string, seq uint32, data []byte) error
3131
RpcPush(msgName string, data []byte) error
3232
CheckRpc(seq uint32, rsp IMessage) bool

core/liNet/connection.go

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@ import (
1313
)
1414

1515
type connReq struct {
16-
function func(rsp liFace.IRespond)
17-
req liFace.IRequest
18-
time int64
16+
successFun func(rsp liFace.IRespond)
17+
failFun func(rsp liFace.IRespond)
18+
req liFace.IRequest
19+
time int64
1920
}
2021

2122
type Connection struct {
@@ -220,7 +221,7 @@ func (c *Connection) RemoteAddr() net.Addr {
220221

221222

222223
//直接将Message数据发送数据给远程的TCP客户端
223-
func (c *Connection) RpcCall(msgName string, data []byte, f func(rsp liFace.IRespond)) error {
224+
func (c *Connection) RpcCall(msgName string, data []byte, success func(rsp liFace.IRespond), fail func(rsp liFace.IRespond)) error {
224225
c.rpcLock.Lock()
225226
defer c.rpcLock.Unlock()
226227

@@ -234,9 +235,10 @@ func (c *Connection) RpcCall(msgName string, data []byte, f func(rsp liFace.IRes
234235
if err == nil{
235236
r := Request{msg:m,conn:c}
236237
rpc := connReq{
237-
function: f,
238-
req: &r,
239-
time: time.Now().UnixNano(),
238+
successFun: success,
239+
failFun: fail,
240+
req: &r,
241+
time: time.Now().UnixNano(),
240242
}
241243
c.rpcMap[c.lastSeq] = rpc
242244
}
@@ -284,8 +286,8 @@ func (c *Connection) CheckRpc(seq uint32, rsp liFace.IMessage) bool{
284286
return false
285287
}else{
286288
r := Respond{msg:rsp, req:rpc.req}
287-
if rpc.function != nil{
288-
rpc.function(&r)
289+
if rpc.successFun != nil{
290+
rpc.successFun(&r)
289291
}
290292
delete(c.rpcMap, seq)
291293
return true
@@ -309,8 +311,8 @@ func (c *Connection) checkTimeOut(){
309311
msg.SetMsgName(rpc.req.GetMessage().GetMsgName())
310312
msg.SetSeq(rpc.req.GetMessage().GetSeq())
311313
r := Respond{msg:&msg, req:rpc.req}
312-
if rpc.function != nil{
313-
rpc.function(&r)
314+
if rpc.failFun != nil{
315+
rpc.failFun(&r)
314316
}
315317
delete(c.rpcMap, rpc.req.GetMessage().GetSeq())
316318
utils.Log.Info("rpc %s timeout", rpc.req.GetMessage().GetMsgName())

core/liNet/msghandler.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ func (mh *MsgHandle) DoMsgHandler(request liFace.IRequest, respond liFace.IMessa
6464
//fmt.Println("DoMsgHandler reflect value",v)
6565
method := v.MethodByName(funcName)
6666
if method.IsValid() == false {
67-
utils.Log.Warn("DoMsgHandler warning %s function not found",funcName)
67+
utils.Log.Warn("DoMsgHandler warning %s successFun not found",funcName)
6868
}else{
6969
isFound = true
7070
router := handler.Value.(liFace.IRouter)

gateserver.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ func routerToTarget(wsConn* liNet.WsConnection, msgName string, msgProxyId strin
159159
isLive = true
160160
proxyClient.GetConn().SetProperty("gateConn", wsConn)
161161
proxyClient.GetConn().SetProperty("proxy", msgProxyId)
162-
proxyClient.GetConn().RpcCall(msgName, sendData, gate.Router.Handle)
162+
proxyClient.GetConn().RpcCall(msgName, sendData, gate.Router.Handle, gate.Router.HandleFail)
163163
}
164164
}else{
165165

proto/errcode.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package proto
33
const (
44
Code_Success = iota //0
55
Code_Illegal
6-
Code_User_Not_Exist
6+
Code_ReqFail
77
Code_User_Error //账号或密码错误
88
Code_User_Exist
99
Code_User_Forbid

server/app/loginclient.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ func loginPingTimer(v ...interface{}) {
7272

7373
data ,err := json.Marshal(info)
7474
if err == nil{
75-
conn.RpcCall(proto.SystemPing, data, nil)
75+
conn.RpcCall(proto.SystemPing, data, nil, nil)
7676
}else{
7777
utils.Log.Info("loginPingTimer error:%s", err.Error())
7878
}

server/app/masterclient.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ func mPingTimer(v ...interface{}) {
8787

8888
data ,err := json.Marshal(info)
8989
if err == nil{
90-
conn.RpcCall(proto.SystemPing, data, nil)
90+
conn.RpcCall(proto.SystemPing, data, nil, nil)
9191
}else{
9292
utils.Log.Info("mReportTimer error:%s", err.Error())
9393
}
@@ -109,7 +109,7 @@ func mReportTimer(v ...interface{}) {
109109

110110
data ,err := json.Marshal(info)
111111
if err == nil{
112-
conn.RpcCall(proto.SystemServerInfoReport, data, nil)
112+
conn.RpcCall(proto.SystemServerInfoReport, data, nil, nil)
113113
}else{
114114
utils.Log.Info("mReportTimer error:%s", err.Error())
115115
}
@@ -124,7 +124,7 @@ func mServerListTimer(v ...interface{}) {
124124

125125
data ,err := json.Marshal(info)
126126
if err == nil{
127-
conn.RpcCall(proto.SystemServerListReq, data, MClientRouter.ServerListAck)
127+
conn.RpcCall(proto.SystemServerListReq, data, MClientRouter.ServerListAck, nil)
128128
}else{
129129
utils.Log.Info("mServerListTimer error:%s", err.Error())
130130
}

server/game/enter.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ func (s *enterGame) EveryThingHandle(req liFace.IRequest, rsp liFace.IMessage) {
8181
}else{
8282
v := msg.GetMsgName()
8383
fmt.Println(v)
84-
req.GetConnection().RpcCall(proto.AuthError, nil, nil)
84+
req.GetConnection().RpcCall(proto.AuthError, nil, nil, nil)
8585
}
8686
}
8787

server/gate/gate.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ func (g*gate) Reconnect(wsConn* liNet.WsConnection, handshakeId string) string{
159159
pack.UserId = userId
160160
pack.Type = proto.UserOnline
161161
data, _ := json.Marshal(pack)
162-
c.RpcCall(proto.SystemUserOnOrOffReq, data, nil)
162+
c.RpcCall(proto.SystemUserOnOrOffReq, data, nil, nil)
163163
}
164164
}
165165
}
@@ -201,7 +201,7 @@ func (g*gate) ConnectExit(wsConn* liNet.WsConnection){
201201
pack.UserId = userId
202202
pack.Type = proto.UserOffline
203203
data, _ := json.Marshal(pack)
204-
c.RpcCall(proto.SystemUserOnOrOffReq, data, nil)
204+
c.RpcCall(proto.SystemUserOnOrOffReq, data, nil, nil)
205205
}
206206
}
207207
}

server/gate/gaterouter.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,28 @@ func (s *router) Handle(rsp liFace.IRespond) {
7676
}
7777

7878
}
79+
80+
func (s *router) HandleFail(rsp liFace.IRespond) {
81+
req := rsp.GetRequest()
82+
conn, err := req.GetConnection().GetProperty("gateConn")
83+
if err != nil{
84+
utils.Log.Warn("Handle not found gateConn")
85+
}
86+
87+
ack := proto.BaseAck{}
88+
ack.Code =proto.Code_ReqFail
89+
90+
msg := rsp.GetMessage()
91+
msgName := msg.GetMsgName()
92+
gateConn := conn.(*liNet.WsConnection)
93+
data, _ := json.Marshal(ack)
94+
msg.SetBody(data)
95+
96+
proxy, e := req.GetConnection().GetProperty("proxy")
97+
if e != nil{
98+
gateConn.Push("", msgName, rsp.GetData())
99+
}else{
100+
proxyName := proxy.(string)
101+
gateConn.Response(proxyName, msgName, msg.GetSeq(), rsp.GetData())
102+
}
103+
}

0 commit comments

Comments
 (0)