-
Notifications
You must be signed in to change notification settings - Fork 2
/
context.go
167 lines (148 loc) · 3.6 KB
/
context.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package flyrpc
import (
"log"
"time"
)
type Context struct {
Protocol Protocol
Debug bool
Tag string
ClientId int
Session interface{}
Packet *Packet
Router Router
// private
serializer Serializer
nextSeq TSeq
replyChans map[TSeq]chan *Packet
timeout time.Duration
// close handler
closeHandler func(*Context)
}
func NewContext(protocol Protocol, router Router, clientId int, serializer Serializer) *Context {
return &Context{
Protocol: protocol,
Router: router,
ClientId: clientId,
serializer: serializer,
replyChans: make(map[TSeq]chan *Packet),
timeout: 10 * time.Second,
}
}
func (ctx *Context) debug(args ...interface{}) {
if ctx.Debug {
if ctx.Tag != "" {
args = append([]interface{}{"[" + ctx.Tag + "]"}, args...)
}
log.Println(args...)
}
}
func (ctx *Context) sendPacket(flag byte, code string, seq TSeq, payload []byte) error {
return ctx.Protocol.SendPacket(&Packet{
ClientId: ctx.ClientId,
Flag: flag,
Code: code,
Seq: seq,
Payload: payload,
})
}
func (ctx *Context) sendError(code string, seq TSeq, err error) error {
payload := []byte{}
return ctx.sendPacket(
FlagResponse,
err.Error(),
seq,
payload,
)
}
func (ctx *Context) SendMessage(code string, message Message) error {
payload, err := MessageToBytes(message, ctx.serializer)
if err != nil {
return err
}
return ctx.sendPacket(FlagWaitResponse, code, ctx.getNextSeq(), payload)
}
func (ctx *Context) GetReply(code string, message Message) ([]byte, error) {
ctx.debug("Call", code, message)
payload, err := MessageToBytes(message, ctx.serializer)
if err != nil {
return nil, err
}
packet := &Packet{
Flag: FlagWaitResponse,
Code: code,
Seq: ctx.getNextSeq(),
Payload: payload,
}
// Send Packet
if err := ctx.Protocol.SendPacket(packet); err != nil {
return nil, err
}
// init channel before send packet
replyChan := make(chan *Packet, 1)
// set replyChan for code | seq
ctx.replyChans[packet.Seq] = replyChan
// make sure that replyChan is released
defer delete(ctx.replyChans, packet.Seq)
select {
case rPacket := <-replyChan:
ctx.debug("reply payload", rPacket.Payload)
if rPacket.Code != "" {
ctx.debug("reply error", string(rPacket.Code))
return nil, newReplyError(string(rPacket.Code), rPacket)
}
return rPacket.Payload, nil
case <-time.After(ctx.timeout):
return nil, newError(ErrTimeOut)
}
}
func (ctx *Context) Call(code string, message Message, reply Message) error {
bytes, err := ctx.GetReply(code, message)
if err != nil {
return err
}
if reply != nil {
return ctx.serializer.Unmarshal(bytes, reply)
}
return nil
}
func (ctx *Context) GetAsync(code string, message Message) (chan<- []byte, chan<- error) {
buffChan := make(chan []byte, 1)
errChan := make(chan error, 1)
go func() {
bytes, err := ctx.GetReply(code, message)
buffChan <- bytes
errChan <- err
}()
return buffChan, errChan
}
func (ctx *Context) emitPacket(pkt *Packet) {
if pkt.Flag&FlagResponse != 0 {
replyChan := ctx.replyChans[pkt.Seq]
if replyChan == nil {
ctx.debug("No channel found, pkt is :", pkt)
return
}
replyChan <- pkt
return
}
ctx.Packet = pkt
ctx.debug("OnMessage", pkt.Code, pkt.Flag, pkt.Payload)
if err := ctx.Router.emitPacket(ctx, pkt); err != nil {
ctx.debug("Error to call packet", err)
}
}
func (ctx *Context) getNextSeq() TSeq {
ctx.nextSeq++
return ctx.nextSeq
}
func (ctx *Context) OnClose(handler func(*Context)) {
ctx.closeHandler = handler
}
func (ctx *Context) Close() {
ctx.debug("closing")
ctx.Protocol.Close()
if ctx.closeHandler != nil {
ctx.closeHandler(ctx)
}
}