-
Notifications
You must be signed in to change notification settings - Fork 3
/
rpc.go
65 lines (57 loc) · 1.27 KB
/
rpc.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
package gnet
import (
"sync"
"sync/atomic"
)
var (
_rpcCallSerialId = uint32(0)
)
type rpcCall struct {
// unique id of every rpc call
id uint32
reply chan Packet
}
// manage the pending rpcCall map
type rpcCalls struct {
rpcCallMutex sync.Mutex
rpcCalls map[uint32]*rpcCall
}
func newRpcCalls() *rpcCalls {
return &rpcCalls{
rpcCalls: make(map[uint32]*rpcCall),
}
}
func (this *rpcCalls) newRpcCall() *rpcCall {
call := &rpcCall{
id: atomic.AddUint32(&_rpcCallSerialId, 1),
reply: make(chan Packet),
}
if call.id == 0 {
call.id = atomic.AddUint32(&_rpcCallSerialId, 1)
}
this.rpcCallMutex.Lock()
this.rpcCalls[call.id] = call
this.rpcCallMutex.Unlock()
return call
}
func (this *rpcCalls) putReply(replyPacket Packet) bool {
if rpcCallIdSetter, ok := replyPacket.(RpcCallIdSetter); ok && rpcCallIdSetter.RpcCallId() > 0 {
this.rpcCallMutex.Lock()
call, exist := this.rpcCalls[rpcCallIdSetter.RpcCallId()]
if exist {
delete(this.rpcCalls, rpcCallIdSetter.RpcCallId())
}
this.rpcCallMutex.Unlock()
if !exist {
return false
}
call.reply <- replyPacket
return true
}
return false
}
func (this *rpcCalls) removeReply(rpcCallId uint32) {
this.rpcCallMutex.Lock()
defer this.rpcCallMutex.Unlock()
delete(this.rpcCalls, rpcCallId)
}