forked from davidfowl/signalr-ports
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.go
246 lines (225 loc) · 7.33 KB
/
server.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
package signalr
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
"os"
"reflect"
"strings"
"sync"
"time"
)
// Server is a SignalR server for one type of hub
type Server struct {
newHub func() HubInterface
lifetimeManager HubLifetimeManager
defaultHubClients *defaultHubClients
groupManager GroupManager
info log.Logger
dbg log.Logger
hubChanReceiveTimeout time.Duration
}
// NewServer creates a new server for one type of hub
// newHub is called each time a hub method is invoked by a client to create the transient hub instance
func NewServer(options ...func(*Server) error) (*Server, error) {
lifetimeManager := defaultHubLifetimeManager{}
i, d := buildInfoDebugLogger(log.NewLogfmtLogger(os.Stderr), false)
server := &Server{
lifetimeManager: &lifetimeManager,
defaultHubClients: &defaultHubClients{
lifetimeManager: &lifetimeManager,
allCache: allClientProxy{lifetimeManager: &lifetimeManager},
},
groupManager: &defaultGroupManager{
lifetimeManager: &lifetimeManager,
},
info: i,
dbg: d,
hubChanReceiveTimeout: time.Millisecond * 5000,
}
for _, option := range options {
if option != nil {
if err := option(server); err != nil {
return nil, err
}
}
}
if server.newHub == nil {
return server, errors.New("cannot determine hub type. Neither UseHub, HubFactory or SimpleHubFactory given as option")
}
return server, nil
}
// Run runs the server on one connection. The same server might be run on different connections in parallel
func (s *Server) Run(conn Connection) {
if protocol, err := s.processHandshake(conn); err != nil {
info, _ := s.prefixLogger()
_ = info.Log(evt, "processHandshake", "error", err, react, "do not connect")
} else {
s.newServerLoop(conn, protocol).Run()
}
}
func (s *Server) prefixLogger() (info log.Logger, debug log.Logger) {
return log.WithPrefix(s.info, "ts", log.DefaultTimestampUTC,
"class", "Server",
"hub", reflect.ValueOf(s.newHub()).Elem().Type()),
log.WithPrefix(s.dbg, "ts", log.DefaultTimestampUTC,
"class", "Server",
"hub", reflect.ValueOf(s.newHub()).Elem().Type())
}
func buildInfoDebugLogger(logger log.Logger, debug bool) (log.Logger, log.Logger) {
if debug {
logger = level.NewFilter(logger, level.AllowDebug())
} else {
logger = level.NewFilter(logger, level.AllowInfo())
}
return level.Info(logger), log.With(level.Debug(logger), "caller", log.DefaultCaller)
}
func startPingClientLoop(conn hubConnection) *sync.WaitGroup {
var waitgroup sync.WaitGroup
waitgroup.Add(1)
go func(waitGroup *sync.WaitGroup, conn hubConnection) {
defer waitGroup.Done()
for conn.IsConnected() {
conn.Ping()
time.Sleep(5 * time.Second)
}
}(&waitgroup, conn)
return &waitgroup
}
func (s *Server) newConnectionHubContext(conn hubConnection) HubContext {
return &connectionHubContext{
clients: &callerHubClients{
defaultHubClients: s.defaultHubClients,
connectionID: conn.GetConnectionID(),
},
groups: s.groupManager,
items: conn.Items(),
}
}
func (s *Server) getHub(conn hubConnection) HubInterface {
hub := s.newHub()
hub.Initialize(s.newConnectionHubContext(conn))
return hub
}
func getMethod(hub HubInterface, name string) (reflect.Value, bool) {
hubType := reflect.TypeOf(hub)
hubValue := reflect.ValueOf(hub)
name = strings.ToLower(name)
for i := 0; i < hubType.NumMethod(); i++ {
if m := hubType.Method(i); strings.ToLower(m.Name) == name {
return hubValue.Method(i), true
}
}
return reflect.Value{}, false
}
func buildMethodArguments(method reflect.Value, invocation invocationMessage,
streamClient *streamClient, protocol HubProtocol) (arguments []reflect.Value, clientStreaming bool, err error) {
arguments = make([]reflect.Value, method.Type().NumIn())
chanCount := 0
for i := 0; i < method.Type().NumIn(); i++ {
t := method.Type().In(i)
// Is it a channel for client streaming?
if arg, clientStreaming, err := streamClient.buildChannelArgument(invocation, t, chanCount); err != nil {
// it is, but channel count in invocation and method mismatch
return nil, false, err
} else if clientStreaming {
// it is
chanCount++
arguments[i] = arg
} else {
// it is not, so do the normal thing
arg := reflect.New(t)
if err := protocol.UnmarshalArgument(invocation.Arguments[i-chanCount], arg.Interface()); err != nil {
return arguments, chanCount > 0, err
}
arguments[i] = arg.Elem()
}
}
if len(invocation.StreamIds) > chanCount {
return arguments, chanCount > 0, fmt.Errorf("to many StreamIds for channel parameters of method %v", invocation.Target)
}
return arguments, chanCount > 0, nil
}
type connFunc func(conn hubConnection, invocation invocationMessage, value interface{})
func completion(conn hubConnection, invocation invocationMessage, value interface{}) {
conn.Completion(invocation.InvocationID, value, "")
}
func streamItem(conn hubConnection, invocation invocationMessage, value interface{}) {
conn.StreamItem(invocation.InvocationID, value)
}
func invokeConnection(conn hubConnection, invocation invocationMessage, connFunc connFunc, result []reflect.Value) {
values := make([]interface{}, len(result))
for i, rv := range result {
values[i] = rv.Interface()
}
switch len(result) {
case 0:
conn.Completion(invocation.InvocationID, nil, "")
case 1:
connFunc(conn, invocation, values[0])
default:
connFunc(conn, invocation, values)
}
}
func (s *Server) processHandshake(conn Connection) (HubProtocol, error) {
var err error
var protocol HubProtocol
var ok bool
const handshakeResponse = "{}\u001e"
const errorHandshakeResponse = "{\"error\":\"%s\"}\u001e"
info, dbg := s.prefixLogger()
// TODO 5 seconds to process the handshake
// ws.SetReadDeadline(time.Now().Add(5 * time.Second))
var buf bytes.Buffer
data := make([]byte, 1<<12)
for {
var n int
if n, err = conn.Read(data); err != nil {
break
} else {
buf.Write(data[:n])
var rawHandshake []byte
if rawHandshake, err = parseTextMessageFormat(&buf); err != nil {
// Partial message, read more data
buf.Write(data[:n])
} else {
_ = dbg.Log(evt, "handshake received", "msg", string(rawHandshake))
request := handshakeRequest{}
if err = json.Unmarshal(rawHandshake, &request); err != nil {
// Malformed handshake
break
}
if protocol, ok = protocolMap[request.Protocol]; ok {
// Send the handshake response
if _, err = conn.Write([]byte(handshakeResponse)); err != nil {
_ = dbg.Log(evt, "handshake sent", "error", err)
} else {
_ = dbg.Log(evt, "handshake sent", "msg", handshakeResponse)
}
} else {
err = fmt.Errorf("protocol %v not supported", request.Protocol)
_ = info.Log(evt, "protocol requested", "error", err)
if _, respErr := conn.Write([]byte(fmt.Sprintf(errorHandshakeResponse, err))); respErr != nil {
_ = dbg.Log(evt, "handshake sent", "error", respErr)
err = respErr
}
}
break
}
}
}
// TODO Disable the timeout (either we already timeout out or)
//ws.SetReadDeadline(time.Time{})
return protocol, err
}
var protocolMap = map[string]HubProtocol{
"json": &JSONHubProtocol{},
}
// const for logging
const evt string = "event"
const msgRecv string = "message received"
const msg string = "message"
const react string = "reaction"