-
Notifications
You must be signed in to change notification settings - Fork 9
/
server.v3.runback.go
78 lines (66 loc) · 2.01 KB
/
server.v3.runback.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
package socketio
import (
"fmt"
siop "github.com/njones/socketio/protocol"
siot "github.com/njones/socketio/transport"
)
func doConnectPacketV3(v3 *ServerV3) func(SocketID, siot.Socket, *Request) error {
return func(socketID SocketID, socket siot.Socket, req *Request) (err error) {
unlock := v3.prev.prev.r()
tr := v3.tr()
unlock()
tr.Join(socket.Namespace, socketID, socketID.Room(socketIDPrefix))
v3.setPrefix()
v3.setSocketID(socketID)
v3.setNsp(socket.Namespace)
if fn, ok := v3.onConnect[socket.Namespace]; ok {
return fn(&SocketV3{inSocketV3: v3.inSocketV3.clone(), req: req})
}
return ErrNamespaceNotFound.F(socket.Namespace)
}
}
func doBinaryAckPacket(v1 *ServerV1) func(SocketID, siot.Socket) error {
return func(socketID SocketID, socket siot.Socket) (err error) {
event := fmt.Sprintf("%s%d", ackIDEventPrefix, socket.AckID)
switch data := socket.Data.(type) {
case []interface{}:
if fn, ok := v1.events[socket.Namespace][event][socketID]; ok {
return fn.Callback(data...)
}
if fn, ok := v1.events[socket.Namespace][event][serverEvent]; ok {
return fn.Callback(data...)
}
case []string:
event := data[0]
if fn, ok := v1.events[socket.Namespace][event][socketID]; ok {
err = fn.Callback(stoi(data[1:])...)
}
default:
return ErrUnexpectedBinaryData.F(socket.Data)
}
return err
}
}
func runV3(v3 *ServerV3) func(SocketID, *Request) error {
return func(socketID SocketID, req *Request) error {
unlock := v3.prev.prev.r()
tr := v3.tr()
unlock()
for socket := range tr.Receive(socketID) {
if err := doV3(v3, socketID, socket, req); err != nil {
return err
}
}
return nil
}
}
func doV3(v3 *ServerV3, socketID SocketID, socket siot.Socket, req *Request) error {
switch socket.Type {
case siop.BinaryAckPacket.Byte():
if err := v3.doBinaryAckPacket(socketID, socket); err != nil {
v3.tr().Send(socketID, serviceError(err), siop.WithType(byte(siop.ErrorPacket)))
}
return nil
}
return doV2(v3.prev, socketID, socket, req)
}