Skip to content

Commit ea73ccd

Browse files
authored
Merge pull request #13 from maxxfrazer/helper-to-delegates
MultipeerHelper in Delegate Params
2 parents 03c2690 + 92a4ffe commit ea73ccd

File tree

4 files changed

+89
-30
lines changed

4 files changed

+89
-30
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
## Build generated
1111
build/
1212
DerivedData/
13+
MultipeerHelper.xcodeproj
1314

1415
## Various settings
1516
*.pbxuser

Sources/MultipeerHelper/HasSynchronization+Extensions.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,15 @@
88

99
#if !os(tvOS)
1010
import RealityKit
11-
#endif
1211

12+
/// Error enum for some common Multipeer Errors
1313
public enum MHelperErrors: Error {
14+
/// Request timed out
1415
case timedOut
16+
/// Request failed
1517
case failure
1618
}
1719

18-
#if !os(tvOS)
1920
@available(iOS 13.0, macOS 10.15, *)
2021
public extension HasSynchronization {
2122
/// Execute the escaping completion if you are the entity owner, once you receive ownership

Sources/MultipeerHelper/MultipeerHelper.swift

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import Foundation
1111
import RealityKit
1212
#endif
1313

14+
/// A class made to wrap around an [MCSession](doc://MultipeerHelper/documentation/multipeerhelper/multipeerhelper/session) to simplify usage.
1415
public class MultipeerHelper: NSObject {
1516
/// What type of session you want to make.
1617
///
@@ -23,12 +24,16 @@ public class MultipeerHelper: NSObject {
2324
case both = 3
2425
}
2526

27+
/// Key added to discovery info to check RealityKit compatibility token
2628
public static let compTokenKey = "MPH_CompToken"
29+
/// Key added to discovery info to show OS version
2730
public static let osVersionKey = "MPH_OSVersion"
31+
/// Key added to discovery info to show device platform
2832
public static let platformKey = "MPH_Platform"
2933

3034
/// Detemines whether your service is advertising, browsing, or both.
3135
public let sessionType: SessionType
36+
/// Name of the service, created at initialisation
3237
public let serviceName: String
3338

3439
#if canImport(RealityKit)
@@ -39,16 +44,20 @@ public class MultipeerHelper: NSObject {
3944
}
4045
#endif
4146

42-
public var myPeerID: MCPeerID
47+
/// ``MCPeerID`` set at initialisation. Default will be device name.
48+
public internal(set) var myPeerID: MCPeerID
4349

4450
/// Quick lookup for a peer given their displayName
4551
private var peerIDLookup: [String: MCPeerID] = [:]
4652

4753
/// The MultipeerConnectivity session being used
4854
public private(set) var session: MCSession!
55+
/// MultipeerConnectivity advertiser
4956
public private(set) var serviceAdvertiser: MCNearbyServiceAdvertiser?
57+
/// MultipeerConnectivity browser
5058
public private(set) var serviceBrowser: MCNearbyServiceBrowser?
5159

60+
/// Delegate used to get some callback methods including data received
5261
public weak var delegate: MultipeerHelperDelegate?
5362
/// Initializes a Multipeer Helper.
5463
/// Pass your own peerName to avoid exceptions, as there are restrictions set by Apple on what is allowed.
@@ -213,16 +222,16 @@ extension MultipeerHelper: MCSessionDelegate {
213222
) {
214223
if state == .connected {
215224
peerIDLookup[peerID.displayName] = peerID
216-
delegate?.peerJoined?(peerID)
225+
delegate?.peerJoined?(peerHelper: self, peerID)
217226
self.serviceBrowser?.stopBrowsingForPeers()
218227
} else if state == .notConnected {
219228
peerIDLookup.removeValue(forKey: peerID.displayName)
220-
delegate?.peerLeft?(peerID)
229+
delegate?.peerLeft?(peerHelper: self, peerID)
221230
}
222231
}
223232

224233
public func session(_: MCSession, didReceive data: Data, fromPeer peerID: MCPeerID) {
225-
delegate?.receivedData?(data, peerID)
234+
delegate?.receivedData?(peerHelper: self, data, peerID)
226235
}
227236

228237
public func session(
@@ -231,7 +240,7 @@ extension MultipeerHelper: MCSessionDelegate {
231240
withName streamName: String,
232241
fromPeer peerID: MCPeerID
233242
) {
234-
delegate?.receivedStream?(stream, streamName, peerID)
243+
delegate?.receivedStream?(peerHelper: self, stream, streamName, peerID)
235244
}
236245

237246
public func session(
@@ -240,7 +249,7 @@ extension MultipeerHelper: MCSessionDelegate {
240249
fromPeer peerID: MCPeerID,
241250
with progress: Progress
242251
) {
243-
delegate?.receivingResource?(resourceName, peerID, progress)
252+
delegate?.receivingResource?(peerHelper: self, resourceName, peerID, progress)
244253
}
245254

246255
public func session(
@@ -250,7 +259,7 @@ extension MultipeerHelper: MCSessionDelegate {
250259
at localURL: URL?,
251260
withError error: Error?
252261
) {
253-
delegate?.receivedResource?(resourceName, peerID, localURL, error)
262+
delegate?.receivedResource?(peerHelper: self, resourceName, peerID, localURL, error)
254263
}
255264

256265
public func session(
@@ -260,7 +269,7 @@ extension MultipeerHelper: MCSessionDelegate {
260269
certificateHandler: @escaping (Bool) -> Void
261270
) {
262271
if let certificateApproved = self.delegate?.receivedCertificate?(
263-
certificate: certificate, fromPeer: peerID
272+
peerHelper: self, certificate: certificate, fromPeer: peerID
264273
) {
265274
certificateHandler(certificateApproved)
266275
return
@@ -277,13 +286,13 @@ extension MultipeerHelper: MCNearbyServiceBrowserDelegate {
277286
withDiscoveryInfo info: [String: String]?
278287
) {
279288
// Ask the handler whether we should invite this peer or not
280-
if delegate?.shouldSendJoinRequest == nil || (delegate?.shouldSendJoinRequest?(peerID, with: info) ?? false) {
289+
if delegate?.shouldSendJoinRequest == nil || (delegate?.shouldSendJoinRequest?(peerHelper: self, peerID, with: info) ?? false) {
281290
browser.invitePeer(peerID, to: session, withContext: nil, timeout: 10)
282291
}
283292
}
284293

285294
public func browser(_: MCNearbyServiceBrowser, lostPeer peerID: MCPeerID) {
286-
delegate?.peerLost?(peerID)
295+
delegate?.peerLost?(peerHelper: self, peerID)
287296
}
288297
}
289298

@@ -296,7 +305,7 @@ extension MultipeerHelper: MCNearbyServiceAdvertiserDelegate {
296305
invitationHandler: @escaping (Bool, MCSession?) -> Void
297306
) {
298307
// Call the handler to accept the peer's invitation to join.
299-
let shouldAccept = self.delegate?.shouldAcceptJoinRequest?(peerID: peerID, context: data)
308+
let shouldAccept = self.delegate?.shouldAcceptJoinRequest?(peerHelper: self, peerID: peerID, context: data)
300309
invitationHandler(shouldAccept != nil ? shouldAccept! : true, self.session)
301310
}
302311
}

Sources/MultipeerHelper/MultipeerHelperDelegate.swift

Lines changed: 65 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,47 +7,95 @@
77

88
import MultipeerConnectivity
99

10+
/// Delegate for some useful multipeer connectivity methods
1011
@objc public protocol MultipeerHelperDelegate: AnyObject {
1112
/// Data that has been recieved from another peer
1213
/// - Parameters:
13-
/// - data: The data which has been recieved
14-
/// - peer: The peer that sent the data
15-
@objc optional func receivedData(_ data: Data, _ peer: MCPeerID)
14+
/// - peerHelper: The ``MultipeerHelper`` session that manages the multipeer connectivity
15+
/// - data: The data which has been recieved
16+
/// - peer: The peer that sent the data
17+
@objc optional func receivedData(peerHelper: MultipeerHelper, _ data: Data, _ peer: MCPeerID)
1618

1719
/// Callback for when a peer joins the network
18-
/// - Parameter peer: the `MCPeerID` of the newly joined peer
19-
@objc optional func peerJoined(_ peer: MCPeerID)
20+
/// - Parameters:
21+
/// - peerHelper: The ``MultipeerHelper`` session that manages the multipeer connectivity
22+
/// - peer: the `MCPeerID` of the newly joined peer
23+
@objc optional func peerJoined(peerHelper: MultipeerHelper, _ peer: MCPeerID)
2024

2125
/// Callback for when a peer leaves the network
22-
/// - Parameter peer: the `MCPeerID` of the peer that left
23-
@objc optional func peerLeft(_ peer: MCPeerID)
26+
/// - Parameters:
27+
/// - peerHelper: The ``MultipeerHelper`` session that manages the multipeer connectivity
28+
/// - peer: the `MCPeerID` of the peer that left
29+
@objc optional func peerLeft(peerHelper: MultipeerHelper, _ peer: MCPeerID)
2430

2531
/// Callback for when a new peer has been found. will default to accept all peers
26-
/// - Parameter peer: the `MCPeerID` of the peer who wants to join the network
27-
/// - Parameter discoveryInfo: The info dictionary advertised by the discovered peer. For more information on the contents of this dictionary, see the documentation for
32+
/// - Parameters:
33+
/// - peerHelper: The ``MultipeerHelper`` session that manages the multipeer connectivity
34+
/// - peer: the `MCPeerID` of the peer who wants to join the network
35+
/// - discoveryInfo: The info dictionary advertised by the discovered peer. For more information on the contents of this dictionary, see the documentation for
2836
/// [init(peer:discoveryInfo:serviceType:)](apple-reference-documentation://ls%2Fdocumentation%2Fmultipeerconnectivity%2Fmcnearbyserviceadvertiser%2F1407102-init) in [MCNearbyServiceAdvertiser](apple-reference-documentation://ls%2Fdocumentation%2Fmultipeerconnectivity%2Fmcnearbyserviceadvertiser).
2937
/// - Returns: Bool if the peer request to join the network or not
30-
@objc optional func shouldSendJoinRequest(_ peer: MCPeerID, with discoveryInfo: [String: String]?) -> Bool
38+
@objc optional func shouldSendJoinRequest(peerHelper: MultipeerHelper, _ peer: MCPeerID, with discoveryInfo: [String: String]?) -> Bool
3139

3240
/// Handle when a peer has requested to join the network
3341
/// - Parameters:
42+
/// - peerHelper: The ``MultipeerHelper`` session that manages the multipeer connectivity
3443
/// - peerID: Peer requesting to join
3544
/// - context: Any data the requesting peer may have sent with their request
3645
/// - Returns: Bool if the peer's join request should be accepted
37-
@objc optional func shouldAcceptJoinRequest(peerID: MCPeerID, context: Data?) -> Bool
46+
@objc optional func shouldAcceptJoinRequest(peerHelper: MultipeerHelper, peerID: MCPeerID, context: Data?) -> Bool
3847

3948
/// This will be set as the base for the discoveryInfo, which is sent out by the advertiser (host).
4049
/// The key "MultipeerHelper.compTokenKey" is in use by MultipeerHelper, for checking the
4150
/// compatibility of RealityKit versions.
51+
/// - Returns: Discovery Info
4252
@objc optional func setDiscoveryInfo() -> [String: String]
4353

4454
/// Peer can no longer be found on the network, and thus cannot receive data
45-
/// - Parameter peer: If a peer has left the network in a non typical way
46-
@objc optional func peerLost(_ peer: MCPeerID)
47-
@objc optional func receivedStream(_ stream: InputStream, _ streamName: String, _ peer: MCPeerID)
48-
@objc optional func receivingResource(_ resourceName: String, _ peer: MCPeerID, _ progress: Progress)
49-
@objc optional func receivedResource(_ resourceName: String, _ peer: MCPeerID, _ url: URL?, _ error: Error?)
50-
@objc optional func receivedCertificate(certificate: [Any]?, fromPeer peerID: MCPeerID) -> Bool
55+
/// - Parameters:
56+
/// - peerHelper: The ``MultipeerHelper`` session that manages the nearby peer whose state changed
57+
/// - peer: If a peer has left the network in a non typical way
58+
@objc optional func peerLost(
59+
peerHelper: MultipeerHelper, _ peer: MCPeerID
60+
)
61+
62+
/// Received a byte stream from remote peer.
63+
/// - Parameters:
64+
/// - peerHelper: The ``MultipeerHelper`` session through which the byte stream was opened
65+
/// - stream: An NSInputStream object that represents the local endpoint for the byte stream.
66+
/// - streamName: The name of the stream, as provided by the originator.
67+
/// - peerID: The peer ID of the originator of the stream.
68+
@objc optional func receivedStream(
69+
peerHelper: MultipeerHelper, _ stream: InputStream, _ streamName: String, _ peer: MCPeerID
70+
)
71+
/// Start receiving a resource from remote peer.
72+
/// - Parameters:
73+
/// - peerHelper: The ``MultipeerHelper`` session that started receiving the resource
74+
/// - resourceName: name of the resource, as provided by the sender.
75+
/// - peerID: sender’s peer ID.
76+
/// - progress: NSProgress object that can be used to cancel the transfer or queried to determine how far the transfer has progressed.
77+
@objc optional func receivingResource(
78+
peerHelper: MultipeerHelper, _ resourceName: String, _ peer: MCPeerID, _ progress: Progress
79+
)
80+
/// Received a resource from remote peer.
81+
/// - Parameters:
82+
/// - peerHelper: The ``MultipeerHelper`` session through which the data were received
83+
/// - resourceName: The name of the resource, as provided by the sender.
84+
/// - peerID: The peer ID of the sender.
85+
/// - localURL: An NSURL object that provides the location of a temporary file containing the received data.
86+
/// - error: An error object indicating what went wrong if the file was not received successfully, or nil.
87+
@objc optional func receivedResource(
88+
peerHelper: MultipeerHelper, _ resourceName: String, _ peerID: MCPeerID, _ localUrl: URL?, _ error: Error?
89+
)
90+
/// Made first contact with peer and have identity information about the
91+
/// remote peer (certificate may be nil).
92+
/// - Parameters:
93+
/// - peerHelper: The ``MultipeerHelper`` session that manages the nearby peer whose state changed
94+
/// - certificate: A certificate chain, presented as an array of SecCertificateRef certificate objects. The first certificate in this chain is the peer’s certificate, which is derived from the identity that the peer provided when it called the `initWithPeer:securityIdentity:encryptionPreference:` method. The other certificates are the (optional) additional chain certificates provided in that same array.
95+
/// If the nearby peer did not provide a security identity, then this parameter’s value is nil.
96+
/// - peerID: The peer ID of the sender.
97+
@objc optional func receivedCertificate(
98+
peerHelper: MultipeerHelper, certificate: [Any]?, fromPeer peerID: MCPeerID) -> Bool
5199
}
52100

53101
#if canImport(RealityKit)

0 commit comments

Comments
 (0)