-
Notifications
You must be signed in to change notification settings - Fork 75
/
stream.go
219 lines (191 loc) · 4.88 KB
/
stream.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package arpc
import (
"context"
"io"
"sync/atomic"
"github.com/lesismal/arpc/util"
)
// Stream .
type Stream struct {
id uint64
cli *Client
method string
local bool
chData chan *Message
stateRecv int32
stateSend int32
stateCloseCnt int32
}
func (s *Stream) Id() uint64 {
return s.id
}
func (s *Stream) onMessage(msg *Message) {
if len(msg.Data()) == 0 {
return
}
if atomic.LoadInt32(&s.stateRecv) == 1 {
s.cli.Handler.OnMessageDone(s.cli, msg)
return
}
if msg != nil {
select {
case s.chData <- msg:
case <-s.cli.chClose:
}
}
}
func (s *Stream) CloseRecv() {
if atomic.CompareAndSwapInt32(&s.stateRecv, 0, 1) {
close(s.chData)
s.halfClose()
}
}
func (s *Stream) CloseRecvContext(ctx context.Context) {
s.CloseRecv()
}
func (s *Stream) CloseSend() {
go s.CloseSendContext(context.Background())
}
func (s *Stream) CloseSendContext(ctx context.Context) {
if atomic.CompareAndSwapInt32(&s.stateSend, 0, 1) {
eof := true
s.send(ctx, []byte{}, eof)
s.halfClose()
}
}
func (s *Stream) Recv(v interface{}) error {
if atomic.LoadInt32(&s.stateRecv) == 1 && len(s.chData) == 0 {
return io.EOF
}
select {
case msg, ok := <-s.chData:
if !ok {
return io.EOF
}
data := msg.Data()
err := util.BytesToValue(s.cli.Codec, data, v)
s.cli.Handler.OnMessageDone(s.cli, msg)
return err
case <-s.cli.chClose:
return ErrClientStopped
}
}
func (s *Stream) RecvContext(ctx context.Context, v interface{}) error {
select {
case msg := <-s.chData:
data := msg.Data()
err := util.BytesToValue(s.cli.Codec, data, v)
s.cli.Handler.OnMessageDone(s.cli, msg)
return err
case <-ctx.Done():
return ErrTimeout
case <-s.cli.chClose:
return ErrClientStopped
}
}
func (s *Stream) RecvWith(ctx context.Context, v interface{}) error {
return s.RecvContext(ctx, v)
}
func (s *Stream) newMessage(v interface{}, args ...interface{}) *Message {
if len(args) == 0 {
return newMessage(CmdStream, s.method, v, false, false, s.id, s.cli.Handler, s.cli.Codec, nil)
}
return newMessage(CmdStream, s.method, v, false, false, s.id, s.cli.Handler, s.cli.Codec, args[0].(map[interface{}]interface{}))
}
func (s *Stream) Send(v interface{}, args ...interface{}) error {
return s.SendContext(context.Background(), v, args...)
}
func (s *Stream) SendContext(ctx context.Context, v interface{}, args ...interface{}) error {
eof := false
return s.checkStateAndSend(ctx, v, eof, args...)
}
func (s *Stream) SendWith(ctx context.Context, v interface{}, args ...interface{}) error {
return s.SendContext(ctx, v, args...)
}
func (s *Stream) SendAndClose(v interface{}, args ...interface{}) error {
return s.SendAndCloseContext(context.Background(), v, args...)
}
func (s *Stream) SendAndCloseContext(ctx context.Context, v interface{}, args ...interface{}) error {
eof := true
return s.checkStateAndSend(ctx, v, eof, args...)
}
func (s *Stream) SendAndCloseWith(ctx context.Context, v interface{}, args ...interface{}) error {
return s.SendAndCloseContext(ctx, v, args...)
}
func (s *Stream) checkStateAndSend(ctx context.Context, v interface{}, eof bool, args ...interface{}) error {
if atomic.LoadInt32(&s.stateSend) == 1 {
return ErrStreamClosedSend
}
return s.send(ctx, v, eof, args...)
}
func (s *Stream) send(ctx context.Context, v interface{}, eof bool, args ...interface{}) error {
c := s.cli
err := c.CheckState()
if err != nil {
return err
}
data := util.ValueToBytes(c.Codec, v)
msg := s.newMessage(data, args...)
msg.SetStreamLocal(s.local)
msg.SetStreamEOF(eof)
if eof && atomic.CompareAndSwapInt32(&s.stateSend, 0, 1) {
s.halfClose()
}
if c.Handler.AsyncWrite() {
select {
case c.chSend <- msg:
case <-c.chClose:
// c.Handler.OnOverstock(c, msg)
c.Handler.OnMessageDone(c, msg)
return ErrClientStopped
case <-ctx.Done():
return ErrTimeout
}
} else {
if !c.reconnecting {
coders := c.Handler.Coders()
for j := 0; j < len(coders); j++ {
msg = coders[j].Encode(c, msg)
}
_, err := c.Handler.Send(c.Conn, msg.Buffer)
if err != nil {
c.Conn.Close()
}
c.Handler.OnMessageDone(c, msg)
return err
} else {
c.dropMessage(msg)
return ErrClientReconnecting
}
}
return nil
}
func (s *Stream) halfClose() {
if atomic.AddInt32(&s.stateCloseCnt, 1) == 2 {
s.cli.deleteStream(s.id, s.local)
}
}
// NewStream creates a stream.
func (client *Client) NewStream(method string) *Stream {
return client.newStream(method, 0, true)
}
func (client *Client) newStream(method string, id uint64, local bool) *Stream {
if id == 0 {
id = atomic.AddUint64(&client.seq, 1)
}
stream := &Stream{
id: id,
cli: client,
method: method,
chData: make(chan *Message, client.Handler.StreamQueueSize()),
local: local,
}
client.mux.Lock()
if local {
client.streamLocalMap[stream.id] = stream
} else {
client.streamRemoteMap[stream.id] = stream
}
client.mux.Unlock()
return stream
}