Post

Replies

Boosts

Views

Activity

Reply to How to add a String to NWProtocolTLS.Options()?
@meaton If you want to open a TSI I can take a deeper look at this and the code you have so far for this project. hi, I created a very small project for a TSI but I haven't submitted it yet. How do I submit it so that it comes to you directly? I created a TSI for ARKit in the past, I don't remember there being a way to send it to a specific specialist.
Dec ’20
Reply to How to add a String to NWProtocolTLS.Options()?
@meaton hi and thanks for the help. I checked the endPoint and it's definitely set (below) I do not believe there is an option to add data to TLS options that does not work inside the context of TLS. How is the extension from NWParameters expected to work in this case for NWListener and NWConnection?  Based on what @eskimo said about each peer having its own a unique ID, this seemed like the only way to add it. That is the only reason I took that approach. Since you said that the way I'm doing isn't possible, then I wonder what he meant by that. I'll go back to that thread and ask him there or email him for further explanation. As per the multiple call problem that I'm having, yes you are correct: you are getting a browsing client reaching your Bonjour service and the listener is accepting the new connection Using a device as the browser and simulator as the listener, I just added a print statement to listener.newConnectionHandler and when the connection came in, this is the print out: endpoint: fe80::149d:ff95:503f:9d9%en4.59013 endpoint: fe80::149d:ff95:503f:9d9%en4.59014 endpoint: fe80::149d:ff95:503f:9d9%en4.59015 endpoint: fe80::837:3ac4:fb1f:492e%en1.63936 Connection preparing // stops here Listener: listener.newConnectionHandler = { [weak self](newConnection) in 		self?.delegate?.createNewIncoming(newConnection) // called in the below vc that conforms to the delegate } vc: var	connectionIncoming: ConnectionIncoming? func createNewIncoming(_ connection: NWConnection) { 		print("endpoint: ", connection.endPoint) 		connectionIncoming = ConnectionIncoming(connection:connection, delegate: self) } NWConnection class named ConnectionIncoming: class ConnectionIncoming {     private var connection: NWConnection? 		// ... 		connection.stateUpdateHandler = { [weak self](nwConnectionState) in 				 switch nwConnectionState {			 				 case .preparing:           	 							 print("Connection preparing") // still stuck here				 				 // ...		 				} 		} } Here is the basic browser code if it makes any difference: protocol PeerBrowserDelegate: class {     func createConnection(from endPoint: NWEndpoint) } class PeerBrowser { 		weak var delegate: PeerBrowserDelegate? 		fileprivate var browser: NWBrowser? 		init() {         let parameters = NWParameters()         parameters.includePeerToPeer = true         browser = NWBrowser(for: .bonjour(type: "_myApp._tcp", domain: "local"), using: parameters)         startBrowsing()     } 		fileprivate func startBrowsing() {         guard let browser = browser else { return }         browser.stateUpdateHandler = { [weak self](newState) in             switch newState {             case .setup: print("Browser - setup")             case .ready: print("Browser - ready")             case .cancelled: print("Browser - Cancelled")             case .failed(let error): /* restart code ... */             default:break             }         }         browser.browseResultsChangedHandler = { [weak self](results, changes) in             for result in results {                 let endPoint = result.endpoint                 self?.delegate?.createConnection(from: endPoint) // will create a NWConnection from endPoint                 break             }         }         browser.start(queue: .main)     } }
Dec ’20
Reply to NWConnection's connection.receiveMessage method gets breakpoint hit but callback not entered
UPDATE I couldn't add this to the question because the Edit button somehow disappeared. This doesn't resolve the call back problem from my question but it did stop the multiple print statements however with a caveat. I followed an answer in this thread - https://developer.apple.com/forums/thread/653925 from @meaton. Inside the NWListener class he used a bool property, private var didConnectionSetup: Bool = false, to stop listener.newConnectionHandler from getting called multiple times. It did work to stop the print statements for the firs device but the caveat is when another device (simulator) came on line and its NWBrowser was found, the bool property was stuck on true so delegate?.createNewIncoming never ran. I tried several things to change it like setting didConnectionSetup = false right afterwards but the print statement problem started all over again. private var didConnectionSetup: Bool = false listener.newConnectionHandler = { [weak self](newConnection) in 		guard let safeSelf = self else { return } 		if safeSelf.didConnectionSetup { return } 		safeSelf.didConnectionSetup = true 		self?.delegate?.createNewIncoming(newConnection) // once any new device is found/browsed this never runs } this didn't work, print problem was still in effect on the first device and all subsequent devices: listener.newConnectionHandler = { [weak self](newConnection) in 		guard let safeSelf = self else { return } 		if safeSelf.didConnectionSetup { return } 		safeSelf.didConnectionSetup = true 		self?.delegate?.createNewIncoming(newConnection) 		safeSelf.didConnectionSetup = false }
Dec ’20
Reply to How to set peer limit when using NWConnection
@meaton Thanks for the help. I had a feeling you was going to say that. For anyone else who needs to handle this just keep an array of NWConnections and check the count. let yourMaxCount = 10 var connections = [NWConnection]() listener.newConnectionHandler = { [weak self](newConnection) in 		guard let safeSelf = self else { return } 		if safeSelf.connections.count >= safeSelf.yourMaxCount { return }             		// add connection to the array and continue on ... }
Topic: Programming Languages SubTopic: Swift Tags:
Dec ’20