Skip to content

Commit

Permalink
make an explicit start and stop for listening on Peer2Peer network
Browse files Browse the repository at this point in the history
  • Loading branch information
heckj committed Apr 11, 2024
1 parent 84f63f1 commit 719e5b6
Showing 1 changed file with 18 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -221,9 +221,6 @@ public actor PeerToPeerProvider: NetworkProvider {
let defaultName = await PeerToPeerProviderConfiguration.defaultSharingIdentity()
resetName(defaultName)
}
if listener == nil {
self.setupBonjourListener()
}
}
}

Expand All @@ -247,6 +244,24 @@ public actor PeerToPeerProvider: NetworkProvider {
}
}

public func startListening(as peerName: String? = nil) async throws {
if let peerName {
resetName(peerName)
}
if self.peerName.isEmpty {
throw Errors.NetworkProviderError(msg: "No peer name is set on the provider")
}
if listener == nil {
self.setupBonjourListener()
}
}

public func stopListening() async {
await disconnect()
listener?.cancel()
listener = nil
}

// MARK: Outgoing connection functions

// Returns a new websocketTask to track (at which point, save the url as the endpoint)
Expand Down Expand Up @@ -726,12 +741,6 @@ public actor PeerToPeerProvider: NetworkProvider {
Logger.peerProtocol.warning("receive and reconnect loop for \(endpoint.debugDescription) terminated")
}

// Stop all listeners.
fileprivate func stopListening() {
listener?.cancel()
listener = nil
}

// Update the advertised name on the network.
fileprivate func resetName(_ name: String) {
self.peerName = name
Expand Down

0 comments on commit 719e5b6

Please sign in to comment.