/
connection.go
277 lines (233 loc) · 7.91 KB
/
connection.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
package mudoo
import (
"errors"
"fmt"
"net"
"sync"
"time"
)
var (
// ErrDestroyed is used when the connection has been disconnected (i.e. can't be used anymore).
ErrDestroyed = errors.New("connection is disconnected")
// ErrQueueFull is used when the send queue is full.
ErrQueueFull = errors.New("send queue is full")
// ErrNotConnected is used when some action required the connection to be online,
// but it wasn't.
ErrNotConnected = errors.New("not connected")
// ErrConnected is used when some action required the connection to be offline,
// but it wasn't.
ErrConnected = errors.New("already connected")
emptyResponse = []byte{}
)
// Conn represents a single session and handles its handshaking,
// message buffering and reconnections.
type Conn struct {
mutex sync.Mutex
serv *Server
fd uint32
nc *net.TCPConn
raddr string
online bool
lastConnected int64
lastDisconnected int64
numHeartbeats int
numConns int // Total number of reconnects.
wakeupFlusher chan byte // Used internally to wake up the flusher.
wakeupReader chan byte // Used internally to wake up the reader.
decBuf *Buffer
}
// NewConn creates a new connection for the sio. It generates the session id and
// prepares the internal structure for usage.
func newConn(serv *Server, fd uint32, nc *net.TCPConn) (c *Conn, err error) {
host, _, err := net.SplitHostPort(nc.RemoteAddr().String())
if err != nil {
serv.Log("mudoo/newConn: GetRemoteAddr:", err)
return
}
c = &Conn{
serv: serv,
fd: fd,
nc: nc,
raddr: host,
online: true,
lastConnected: time.Now().UnixNano(),
wakeupFlusher: make(chan byte),
wakeupReader: make(chan byte),
numConns: 0,
numHeartbeats: 0,
decBuf: new(Buffer),
}
nc.SetReadBuffer(serv.config.ReadBufferSize)
nc.SetWriteBuffer(serv.config.WriteBufferSize)
go c.keepalive()
// go c.flusher()
go c.reader()
return
}
// String returns a string representation of the connection and implements the
// fmt.Stringer interface.
func (c *Conn) String() string {
return fmt.Sprintf("(%d)[%s]", c.fd, c.raddr)
}
// RemoteAddr returns the remote network address of the connection in IP:port format
func (c *Conn) RemoteAddr() string {
return c.raddr
}
// Send queues data for a delivery. It is totally content agnostic with one exception:
// the given data must be marshallable by the standard protobuf package. If the queue to send
// has reached sio.config.QueueLength or the connection has been disconnected,
// then the data is dropped and an error is returned.
func (c *Conn) Send(data Message) error {
c.mutex.Lock()
defer c.mutex.Unlock()
if !c.online {
return ErrNotConnected
}
return c.serv.codec.Send(c, data)
}
func (c *Conn) Close() error {
c.mutex.Lock()
if !c.online {
c.mutex.Unlock()
return ErrNotConnected
}
c.disconnect()
c.mutex.Unlock()
c.serv.doDisconnect(c)
return nil
}
func (c *Conn) disconnect() {
c.serv.Log("mudoo/conn: disconnected:", c)
c.nc.Close()
c.online = false
close(c.wakeupFlusher)
close(c.wakeupReader)
}
// Receive decodes and handles data received from the socket.
// It uses c.sio.codec to decode the data. The received non-heartbeat
// messages (frames) are then passed to c.sio.onMessage method and the
// heartbeats are processed right away (TODO).
func (c *Conn) receive(data []byte) {
c.decBuf.WriteRawBytes(data)
c.serv.config.Codec.OnMessage(c, c.decBuf, time.Now().UnixNano())
}
func (c *Conn) keepalive() {
// c.ticker = time.NewTicker(time.Duration(c.serv.config.HeartbeatInterval) * time.Second)
// defer c.ticker.Stop()
// Loop:
// for t := range c.ticker.C {
// c.mutex.Lock()
// if !c.online {
// c.mutex.Unlock()
// return
// }
// if (!c.online && (time.Now().UnixNano()-c.lastDisconnected > c.serv.config.ReconnectTimeout)) || int(c.lastHeartbeat) < c.numHeartbeats {
// c.disconnect()
// c.mutex.Unlock()
// break
// }
// c.numHeartbeats++
// select {
// case c.queue <- heartbeat(c.numHeartbeats):
// default:
// c.serv.Log("mudoo/keepalive: unable to queue heartbeat. fail now. TODO: FIXME", c)
// c.disconnect()
// c.mutex.Unlock()
// break Loop
// }
// c.mutex.Unlock()
// }
// c.serv.doDisconnect(c)
}
// Flusher waits for messages on the queue. It then
// tries to write the messages to the underlaying socket and
// will keep on trying until the wakeupFlusher is killed or the payload
// can be delivered. It is responsible for persisting messages until they
// can be succesfully delivered. No more than c.sio.config.QueueLength messages
// should ever be waiting for a delivery.
//
// NOTE: the c.sio.config.QueueLength is not a "hard limit", because one could have
// max amount of messages waiting in the queue and in the payload itself
// simultaneously.
// func (c *Conn) flusher() {
// buf := new(bytes.Buffer)
// var err error
// var msg interface{}
// var n int
// for msg = range c.queue {
// buf.Reset()
// err = c.enc.Encode(buf, msg)
// n = 1
// if err == nil {
// DrainLoop:
// for n < c.serv.config.QueueLength {
// select {
// case msg = <-c.queue:
// n++
// if err = c.enc.Encode(buf, msg); err != nil {
// break DrainLoop
// }
// default:
// break DrainLoop
// }
// }
// }
// if err != nil {
// c.serv.Logf("mudoo/conn: flusher/encode: lost %d messages (%d bytes): %s %s", n, buf.Len(), err, c)
// continue
// }
// FlushLoop:
// for {
// for {
// c.mutex.Lock()
// _, err = buf.WriteTo(c.nc)
// c.mutex.Unlock()
// if err == nil {
// break FlushLoop
// }
// }
// if _, ok := <-c.wakeupFlusher; !ok {
// return
// }
// }
// }
// }
// Reader reads from the c.socket until the c.wakeupReader is closed.
// It is responsible for detecting unrecoverable read errors and timeouting
// the connection. When a read fails previously mentioned reasons, it will
// call the c.disconnect method and start waiting for the next event on the
// c.wakeupReader channel.
func (c *Conn) reader() {
buf := make([]byte, c.serv.config.ReadBufferSize)
for {
c.mutex.Lock()
socket := c.nc
c.mutex.Unlock()
for {
nr, err := c.nc.Read(buf)
if err != nil {
if neterr, ok := err.(*net.OpError); ok && neterr.Timeout() {
c.serv.Log("mudoo/conn: lost connection (timeout):", c)
socket.Write(emptyResponse)
} else {
c.serv.Log("mudoo/conn: lost connection:", c)
}
break
} else if nr < 0 {
break
} else if nr > 0 {
c.receive(buf[0:nr])
}
}
c.mutex.Lock()
c.lastDisconnected = time.Now().UnixNano()
socket.Close()
if c.nc == socket {
c.online = false
}
c.mutex.Unlock()
if _, ok := <-c.wakeupReader; !ok {
break
}
}
}