RTMPNWSoccket will be gracefully close.

This commit is contained in:
shogo4405 2023-04-23 21:53:28 +09:00
parent 534076025b
commit d5fd16b334
1 changed files with 16 additions and 6 deletions

View File

@ -54,15 +54,16 @@ final class RTMPNWSocket: RTMPSocketCompatible {
private var connection: NWConnection? {
didSet {
oldValue?.stateUpdateHandler = nil
oldValue?.forceCancel()
oldValue?.cancel()
if connection == nil {
connected = false
readyState = .closed
}
}
}
private var parameters: NWParameters = .tcp
private lazy var inputQueue = DispatchQueue(label: "com.haishinkit.HaishinKit.NWSocket.input", qos: qualityOfService)
private lazy var outputQueue = DispatchQueue(label: "com.haishinkit.HaishinKit.NWSocket.output", qos: qualityOfService)
private lazy var inputQueue = DispatchQueue(label: "com.haishinkit.HaishinKit.RTMPNWSocket.input", qos: qualityOfService)
private lazy var outputQueue = DispatchQueue(label: "com.haishinkit.HaishinKit.RTMPNWSocket.output", qos: qualityOfService)
private var timeoutHandler: DispatchWorkItem?
func connect(withName: String, port: Int) {
@ -93,7 +94,7 @@ final class RTMPNWSocket: RTMPSocketCompatible {
}
func close(isDisconnected: Bool) {
guard connection != nil else {
guard let connection else {
return
}
if isDisconnected {
@ -102,8 +103,17 @@ final class RTMPNWSocket: RTMPSocketCompatible {
events.append(Event(type: .rtmpStatus, bubbles: false, data: data))
}
readyState = .closing
if connection.state == .ready {
outputQueue.async {
let completion: NWConnection.SendCompletion = .contentProcessed { (_: Error?) in
self.connection = nil
}
connection.send(content: nil, contentContext: .finalMessage, isComplete: true, completion: completion)
}
} else {
self.connection = nil
}
timeoutHandler?.cancel()
connection = nil
}
@discardableResult
@ -195,7 +205,7 @@ final class RTMPNWSocket: RTMPSocketCompatible {
}
inputBuffer.removeAll()
readyState = .handshakeDone
case .handshakeDone:
case .handshakeDone, .closing:
if inputBuffer.isEmpty {
break
}