-
-
Notifications
You must be signed in to change notification settings - Fork 310
/
transport.coffee
222 lines (185 loc) · 5.9 KB
/
transport.coffee
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
events = require('events')
uuid = require('node-uuid')
class Transport
Transport.CONNECTING = 0
Transport.OPEN = 1
Transport.CLOSING = 2
Transport.CLOSED = 3
closeFrame = (status, reason) ->
return 'c' + JSON.stringify([status, reason])
keepalive_delay = 25000
MAP = {}
class Session extends events.EventEmitter
constructor: (@session_id, server) ->
@id = uuid()
@send_buffer = []
@is_closing = false
@readyState = Transport.CONNECTING
if @session_id
MAP[@session_id] = @
@timeout_cb = => @didTimeout()
@to_tref = setTimeout(@timeout_cb, 5000)
@emit_open = =>
@emit_open = null
server.emit('open', @)
register: (recv) ->
if @recv
recv.doSendFrame(closeFrame(2010, "Another connection still open"))
return
if @to_tref
clearTimeout(@to_tref)
@to_tref = null
if @readyState is Transport.CLOSING
recv.doSendFrame(@close_frame)
@to_tref = setTimeout(@timeout_cb, 5000)
return
# Registering. From now on 'unregister' is responsible for
# setting the timer.
@recv = recv
@recv.session = @
# first, send the open frame
if @readyState is Transport.CONNECTING
@recv.doSendFrame('o')
@readyState = Transport.OPEN
# Emit the open event, but not right now
process.nextTick @emit_open
# At this point the transport might have gotten away (jsonp).
if not @recv
return
@tryFlush()
return
unregister: ->
@recv.session = null
@recv = null
if @to_tref
clearTimeout(@to_tref)
@to_tref = setTimeout(@timeout_cb, 5000)
tryFlush: ->
if @send_buffer.length > 0
[sb, @send_buffer] = [@send_buffer, []]
@recv.doSendBulk(sb)
else
if @to_tref
clearTimeout(@to_tref)
x = =>
if @recv
@to_tref = setTimeout(x, keepalive_delay)
@recv.doSendFrame("h")
# We have a timeout for konqeror - 35 seconds.
@to_tref = setTimeout(x, keepalive_delay)
return
didTimeout: ->
if @readyState isnt Transport.CONNECTING and
@readyState isnt Transport.OPEN and
@readyState isnt Transport.CLOSING
throw Error('INVALID_STATE_ERR')
if @recv
throw Error('RECV_STILL_THERE')
@readyState = Transport.CLOSED
@emit('close', {status: 1001, reason: "Session timeouted"})
if @session_id
delete MAP[@session_id]
@session_id = null
didMessage: (payload) ->
if @readyState is Transport.OPEN
@emit('message', {data: payload})
return
send: (payload) ->
if @readyState isnt Transport.OPEN
throw Error('INVALID_STATE_ERR')
@send_buffer.push( payload )
if @recv
@tryFlush()
close: (status=1000, reason="Normal closure") ->
if @readyState isnt Transport.OPEN
return false
@readyState = Transport.CLOSING
@close_frame = closeFrame(status, reason)
if @recv
# Go away.
@recv.doSendFrame(@close_frame)
if @recv
@unregister
toString: ->
r = ['#'+@id]
if @session_id
r.push( @session_id )
if @recv
r.push( @recv.protocol )
return r.join('/')
Session.bySessionId = (session_id) ->
return MAP[session_id] or null
Session.bySessionIdOrNew = (session_id, server) ->
session = Session.bySessionId(session_id)
if not session
session = new Session(session_id, server)
return session
class GenericReceiver
constructor: (@thingy) ->
@setUp(@thingy)
setUp: ->
@thingy_end_cb = () => @didClose(1006, "Connection closed")
@thingy.addListener('end', @thingy_end_cb)
tearDown: ->
@thingy.removeListener('end', @thingy_end_cb)
@thingy_end_cb = null
didClose: (status, reason) ->
if @thingy
@tearDown(@thingy)
try
@thingy.end()
catch x
@thingy = null
if @session
@session.unregister(status, reason)
doSendBulk: (messages) ->
if messages.length is 1
@doSendFrame('m' + JSON.stringify(messages[0]))
else
@doSendFrame('a' + JSON.stringify(messages))
# Write stuff directly to connection.
class ConnectionReceiver extends GenericReceiver
constructor: (@connection) ->
try
@connection.setKeepAlive(true, 5000)
catch x
super @connection
doSendFrame: (payload, encoding='utf-8') ->
if not @connection
return false
try
@connection.write(payload, encoding)
return true
catch e
return false
didClose: ->
super
@connection = null
# Write stuff to response, using chunked encoding if possible.
class ResponseReceiver extends GenericReceiver
max_response_size: undefined
constructor: (@response) ->
@curr_response_size = 0
try
@response.connection.setKeepAlive(true, 5000)
catch x
super (@response)
doSendFrame: (payload) ->
@curr_response_size += payload.length
r = false
try
@response.write(payload)
r = true
catch x
if @max_response_size and @curr_response_size >= @max_response_size
if @session
@session.unregister()
@response.end()
return r
didClose: ->
super
@response = null
exports.Transport = Transport
exports.Session = Session
exports.ConnectionReceiver = ConnectionReceiver
exports.ResponseReceiver = ResponseReceiver