Skip to content

Commit

Permalink
fix: added peerClosed case for WebSocketEvent enum (#946)
Browse files Browse the repository at this point in the history
  • Loading branch information
JeneaVranceanu authored Aug 16, 2023
1 parent cb131f8 commit f900d67
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 5 deletions.
2 changes: 2 additions & 0 deletions Sources/Engine/WSEngine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,8 @@ FrameCollectorDelegate, HTTPHandlerDelegate {
}
case .cancelled:
broadcast(event: .cancelled)
case .peerClosed:
broadcast(event: .peerClosed)
}
}

Expand Down
2 changes: 2 additions & 0 deletions Sources/Server/WebSocketServer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,8 @@ public class ServerConnection: Connection, HTTPServerDelegate, FramerEventClient
case .cancelled:
print("server connection cancelled!")
//broadcast(event: .cancelled)
case .peerClosed:
delegate?.didReceive(event: .disconnected(self, "Connection closed by peer", UInt16(FrameOpCode.connectionClose.rawValue)))
}
}

Expand Down
1 change: 1 addition & 0 deletions Sources/Starscream/WebSocket.swift
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ public enum WebSocketEvent {
case viabilityChanged(Bool)
case reconnectSuggested(Bool)
case cancelled
case peerClosed
}

public protocol WebSocketDelegate: AnyObject {
Expand Down
7 changes: 7 additions & 0 deletions Sources/Transport/TCPTransport.swift
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,13 @@ public class TCPTransport: Transport {

// Refer to https://developer.apple.com/documentation/network/implementing_netcat_with_network_framework
if let context = context, context.isFinal, isComplete {
if let delegate = s.delegate {
// Let the owner of this TCPTransport decide what to do next: disconnect or reconnect?
delegate.connectionChanged(state: .peerClosed)
} else {
// No use to keep connection alive
s.disconnect()
}
return
}

Expand Down
20 changes: 15 additions & 5 deletions Sources/Transport/Transport.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,31 @@
import Foundation

public enum ConnectionState {
/// Ready connections can send and receive data
case connected

/// Waiting connections have not yet been started, or do not have a viable network
case waiting

/// Cancelled connections have been invalidated by the client and will send no more events
case cancelled

/// Failed connections are disconnected and can no longer send or receive data
case failed(Error?)

//the viability (connection status) of the connection has updated
//e.g. connection is down, connection came back up, etc
/// Viability (connection status) of the connection has updated
/// e.g. connection is down, connection came back up, etc.
case viability(Bool)

//the connection has upgrade to wifi from cellular.
//you should consider reconnecting to take advantage of this
/// Connection ca be upgraded to wifi from cellular.
/// You should consider reconnecting to take advantage of this.
case shouldReconnect(Bool)

//the connection receive data
/// Received data
case receive(Data)

/// Remote peer has closed the network connection.
case peerClosed
}

public protocol TransportEventClient: AnyObject {
Expand Down
2 changes: 2 additions & 0 deletions Tests/FuzzingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ class FuzzingTests: XCTestCase {
break
case .cancelled:
break
case .peerClosed:
break
}
}
websocket.connect()
Expand Down

0 comments on commit f900d67

Please sign in to comment.