-
-
Notifications
You must be signed in to change notification settings - Fork 79
/
WebSocket.swift
370 lines (335 loc) · 13.6 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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
import NIOCore
import NIOWebSocket
import NIOHTTP1
import NIOSSL
import Foundation
import NIOFoundationCompat
import NIOConcurrencyHelpers
public final class WebSocket: Sendable {
enum PeerType: Sendable {
case server
case client
}
public var eventLoop: EventLoop {
return channel.eventLoop
}
public var isClosed: Bool {
!self.channel.isActive
}
public var closeCode: WebSocketErrorCode? {
_closeCode.withLockedValue { $0 }
}
private let _closeCode: NIOLockedValueBox<WebSocketErrorCode?>
public var onClose: EventLoopFuture<Void> {
self.channel.closeFuture
}
@usableFromInline
/* private but @usableFromInline */
internal let channel: Channel
private let onTextCallback: NIOLoopBoundBox<@Sendable (WebSocket, String) -> ()>
private let onBinaryCallback: NIOLoopBoundBox<@Sendable (WebSocket, ByteBuffer) -> ()>
private let onPongCallback: NIOLoopBoundBox<@Sendable (WebSocket, ByteBuffer) -> ()>
private let onPingCallback: NIOLoopBoundBox<@Sendable (WebSocket, ByteBuffer) -> ()>
private let type: PeerType
private let waitingForPong: NIOLockedValueBox<Bool>
private let waitingForClose: NIOLockedValueBox<Bool>
private let scheduledTimeoutTask: NIOLockedValueBox<Scheduled<Void>?>
private let frameSequence: NIOLockedValueBox<WebSocketFrameSequence?>
private let _pingInterval: NIOLockedValueBox<TimeAmount?>
init(channel: Channel, type: PeerType) {
self.channel = channel
self.type = type
self.onTextCallback = .init({ _, _ in }, eventLoop: channel.eventLoop)
self.onBinaryCallback = .init({ _, _ in }, eventLoop: channel.eventLoop)
self.onPongCallback = .init({ _, _ in }, eventLoop: channel.eventLoop)
self.onPingCallback = .init({ _, _ in }, eventLoop: channel.eventLoop)
self.waitingForPong = .init(false)
self.waitingForClose = .init(false)
self.scheduledTimeoutTask = .init(nil)
self._closeCode = .init(nil)
self.frameSequence = .init(nil)
self._pingInterval = .init(nil)
}
@preconcurrency public func onText(_ callback: @Sendable @escaping (WebSocket, String) -> ()) {
self.onTextCallback.value = callback
}
@preconcurrency public func onBinary(_ callback: @Sendable @escaping (WebSocket, ByteBuffer) -> ()) {
self.onBinaryCallback.value = callback
}
public func onPong(_ callback: @Sendable @escaping (WebSocket, ByteBuffer) -> ()) {
self.onPongCallback.value = callback
}
@available(*, deprecated, message: "Please use `onPong { socket, data in /* … */ }` with the additional `data` parameter.")
@preconcurrency public func onPong(_ callback: @Sendable @escaping (WebSocket) -> ()) {
self.onPongCallback.value = { ws, _ in callback(ws) }
}
public func onPing(_ callback: @Sendable @escaping (WebSocket, ByteBuffer) -> ()) {
self.onPingCallback.value = callback
}
@available(*, deprecated, message: "Please use `onPing { socket, data in /* … */ }` with the additional `data` parameter.")
@preconcurrency public func onPing(_ callback: @Sendable @escaping (WebSocket) -> ()) {
self.onPingCallback.value = { ws, _ in callback(ws) }
}
/// 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 inactive 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 inactive connections, such as a Load Balancer deployed in
/// front of the server.
public var pingInterval: TimeAmount? {
get {
return _pingInterval.withLockedValue { $0 }
}
set {
_pingInterval.withLockedValue { $0 = newValue }
if newValue != nil {
if scheduledTimeoutTask.withLockedValue({ $0 == nil }) {
waitingForPong.withLockedValue { $0 = false }
self.pingAndScheduleNextTimeoutTask()
}
} else {
scheduledTimeoutTask.withLockedValue { $0?.cancel() }
}
}
}
@inlinable
public func send<S>(_ text: S, promise: EventLoopPromise<Void>? = nil)
where S: Collection, S.Element == Character
{
let string = String(text)
let buffer = channel.allocator.buffer(string: string)
self.send(buffer, opcode: .text, fin: true, promise: promise)
}
public func send(_ binary: some DataProtocol, promise: EventLoopPromise<Void>? = nil) {
self.send(raw: binary, opcode: .binary, fin: true, promise: promise)
}
public func sendPing(promise: EventLoopPromise<Void>? = nil) {
sendPing(Data(), promise: promise)
}
public func sendPing(_ data: Data, promise: EventLoopPromise<Void>? = nil) {
self.send(
raw: data,
opcode: .ping,
fin: true,
promise: promise
)
}
@inlinable
public func send<Data>(
raw data: Data,
opcode: WebSocketOpcode,
fin: Bool = true,
promise: EventLoopPromise<Void>? = nil
)
where Data: DataProtocol
{
if let byteBufferView = data as? ByteBufferView {
// optimisation: converting from `ByteBufferView` to `ByteBuffer` doesn't allocate or copy any data
send(ByteBuffer(byteBufferView), opcode: opcode, fin: fin, promise: promise)
} else {
let buffer = channel.allocator.buffer(bytes: data)
send(buffer, opcode: opcode, fin: fin, promise: promise)
}
}
/// Send the provided data in a WebSocket frame.
/// - Parameters:
/// - data: Data to be sent.
/// - opcode: Frame opcode.
/// - fin: The value of the fin bit.
/// - promise: A promise to be completed when the write is complete.
public func send(
_ data: ByteBuffer,
opcode: WebSocketOpcode = .binary,
fin: Bool = true,
promise: EventLoopPromise<Void>? = nil
) {
let frame = WebSocketFrame(
fin: fin,
opcode: opcode,
maskKey: self.makeMaskKey(),
data: data
)
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.withLockedValue({ $0 }) else {
promise?.succeed(())
return
}
self.waitingForClose.withLockedValue { $0 = true }
self._closeCode.withLockedValue { $0 = 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:
/// See https://github.com/apple/swift/issues/66099
var generator = SystemRandomNumberGenerator()
return WebSocketMaskingKey.random(using: &generator)
case .server:
return nil
}
}
func handle(incoming frame: WebSocketFrame) {
switch frame.opcode {
case .connectionClose:
if self.waitingForClose.withLockedValue({ $0 }) {
// 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.value(self, ByteBuffer(buffer: frameData))
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.withLockedValue { $0 = false }
self.onPongCallback.value(self, ByteBuffer(buffer: frameData))
} else {
self.close(code: .protocolError, promise: nil)
}
case .text, .binary:
// create a new frame sequence or use existing
self.frameSequence.withLockedValue { currentFrameSequence in
var frameSequence = currentFrameSequence ?? .init(type: frame.opcode)
// append this frame and update the sequence
frameSequence.append(frame)
currentFrameSequence = frameSequence
}
case .continuation:
/// continuations are filtered by ``NIOWebSocketFrameAggregator``
preconditionFailure("We will never receive a continuation frame")
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
self.frameSequence.withLockedValue { currentFrameSequence in
if let frameSequence = currentFrameSequence, frame.fin {
switch frameSequence.type {
case .binary:
self.onBinaryCallback.value(self, frameSequence.binaryBuffer)
case .text:
self.onTextCallback.value(self, frameSequence.textBuffer)
case .ping, .pong:
assertionFailure("Control frames never have a frameSequence")
default: break
}
currentFrameSequence = nil
}
}
}
@Sendable
private func pingAndScheduleNextTimeoutTask() {
guard channel.isActive, let pingInterval = pingInterval else {
return
}
if waitingForPong.withLockedValue({ $0 }) {
// 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.withLockedValue { $0 = true }
self.scheduledTimeoutTask.withLockedValue {
$0 = self.eventLoop.scheduleTask(
deadline: .now() + pingInterval,
self.pingAndScheduleNextTimeoutTask
)
}
}
}
deinit {
assert(self.isClosed, "WebSocket was not closed before deinit.")
}
}
private struct WebSocketFrameSequence: Sendable {
var binaryBuffer: ByteBuffer
var textBuffer: String
let type: WebSocketOpcode
let lock: NIOLock
init(type: WebSocketOpcode) {
self.binaryBuffer = ByteBufferAllocator().buffer(capacity: 0)
self.textBuffer = .init()
self.type = type
self.lock = .init()
}
mutating func append(_ frame: WebSocketFrame) {
self.lock.withLockVoid {
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
}
}
}
}