Skip to content

Commit

Permalink
Fix multi-thread issues on socket client
Browse files Browse the repository at this point in the history
  • Loading branch information
enums committed Jan 2, 2022
1 parent 1c82d12 commit 858cb47
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions Sources/Heze/Socket/HezeSocketHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ open class HezeSocketHandler: HezeHandler, WebSocketSessionHandler {
public var socket: WebSocket? = nil
public var remoteHost: String?
public var remotePort: UInt16?
public var queue = DispatchQueue(label: "heze.socket.handler", attributes: .concurrent)
public var queue = DispatchQueue(label: "heze.socket.handler")
public var buffer = [UInt8]()

open var socketProtocol: String? {
Expand Down Expand Up @@ -108,10 +108,14 @@ open class HezeSocketHandler: HezeHandler, WebSocketSessionHandler {
open func handleBytes(_ bytes: [UInt8], opcodeType: WebSocket.OpcodeType, final: Bool, socket: WebSocket, completion: @escaping (HezeResponsable?) -> Void) {
buffer.append(contentsOf: bytes)
if final {
defer {
buffer.removeAll(keepingCapacity: false)
receiveBytes(buffer) { [weak self] in
guard let self = self else {
completion($0)
return
}
self.buffer.removeAll(keepingCapacity: false)
completion($0)
}
receiveBytes(buffer, completion: completion)
} else {
completion(nil)
}
Expand Down

0 comments on commit 858cb47

Please sign in to comment.