Skip to content

Commit

Permalink
feat: faster chunks
Browse files Browse the repository at this point in the history
  • Loading branch information
koraykoska committed Aug 17, 2023
1 parent 271b1a0 commit 9ccfd2a
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions Sources/libwebsockets/WebsocketClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -523,17 +523,19 @@ public class WebsocketClient: WebsocketConnection {

switch opcode {
case .binary, .text, .continuation:
let splitted = data.chunked(into: maxFrameSize)
let splitted = data.count > maxFrameSize ? data.chunked(into: maxFrameSize) : [data]
var writeChunks = [(data: Data, opcode: WebsocketOpcode, fin: Bool, promise: EventLoopPromise<Void>?)]()
for i in 0..<splitted.count {
toBeWritten.withLockedValue({
$0.append((
data: splitted[i],
opcode: i == 0 ? opcode : .continuation,
fin: i == splitted.count - 1 ? fin : false,
promise: i == splitted.count - 1 ? promise : nil
))
})
writeChunks.append((
data: splitted[i],
opcode: i == 0 ? opcode : .continuation,
fin: i == splitted.count - 1 ? fin : false,
promise: i == splitted.count - 1 ? promise : nil
))
}
toBeWritten.withLockedValue({
$0.append(contentsOf: writeChunks)
})
case .ping, .close:
toBeWritten.withLockedValue({
$0.append((
Expand Down

0 comments on commit 9ccfd2a

Please sign in to comment.