diff --git a/internal/platform/implementation/apple/Mediums/BUILD b/internal/platform/implementation/apple/Mediums/BUILD index 7f7cdd3e..649bd04f 100644 --- a/internal/platform/implementation/apple/Mediums/BUILD +++ b/internal/platform/implementation/apple/Mediums/BUILD @@ -22,33 +22,25 @@ objc_library( "Ble/GNCMBleConnection.m", "Ble/GNCMBlePeripheral.m", "Ble/GNCMBleUtils.mm", + "GNCLeaks.h", "GNCLeaks.m", "GNCMConnection.m", - "WifiLan/GNCMBonjourBrowser.m", - "WifiLan/GNCMBonjourConnection.m", - "WifiLan/GNCMBonjourService.m", - "WifiLan/GNCMBonjourUtils.m", - "WifiLanV2/GNCWiFiLANError.m", - "WifiLanV2/GNCWiFiLANMedium.m", - "WifiLanV2/GNCWiFiLANServerSocket.m", - "WifiLanV2/GNCWiFiLANServerSocket+Internal.h", - "WifiLanV2/GNCWiFiLANSocket.m", + "WiFiLAN/GNCWiFiLANError.m", + "WiFiLAN/GNCWiFiLANMedium.m", + "WiFiLAN/GNCWiFiLANServerSocket.m", + "WiFiLAN/GNCWiFiLANServerSocket+Internal.h", + "WiFiLAN/GNCWiFiLANSocket.m", ], hdrs = [ "Ble/GNCMBleCentral.h", "Ble/GNCMBleConnection.h", "Ble/GNCMBlePeripheral.h", "Ble/GNCMBleUtils.h", - "GNCLeaks.h", "GNCMConnection.h", - "WifiLan/GNCMBonjourBrowser.h", - "WifiLan/GNCMBonjourConnection.h", - "WifiLan/GNCMBonjourService.h", - "WifiLan/GNCMBonjourUtils.h", - "WifiLanV2/GNCWiFiLANError.h", - "WifiLanV2/GNCWiFiLANMedium.h", - "WifiLanV2/GNCWiFiLANServerSocket.h", - "WifiLanV2/GNCWiFiLANSocket.h", + "WiFiLAN/GNCWiFiLANError.h", + "WiFiLAN/GNCWiFiLANMedium.h", + "WiFiLAN/GNCWiFiLANServerSocket.h", + "WiFiLAN/GNCWiFiLANSocket.h", ], deps = [ "//internal/platform/implementation/apple:Shared", diff --git a/internal/platform/implementation/apple/Mediums/WifiLanV2/GNCWiFiLANError.h b/internal/platform/implementation/apple/Mediums/WiFiLAN/GNCWiFiLANError.h similarity index 100% rename from internal/platform/implementation/apple/Mediums/WifiLanV2/GNCWiFiLANError.h rename to internal/platform/implementation/apple/Mediums/WiFiLAN/GNCWiFiLANError.h diff --git a/internal/platform/implementation/apple/Mediums/WifiLanV2/GNCWiFiLANError.m b/internal/platform/implementation/apple/Mediums/WiFiLAN/GNCWiFiLANError.m similarity index 88% rename from internal/platform/implementation/apple/Mediums/WifiLanV2/GNCWiFiLANError.m rename to internal/platform/implementation/apple/Mediums/WiFiLAN/GNCWiFiLANError.m index 10b04923..062ce267 100644 --- a/internal/platform/implementation/apple/Mediums/WifiLanV2/GNCWiFiLANError.m +++ b/internal/platform/implementation/apple/Mediums/WiFiLAN/GNCWiFiLANError.m @@ -12,6 +12,6 @@ // See the License for the specific language governing permissions and // limitations under the License. -#import "internal/platform/implementation/apple/Mediums/WifiLanV2/GNCWiFiLANError.h" +#import "internal/platform/implementation/apple/Mediums/WiFiLAN/GNCWiFiLANError.h" NSErrorDomain const GNCWiFiLANErrorDomain = @"com.google.nearby.wifilan.error"; diff --git a/internal/platform/implementation/apple/Mediums/WifiLanV2/GNCWiFiLANMedium.h b/internal/platform/implementation/apple/Mediums/WiFiLAN/GNCWiFiLANMedium.h similarity index 100% rename from internal/platform/implementation/apple/Mediums/WifiLanV2/GNCWiFiLANMedium.h rename to internal/platform/implementation/apple/Mediums/WiFiLAN/GNCWiFiLANMedium.h diff --git a/internal/platform/implementation/apple/Mediums/WifiLanV2/GNCWiFiLANMedium.m b/internal/platform/implementation/apple/Mediums/WiFiLAN/GNCWiFiLANMedium.m similarity index 96% rename from internal/platform/implementation/apple/Mediums/WifiLanV2/GNCWiFiLANMedium.m rename to internal/platform/implementation/apple/Mediums/WiFiLAN/GNCWiFiLANMedium.m index 8249caa2..202a95df 100644 --- a/internal/platform/implementation/apple/Mediums/WifiLanV2/GNCWiFiLANMedium.m +++ b/internal/platform/implementation/apple/Mediums/WiFiLAN/GNCWiFiLANMedium.m @@ -12,15 +12,15 @@ // See the License for the specific language governing permissions and // limitations under the License. -#import "internal/platform/implementation/apple/Mediums/WifiLanV2/GNCWiFiLANMedium.h" +#import "internal/platform/implementation/apple/Mediums/WiFiLAN/GNCWiFiLANMedium.h" #import #import -#import "internal/platform/implementation/apple/Mediums/WifiLanV2/GNCWiFiLANError.h" -#import "internal/platform/implementation/apple/Mediums/WifiLanV2/GNCWiFiLANServerSocket+Internal.h" -#import "internal/platform/implementation/apple/Mediums/WifiLanV2/GNCWiFiLANServerSocket.h" -#import "internal/platform/implementation/apple/Mediums/WifiLanV2/GNCWiFiLANSocket.h" +#import "internal/platform/implementation/apple/Mediums/WiFiLAN/GNCWiFiLANError.h" +#import "internal/platform/implementation/apple/Mediums/WiFiLAN/GNCWiFiLANServerSocket+Internal.h" +#import "internal/platform/implementation/apple/Mediums/WiFiLAN/GNCWiFiLANServerSocket.h" +#import "internal/platform/implementation/apple/Mediums/WiFiLAN/GNCWiFiLANSocket.h" #import "GoogleToolboxForMac/GTMLogger.h" // An arbitrary timeout that should be pretty lenient. diff --git a/internal/platform/implementation/apple/Mediums/WifiLanV2/GNCWiFiLANServerSocket+Internal.h b/internal/platform/implementation/apple/Mediums/WiFiLAN/GNCWiFiLANServerSocket+Internal.h similarity index 94% rename from internal/platform/implementation/apple/Mediums/WifiLanV2/GNCWiFiLANServerSocket+Internal.h rename to internal/platform/implementation/apple/Mediums/WiFiLAN/GNCWiFiLANServerSocket+Internal.h index 6e098f55..e96f0637 100644 --- a/internal/platform/implementation/apple/Mediums/WifiLanV2/GNCWiFiLANServerSocket+Internal.h +++ b/internal/platform/implementation/apple/Mediums/WiFiLAN/GNCWiFiLANServerSocket+Internal.h @@ -14,7 +14,7 @@ #import -#import "internal/platform/implementation/apple/Mediums/WifiLanV2/GNCWiFiLANServerSocket.h" +#import "internal/platform/implementation/apple/Mediums/WiFiLAN/GNCWiFiLANServerSocket.h" @interface GNCWiFiLANServerSocket (Internal) diff --git a/internal/platform/implementation/apple/Mediums/WifiLanV2/GNCWiFiLANServerSocket.h b/internal/platform/implementation/apple/Mediums/WiFiLAN/GNCWiFiLANServerSocket.h similarity index 100% rename from internal/platform/implementation/apple/Mediums/WifiLanV2/GNCWiFiLANServerSocket.h rename to internal/platform/implementation/apple/Mediums/WiFiLAN/GNCWiFiLANServerSocket.h diff --git a/internal/platform/implementation/apple/Mediums/WifiLanV2/GNCWiFiLANServerSocket.m b/internal/platform/implementation/apple/Mediums/WiFiLAN/GNCWiFiLANServerSocket.m similarity index 96% rename from internal/platform/implementation/apple/Mediums/WifiLanV2/GNCWiFiLANServerSocket.m rename to internal/platform/implementation/apple/Mediums/WiFiLAN/GNCWiFiLANServerSocket.m index 2fa2c955..27e7dbe6 100644 --- a/internal/platform/implementation/apple/Mediums/WifiLanV2/GNCWiFiLANServerSocket.m +++ b/internal/platform/implementation/apple/Mediums/WiFiLAN/GNCWiFiLANServerSocket.m @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -#import "internal/platform/implementation/apple/Mediums/WifiLanV2/GNCWiFiLANServerSocket.h" +#import "internal/platform/implementation/apple/Mediums/WiFiLAN/GNCWiFiLANServerSocket.h" #import #import @@ -22,9 +22,9 @@ #include #include -#import "internal/platform/implementation/apple/Mediums/WifiLanV2/GNCWiFiLANError.h" -#import "internal/platform/implementation/apple/Mediums/WifiLanV2/GNCWiFiLANServerSocket+Internal.h" -#import "internal/platform/implementation/apple/Mediums/WifiLanV2/GNCWiFiLANSocket.h" +#import "internal/platform/implementation/apple/Mediums/WiFiLAN/GNCWiFiLANError.h" +#import "internal/platform/implementation/apple/Mediums/WiFiLAN/GNCWiFiLANServerSocket+Internal.h" +#import "internal/platform/implementation/apple/Mediums/WiFiLAN/GNCWiFiLANSocket.h" #import "GoogleToolboxForMac/GTMLogger.h" @interface GNCWiFiLANServerSocket () diff --git a/internal/platform/implementation/apple/Mediums/WifiLanV2/GNCWiFiLANSocket.h b/internal/platform/implementation/apple/Mediums/WiFiLAN/GNCWiFiLANSocket.h similarity index 100% rename from internal/platform/implementation/apple/Mediums/WifiLanV2/GNCWiFiLANSocket.h rename to internal/platform/implementation/apple/Mediums/WiFiLAN/GNCWiFiLANSocket.h diff --git a/internal/platform/implementation/apple/Mediums/WifiLanV2/GNCWiFiLANSocket.m b/internal/platform/implementation/apple/Mediums/WiFiLAN/GNCWiFiLANSocket.m similarity index 97% rename from internal/platform/implementation/apple/Mediums/WifiLanV2/GNCWiFiLANSocket.m rename to internal/platform/implementation/apple/Mediums/WiFiLAN/GNCWiFiLANSocket.m index ac2b81f2..3e85bbc0 100644 --- a/internal/platform/implementation/apple/Mediums/WifiLanV2/GNCWiFiLANSocket.m +++ b/internal/platform/implementation/apple/Mediums/WiFiLAN/GNCWiFiLANSocket.m @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -#import "internal/platform/implementation/apple/Mediums/WifiLanV2/GNCWiFiLANSocket.h" +#import "internal/platform/implementation/apple/Mediums/WiFiLAN/GNCWiFiLANSocket.h" #import #import diff --git a/internal/platform/implementation/apple/Mediums/WifiLan/GNCMBonjourBrowser.h b/internal/platform/implementation/apple/Mediums/WifiLan/GNCMBonjourBrowser.h deleted file mode 100644 index 093afb5c..00000000 --- a/internal/platform/implementation/apple/Mediums/WifiLan/GNCMBonjourBrowser.h +++ /dev/null @@ -1,42 +0,0 @@ -// Copyright 2020 Google LLC -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// https://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#import - -#import "internal/platform/implementation/apple/Mediums/GNCMConnection.h" - -NS_ASSUME_NONNULL_BEGIN - -/** - * GNCMBonjourBrowser browses mDNS services publishing the specified mDNS type and domain. The mDNS - * type is a string formatted as "_[serviceIdHash]._tcp." in which [serviceIdHash] is generated as - * a SHA-256 hash from the service ID and taken the 6 first bytes of string in upper case. - * Calls the specififed endpoint found handler when one is found. The endpoint found handler - * supplies a requester block, which can be called to establish a socket to the service found. - * - * Don't hold the strong reference of caller self in endpointFoundHandler to ensure there is no - * retain cycle between them. - * - * @param serviceType An mDNS type that uniquely identifies the published service to search for. - * @param endpointFoundHandler The handler that is called when an endpoint publishing the service - * ID is discovered. - */ -@interface GNCMBonjourBrowser : NSObject - -- (instancetype)initWithServiceType:(NSString *)serviceType - endpointFoundHandler:(GNCMEndpointFoundHandler)handler; - -@end - -NS_ASSUME_NONNULL_END diff --git a/internal/platform/implementation/apple/Mediums/WifiLan/GNCMBonjourBrowser.m b/internal/platform/implementation/apple/Mediums/WifiLan/GNCMBonjourBrowser.m deleted file mode 100644 index e86cc64a..00000000 --- a/internal/platform/implementation/apple/Mediums/WifiLan/GNCMBonjourBrowser.m +++ /dev/null @@ -1,176 +0,0 @@ -// Copyright 2020 Google LLC -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// https://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#import "internal/platform/implementation/apple/Mediums/WifiLan/GNCMBonjourBrowser.h" - -#import "internal/platform/implementation/apple/Mediums/GNCMConnection.h" -#import "internal/platform/implementation/apple/Mediums/WifiLan/GNCMBonjourConnection.h" -#import "internal/platform/implementation/apple/Mediums/WifiLan/GNCMBonjourUtils.h" -#import "GoogleToolboxForMac/GTMLogger.h" - -typedef NSString *GNCEndpointId; - -@interface GNCMNetServiceInfo : NSObject -@property(nonatomic) NSNetService *service; -@property(nonatomic, copy) GNCMEndpointLostHandler endpointLostHandler; -@property(nonatomic, copy, nullable) GNCMConnectionHandler connectionHandler; -@end - -@implementation GNCMNetServiceInfo - -+ (instancetype)infoWithService:(NSNetService *)service { - GNCMNetServiceInfo *info = [[GNCMNetServiceInfo alloc] init]; - info.service = service; - return info; -} - -- (BOOL)isEqual:(GNCMNetServiceInfo *)object { - return [_service isEqual:object.service]; -} - -- (NSUInteger)hash { - return [_service hash]; -} - -@end - -@interface GNCMBonjourBrowser () - -@property(nonatomic, copy) GNCMEndpointFoundHandler endpointFoundHandler; - -@property(nonatomic) NSNetServiceBrowser *netBrowser; -@property(nonatomic) NSMutableDictionary *endpoints; - -@end - -@implementation GNCMBonjourBrowser - -- (instancetype)initWithServiceType:(NSString *)serviceType - endpointFoundHandler:(GNCMEndpointFoundHandler)handler { - self = [super init]; - if (self) { - _endpointFoundHandler = handler; - - _netBrowser = [[NSNetServiceBrowser alloc] init]; - _netBrowser.delegate = self; - [_netBrowser scheduleInRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode]; - [_netBrowser searchForServicesOfType:serviceType inDomain:GNCMBonjourDomain]; - _endpoints = [NSMutableDictionary dictionary]; - } - return self; -} - -#pragma mark NSNetServiceBrowserDelegate - -- (void)netServiceBrowserWillSearch:(NSNetServiceBrowser *)browser { - GTMLoggerDebug(@"Browsing"); -} - -- (void)netServiceBrowserDidStopSearch:(NSNetServiceBrowser *)browser { - GTMLoggerDebug(@"Stop browsing"); -} - -- (void)netServiceBrowser:(NSNetServiceBrowser *)browser - didNotSearch:(NSDictionary *)errorDict { - GTMLoggerDebug(@"Not browsing with errorDict: %@", errorDict); -} - -- (void)netServiceBrowser:(NSNetServiceBrowser *)browser - didFindService:(NSNetService *)service - moreComing:(BOOL)moreComing { - GTMLoggerDebug(@"Found service: %@", service); - - GNCMNetServiceInfo *info = [GNCMNetServiceInfo infoWithService:service]; - - // Just to be safe, check if the service is already known and deal with it accordingly. - NSArray *endpointIds = [_endpoints allKeysForObject:info]; - if (endpointIds.count > 0) return; - - // Add the newly discovered service to the list of services. - GNCEndpointId endpointId = [[NSUUID UUID] UUIDString]; - _endpoints[endpointId] = info; - - service.delegate = self; - [service resolveWithTimeout:0]; -} - -- (void)netServiceBrowser:(NSNetServiceBrowser *)browser - didRemoveService:(NSNetService *)service - moreComing:(BOOL)moreComing { - GTMLoggerDebug(@"Lost service: %@", service); - NSArray *endpointIds = - [_endpoints allKeysForObject:[GNCMNetServiceInfo infoWithService:service]]; - NSAssert(endpointIds.count <= 1, @"Unexpected duplicate service"); - if (endpointIds.count > 0) { - GNCEndpointId endpointId = endpointIds[0]; - GNCMNetServiceInfo *info = _endpoints[endpointId]; - [_endpoints removeObjectForKey:endpointId]; - // Tail call to preserve reentrancy. - if (info.endpointLostHandler) { - info.endpointLostHandler(); - } - } -} - -#pragma mark NSNetServiceDelegate - -- (void)netServiceDidResolveAddress:(NSNetService *)service { - GTMLoggerDebug(@"Resolved service: %@ addresses: %@", service, service.addresses); - - GNCMNetServiceInfo *info = [GNCMNetServiceInfo infoWithService:service]; - - NSArray *endpointIds = [_endpoints allKeysForObject:info]; - if (endpointIds.count > 0) { - // Get TXTRecord data. - NSData *data = [service TXTRecordData]; - NSDictionary *TXTRecordData = - [NSNetService dictionaryFromTXTRecordData:data]; - - // The endpointLostHandler is returned from the endpointFoundHandler. The main benefit of this - // is that it allows rejection from the remote endpoint to be received by the local endpoint - // before the local endpoint has accepted or rejected. - info.endpointLostHandler = _endpointFoundHandler( - endpointIds[0], service.type, service.name, TXTRecordData, - ^(GNCMConnectionHandler connectionHandler) { - // A connection is requested, so resolve the service to get the I/O streams. - info.connectionHandler = connectionHandler; - if (info.connectionHandler) { - dispatch_sync(dispatch_get_main_queue(), ^{ - NSInputStream *inputStream; - NSOutputStream *outputStream; - [info.service scheduleInRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode]; - [info.service getInputStream:&inputStream outputStream:&outputStream]; - GNCMBonjourConnection *connection = - [[GNCMBonjourConnection alloc] initWithInputStream:inputStream - outputStream:outputStream - queue:nil]; - connection.connectionHandlers = info.connectionHandler(connection); - }); - } - }); - _endpoints[endpointIds[0]] = info; - } -} - -- (void)netService:(NSNetService *)service - didNotResolve:(NSDictionary *)errorDict { - GTMLoggerDebug(@"Did not resolve service: %@", service); - NSArray *endpointIds = - [_endpoints allKeysForObject:[GNCMNetServiceInfo infoWithService:service]]; - if (endpointIds.count > 0) { - _endpoints[endpointIds[0]].connectionHandler = nil; - } -} - -@end diff --git a/internal/platform/implementation/apple/Mediums/WifiLan/GNCMBonjourConnection.h b/internal/platform/implementation/apple/Mediums/WifiLan/GNCMBonjourConnection.h deleted file mode 100644 index b15b7b37..00000000 --- a/internal/platform/implementation/apple/Mediums/WifiLan/GNCMBonjourConnection.h +++ /dev/null @@ -1,38 +0,0 @@ -// Copyright 2020 Google LLC -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// https://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#import - -#import "internal/platform/implementation/apple/Mediums/GNCMConnection.h" - -NS_ASSUME_NONNULL_BEGIN - -/** - * This medium connection sends and receives payloads over the NSInputStream and NSOutputStream - * passed to it. - * - * @param inputStream The input stream to read from. - * @param outputStream The output stream to write to. - * @param queue The queue on which the GNCMConnection callbacks will be called. If nil, the main - * queue is used. - */ -@interface GNCMBonjourConnection : NSObject -@property(nonatomic) GNCMConnectionHandlers *connectionHandlers; - -- (instancetype)initWithInputStream:(NSInputStream *)inputStream - outputStream:(NSOutputStream *)outputStream - queue:(nullable dispatch_queue_t)queue; -@end - -NS_ASSUME_NONNULL_END diff --git a/internal/platform/implementation/apple/Mediums/WifiLan/GNCMBonjourConnection.m b/internal/platform/implementation/apple/Mediums/WifiLan/GNCMBonjourConnection.m deleted file mode 100644 index 59357604..00000000 --- a/internal/platform/implementation/apple/Mediums/WifiLan/GNCMBonjourConnection.m +++ /dev/null @@ -1,224 +0,0 @@ -// Copyright 2020 Google LLC -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// https://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#import "internal/platform/implementation/apple/Mediums/WifiLan/GNCMBonjourConnection.h" - -#import "internal/platform/implementation/apple/Mediums/GNCMConnection.h" -#import "GoogleToolboxForMac/GTMLogger.h" - -enum { kMaxPacketSize = 32 * 1024 }; - -@interface GNCMBonjourConnection () -@property(nonatomic) NSInputStream *inputStream; -@property(nonatomic) NSOutputStream *outputStream; -@property(nonatomic) dispatch_queue_t callbackQueue; - -@property(nonatomic, copy, nullable) NSData *dataBeingWritten; -@property(nonatomic) NSInteger numberOfBytesLeftToWrite; -@property(nonatomic, copy, nullable) GNCMProgressHandler progressHandler; -@property(nonatomic, copy, nullable) GNCMPayloadResultHandler completion; - -@property(nonatomic) BOOL inputStreamOpen; -@property(nonatomic) BOOL outputStreamOpen; -@end - -@implementation GNCMBonjourConnection - -- (instancetype)initWithInputStream:(NSInputStream *)inputStream - outputStream:(NSOutputStream *)outputStream - queue:(nullable dispatch_queue_t)queue { - self = [super init]; - if (self) { - _inputStreamOpen = NO; - _outputStreamOpen = NO; - - _inputStream = inputStream; - _inputStream.delegate = self; - [_inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode]; - [_inputStream open]; - - _outputStream = outputStream; - _outputStream.delegate = self; - [_outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode]; - [_outputStream open]; - - _callbackQueue = queue; - } - return self; -} - -- (void)dealloc { - [self closeStreams]; -} - -- (void)sendData:(NSData *)payload - progressHandler:(GNCMProgressHandler)progressHandler - completion:(GNCMPayloadResultHandler)completion { - if (_dataBeingWritten) { - GTMLoggerInfo(@"Attempting to send payload while one is already in flight"); - [self dispatchCallback:^{ - completion(GNCMPayloadResultFailure); - }]; - return; - } - - self.dataBeingWritten = payload; - self.numberOfBytesLeftToWrite = payload.length; - self.progressHandler = progressHandler; - self.completion = completion; - [self writeChunk]; -} - -#pragma mark NSStreamDelegate - -- (void)stream:(NSStream *)stream handleEvent:(NSStreamEvent)event { - switch (event) { - case NSStreamEventHasBytesAvailable: { - // Data has arrived on the input stream. - NSAssert(stream == _inputStream, @"Error: Expected input stream"); - - if (_connectionHandlers.payloadHandler) { - uint8_t bytesRead[kMaxPacketSize]; - NSInteger numberOfBytesRead; - @synchronized(self.inputStream) { - numberOfBytesRead = [self.inputStream read:bytesRead maxLength:kMaxPacketSize]; - } - NSData *data = nil; - if (numberOfBytesRead > 0) { - GTMLoggerInfo(@"Read %lu bytes", (u_long)numberOfBytesRead); - data = [NSData dataWithBytes:bytesRead length:numberOfBytesRead]; - } - [self dispatchCallback:^{ - self.connectionHandlers.payloadHandler(data ?: [NSData data]); - }]; - } - break; - } - - case NSStreamEventHasSpaceAvailable: - // There is space available on the output stream. - NSAssert(stream == _outputStream, @"Error: Expected output stream"); - - // Schedule this in a future runloop cycle because -writeChunk, which can cause this event - // to be received synchronously, is not reentrant. - [self performSelector:@selector(writeChunk) withObject:nil afterDelay:0.0]; - break; - - case NSStreamEventErrorOccurred: - GTMLoggerInfo(@"Stream error: %@", [stream streamError]); - // Fall through. - case NSStreamEventEndEncountered: { - GTMLoggerInfo(@"Stream closing"); - [self closeStreams]; - if (_connectionHandlers.disconnectedHandler) { - [self dispatchCallback:^{ - self.connectionHandlers.disconnectedHandler(); - }]; - } - break; - } - - case NSStreamEventOpenCompleted: - if (stream == _inputStream) { - _inputStreamOpen = YES; - } - if (stream == _outputStream) { - _outputStreamOpen = YES; - } - // Schedule this in a future runloop cycle because -writeChunk, which can cause this event - // to be received synchronously, is not reentrant. - [self performSelector:@selector(writeChunk) withObject:nil afterDelay:0.0]; - break; - - case NSStreamEventNone: - default: - break; - } -} - -#pragma mark Private - -// Calls a block on the callback queue. -- (void)dispatchCallback:(dispatch_block_t)block { - dispatch_async(_callbackQueue ?: dispatch_get_main_queue(), block); -} - -// Writes a chunk of the outgoing data to the output stream, calling the progress and completion -// handlers as needed. -- (void)writeChunk { - void (^reportProgress)(size_t) = ^(size_t count) { - // Captures the progress handler because the property is nilled out below. - GNCMProgressHandler progressHandler = _progressHandler; - if (progressHandler != nil) { - [self dispatchCallback:^{ - progressHandler(count); - }]; - } - }; - - void (^completed)(GNCMPayloadResult) = ^(GNCMPayloadResult result) { - reportProgress(_dataBeingWritten.length); - _progressHandler = nil; - _dataBeingWritten = nil; - - // Captures the completion because the property is nilled out below. - GNCMPayloadResultHandler completion = _completion; - if (completion != nil) { - [self dispatchCallback:^{ - completion(result); - }]; - } - _completion = nil; - }; - - @synchronized(_outputStream) { - if (_inputStreamOpen && _outputStreamOpen && _numberOfBytesLeftToWrite) { - NSUInteger dataLength = (UInt32)_dataBeingWritten.length; - if (_numberOfBytesLeftToWrite == dataLength) { - GTMLoggerInfo(@"Starting a write operation of length %lu", (u_long)dataLength); - } - NSInteger numberOfPayloadBytesWritten = - [_outputStream write:&_dataBeingWritten.bytes[dataLength - _numberOfBytesLeftToWrite] - maxLength:_numberOfBytesLeftToWrite]; - - GTMLoggerInfo(@"Wrote %lu bytes", (u_long)numberOfPayloadBytesWritten); - if (numberOfPayloadBytesWritten >= 0) { - _numberOfBytesLeftToWrite -= numberOfPayloadBytesWritten; - reportProgress(_dataBeingWritten.length - _numberOfBytesLeftToWrite); - if (_numberOfBytesLeftToWrite < 0) { - GTMLoggerInfo(@"Unexpected number of bytes written"); - _numberOfBytesLeftToWrite = 0; - } - if (_numberOfBytesLeftToWrite == 0) completed(GNCMPayloadResultSuccess); - } else { - GTMLoggerInfo(@"Error writing to output stream"); - completed(GNCMPayloadResultFailure); - } - } - } -} - -- (void)closeStreams { - @synchronized(_inputStream) { - [_inputStream close]; - _inputStream = nil; - } - - @synchronized(_outputStream) { - [_outputStream close]; - _outputStream = nil; - } -} - -@end diff --git a/internal/platform/implementation/apple/Mediums/WifiLan/GNCMBonjourService.h b/internal/platform/implementation/apple/Mediums/WifiLan/GNCMBonjourService.h deleted file mode 100644 index db0649c8..00000000 --- a/internal/platform/implementation/apple/Mediums/WifiLan/GNCMBonjourService.h +++ /dev/null @@ -1,48 +0,0 @@ -// Copyright 2020 Google LLC -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// https://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#import - -#import "internal/platform/implementation/apple/Mediums/GNCMConnection.h" - -NS_ASSUME_NONNULL_BEGIN - -/** - * GNCMBonjourService publishes mDNS type and domain with the specified service ID via Apple - * Bonjour service. The mDNS type is a string formatted as "_[serviceIdHash]._tcp." in which - * [serviceIdHash] is generated as a SHA-256 hash from the service ID and taken the 6 first bytes - * of string in upper case. - * When the service connects, the specified |endpointConnectedHandler| is called, which establishes - * a connection to the browser. - * - * Don't hold the strong reference of caller self in endpointConnectedHandler to ensure there is no - * retain cycle between them. - * - * @param serviceName A service name that embeds the |WifiLanServiceInfo| information. - * @param serviceType An mDNS type that uniquely identifies the published service to search for. - * @param port The requesting socket port number. - * @param txtRecordData The TXTRecord data. - * @param endpointConnectedHandler The handler that is called when a browser connects. - */ -@interface GNCMBonjourService : NSObject - -- (instancetype)initWithServiceName:(NSString *)serviceName - serviceType:(NSString *)serviceType - port:(NSInteger)port - TXTRecordData:(NSDictionary *)TXTRecordData - endpointConnectedHandler:(GNCMConnectionHandler)handler; - -@end - -NS_ASSUME_NONNULL_END diff --git a/internal/platform/implementation/apple/Mediums/WifiLan/GNCMBonjourService.m b/internal/platform/implementation/apple/Mediums/WifiLan/GNCMBonjourService.m deleted file mode 100644 index 048e7ab3..00000000 --- a/internal/platform/implementation/apple/Mediums/WifiLan/GNCMBonjourService.m +++ /dev/null @@ -1,88 +0,0 @@ -// Copyright 2020 Google LLC -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// https://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#import "internal/platform/implementation/apple/Mediums/WifiLan/GNCMBonjourService.h" - -#import "internal/platform/implementation/apple/Mediums/GNCMConnection.h" -#import "internal/platform/implementation/apple/Mediums/WifiLan/GNCMBonjourConnection.h" -#import "internal/platform/implementation/apple/Mediums/WifiLan/GNCMBonjourUtils.h" -#import "GoogleToolboxForMac/GTMLogger.h" - -@interface GNCMBonjourService () - -@property(nonatomic, copy) NSString *serviceName; -@property(nonatomic, copy) NSString *serviceType; -@property(nonatomic, copy) GNCMConnectionHandler endpointConnectedHandler; - -@property(nonatomic) NSNetService *netService; - -@end - -@implementation GNCMBonjourService - -- (instancetype)initWithServiceName:(NSString *)serviceName - serviceType:(NSString *)serviceType - port:(NSInteger)port - TXTRecordData:(NSDictionary *)TXTRecordData - endpointConnectedHandler:(GNCMConnectionHandler)handler { - self = [super init]; - if (self) { - _serviceName = [serviceName copy]; - _serviceType = [serviceType copy]; - _endpointConnectedHandler = handler; - - _netService = [[NSNetService alloc] initWithDomain:GNCMBonjourDomain - type:_serviceType - name:_serviceName - port:port]; - [_netService setTXTRecordData:[NSNetService dataFromTXTRecordDictionary:TXTRecordData]]; - - _netService.delegate = self; - [_netService scheduleInRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode]; - [_netService publishWithOptions:NSNetServiceListenForConnections]; - } - return self; -} - -- (void)dealloc { - [_netService stop]; -} - -#pragma mark NSNetServiceDelegate - -- (void)netServiceDidPublish:(NSNetService *)service { - GTMLoggerDebug(@"Did publish service: %@", service); -} - -- (void)netService:(NSNetService *)service - didNotPublish:(NSDictionary *)errorDict { - GTMLoggerDebug(@"Error publishing: service: %@, errorDic: %@", service, errorDict); -} - -- (void)netServiceDidStop:(NSNetService *)service { - GTMLoggerDebug(@"Stopped publishing service: %@", service); -} - -- (void)netService:(NSNetService *)service - didAcceptConnectionWithInputStream:(NSInputStream *)inputStream - outputStream:(NSOutputStream *)outputStream { - GTMLoggerDebug(@"Accepted connection, service: %@", service); - GNCMBonjourConnection *connection = - [[GNCMBonjourConnection alloc] initWithInputStream:inputStream - outputStream:outputStream - queue:nil]; - connection.connectionHandlers = _endpointConnectedHandler(connection); -} - -@end diff --git a/internal/platform/implementation/apple/Mediums/WifiLan/GNCMBonjourUtils.h b/internal/platform/implementation/apple/Mediums/WifiLan/GNCMBonjourUtils.h deleted file mode 100644 index e314dec6..00000000 --- a/internal/platform/implementation/apple/Mediums/WifiLan/GNCMBonjourUtils.h +++ /dev/null @@ -1,18 +0,0 @@ -// Copyright 2020 Google LLC -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// https://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#import - -// mDNS domain. -FOUNDATION_EXPORT NSString *_Nonnull const GNCMBonjourDomain; diff --git a/internal/platform/implementation/apple/Mediums/WifiLan/GNCMBonjourUtils.m b/internal/platform/implementation/apple/Mediums/WifiLan/GNCMBonjourUtils.m deleted file mode 100644 index 6883687f..00000000 --- a/internal/platform/implementation/apple/Mediums/WifiLan/GNCMBonjourUtils.m +++ /dev/null @@ -1,17 +0,0 @@ -// Copyright 2020 Google LLC -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// https://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#import "internal/platform/implementation/apple/Mediums/WifiLan/GNCMBonjourUtils.h" - -NSString *const GNCMBonjourDomain = @"local"; diff --git a/internal/platform/implementation/apple/wifi_lan.mm b/internal/platform/implementation/apple/wifi_lan.mm index c6d6b98b..7cadd50a 100644 --- a/internal/platform/implementation/apple/wifi_lan.mm +++ b/internal/platform/implementation/apple/wifi_lan.mm @@ -18,9 +18,9 @@ #include #include -#import "internal/platform/implementation/apple/Mediums/WifiLanV2/GNCWiFiLANMedium.h" -#import "internal/platform/implementation/apple/Mediums/WifiLanV2/GNCWiFiLANServerSocket.h" -#import "internal/platform/implementation/apple/Mediums/WifiLanV2/GNCWiFiLANSocket.h" +#import "internal/platform/implementation/apple/Mediums/WiFiLAN/GNCWiFiLANMedium.h" +#import "internal/platform/implementation/apple/Mediums/WiFiLAN/GNCWiFiLANServerSocket.h" +#import "internal/platform/implementation/apple/Mediums/WiFiLAN/GNCWiFiLANSocket.h" #import "GoogleToolboxForMac/GTMLogger.h" namespace nearby {