forked from davidfowl/signalr-ports
-
Notifications
You must be signed in to change notification settings - Fork 0
/
serverloop.go
192 lines (179 loc) · 6.37 KB
/
serverloop.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
package signalr
import (
"fmt"
"github.com/go-kit/kit/log"
"reflect"
"runtime/debug"
"sync"
)
type serverLoop struct {
server *Server
info StructuredLogger
dbg StructuredLogger
protocol HubProtocol
hubConn hubConnection
pings *sync.WaitGroup
streamer *streamer
streamClient *streamClient
}
func (s *Server) newServerLoop(conn Connection, protocol HubProtocol) *serverLoop {
info, dbg := s.prefixLogger()
protocol = reflect.New(reflect.ValueOf(protocol).Elem().Type()).Interface().(HubProtocol)
protocol.setDebugLogger(s.dbg)
hubConn := newHubConnection(conn, protocol, s.info, s.dbg)
return &serverLoop{
server: s,
info: info,
dbg: dbg,
protocol: protocol,
hubConn: hubConn,
pings: startPingClientLoop(hubConn),
streamer: newStreamer(hubConn),
streamClient: newStreamClient(s.hubChanReceiveTimeout),
}
}
func (sl *serverLoop) Run() {
sl.hubConn.Start()
sl.server.lifetimeManager.OnConnected(sl.hubConn)
sl.server.getHub(sl.hubConn).OnConnected(sl.hubConn.GetConnectionID())
// Process messages
var message interface{}
var connErr error
messageLoop:
for sl.hubConn.IsConnected() {
if message, connErr = sl.hubConn.Receive(); connErr != nil {
_ = sl.info.Log(evt, msgRecv, "error", connErr, msg, message, react, "disconnect")
break messageLoop
} else {
switch message.(type) {
case invocationMessage:
sl.handleInvocationMessage(message)
case cancelInvocationMessage:
_ = sl.dbg.Log(evt, msgRecv, msg, message.(cancelInvocationMessage))
sl.streamer.Stop(message.(cancelInvocationMessage).InvocationID)
case streamItemMessage:
connErr = sl.handleStreamItemMessage(message)
case completionMessage:
connErr = sl.handleCompletionMessage(message)
case closeMessage:
_ = sl.dbg.Log(evt, msgRecv, msg, message.(closeMessage))
break messageLoop
case hubMessage:
connErr = sl.handleOtherMessage(message)
}
if connErr != nil {
break
}
}
}
sl.server.getHub(sl.hubConn).OnDisconnected(sl.hubConn.GetConnectionID())
sl.server.lifetimeManager.OnDisconnected(sl.hubConn)
sl.hubConn.Close(fmt.Sprintf("%v", connErr))
// Wait for pings to complete
sl.pings.Wait()
_ = sl.dbg.Log(evt, "messageloop ended")
}
func (sl *serverLoop) handleInvocationMessage(message interface{}) {
invocation := message.(invocationMessage)
_ = sl.dbg.Log(evt, msgRecv, msg, fmt.Sprintf("%v", invocation))
// Transient hub, dispatch invocation here
if method, ok := getMethod(sl.server.getHub(sl.hubConn), invocation.Target); !ok {
// Unable to find the method
_ = sl.info.Log(evt, "getMethod", "error", "missing method", "name", invocation.Target, react, "send completion with error")
sl.hubConn.Completion(invocation.InvocationID, nil, fmt.Sprintf("Unknown method %s", invocation.Target))
} else if in, clientStreaming, err := buildMethodArguments(method, invocation, sl.streamClient, sl.protocol); err != nil {
// argument build failed
_ = sl.info.Log(evt, "buildMethodArguments", "error", err, "name", invocation.Target, react, "send completion with error")
sl.hubConn.Completion(invocation.InvocationID, nil, err.Error())
} else if clientStreaming {
// let the receiving method run independently
go func() {
defer recoverInvocationPanic(sl.info, invocation, sl.hubConn)
method.Call(in)
}()
} else {
// hub method might take a long time
go func() {
result := func() []reflect.Value {
defer recoverInvocationPanic(sl.info, invocation, sl.hubConn)
return method.Call(in)
}()
returnInvocationResult(sl.hubConn, invocation, sl.streamer, result)
}()
}
}
func returnInvocationResult(conn hubConnection, invocation invocationMessage, streamer *streamer, result []reflect.Value) {
// No invocation id, no completion
if invocation.InvocationID != "" {
// if the hub method returns a chan, it should be considered asynchronous or source for a stream
if len(result) == 1 && result[0].Kind() == reflect.Chan {
switch invocation.Type {
// Simple invocation
case 1:
go func() {
// Recv might block, so run continue in a goroutine
if chanResult, ok := result[0].Recv(); ok {
invokeConnection(conn, invocation, completion, []reflect.Value{chanResult})
} else {
conn.Completion(invocation.InvocationID, nil, "hub func returned closed chan")
}
}()
// StreamInvocation
case 4:
streamer.Start(invocation.InvocationID, result[0])
}
} else {
switch invocation.Type {
// Simple invocation
case 1:
invokeConnection(conn, invocation, completion, result)
case 4:
// Stream invocation of method with no stream result.
// Return a single StreamItem and an empty Completion
invokeConnection(conn, invocation, streamItem, result)
conn.Completion(invocation.InvocationID, nil, "")
}
}
}
}
func (sl *serverLoop) handleStreamItemMessage(message interface{}) error {
streamItemMessage := message.(streamItemMessage)
_ = sl.dbg.Log(evt, msgRecv, msg, streamItemMessage)
if err := sl.streamClient.receiveStreamItem(streamItemMessage); err != nil {
switch t := err.(type) {
case *hubChanTimeoutError:
sl.hubConn.Completion(streamItemMessage.InvocationID, nil, t.Error())
default:
_ = sl.info.Log(evt, msgRecv, "error", err, msg, message, react, "disconnect")
return err
}
}
return nil
}
func (sl *serverLoop) handleCompletionMessage(message interface{}) error {
_ = sl.dbg.Log(evt, msgRecv, msg, message.(completionMessage))
var err error
if err = sl.streamClient.receiveCompletionItem(message.(completionMessage)); err != nil {
_ = sl.info.Log(evt, msgRecv, "error", err, msg, message, react, "disconnect")
}
return err
}
func (sl *serverLoop) handleOtherMessage(message interface{}) error {
hubMessage := message.(hubMessage)
_ = sl.dbg.Log(evt, msgRecv, msg, hubMessage)
// Not Ping
if hubMessage.Type != 6 {
err := fmt.Errorf("invalid message type %v", message)
_ = sl.info.Log(evt, msgRecv, "error", err, msg, message, react, "disconnect")
return err
}
return nil
}
func recoverInvocationPanic(info log.Logger, invocation invocationMessage, hubConn hubConnection) {
if err := recover(); err != nil {
_ = info.Log(evt, "recover", "error", err, "name", invocation.Target, react, "send completion with error")
if invocation.InvocationID != "" {
hubConn.Completion(invocation.InvocationID, nil, fmt.Sprintf("%v\n%v", err, string(debug.Stack())))
}
}
}