-
-
Notifications
You must be signed in to change notification settings - Fork 49
/
conn.go
1045 lines (866 loc) · 26.8 KB
/
conn.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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package neffos
import (
"context"
"errors"
"net"
"net/http"
"sync"
"sync/atomic"
"time"
)
type (
// Socket is the interface that an underline protocol implementation should implement.
Socket interface {
// NetConn returns the underline net connection.
NetConn() net.Conn
// Request returns the http request value.
Request() *http.Request
// ReadData reads binary or text messages from the remote connection.
ReadData(timeout time.Duration) (body []byte, typ MessageType, err error)
// WriteBinary sends a binary message to the remote connection.
WriteBinary(body []byte, timeout time.Duration) error
// WriteText sends a text message to the remote connection.
WriteText(body []byte, timeout time.Duration) error
}
// MessageType is a type for readen and to-send data, helpful to set `msg.SetBinary`
// to the rest of the clients through a Broadcast, as SetBinary is not part of the deserialization.
MessageType uint8
)
// See `MessageType` definition for details.
const (
TextMessage = iota + 1
BinaryMessage
)
// Conn contains the websocket connection and the neffos communication functionality.
// Its `Connection` will return a new `NSConn` instance.
// Each connection can connect to one or more declared namespaces.
// Each `NSConn` can join to multiple rooms.
type Conn struct {
// the ID generated by `Server#IDGenerator`.
id string
// serverConnID is unique per server instance and it can be comparable only within the
// same server instance. Even if Server#IDGenerator
// returns the same ID from the request.
serverConnID string
// a context-scope storage, initialized on first `Set`.
store map[string]interface{}
storeMutex sync.RWMutex
// the gorilla or gobwas socket.
socket Socket
// ReconnectTries, if > 0 then this connection is a result of a client-side reconnection,
// see `WasReconnected() bool`.
ReconnectTries int
// non-nil if server-side connection.
server *Server
// when sever or client is ready to handle messages,
// ack and queue is available,
// see `Server#ServeHTTP.?OnConnect!=nil`.
readiness *waiterOnce
// maximum wait time allowed to read a message from the connection.
// Defaults to no timeout.
readTimeout time.Duration
// maximum wait time allowed to write a message to the connection.
// Defaults to no timeout.
writeTimeout time.Duration
// the defined namespaces, allowed to connect.
namespaces Namespaces
// more than 0 if acknowledged.
acknowledged *uint32
// the connection's current connected namespace.
connectedNamespaces map[string]*NSConn
connectedNamespacesMutex sync.RWMutex
// used to block certain actions until other action is finished,
// i.e `askConnect: myNamespace` blocks the `tryNamespace: myNamespace` until finish.
processes *processes
isInsideHandler *uint32
// messages that this connection waits for a reply.
waitingMessages map[string]chan Message
waitingMessagesMutex sync.RWMutex
allowNativeMessages bool
shouldHandleOnlyNativeMessages bool
queue map[MessageType][][]byte
queueMutex sync.Mutex
// used to fire `conn#Close` once.
closed *uint32
// useful to terminate the broadcaster, see `Server#ServeHTTP.waitMessages`.
closeCh chan struct{}
}
func newConn(socket Socket, namespaces Namespaces) *Conn {
c := &Conn{
socket: socket,
namespaces: namespaces,
readiness: newWaiterOnce(),
acknowledged: new(uint32),
connectedNamespaces: make(map[string]*NSConn),
processes: newProcesses(),
isInsideHandler: new(uint32),
waitingMessages: make(map[string]chan Message),
allowNativeMessages: false,
shouldHandleOnlyNativeMessages: false,
closed: new(uint32),
closeCh: make(chan struct{}),
}
if emptyNamespace := namespaces[""]; emptyNamespace != nil && emptyNamespace[OnNativeMessage] != nil {
c.allowNativeMessages = true
// if allow native messages and only this namespace empty namespaces is registered (via Events{} for example)
// and the only one event is the `OnNativeMessage`
// then no need to call Connect(...) because:
// client-side can use raw websocket without the neffos.js library
// so no access to connect to a namespace.
if len(c.namespaces) == 1 && len(emptyNamespace) == 1 {
c.connectedNamespaces[""] = newNSConn(c, "", emptyNamespace)
c.shouldHandleOnlyNativeMessages = true
atomic.StoreUint32(c.acknowledged, 1)
c.readiness.unwait(nil)
}
}
return c
}
// Is reports whether the "connID" is part of this server's connections and their IDs are equal.
func (c *Conn) Is(connID string) bool {
if connID == "" {
return false
}
if c.IsClient() {
return c.id == connID
}
return c.serverConnID == connID
}
// ID method returns the unique identifier of the connection.
// If this is a server-side connection then this value is the generated one by the `Server#IDGenerator`.
// If this is a client-side connection then this value is filled on the acknowledgment process which is done on the `Client#Dial`.
func (c *Conn) ID() string {
return c.id
}
// String method simply returns the ID(). Useful for fmt usage and
// to a connection to be passed on `Server#Broadcast` method
// to exclude itself from the broadcasted message's receivers.
func (c *Conn) String() string {
return c.ID()
}
// Socket method returns the underline socket implementation.
func (c *Conn) Socket() Socket {
return c.socket
}
// IsClient method reports whether this connections is a client-side connetion.
func (c *Conn) IsClient() bool {
return c.server == nil
}
// Server method returns the backend server, it returns null on client-side connections.
func (c *Conn) Server() *Server {
if c.IsClient() {
return nil
}
return c.server
}
// Set sets a value to this connection's store.
func (c *Conn) Set(key string, value interface{}) {
c.storeMutex.Lock()
if c.store == nil {
c.store = make(map[string]interface{})
}
c.store[key] = value
c.storeMutex.Unlock()
}
// Get retruns a value based on the given "key"
func (c *Conn) Get(key string) interface{} {
c.storeMutex.RLock()
if c.store == nil {
c.storeMutex.RUnlock()
return nil
}
v := c.store[key]
c.storeMutex.RUnlock()
return v
}
// Increment works like `Set` method.
// It's just a helper for incrementing integer values.
// If value does exist,
// and it's an integer then it increments it by 1,
// otherwise the value is overridden to value 1.
// If value does not exist,
// then it assumes the default value is 0 and it increments it by one,
// the result will be 1.
//
// Returns the incremented value.
func (c *Conn) Increment(key string) int {
value := c.Get(key)
if value == nil {
c.Set(key, 1)
return 1
}
intValue, ok := value.(int)
if !ok {
// override.
c.Set(key, 1)
return 1
}
intValue++
c.Set(key, intValue)
return intValue
}
// Decrement works like `Set` method.
// It's just a helper for decrementing integer values.
// If value does exist,
// and it's an integer then it decrements it by 1,
// otherwise the value is overridden to value -1.
// If value does not exist,
// then it assumes the default value is 0 and it decrements it by one,
// the result will be -1.
//
// Calling it twice for example it will set the value to -2,
// even if doesn't exist before.
//
// Returns the decremented value.
func (c *Conn) Decrement(key string) int {
value := c.Get(key)
if value == nil {
c.Set(key, -1)
return -1
}
intValue, ok := value.(int)
if !ok {
// override.
c.Set(key, -1)
return -1
}
intValue--
c.Set(key, intValue)
return intValue
}
// WasReconnected reports whether the current connection is a result of a client-side reconnection.
// To get the numbers of total retries see the `ReconnectTries` field.
func (c *Conn) WasReconnected() bool {
return c.ReconnectTries > 0
}
func (c *Conn) isAcknowledged() bool {
return atomic.LoadUint32(c.acknowledged) > 0
}
const (
ackBinary = 'M' // byte(0x1) // comes from client to server at startup.
ackIDBinary = 'A' // byte(0x2) // comes from server to client after ackBinary and ready as a prefix, the rest message is the conn's ID.
// ackOKBinary = 'K' // byte(0x3) // comes from client to server when id received and set-ed.
ackNotOKBinary = 'H' // byte(0x4) // comes from server to client if `Server#OnConnected` errored as a prefix, the rest message is the error text.
)
var (
ackBinaryB = []byte{ackBinary}
ackIDBinaryB = []byte{ackIDBinary}
ackNotOKBinaryB = []byte{ackNotOKBinary}
)
func (c *Conn) sendClientACK() error {
// if neffos client used but in reality nor of its features are used
// because end-dev set it as native only sender and receiver so any webscoket client can be used
// even the browser's default; we can't accept a custom ack neither a namespace connection or two-way error handling.
if c.shouldHandleOnlyNativeMessages {
return nil
}
ok := c.write(ackBinaryB, false)
if !ok {
c.Close()
return ErrWrite
}
err := c.readiness.wait()
if err != nil {
c.Close()
}
return err
}
func (c *Conn) startReader() {
if c.IsClosed() {
return
}
defer c.Close()
// CLIENT is ready when ACK done
// SERVER is ready when ACK is done AND `Server#OnConnected` returns with nil error.
for {
b, msgTyp, err := c.socket.ReadData(c.readTimeout)
if err != nil {
c.readiness.unwait(err)
return
}
if len(b) == 0 {
continue
}
if !c.isAcknowledged() {
if !c.handleACK(msgTyp, b) {
return
}
continue
}
atomic.StoreUint32(c.isInsideHandler, 1)
c.HandlePayload(msgTyp, b)
atomic.StoreUint32(c.isInsideHandler, 0)
}
}
func (c *Conn) handleACK(msgTyp MessageType, b []byte) bool {
switch typ := b[0]; typ {
case ackBinary:
// from client startup to server.
err := c.readiness.wait()
if err != nil {
// it's not Ok, send error which client's Dial should return.
c.write(append(ackNotOKBinaryB, []byte(err.Error())...), false)
return false
}
atomic.StoreUint32(c.acknowledged, 1)
c.handleQueue()
// it's ok send ID.
return c.write(append(ackIDBinaryB, []byte(c.id)...), false)
// case ackOKBinary:
// // from client to server.
// atomic.StoreUint32(c.acknowledged, 1)
// c.handleQueue()
case ackIDBinary:
// from server to client.
id := string(b[1:])
c.id = id
atomic.StoreUint32(c.acknowledged, 1)
c.readiness.unwait(nil)
// c.write([]byte{ackOKBinary})
// println("ackIDBinary: pass with nil")
// c.handleQueue()
case ackNotOKBinary:
// from server to client.
errText := string(b[1:])
err := errors.New(errText)
c.readiness.unwait(err)
return false
default:
c.queueMutex.Lock()
if c.queue == nil {
c.queue = make(map[MessageType][][]byte)
}
c.queue[msgTyp] = append(c.queue[msgTyp], b)
c.queueMutex.Unlock()
}
return true
}
func (c *Conn) handleQueue() {
c.queueMutex.Lock()
defer c.queueMutex.Unlock()
for msgTyp, q := range c.queue {
for _, b := range q {
c.HandlePayload(msgTyp, b)
}
delete(c.queue, msgTyp)
}
}
// ErrInvalidPayload can be returned by the internal `handleMessage`.
// In the future it may be exposed by an error listener.
var ErrInvalidPayload = errors.New("invalid payload")
func (c *Conn) handleMessage(msg Message) error {
if msg.isInvalid {
return ErrInvalidPayload
}
if msg.IsNative && c.shouldHandleOnlyNativeMessages {
ns := c.Namespace("")
return ns.events.fireEvent(ns, msg)
}
if isClient := c.IsClient(); msg.IsWait(isClient) {
if !isClient {
if msg.FromStackExchange && c.server.usesStackExchange() {
// Currently let's not export the wait field, instead
// just accept it on the stackexchange.
return c.server.StackExchange.NotifyAsk(msg, msg.wait)
}
c.server.waitingMessagesMutex.RLock()
ch, ok := c.server.waitingMessages[msg.wait]
c.server.waitingMessagesMutex.RUnlock()
if ok {
ch <- msg
return nil
}
}
c.waitingMessagesMutex.RLock()
ch, ok := c.waitingMessages[msg.wait]
c.waitingMessagesMutex.RUnlock()
if ok {
ch <- msg
return nil
}
}
switch msg.Event {
case OnNamespaceConnect:
c.replyConnect(msg)
case OnNamespaceDisconnect:
c.replyDisconnect(msg)
case OnRoomJoin:
if ns, ok := c.tryNamespace(msg); ok {
ns.replyRoomJoin(msg)
}
case OnRoomLeave:
if ns, ok := c.tryNamespace(msg); ok {
ns.replyRoomLeave(msg)
}
default:
ns, ok := c.tryNamespace(msg)
if !ok {
// println(msg.Namespace + " namespace and incoming message of event: " + msg.Event + " is not connected or not exists and wait?: " + msg.wait + "\n\n")
return ErrBadNamespace
}
msg.IsLocal = false
err := ns.events.fireEvent(ns, msg)
if err != nil {
msg.Err = err
c.Write(msg)
return err
}
}
return nil
}
// DeserializeMessage returns a Message from the "payload".
func (c *Conn) DeserializeMessage(msgTyp MessageType, payload []byte) Message {
return DeserializeMessage(msgTyp, payload, c.allowNativeMessages, c.shouldHandleOnlyNativeMessages)
}
// HandlePayload fires manually a local event based on the "payload".
func (c *Conn) HandlePayload(msgTyp MessageType, payload []byte) error {
return c.handleMessage(c.DeserializeMessage(msgTyp, payload))
}
const syncWaitDur = 15 * time.Millisecond
// 10 seconds is high value which is not realistic on healthy networks, but may useful for slow connections.
// This value is used just for the ack(which is usually done before the Connect call itself) wait on Connect when on server-side only.
const maxSyncWaitDur = 10 * time.Second
// Connect method returns a new connected to the specific "namespace" `NSConn` value.
// The "namespace" should be declared in the `connHandler` of both server and client sides.
// If this is a client-side connection then the server-side namespace's `OnNamespaceConnect` event callback MUST return null
// in order to allow this client-side connection to connect, otherwise a non-nil error is returned instead.
func (c *Conn) Connect(ctx context.Context, namespace string) (*NSConn, error) {
// if c.IsClosed() {
// return nil, ErrWrite
// }
if !c.IsClient() {
c.readiness.unwait(nil)
// server-side check for ack-ed, it should be done almost immediately the client connected
// but give it sometime for slow networks and add an extra check for closed after 5 seconds and a deadline of 10seconds.
t := maxSyncWaitDur
for !c.isAcknowledged() {
time.Sleep(syncWaitDur)
t -= syncWaitDur
if t <= maxSyncWaitDur/2 { // check once after 5 seconds if closed.
if c.IsClosed() {
return nil, ErrWrite
}
}
if t == 0 {
// when maxSyncWaitDur passed,
// we could use the context's deadline but it will make things slower (extracting its value slower than the sleep time).
if c.IsClosed() {
return nil, ErrWrite
}
return nil, context.DeadlineExceeded
}
}
}
return c.askConnect(ctx, namespace)
}
// const defaultNS = ""
// func (c *Conn) DefaultNamespace() *NSConn {
// ns, _ := c.Connect(nil, defaultNS)
// return ns
// }
// WaitConnect method can be used instead of the `Connect` if the other side force-calls `Connect` to this connection
// and this side wants to "waits" for that signal.
//
// Nil context means try without timeout, wait until it connects to the specific namespace.
// Note that, this function will not return an `ErrBadNamespace` if namespace does not exist in the server-side
// or it's not defined in the client-side, it waits until deadline (if any, or loop forever, so a context with deadline is highly recommended).
func (c *Conn) WaitConnect(ctx context.Context, namespace string) (ns *NSConn, err error) {
if ctx == nil {
ctx = context.TODO()
}
for {
select {
case <-ctx.Done():
return nil, ctx.Err()
default:
if ns == nil {
ns = c.Namespace(namespace)
}
if ns != nil && c.isAcknowledged() {
return
}
time.Sleep(syncWaitDur)
}
}
}
// Namespace method returns an already-connected `NSConn` value based on the given "namespace".
func (c *Conn) Namespace(namespace string) *NSConn {
c.connectedNamespacesMutex.RLock()
ns := c.connectedNamespaces[namespace]
c.connectedNamespacesMutex.RUnlock()
return ns
}
func (c *Conn) tryNamespace(in Message) (*NSConn, bool) {
c.processes.get(in.Namespace).Wait() // wait any `askConnect` process (if any) of that "in.Namespace".
ns := c.Namespace(in.Namespace)
if ns == nil {
// if _, canConnect := c.namespaces[msg.Namespace]; !canConnect {
// msg.Err = ErrForbiddenNamespace
// }
in.Err = ErrBadNamespace
c.Write(in)
return nil, false
}
return ns, true
}
// server#OnConnected -> conn#Connect
// client#WaitConnect
// or
// client#Connect
func (c *Conn) askConnect(ctx context.Context, namespace string) (*NSConn, error) {
p := c.processes.get(namespace)
p.Start() // block any `tryNamespace` with that "namespace".
defer p.Done() // unblock.
// defer c.processes.get(namespace).run()()
// for !atomic.CompareAndSwapUint32(c.isConnectingProcess, 0, 1) {
// }
// defer atomic.StoreUint32(c.isConnectingProcess, 0)
ns := c.Namespace(namespace)
if ns != nil {
return ns, nil
}
events, ok := c.namespaces[namespace]
if !ok {
return nil, ErrBadNamespace
}
connectMessage := Message{
Namespace: namespace,
Event: OnNamespaceConnect,
IsLocal: true,
}
ns = newNSConn(c, namespace, events)
err := events.fireEvent(ns, connectMessage)
if err != nil {
return nil, err
}
// println("ask connect")
_, err = c.Ask(ctx, connectMessage) // waits for answer no matter if already connected on the other side.
if err != nil {
return nil, err
}
// println("got connect")
// re-check, maybe connected so far (can happen by a simultaneously `Connect` calls on both server and client, which is not the standard way)
// c.connectedNamespacesMutex.RLock()
// ns, ok = c.connectedNamespaces[namespace]
// c.connectedNamespacesMutex.RUnlock()
// if ok {
// return ns, nil
// }
c.connectedNamespacesMutex.Lock()
c.connectedNamespaces[namespace] = ns
c.connectedNamespacesMutex.Unlock()
// println("we're connected")
// c.writeEmptyReply(genWaitConfirmation(reply.wait))
// println("wrote: " + genWaitConfirmation(reply.wait))
// c.sendConfirmation(reply.wait)
c.notifyNamespaceConnected(ns, connectMessage)
return ns, nil
}
func (c *Conn) replyConnect(msg Message) {
// must give answer even a noOp if already connected.
if msg.wait == "" || msg.isNoOp {
return
}
ns := c.Namespace(msg.Namespace)
if ns != nil {
c.writeEmptyReply(msg.wait)
return
}
events, ok := c.namespaces[msg.Namespace]
if !ok {
msg.Err = ErrBadNamespace
c.Write(msg)
return
}
ns = newNSConn(c, msg.Namespace, events)
err := events.fireEvent(ns, msg)
if err != nil {
msg.Err = err
c.Write(msg)
return
}
c.connectedNamespacesMutex.Lock()
c.connectedNamespaces[msg.Namespace] = ns
c.connectedNamespacesMutex.Unlock()
c.writeEmptyReply(msg.wait)
c.notifyNamespaceConnected(ns, msg)
}
func (c *Conn) notifyNamespaceConnected(ns *NSConn, connectMsg Message) {
connectMsg.Event = OnNamespaceConnected
ns.events.fireEvent(ns, connectMsg) // omit error, it's connected.
if !c.IsClient() && c.server.usesStackExchange() {
c.server.StackExchange.Subscribe(c, ns.namespace)
}
}
func (c *Conn) notifyNamespaceDisconnect(_ *NSConn, disconnectMsg Message) {
if !c.IsClient() && c.server.usesStackExchange() {
c.server.StackExchange.Unsubscribe(c, disconnectMsg.Namespace)
}
}
// DisconnectAll method disconnects from all namespaces,
// `OnNamespaceDisconnect` even will be fired and its `Message.IsLocal` will be true.
// The remote side gets notified.
func (c *Conn) DisconnectAll(ctx context.Context) error {
if c.shouldHandleOnlyNativeMessages {
return nil
}
c.connectedNamespacesMutex.Lock()
defer c.connectedNamespacesMutex.Unlock()
disconnectMsg := Message{Event: OnNamespaceDisconnect, IsLocal: true, locked: true}
for namespace := range c.connectedNamespaces {
disconnectMsg.Namespace = namespace
if err := c.askDisconnect(ctx, disconnectMsg, false); err != nil {
return err
}
}
return nil
}
func (c *Conn) askDisconnect(ctx context.Context, msg Message, lock bool) error {
if lock {
c.connectedNamespacesMutex.RLock()
}
ns := c.connectedNamespaces[msg.Namespace]
if lock {
c.connectedNamespacesMutex.RUnlock()
}
if ns == nil {
return ErrBadNamespace
}
_, err := c.Ask(ctx, msg)
if err != nil {
return err
}
// if disconnect is allowed then leave rooms first with force property
// before namespace's deletion.
ns.forceLeaveAll(true)
if lock {
c.connectedNamespacesMutex.Lock()
}
delete(c.connectedNamespaces, msg.Namespace)
if lock {
c.connectedNamespacesMutex.Unlock()
}
msg.IsLocal = true
ns.events.fireEvent(ns, msg)
c.notifyNamespaceDisconnect(ns, msg)
return nil
}
func (c *Conn) replyDisconnect(msg Message) {
if msg.wait == "" || msg.isNoOp {
return
}
ns := c.Namespace(msg.Namespace)
if ns == nil {
c.writeEmptyReply(msg.wait)
return
}
// if client then we need to respond to server and delete the namespace without ask the local event.
if c.IsClient() {
// if disconnect is allowed then leave rooms first with force property
// before namespace's deletion.
ns.forceLeaveAll(false)
c.connectedNamespacesMutex.Lock()
delete(c.connectedNamespaces, msg.Namespace)
c.connectedNamespacesMutex.Unlock()
c.writeEmptyReply(msg.wait)
ns.events.fireEvent(ns, msg)
return
}
// server-side, check for error on the local event first.
err := ns.events.fireEvent(ns, msg)
if err != nil {
msg.Err = err
c.Write(msg)
return
}
ns.forceLeaveAll(false)
c.connectedNamespacesMutex.Lock()
delete(c.connectedNamespaces, msg.Namespace)
c.connectedNamespacesMutex.Unlock()
c.notifyNamespaceDisconnect(ns, msg)
c.writeEmptyReply(msg.wait)
}
func (c *Conn) write(b []byte, binary bool) bool {
var err error
if binary {
err = c.socket.WriteBinary(b, c.writeTimeout)
} else {
err = c.socket.WriteText(b, c.writeTimeout)
}
if err != nil {
if IsCloseError(err) {
c.Close()
}
return false
}
return true
}
func (c *Conn) canWrite(msg Message) bool {
if c.IsClosed() {
return false
}
if !c.IsClient() {
// for server-side if tries to send, then error will be not ignored but events should continue.
c.readiness.unwait(nil)
}
if !msg.isConnect() && !msg.isDisconnect() {
if !msg.locked {
c.connectedNamespacesMutex.RLock()
}
ns := c.connectedNamespaces[msg.Namespace]
if !msg.locked {
c.connectedNamespacesMutex.RUnlock()
}
if ns == nil {
return false
}
if msg.Room != "" && !msg.isRoomJoin() && !msg.isRoomLeft() {
if !msg.locked {
ns.roomsMutex.RLock()
}
_, ok := ns.rooms[msg.Room]
if !msg.locked {
ns.roomsMutex.RUnlock()
}
if !ok {
// tried to send to a not joined room.
return false
}
}
}
// if !c.IsClient() && !msg.FromStackExchange {
// if exc := c.Server().StackExchange; exc != nil {
// if exc.Publish(c, msg) {
// return true
// }
// }
// }
// don't write if explicit "from" field is set
// to this server's instance client connection ~~~but give a chance to Publish
// it to other instances with the same conn ID, if any~~~.
if c.Is(msg.FromExplicit) {
return false
}
return true
}
// Write method sends a message to the remote side,
// reports whether the connection is still available
// or when this message is not allowed to be sent to the remote side.
func (c *Conn) Write(msg Message) bool {
if !c.canWrite(msg) {
return false
}
msg.FromExplicit = ""
return c.write(serializeMessage(msg), msg.SetBinary)
}
// used when `Ask` caller cares only for successful call and not the message, for performance reasons we just use raw bytes.
func (c *Conn) writeEmptyReply(wait string) bool {
return c.write(genEmptyReplyToWait(wait), false)
}
// func (c *Conn) waitConfirmation(wait string) {
// wait = genWaitConfirmation(wait)
// ch := make(chan Message)
// c.waitingMessagesMutex.Lock()
// c.waitingMessages[wait] = ch
// c.waitingMessagesMutex.Unlock()
// <-ch
// }
// func (c *Conn) sendConfirmation(wait string) {
// wait = genWaitConfirmation(wait)
// c.writeEmptyReply(wait)
// }
// Ask method sends a message to the remote side and blocks until a response or an error received from the specific `Message.Event`.
func (c *Conn) Ask(ctx context.Context, msg Message) (Message, error) {
mustWaitOnlyTheNextMessage := atomic.LoadUint32(c.isInsideHandler) == 1
return c.ask(ctx, msg, mustWaitOnlyTheNextMessage)
}
func (c *Conn) ask(ctx context.Context, msg Message, mustWaitOnlyTheNextMessage bool) (Message, error) {
if c.shouldHandleOnlyNativeMessages {
// should panic or...
return Message{}, nil
}
if c.IsClosed() {
return msg, CloseError{Code: -1, error: ErrWrite}
}
if ctx == nil {
ctx = context.TODO()
} else if ctx == context.TODO() {
} else {
if deadline, has := ctx.Deadline(); has {
if deadline.Before(time.Now().Add(-1 * time.Second)) {
return Message{}, context.DeadlineExceeded
}
}
}
ch := make(chan Message, 1)
msg.wait = genWait(c.IsClient())
if mustWaitOnlyTheNextMessage {
// msg.wait is not required on this state
// but we still set it.
go func() {
b, msgTyp, err := c.Socket().ReadData(c.readTimeout)
if err != nil {
ch <- Message{Err: err, isError: true}
return
}
ch <- c.DeserializeMessage(msgTyp, b)
}()
} else {
c.waitingMessagesMutex.Lock()
c.waitingMessages[msg.wait] = ch
c.waitingMessagesMutex.Unlock()
}
if !c.Write(msg) {
return Message{}, ErrWrite
}
select {
case <-ctx.Done():
if c.IsClosed() {
return Message{}, ErrWrite
}
return Message{}, ctx.Err()
case receive := <-ch:
if !mustWaitOnlyTheNextMessage {
c.waitingMessagesMutex.Lock()
delete(c.waitingMessages, msg.wait)
c.waitingMessagesMutex.Unlock()
}
return receive, receive.Err