-
Notifications
You must be signed in to change notification settings - Fork 41
/
hubconnection.go
263 lines (243 loc) · 6.81 KB
/
hubconnection.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
package signalr
import (
"bytes"
"context"
"fmt"
"io"
"sync"
"time"
)
// hubConnection is used by HubContext, Server and Client to realize the external API.
// hubConnection uses a transport connection (of type Connection) and a hubProtocol to send and receive SignalR messages.
type hubConnection interface {
ConnectionID() string
Receive() <-chan receiveResult
SendInvocation(id string, target string, args []interface{}) error
SendStreamInvocation(id string, target string, args []interface{}) error
SendInvocationWithStreamIds(id string, target string, args []interface{}, streamIds []string) error
StreamItem(id string, item interface{}) error
Completion(id string, result interface{}, error string) error
Close(error string, allowReconnect bool) error
Ping() error
LastWriteStamp() time.Time
Items() *sync.Map
Context() context.Context
Abort()
}
type receiveResult struct {
message interface{}
err error
}
func newHubConnection(connection Connection, protocol hubProtocol, maximumReceiveMessageSize uint, info StructuredLogger) hubConnection {
ctx, cancelFunc := context.WithCancel(connection.Context())
c := &defaultHubConnection{
ctx: ctx,
cancelFunc: cancelFunc,
protocol: protocol,
mx: sync.Mutex{},
connection: connection,
maximumReceiveMessageSize: maximumReceiveMessageSize,
items: &sync.Map{},
info: info,
}
if connectionWithTransferMode, ok := connection.(ConnectionWithTransferMode); ok {
connectionWithTransferMode.SetTransferMode(protocol.transferMode())
}
return c
}
type defaultHubConnection struct {
ctx context.Context
cancelFunc context.CancelFunc
protocol hubProtocol
mx sync.Mutex
connection Connection
maximumReceiveMessageSize uint
items *sync.Map
lastWriteStamp time.Time
info StructuredLogger
}
func (c *defaultHubConnection) Items() *sync.Map {
return c.items
}
func (c *defaultHubConnection) Close(errorText string, allowReconnect bool) error {
var closeMessage = closeMessage{
Type: 7,
Error: errorText,
AllowReconnect: allowReconnect,
}
return c.protocol.WriteMessage(closeMessage, c.connection)
}
func (c *defaultHubConnection) ConnectionID() string {
return c.connection.ConnectionID()
}
func (c *defaultHubConnection) Context() context.Context {
return c.ctx
}
func (c *defaultHubConnection) Abort() {
c.cancelFunc()
}
func (c *defaultHubConnection) Receive() <-chan receiveResult {
recvChan := make(chan receiveResult, 20)
// Prepare cleanup
writerDone := make(chan struct{}, 1)
// the pipe connects the goroutine which reads from the connection and the goroutine which parses the read data
reader, writer := io.Pipe()
go func() {
<-c.ctx.Done()
_ = writer.CloseWithError(c.ctx.Err())
_ = reader.CloseWithError(c.ctx.Err())
}()
p := make([]byte, c.maximumReceiveMessageSize)
go func(ctx context.Context, connection io.Reader, writer io.Writer, recvChan chan<- receiveResult, writerDone chan<- struct{}) {
loop:
for {
select {
case <-ctx.Done():
break loop
default:
n, err := connection.Read(p)
if err != nil {
select {
case recvChan <- receiveResult{err: err}:
case <-ctx.Done():
break loop
}
}
if n > 0 {
_, err = writer.Write(p[:n])
if err != nil {
select {
case recvChan <- receiveResult{err: err}:
case <-ctx.Done():
break loop
}
}
}
}
}
// The pipe writer is done
close(writerDone)
}(c.ctx, c.connection, writer, recvChan, writerDone)
// parse
go func(ctx context.Context, reader io.Reader, recvChan chan<- receiveResult, writerDone <-chan struct{}) {
remainBuf := bytes.Buffer{}
loop:
for {
select {
case <-ctx.Done():
break loop
case <-writerDone:
break loop
default:
messages, err := c.protocol.ParseMessages(reader, &remainBuf)
if err != nil {
select {
case recvChan <- receiveResult{err: err}:
case <-ctx.Done():
break loop
case <-writerDone:
break loop
}
} else {
for _, message := range messages {
select {
case recvChan <- receiveResult{message: message}:
case <-ctx.Done():
break loop
case <-writerDone:
break loop
}
}
}
}
}
}(c.ctx, reader, recvChan, writerDone)
return recvChan
}
func (c *defaultHubConnection) SendInvocation(id string, target string, args []interface{}) error {
if args == nil {
args = make([]interface{}, 0)
}
var invocationMessage = invocationMessage{
Type: 1,
InvocationID: id,
Target: target,
Arguments: args,
}
return c.writeMessage(invocationMessage)
}
func (c *defaultHubConnection) SendStreamInvocation(id string, target string, args []interface{}) error {
if args == nil {
args = make([]interface{}, 0)
}
var invocationMessage = invocationMessage{
Type: 4,
InvocationID: id,
Target: target,
Arguments: args,
}
return c.writeMessage(invocationMessage)
}
func (c *defaultHubConnection) SendInvocationWithStreamIds(id string, target string, args []interface{}, streamIds []string) error {
var invocationMessage = invocationMessage{
Type: 1,
InvocationID: id,
Target: target,
Arguments: args,
StreamIds: streamIds,
}
return c.writeMessage(invocationMessage)
}
func (c *defaultHubConnection) StreamItem(id string, item interface{}) error {
var streamItemMessage = streamItemMessage{
Type: 2,
InvocationID: id,
Item: item,
}
return c.writeMessage(streamItemMessage)
}
func (c *defaultHubConnection) Completion(id string, result interface{}, error string) error {
var completionMessage = completionMessage{
Type: 3,
InvocationID: id,
Result: result,
Error: error,
}
return c.writeMessage(completionMessage)
}
func (c *defaultHubConnection) Ping() error {
var pingMessage = hubMessage{
Type: 6,
}
return c.writeMessage(pingMessage)
}
func (c *defaultHubConnection) LastWriteStamp() time.Time {
defer c.mx.Unlock()
c.mx.Lock()
return c.lastWriteStamp
}
func (c *defaultHubConnection) writeMessage(message interface{}) error {
c.mx.Lock()
c.lastWriteStamp = time.Now()
c.mx.Unlock()
err := func() error {
if c.ctx.Err() != nil {
return fmt.Errorf("hubConnection canceled: %w", c.ctx.Err())
}
e := make(chan error, 1)
go func() { e <- c.protocol.WriteMessage(message, c.connection) }()
select {
case <-c.ctx.Done():
return fmt.Errorf("hubConnection canceled: %w", c.ctx.Err())
case err := <-e:
if err != nil {
c.Abort()
}
return err
}
}()
if err != nil {
_ = c.info.Log(evt, msgSend, "message", fmtMsg(message), "error", err)
}
return err
}