Improve socket handling
This commit is contained in:
parent
562c6fb9c1
commit
5c4a6597d2
@ -16,9 +16,11 @@ final class DeviceManager {
|
|||||||
/// Indicate that the socket is fully initialized with an authorized device
|
/// Indicate that the socket is fully initialized with an authorized device
|
||||||
var deviceIsAuthenticated = false
|
var deviceIsAuthenticated = false
|
||||||
|
|
||||||
|
private var isOpeningNewConnection = false
|
||||||
|
|
||||||
/// Indicator for device availability
|
/// Indicator for device availability
|
||||||
var deviceIsConnected: Bool {
|
var deviceIsConnected: Bool {
|
||||||
!(connection?.isClosed ?? true) && deviceIsAuthenticated
|
deviceIsAuthenticated && !(connection?.isClosed ?? true)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A promise to finish the request once the device responds or times out
|
/// A promise to finish the request once the device responds or times out
|
||||||
@ -75,8 +77,12 @@ final class DeviceManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func didCloseDeviceSocket() {
|
func didCloseDeviceSocket() {
|
||||||
|
guard !isOpeningNewConnection else {
|
||||||
|
return
|
||||||
|
}
|
||||||
deviceIsAuthenticated = false
|
deviceIsAuthenticated = false
|
||||||
guard connection != nil else {
|
guard connection != nil else {
|
||||||
|
print("Socket closed, but no connection anyway")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
connection = nil
|
connection = nil
|
||||||
@ -84,14 +90,20 @@ final class DeviceManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func removeDeviceConnection() {
|
func removeDeviceConnection() {
|
||||||
_ = connection?.close()
|
deviceIsAuthenticated = false
|
||||||
|
guard let socket = connection else {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
try? socket.close().wait()
|
||||||
connection = nil
|
connection = nil
|
||||||
|
print("Removed device connection")
|
||||||
}
|
}
|
||||||
|
|
||||||
func createNewDeviceConnection(_ socket: WebSocket) {
|
func createNewDeviceConnection(_ socket: WebSocket) {
|
||||||
|
isOpeningNewConnection = true
|
||||||
removeDeviceConnection()
|
removeDeviceConnection()
|
||||||
connection = socket
|
connection = socket
|
||||||
deviceIsAuthenticated = false
|
|
||||||
print("Socket connected")
|
print("Socket connected")
|
||||||
|
isOpeningNewConnection = false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user