forked from vapor/websocket-kit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWebSocket.swift
323 lines (293 loc) · 10.8 KB
/
WebSocket.swift
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
import NIO
import NIOWebSocket
import NIOHTTP1
import NIOSSL
import Foundation
import NIOFoundationCompat
public final class WebSocket {
enum PeerType {
case server
case client
}
public var eventLoop: EventLoop {
return channel.eventLoop
}
public var isClosed: Bool {
!self.channel.isActive
}
public private(set) var closeCode: WebSocketErrorCode?
public var onClose: EventLoopFuture<Void> {
self.channel.closeFuture
}
private let channel: Channel
private var onTextCallback: (WebSocket, String) -> ()
private var onBinaryCallback: (WebSocket, ByteBuffer) -> ()
private var onPongCallback: (WebSocket) -> ()
private var onPingCallback: (WebSocket) -> ()
private var frameSequence: WebSocketFrameSequence?
private let type: PeerType
private var waitingForPong: Bool
private var waitingForClose: Bool
private var scheduledTimeoutTask: Scheduled<Void>?
init(channel: Channel, type: PeerType) {
self.channel = channel
self.type = type
self.onTextCallback = { _, _ in }
self.onBinaryCallback = { _, _ in }
self.onPongCallback = { _ in }
self.onPingCallback = { _ in }
self.waitingForPong = false
self.waitingForClose = false
self.scheduledTimeoutTask = nil
}
public func onText(_ callback: @escaping (WebSocket, String) -> ()) {
self.onTextCallback = callback
}
public func onBinary(_ callback: @escaping (WebSocket, ByteBuffer) -> ()) {
self.onBinaryCallback = callback
}
public func onPong(_ callback: @escaping (WebSocket) -> ()) {
self.onPongCallback = callback
}
public func onPing(_ callback: @escaping (WebSocket) -> ()) {
self.onPingCallback = callback
}
/// If set, this will trigger automatic pings on the connection. If ping is not answered before
/// the next ping is sent, then the WebSocket will be presumed innactive and will be closed
/// automatically.
/// These pings can also be used to keep the WebSocket alive if there is some other timeout
/// mechanism shutting down innactive connections, such as a Load Balancer deployed in
/// front of the server.
public var pingInterval: TimeAmount? {
didSet {
if pingInterval != nil {
if scheduledTimeoutTask == nil {
waitingForPong = false
self.pingAndScheduleNextTimeoutTask()
}
} else {
scheduledTimeoutTask?.cancel()
}
}
}
public func send<S>(_ text: S, promise: EventLoopPromise<Void>? = nil)
where S: Collection, S.Element == Character
{
let string = String(text)
var buffer = channel.allocator.buffer(capacity: text.count)
buffer.writeString(string)
self.send(raw: buffer.readableBytesView, opcode: .text, fin: true, promise: promise)
}
public func send(_ binary: [UInt8], promise: EventLoopPromise<Void>? = nil) {
self.send(raw: binary, opcode: .binary, fin: true, promise: promise)
}
public func sendPing(promise: EventLoopPromise<Void>? = nil) {
self.send(
raw: Data(),
opcode: .ping,
fin: true,
promise: promise
)
}
public func send<Data>(
raw data: Data,
opcode: WebSocketOpcode,
fin: Bool = true,
promise: EventLoopPromise<Void>? = nil
)
where Data: DataProtocol
{
var buffer = channel.allocator.buffer(capacity: data.count)
buffer.writeBytes(data)
let frame = WebSocketFrame(
fin: fin,
opcode: opcode,
maskKey: self.makeMaskKey(),
data: buffer
)
self.channel.writeAndFlush(frame, promise: promise)
}
public func close(code: WebSocketErrorCode = .goingAway) -> EventLoopFuture<Void> {
let promise = self.eventLoop.makePromise(of: Void.self)
self.close(code: code, promise: promise)
return promise.futureResult
}
public func close(
code: WebSocketErrorCode = .goingAway,
promise: EventLoopPromise<Void>?
) {
guard !self.isClosed else {
promise?.succeed(())
return
}
guard !self.waitingForClose else {
promise?.succeed(())
return
}
self.waitingForClose = true
self.closeCode = code
let codeAsInt = UInt16(webSocketErrorCode: code)
let codeToSend: WebSocketErrorCode
if codeAsInt == 1005 || codeAsInt == 1006 {
/// Code 1005 and 1006 are used to report errors to the application, but must never be sent over
/// the wire (per https://tools.ietf.org/html/rfc6455#section-7.4)
codeToSend = .normalClosure
} else {
codeToSend = code
}
var buffer = channel.allocator.buffer(capacity: 2)
buffer.write(webSocketErrorCode: codeToSend)
self.send(raw: buffer.readableBytesView, opcode: .connectionClose, fin: true, promise: promise)
}
func makeMaskKey() -> WebSocketMaskingKey? {
switch type {
case .client:
var bytes: [UInt8] = []
for _ in 0..<4 {
bytes.append(.random(in: .min ..< .max))
}
return WebSocketMaskingKey(bytes)
case .server:
return nil
}
}
func handle(incoming frame: WebSocketFrame) {
switch frame.opcode {
case .connectionClose:
if self.waitingForClose {
// peer confirmed close, time to close channel
self.channel.close(mode: .all, promise: nil)
} else {
// peer asking for close, confirm and close output side channel
let promise = self.eventLoop.makePromise(of: Void.self)
var data = frame.data
let maskingKey = frame.maskKey
if let maskingKey = maskingKey {
data.webSocketUnmask(maskingKey)
}
self.close(
code: data.readWebSocketErrorCode() ?? .unknown(1005),
promise: promise
)
promise.futureResult.whenComplete { _ in
self.channel.close(mode: .all, promise: nil)
}
}
case .ping:
if frame.fin {
var frameData = frame.data
let maskingKey = frame.maskKey
if let maskingKey = maskingKey {
frameData.webSocketUnmask(maskingKey)
}
self.onPingCallback(self)
self.send(
raw: frameData.readableBytesView,
opcode: .pong,
fin: true,
promise: nil
)
} else {
self.close(code: .protocolError, promise: nil)
}
case .pong:
if frame.fin {
var frameData = frame.data
let maskingKey = frame.maskKey
if let maskingKey = maskingKey {
frameData.webSocketUnmask(maskingKey)
}
self.waitingForPong = false
self.onPongCallback(self)
} else {
self.close(code: .protocolError, promise: nil)
}
case .text, .binary:
// create a new frame sequence or use existing
var frameSequence: WebSocketFrameSequence
if let existing = self.frameSequence {
frameSequence = existing
} else {
frameSequence = WebSocketFrameSequence(type: frame.opcode)
}
// append this frame and update the sequence
frameSequence.append(frame)
self.frameSequence = frameSequence
case .continuation:
// we must have an existing sequence
if var frameSequence = self.frameSequence {
// append this frame and update
frameSequence.append(frame)
self.frameSequence = frameSequence
} else {
self.close(code: .protocolError, promise: nil)
}
default:
// We ignore all other frames.
break
}
// if this frame was final and we have a non-nil frame sequence,
// output it to the websocket and clear storage
if let frameSequence = self.frameSequence, frame.fin {
switch frameSequence.type {
case .binary:
self.onBinaryCallback(self, frameSequence.binaryBuffer)
case .text:
self.onTextCallback(self, frameSequence.textBuffer)
case .ping, .pong:
assertionFailure("Control frames never have a frameSequence")
default: break
}
self.frameSequence = nil
}
}
private func pingAndScheduleNextTimeoutTask() {
guard channel.isActive, let pingInterval = pingInterval else {
return
}
if waitingForPong {
// We never received a pong from our last ping, so the connection has timed out
let promise = self.eventLoop.makePromise(of: Void.self)
self.close(code: .unknown(1006), promise: promise)
promise.futureResult.whenComplete { _ in
// Usually, closing a WebSocket is done by sending the close frame and waiting
// for the peer to respond with their close frame. We are in a timeout situation,
// so the other side likely will never send the close frame. We just close the
// channel ourselves.
self.channel.close(mode: .all, promise: nil)
}
} else {
self.sendPing()
self.waitingForPong = true
self.scheduledTimeoutTask = self.eventLoop.scheduleTask(
deadline: .now() + pingInterval,
self.pingAndScheduleNextTimeoutTask
)
}
}
deinit {
assert(self.isClosed, "WebSocket was not closed before deinit.")
}
}
private struct WebSocketFrameSequence {
var binaryBuffer: ByteBuffer
var textBuffer: String
var type: WebSocketOpcode
init(type: WebSocketOpcode) {
self.binaryBuffer = ByteBufferAllocator().buffer(capacity: 0)
self.textBuffer = .init()
self.type = type
}
mutating func append(_ frame: WebSocketFrame) {
var data = frame.unmaskedData
switch type {
case .binary:
self.binaryBuffer.writeBuffer(&data)
case .text:
if let string = data.readString(length: data.readableBytes) {
self.textBuffer += string
}
default: break
}
}
}