Skip to content

Commit

Permalink
v1.1.0
Browse files Browse the repository at this point in the history
- Changed all protocol functions to be optional
- Leaving default to accept new peers
  • Loading branch information
maxxfrazer committed Nov 27, 2019
2 parents ff39ccf + bc85e38 commit dbf5d3b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 20 deletions.
18 changes: 9 additions & 9 deletions Sources/MultipeerHelper/MultipeerHelper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -92,14 +92,14 @@ extension MultipeerHelper: MCSessionDelegate {
didChange state: MCSessionState
) {
if state == .connected {
delegate?.peerJoined(peerID)
delegate?.peerJoined?(peerID)
} else if state == .notConnected {
delegate?.peerLeft(peerID)
delegate?.peerLeft?(peerID)
}
}

public func session(_: MCSession, didReceive data: Data, fromPeer peerID: MCPeerID) {
delegate?.receivedData(data, peerID)
delegate?.receivedData?(data, peerID)
}

public func session(
Expand All @@ -108,7 +108,7 @@ extension MultipeerHelper: MCSessionDelegate {
withName streamName: String,
fromPeer peerID: MCPeerID
) {
delegate?.receivedStream(stream, streamName, peerID)
delegate?.receivedStream?(stream, streamName, peerID)
}

public func session(
Expand All @@ -117,7 +117,7 @@ extension MultipeerHelper: MCSessionDelegate {
fromPeer peerID: MCPeerID,
with progress: Progress
) {
delegate?.receivingResource(resourceName, peerID, progress)
delegate?.receivingResource?(resourceName, peerID, progress)
}

public func session(
Expand All @@ -127,7 +127,7 @@ extension MultipeerHelper: MCSessionDelegate {
at localURL: URL?,
withError error: Error?
) {
delegate?.receivedResource(resourceName, peerID, localURL, error)
delegate?.receivedResource?(resourceName, peerID, localURL, error)
}
}

Expand All @@ -139,14 +139,14 @@ extension MultipeerHelper: MCNearbyServiceBrowserDelegate {
withDiscoveryInfo _: [String: String]?
) {
// Ask the handler whether we should invite this peer or not
let accepted = delegate?.peerDiscovered(peerID)
if accepted != false {
let accepted = delegate?.peerDiscovered?(peerID) ?? false
if delegate?.peerDiscovered == nil || accepted != false {
browser.invitePeer(peerID, to: session, withContext: nil, timeout: 10)
}
}

public func browser(_: MCNearbyServiceBrowser, lostPeer peerID: MCPeerID) {
delegate?.peerLost(peerID)
delegate?.peerLost?(peerID)
}
}

Expand Down
20 changes: 9 additions & 11 deletions Sources/MultipeerHelper/MultipeerHelperDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,30 @@

import MultipeerConnectivity

public protocol MultipeerHelperDelegate: AnyObject {}

public extension MultipeerHelperDelegate {
@objc public protocol MultipeerHelperDelegate: AnyObject {
/// Data that has been recieved from another peer
/// - Parameters:
/// - data: The data which has been recieved
/// - peer: The peer that sent the data
func receivedData(_ data: Data, _ peer: MCPeerID) {}
@objc optional func receivedData(_ data: Data, _ peer: MCPeerID)

/// Callback for when a peer joins the network
/// - Parameter peer: the `MCPeerID` of the newly joined peer
func peerJoined(_ peer: MCPeerID) {}
@objc optional func peerJoined(_ peer: MCPeerID)

/// Callback for when a peer leaves the network
/// - Parameter peer: the `MCPeerID` of the peer that left
func peerLeft(_ peer: MCPeerID) {}
@objc optional func peerLeft(_ peer: MCPeerID)

/// Callback for when a peer new peer has been found. will default to accept all peers
/// - Parameter peer: the `MCPeerID` of the peer who wants to join the network
/// - Returns: Bool if the peer should be invited to the network or not
func peerDiscovered(_ peer: MCPeerID) -> Bool { true }
@objc optional func peerDiscovered(_ peer: MCPeerID) -> Bool

/// Peer can no longer be found on the network, and thus cannot receive data
/// - Parameter peer: If a peer has left the network in a non typical way
func peerLost(_ peer: MCPeerID) {}
func receivedStream(_ stream: InputStream, _ streamName: String, _ peer: MCPeerID) {}
func receivingResource(_ resourceName: String, _ peer: MCPeerID, _ progress: Progress) {}
func receivedResource(_ resourceName: String, _ peer: MCPeerID, _ url: URL?, _ error: Error?) {}
@objc optional func peerLost(_ peer: MCPeerID)
@objc optional func receivedStream(_ stream: InputStream, _ streamName: String, _ peer: MCPeerID)
@objc optional func receivingResource(_ resourceName: String, _ peer: MCPeerID, _ progress: Progress)
@objc optional func receivedResource(_ resourceName: String, _ peer: MCPeerID, _ url: URL?, _ error: Error?)
}

0 comments on commit dbf5d3b

Please sign in to comment.