-
Notifications
You must be signed in to change notification settings - Fork 8
/
socket.go
183 lines (159 loc) · 4.19 KB
/
socket.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
package socketio
import (
"errors"
"io"
"net"
"reflect"
"sync"
"github.com/zyxar/socketio/engine"
)
var (
// ErrorNamespaceUnavaialble indicates error of client accessing to a non-existent namespace
ErrorNamespaceUnavaialble = errors.New("namespace unavailable")
)
// Socket is abstraction of bidirectional socket.io connection
type Socket interface {
Emit(event string, args ...interface{}) (err error)
EmitError(arg interface{}) (err error)
Namespace() string
RemoteAddr() net.Addr
LocalAddr() net.Addr
GetHeader(key string) string
SetHeader(key, value string)
Sid() string
io.Closer
}
type nspSock struct {
*socket
name string
}
// Namespace implements Socket.Namespace
func (n *nspSock) Namespace() string { return n.name }
// Emit implements Socket.Emit
func (n *nspSock) Emit(event string, args ...interface{}) (err error) {
return n.socket.emit(n.name, event, args...)
}
// EmitError implements Socket.EmitError
func (n *nspSock) EmitError(arg interface{}) (err error) {
return n.socket.emitError(n.name, arg)
}
type socket struct {
ß *engine.Socket
encoder Encoder
decoder Decoder
acks map[string]*ackHandle
mutex sync.RWMutex
}
func newSocket(ß *engine.Socket, parser Parser) *socket {
return &socket{
ß: ß,
encoder: parser.Encoder(),
decoder: parser.Decoder(),
acks: make(map[string]*ackHandle),
}
}
func (s *socket) attachnsp(nsp string) {
s.mutex.Lock()
s.acks[nsp] = &ackHandle{ackmap: make(map[uint64]*callback)}
s.mutex.Unlock()
}
func (s *socket) detachnsp(nsp string) {
s.mutex.Lock()
_, ok := s.acks[nsp]
if ok {
delete(s.acks, nsp)
}
s.mutex.Unlock()
}
type nspStore interface {
getnsp(nsp string) (n *namespace, ok bool)
}
func detachall(s nspStore, sock *socket) {
sock.mutex.Lock()
for k := range sock.acks {
delete(sock.acks, k)
if nsp, ok := s.getnsp(k); ok {
if nsp.onDisconnect != nil {
nsp.onDisconnect(&nspSock{socket: sock, name: k})
}
}
}
sock.mutex.Unlock()
}
func (s *socket) fireAck(nsp string, id uint64, data []byte, buffer [][]byte, au ArgsUnmarshaler) (err error) {
s.mutex.RLock()
ack, ok := s.acks[nsp]
s.mutex.RUnlock()
if ok {
err = ack.fireAck(&nspSock{socket: s, name: nsp}, id, data, buffer, au)
}
return
}
// Emit implements Socket.Emit
func (s *socket) Emit(event string, args ...interface{}) (err error) {
return s.emit("/", event, args...)
}
// EmitError implements Socket.EmitError
func (s *socket) EmitError(arg interface{}) (err error) { return s.emitError("/", arg) }
// Namespace implements Socket.Namespace
func (*socket) Namespace() string { return "/" }
func (s *socket) emit(nsp string, event string, args ...interface{}) (err error) {
s.mutex.RLock()
ack, ok := s.acks[nsp]
s.mutex.RUnlock()
if !ok {
return ErrorNamespaceUnavaialble
}
data := []interface{}{event}
p := &Packet{Type: PacketTypeEvent, Namespace: nsp}
for i := range args {
if t := reflect.TypeOf(args[i]); t.Kind() == reflect.Func {
p.ID = newid(ack.onAck(args[i]))
} else {
data = append(data, args[i])
}
}
p.Data = data
return s.emitPacket(p)
}
func (s *socket) emitError(nsp string, arg interface{}) (err error) {
p := &Packet{
Type: PacketTypeError,
Namespace: nsp,
Data: arg,
}
return s.emitPacket(p)
}
func (s *socket) ack(p *Packet) (err error) {
p.Type = PacketTypeAck
return s.emitPacket(p)
}
func (s *socket) emitPacket(p *Packet) (err error) {
b, bin, err := s.encoder.Encode(p)
if err != nil {
return
}
if err = s.ß.Emit(engine.EventMessage, MessageTypeString, b); err != nil {
return
}
for _, d := range bin {
if err = s.ß.Emit(engine.EventMessage, MessageTypeBinary, d); err != nil {
return
}
}
return
}
func (s *socket) yield() *Packet {
select {
case p := <-s.decoder.Decoded():
return p
default:
return nil
}
}
func (s *socket) Sid() string { return s.ß.Sid() }
func (s *socket) Close() (err error) { return s.ß.Close() }
func (s *socket) LocalAddr() net.Addr { return s.ß.LocalAddr() }
func (s *socket) RemoteAddr() net.Addr { return s.ß.RemoteAddr() }
func (s *socket) GetHeader(key string) string { return s.ß.GetHeader(key) }
func (s *socket) SetHeader(key, value string) { s.ß.SetHeader(key, value) }