Skip to content

Commit

Permalink
chore: increase size of buffer and remove lock
Browse files Browse the repository at this point in the history
  • Loading branch information
koraykoska committed Oct 3, 2023
1 parent 2d8be89 commit 4778cf5
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 56 deletions.
110 changes: 55 additions & 55 deletions Sources/libwebsockets/WebsocketClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ public class WebsocketClient: WebsocketConnection {
fileprivate let waitingLwsCloseStatus: NIOLockedValueBox<WebsocketCloseStatus?> = .init(nil)

/// Internally managed buffer of frames. Emitted once the full message is there.
fileprivate let frameSequence: NIOLockedValueBox<WebsocketFrameSequence?> = .init(nil)
/// Internal Note: This doesn't need to be a NIOLockedValueBox as we only access it from
/// the EventLoop.
fileprivate var frameSequence: WebsocketFrameSequence? = nil
fileprivate let frameSequenceType: WebsocketFrameSequence.Type

// State variables
Expand Down Expand Up @@ -762,73 +764,71 @@ internal func _lws_swift_websocketClientCallback(
}

websocketClient.eventLoop.execute {
websocketClient.frameSequence.withLockedValue({ currentFrameSequence in
if isFirst && isFinal {
// We can skip everything below. It's a simple message
if isFirst && isFinal {
// We can skip everything below. It's a simple message

// We don't check max message size here. If user set `maxFrameSize`
// greater than `maxMessageSize`, he will have to deal with the consequences.
// If not, this case is handled by `rx_buffer_size` in libwebsockets already.
// We don't check max message size here. If user set `maxFrameSize`
// greater than `maxMessageSize`, he will have to deal with the consequences.
// If not, this case is handled by `rx_buffer_size` in libwebsockets already.

if isBinary {
if isBinary {
websocketClient.eventLoop.execute {
websocketClient.onBinaryCallback?.value(websocketClient, data)
}
} else {
if let stringMessage = String(data: data, encoding: .utf8) {
websocketClient.eventLoop.execute {
websocketClient.onBinaryCallback?.value(websocketClient, data)
websocketClient.onTextCallback?.value(websocketClient, stringMessage)
}
} else {
if let stringMessage = String(data: data, encoding: .utf8) {
websocketClient.eventLoop.execute {
websocketClient.onTextCallback?.value(websocketClient, stringMessage)
}
} else {
websocketClient.close(reason: .invalidPayload)
}
websocketClient.close(reason: .invalidPayload)
}

currentFrameSequence = nil
return
}

var frameSequence = currentFrameSequence ?? websocketClient.frameSequenceType.init(type: isBinary ? .binary : .text)
// Append the frame and update the sequence
frameSequence.append(data)

// Check message size
let messageSize = isBinary ? frameSequence.binaryBuffer.count : frameSequence.textBuffer.count
if let maxMessageSize = websocketClient.maxMessageSize, messageSize > maxMessageSize {
// Close connection
websocketClient.close(reason: .messageTooLarge)
// Reset frame sequence just in case
currentFrameSequence = nil
return
}
websocketClient.frameSequence = nil
return
}

var frameSequence = websocketClient.frameSequence ?? websocketClient.frameSequenceType.init(type: isBinary ? .binary : .text)
// Append the frame and update the sequence
frameSequence.append(data)

// Check message size
let messageSize = isBinary ? frameSequence.binaryBuffer.count : frameSequence.textBuffer.count
if let maxMessageSize = websocketClient.maxMessageSize, messageSize > maxMessageSize {
// Close connection
websocketClient.close(reason: .messageTooLarge)
// Reset frame sequence just in case
websocketClient.frameSequence = nil
return
}

// Set current frame sequence
currentFrameSequence = frameSequence
// Set current frame sequence
websocketClient.frameSequence = frameSequence

if isFinal {
switch frameSequence.type {
case .binary:
websocketClient.eventLoop.execute {
websocketClient.onBinaryCallback?.value(websocketClient, frameSequence.binaryBuffer)
}
break
case .text:
websocketClient.eventLoop.execute {
guard let text = String(data: frameSequence.textBuffer, encoding: .utf8) else {
websocketClient.close(reason: .invalidPayload)
return
}
websocketClient.onTextCallback?.value(websocketClient, text)
if isFinal {
switch frameSequence.type {
case .binary:
websocketClient.eventLoop.execute {
websocketClient.onBinaryCallback?.value(websocketClient, frameSequence.binaryBuffer)
}
break
case .text:
websocketClient.eventLoop.execute {
guard let text = String(data: frameSequence.textBuffer, encoding: .utf8) else {
websocketClient.close(reason: .invalidPayload)
return
}
break
default:
// Should never happen. If it does, do nothing.
break
websocketClient.onTextCallback?.value(websocketClient, text)
}

currentFrameSequence = nil
break
default:
// Should never happen. If it does, do nothing.
break
}
})

websocketClient.frameSequence = nil
}
}
break
case LWS_CALLBACK_CLIENT_WRITEABLE:
Expand Down
2 changes: 1 addition & 1 deletion Sources/libwebsockets/WebsocketClientContext.swift
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ internal final class WebsocketClientContext {
lwsProtocols.callback = _lws_swift_websocketClientCallback
lwsProtocols.per_session_data_size = 0
// TODO: Per instance customization?
lwsProtocols.rx_buffer_size = 100000
lwsProtocols.rx_buffer_size = 1000000

protocolsPointer.initialize(to: lwsProtocols)

Expand Down

0 comments on commit 4778cf5

Please sign in to comment.