Rename WifiLan to NWFramework(Network.Framework) and put them as common codes.

PiperOrigin-RevId: 725486114
This commit is contained in:
Edwin Wu
2025-02-10 22:47:21 -08:00
committed by Copybara-Service
parent 9a98e8dacf
commit 74146df90a
15 changed files with 149 additions and 128 deletions
@@ -35,12 +35,12 @@ objc_library(
"GNCLeaks.h",
"GNCLeaks.m",
"GNCMConnection.m",
"WiFiLAN/GNCIPv4Address.m",
"WiFiLAN/GNCWiFiLANError.m",
"WiFiLAN/GNCWiFiLANMedium.m",
"WiFiLAN/GNCWiFiLANServerSocket.m",
"WiFiLAN/GNCWiFiLANServerSocket+Internal.h",
"WiFiLAN/GNCWiFiLANSocket.m",
"NWFramework/GNCIPv4Address.m",
"NWFramework/GNCNWFramework.m",
"NWFramework/GNCNWFrameworkError.m",
"NWFramework/GNCNWFrameworkServerSocket.m",
"NWFramework/GNCNWFrameworkServerSocket+Internal.h",
"NWFramework/GNCNWFrameworkSocket.m",
],
hdrs = [
"BLEv2/GNCBLEError.h",
@@ -55,11 +55,11 @@ objc_library(
"Ble/GNCMBleConnection.h",
"Ble/GNCMBleUtils.h",
"GNCMConnection.h",
"WiFiLAN/GNCIPv4Address.h",
"WiFiLAN/GNCWiFiLANError.h",
"WiFiLAN/GNCWiFiLANMedium.h",
"WiFiLAN/GNCWiFiLANServerSocket.h",
"WiFiLAN/GNCWiFiLANSocket.h",
"NWFramework/GNCIPv4Address.h",
"NWFramework/GNCNWFramework.h",
"NWFramework/GNCNWFrameworkError.h",
"NWFramework/GNCNWFrameworkServerSocket.h",
"NWFramework/GNCNWFrameworkSocket.h",
],
deps = [
"//internal/platform/implementation/apple:Shared",
@@ -1,4 +1,4 @@
// Copyright 2023 Google LLC
// Copyright 2025 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -1,4 +1,4 @@
// Copyright 2023 Google LLC
// Copyright 2025 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#import "internal/platform/implementation/apple/Mediums/WiFiLAN/GNCIPv4Address.h"
#import "internal/platform/implementation/apple/Mediums/NWFramework/GNCIPv4Address.h"
@implementation GNCIPv4Address
@@ -1,4 +1,4 @@
// Copyright 2023 Google LLC
// Copyright 2025 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -15,18 +15,18 @@
#import <Foundation/Foundation.h>
@class GNCIPv4Address;
@class GNCWiFiLANServerSocket;
@class GNCWiFiLANSocket;
@class GNCNWFrameworkServerSocket;
@class GNCNWFrameworkSocket;
/** A handler that delivers updates about discovered services. */
typedef void (^ServiceUpdateHandler)(NSString *_Nonnull serviceName,
NSDictionary<NSString *, NSString *> *_Nonnull txtRecords);
/**
* The @c GNCWiFiLANMedium object is used as a delegate by the platform abstraction layer for
* Wi-Fi LAN related functionality on Apple platforms.
* The @c GNCNWFramework object is used as a delegate by the platform abstraction layer for
* Network.Framework medium related functionality on Apple platforms.
*/
@interface GNCWiFiLANMedium : NSObject
@interface GNCNWFramework : NSObject
/**
* Listens for incoming connections on a given port.
@@ -41,11 +41,13 @@ typedef void (^ServiceUpdateHandler)(NSString *_Nonnull serviceName,
* @param port The port on which the listener can accept connections. Should be a number between 1
* and 65536 to open a server socket on that exact port. Zero can be used to listen on
* a random port.
* @param includePeerToPeer Whether to include peer-to-peer services.
* @param[out] error Error that will be populated on failure.
* @return Returns a server socket or nil if an error has occured.
*/
- (nullable GNCWiFiLANServerSocket *)listenForServiceOnPort:(NSInteger)port
error:(NSError **_Nullable)error;
- (nullable GNCNWFrameworkServerSocket *)listenForServiceOnPort:(NSInteger)port
includePeerToPeer:(BOOL)includePeerToPeer
error:(NSError **_Nullable)error;
/**
* Creates a Bonjour service that advertises the listener on the local network.
@@ -71,12 +73,14 @@ typedef void (^ServiceUpdateHandler)(NSString *_Nonnull serviceName,
* Starts browsing for a Bonjour service.
*
* @param serviceType The Bonjour type of the service.
* @param includePeerToPeer Whether to include peer-to-peer services.
* @param serviceFoundHandler A handler called when a new service is found.
* @param serviceLostHandler A handler called when a previously discovered service is lost.
* @param[out] error Error that will be populated on failure.
* @return Returns YES when discovery has successfully started.
*/
- (BOOL)startDiscoveryForServiceType:(nonnull NSString *)serviceType
includePeerToPeer:(BOOL)includePeerToPeer
serviceFoundHandler:(ServiceUpdateHandler)serviceFoundHandler
serviceLostHandler:(ServiceUpdateHandler)serviceLostHandler
error:(NSError **_Nullable)error;
@@ -93,12 +97,14 @@ typedef void (^ServiceUpdateHandler)(NSString *_Nonnull serviceName,
*
* @param serviceName The Bonjour name of the service.
* @param serviceType The Bonjour type of the service.
* @param includePeerToPeer Whether to include peer-to-peer services.
* @param[out] error Error that will be populated on failure.
* @return Returns a connected socket or nil if an error has occured.
*/
- (nullable GNCWiFiLANSocket *)connectToServiceName:(nonnull NSString *)serviceName
serviceType:(nonnull NSString *)serviceType
error:(NSError **_Nullable)error;
- (nullable GNCNWFrameworkSocket *)connectToServiceName:(nonnull NSString *)serviceName
serviceType:(nonnull NSString *)serviceType
includePeerToPeer:(BOOL)includePeerToPeer
error:(NSError **_Nullable)error;
/**
* Connects to an IP address and port.
@@ -108,8 +114,8 @@ typedef void (^ServiceUpdateHandler)(NSString *_Nonnull serviceName,
* @param[out] error Error that will be populated on failure.
* @return Returns a connected socket or nil if an error has occured.
*/
- (nullable GNCWiFiLANSocket *)connectToHost:(nonnull GNCIPv4Address *)host
port:(NSInteger)port
error:(NSError **_Nullable)error;
- (nullable GNCNWFrameworkSocket *)connectToHost:(nonnull GNCIPv4Address *)host
port:(NSInteger)port
error:(NSError **_Nullable)error;
@end
@@ -1,4 +1,4 @@
// Copyright 2023 Google LLC
// Copyright 2025 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -12,16 +12,16 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#import "internal/platform/implementation/apple/Mediums/WiFiLAN/GNCWiFiLANMedium.h"
#import "internal/platform/implementation/apple/Mediums/NWFramework/GNCNWFramework.h"
#import <Foundation/Foundation.h>
#import <Network/Network.h>
#import "internal/platform/implementation/apple/Mediums/WiFiLAN/GNCIPv4Address.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 "internal/platform/implementation/apple/Mediums/NWFramework/GNCIPv4Address.h"
#import "internal/platform/implementation/apple/Mediums/NWFramework/GNCNWFrameworkError.h"
#import "internal/platform/implementation/apple/Mediums/NWFramework/GNCNWFrameworkServerSocket+Internal.h"
#import "internal/platform/implementation/apple/Mediums/NWFramework/GNCNWFrameworkServerSocket.h"
#import "internal/platform/implementation/apple/Mediums/NWFramework/GNCNWFrameworkSocket.h"
#import "GoogleToolboxForMac/GTMLogger.h"
// An arbitrary timeout that should be pretty lenient.
@@ -60,14 +60,14 @@ NSDictionary<NSString *, NSString *> *GNCTXTRecordForBrowseResult(nw_browse_resu
return txtRecords;
}
@implementation GNCWiFiLANMedium {
@implementation GNCNWFramework {
// Holds a weak reference to a server socket that is retrievable by port. This allows us to stop
// advertisements for a given port without taking a strong reference. This keeps the ownership
// of the server socket's lifetime with the caller of listenForServiceOnPort:error:.
//
// Usage of .count should be avoided. Zombie keys won't show up in -keyEnumerator, but WILL be
// included in .count until the next time that the internal hashtable is resized.
NSMapTable<NSNumber *, GNCWiFiLANServerSocket *> *_serverSockets;
NSMapTable<NSNumber *, GNCNWFrameworkServerSocket *> *_serverSockets;
// Maps a Bonjour service type to its browser. This allows us to stop the discovery given the
// service type. We maintain ownership of the browser's lifetime, so we can maintain a strong
@@ -83,9 +83,11 @@ NSDictionary<NSString *, NSString *> *GNCTXTRecordForBrowseResult(nw_browse_resu
return self;
}
- (GNCWiFiLANServerSocket *)listenForServiceOnPort:(NSInteger)port error:(NSError **)error {
GNCWiFiLANServerSocket *serverSocket = [[GNCWiFiLANServerSocket alloc] initWithPort:port];
BOOL success = [serverSocket startListeningWithError:error];
- (GNCNWFrameworkServerSocket *)listenForServiceOnPort:(NSInteger)port
includePeerToPeer:(BOOL)includePeerToPeer
error:(NSError **)error {
GNCNWFrameworkServerSocket *serverSocket = [[GNCNWFrameworkServerSocket alloc] initWithPort:port];
BOOL success = [serverSocket startListeningWithError:error includePeerToPeer:includePeerToPeer];
if (success) {
[_serverSockets setObject:serverSocket forKey:@(serverSocket.port)];
return serverSocket;
@@ -97,36 +99,37 @@ NSDictionary<NSString *, NSString *> *GNCTXTRecordForBrowseResult(nw_browse_resu
serviceName:(NSString *)serviceName
serviceType:(NSString *)serviceType
txtRecords:(NSDictionary<NSString *, NSString *> *)txtRecords {
GNCWiFiLANServerSocket *serverSocket = [_serverSockets objectForKey:@(port)];
GNCNWFrameworkServerSocket *serverSocket = [_serverSockets objectForKey:@(port)];
[serverSocket startAdvertisingServiceName:serviceName
serviceType:serviceType
txtRecords:txtRecords];
}
- (void)stopAdvertisingPort:(NSInteger)port {
GNCWiFiLANServerSocket *serverSocket = [_serverSockets objectForKey:@(port)];
GNCNWFrameworkServerSocket *serverSocket = [_serverSockets objectForKey:@(port)];
[serverSocket stopAdvertising];
}
- (BOOL)startDiscoveryForServiceType:(NSString *)serviceType
includePeerToPeer:(BOOL)includePeerToPeer
serviceFoundHandler:(ServiceUpdateHandler)serviceFoundHandler
serviceLostHandler:(ServiceUpdateHandler)serviceLostHandler
error:(NSError **)error {
if ([_serviceBrowsers objectForKey:serviceType] != nil) {
if (error != nil) {
*error = [NSError errorWithDomain:GNCWiFiLANErrorDomain
code:GNCWiFiLANErrorDuplicateDiscovererForServiceType
*error = [NSError errorWithDomain:GNCNWFrameworkErrorDomain
code:GNCNWFrameworkErrorDuplicateDiscovererForServiceType
userInfo:nil];
}
return NO;
}
// Create a parameters object configured to support TCP. TLS MUST be disabled for Nearby to
// function properly. This also is set to include peer-to-peer, but unsure if it's required.
// function properly.
nw_parameters_t parameters =
nw_parameters_create_secure_tcp(/*tls*/ NW_PARAMETERS_DISABLE_PROTOCOL,
/*tcp*/ NW_PARAMETERS_DEFAULT_CONFIGURATION);
nw_parameters_set_include_peer_to_peer(parameters, true);
nw_parameters_set_include_peer_to_peer(parameters, includePeerToPeer);
nw_browse_descriptor_t descriptor =
nw_browse_descriptor_create_bonjour_service([serviceType UTF8String], /*domain=*/nil);
nw_browse_descriptor_set_include_txt_record(descriptor, YES);
@@ -223,8 +226,8 @@ NSDictionary<NSString *, NSString *> *GNCTXTRecordForBrowseResult(nw_browse_resu
if (!didSignal) {
[self stopDiscoveryForServiceType:serviceType];
if (error != nil) {
*error = [NSError errorWithDomain:GNCWiFiLANErrorDomain
code:GNCWiFiLANErrorTimedOut
*error = [NSError errorWithDomain:GNCNWFrameworkErrorDomain
code:GNCNWFrameworkErrorTimedOut
userInfo:nil];
}
return NO;
@@ -251,23 +254,26 @@ NSDictionary<NSString *, NSString *> *GNCTXTRecordForBrowseResult(nw_browse_resu
nw_browser_cancel(browser);
}
- (GNCWiFiLANSocket *)connectToServiceName:(NSString *)serviceName
serviceType:(NSString *)serviceType
error:(NSError **)error {
- (GNCNWFrameworkSocket *)connectToServiceName:(NSString *)serviceName
serviceType:(NSString *)serviceType
includePeerToPeer:(BOOL)includePeerToPeer
error:(NSError **)error {
nw_endpoint_t endpoint = nw_endpoint_create_bonjour_service([serviceName UTF8String],
[serviceType UTF8String], "local");
return [self connectToEndpoint:endpoint error:error];
return [self connectToEndpoint:endpoint includePeerToPeer:includePeerToPeer error:error];
}
- (GNCWiFiLANSocket *)connectToHost:(GNCIPv4Address *)host
- (GNCNWFrameworkSocket *)connectToHost:(GNCIPv4Address *)host
port:(NSInteger)port
error:(NSError **)error {
nw_endpoint_t endpoint =
nw_endpoint_create_host(host.dottedRepresentation.UTF8String, @(port).stringValue.UTF8String);
return [self connectToEndpoint:endpoint error:error];
return [self connectToEndpoint:endpoint includePeerToPeer:(BOOL)NO error:error];
}
- (GNCWiFiLANSocket *)connectToEndpoint:(nw_endpoint_t)endpoint error:(NSError **)error {
- (GNCNWFrameworkSocket *)connectToEndpoint:(nw_endpoint_t)endpoint
includePeerToPeer:(BOOL)includePeerToPeer
error:(NSError **)error {
NSCondition *condition = [[NSCondition alloc] init];
[condition lock];
@@ -277,7 +283,7 @@ NSDictionary<NSString *, NSString *> *GNCTXTRecordForBrowseResult(nw_browse_resu
nw_parameters_t parameters =
nw_parameters_create_secure_tcp(/*tls*/ NW_PARAMETERS_DISABLE_PROTOCOL,
/*tcp*/ NW_PARAMETERS_DEFAULT_CONFIGURATION);
nw_parameters_set_include_peer_to_peer(parameters, true);
nw_parameters_set_include_peer_to_peer(parameters, includePeerToPeer);
nw_connection_t connection = nw_connection_create(endpoint, parameters);
nw_connection_set_queue(connection, dispatch_get_main_queue());
nw_connection_set_state_changed_handler(
@@ -303,8 +309,8 @@ NSDictionary<NSString *, NSString *> *GNCTXTRecordForBrowseResult(nw_browse_resu
if (!didSignal) {
nw_connection_cancel(connection);
if (error != nil) {
*error = [NSError errorWithDomain:GNCWiFiLANErrorDomain
code:GNCWiFiLANErrorTimedOut
*error = [NSError errorWithDomain:GNCNWFrameworkErrorDomain
code:GNCNWFrameworkErrorTimedOut
userInfo:nil];
}
return nil;
@@ -322,7 +328,7 @@ NSDictionary<NSString *, NSString *> *GNCTXTRecordForBrowseResult(nw_browse_resu
case nw_connection_state_cancelled:
return nil;
case nw_connection_state_ready:
return [[GNCWiFiLANSocket alloc] initWithConnection:connection];
return [[GNCNWFrameworkSocket alloc] initWithConnection:connection];
}
}
@@ -1,4 +1,4 @@
// Copyright 2023 Google LLC
// Copyright 2025 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -15,15 +15,15 @@
#import <Foundation/Foundation.h>
/**
* The domain for @c NSErrors raised by the Wi-Fi LAN medium.
* The domain for @c NSErrors raised by the Apple Network.Framework medium.
*/
extern NSErrorDomain const GNCWiFiLANErrorDomain;
extern NSErrorDomain const GNCNWFrameworkErrorDomain;
/**
* Wi-Fi LAN medium error codes.
* Apple Network.Framework medium error codes.
*/
typedef NS_ERROR_ENUM(GNCWiFiLANErrorDomain, GNCWiFiLANError){
GNCWiFiLANErrorUnknown,
GNCWiFiLANErrorTimedOut,
GNCWiFiLANErrorDuplicateDiscovererForServiceType,
typedef NS_ERROR_ENUM(GNCNWFrameworkErrorDomain, GNCNWFrameworkError){
GNCNWFrameworkErrorUnknown,
GNCNWFrameworkErrorTimedOut,
GNCNWFrameworkErrorDuplicateDiscovererForServiceType,
};
@@ -1,4 +1,4 @@
// Copyright 2023 Google LLC
// Copyright 2025 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -12,6 +12,6 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#import "internal/platform/implementation/apple/Mediums/WiFiLAN/GNCWiFiLANError.h"
#import "internal/platform/implementation/apple/Mediums/NWFramework/GNCNWFrameworkError.h"
NSErrorDomain const GNCWiFiLANErrorDomain = @"com.google.nearby.wifilan.error";
NSErrorDomain const GNCNWFrameworkErrorDomain = @"com.google.nearby.network.error";
@@ -1,4 +1,4 @@
// Copyright 2023 Google LLC
// Copyright 2025 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -14,19 +14,21 @@
#import <Foundation/Foundation.h>
#import "internal/platform/implementation/apple/Mediums/WiFiLAN/GNCWiFiLANServerSocket.h"
#import "internal/platform/implementation/apple/Mediums/NWFramework/GNCNWFrameworkServerSocket.h"
@interface GNCWiFiLANServerSocket (Internal)
@interface GNCNWFrameworkServerSocket (Internal)
/**
* Starts listening for inbound connections.
*
* Blocks execution until listening has started or failed.
*
* @param includePeerToPeer Indicates if the server should be configured for peer-to-peer
* connections.
* @param[out] error Error that will be populated on failure.
* @return Returns YES when listening has successfully started.
*/
- (BOOL)startListeningWithError:(NSError **_Nullable)error;
- (BOOL)startListeningWithError:(NSError **_Nullable)error includePeerToPeer:(BOOL)peerToPeer;
/**
* Creates a Bonjour service that advertises the listener on the local network.
@@ -1,4 +1,4 @@
// Copyright 2023 Google LLC
// Copyright 2025 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -15,9 +15,9 @@
#import <Foundation/Foundation.h>
@class GNCIPv4Address;
@class GNCWiFiLANSocket;
@class GNCNWFrameworkSocket;
@interface GNCWiFiLANServerSocket : NSObject
@interface GNCNWFrameworkServerSocket : NSObject
/**
* @remark init is not an available initializer.
@@ -52,7 +52,7 @@
* @param[out] error Error that will be populated on failure.
* @return Returns a connected socket or nil if an error has occured.
*/
- (nullable GNCWiFiLANSocket *)acceptWithError:(NSError **_Nullable)error;
- (nullable GNCNWFrameworkSocket *)acceptWithError:(NSError **_Nullable)error;
/**
* Stops listening for inbound connections.
@@ -1,4 +1,4 @@
// Copyright 2023 Google LLC
// Copyright 2025 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#import "internal/platform/implementation/apple/Mediums/WiFiLAN/GNCWiFiLANServerSocket.h"
#import "internal/platform/implementation/apple/Mediums/NWFramework/GNCNWFrameworkServerSocket.h"
#import <Foundation/Foundation.h>
#import <Network/Network.h>
@@ -22,20 +22,20 @@
#include <netdb.h>
#include <sys/socket.h>
#import "internal/platform/implementation/apple/Mediums/WiFiLAN/GNCIPv4Address.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 "internal/platform/implementation/apple/Mediums/NWFramework/GNCIPv4Address.h"
#import "internal/platform/implementation/apple/Mediums/NWFramework/GNCNWFrameworkError.h"
#import "internal/platform/implementation/apple/Mediums/NWFramework/GNCNWFrameworkServerSocket+Internal.h"
#import "internal/platform/implementation/apple/Mediums/NWFramework/GNCNWFrameworkSocket.h"
#import "GoogleToolboxForMac/GTMLogger.h"
@interface GNCWiFiLANServerSocket ()
@interface GNCNWFrameworkServerSocket ()
@property(nonatomic, readonly) NSMutableArray<nw_connection_t> *pendingConnections;
@property(nonatomic, readonly) NSMutableArray<nw_connection_t> *readyConnections;
@end
@implementation GNCWiFiLANServerSocket {
@implementation GNCNWFrameworkServerSocket {
NSCondition *_condition;
nw_listener_t _listener;
nw_listener_state_t _listenerState;
@@ -74,12 +74,12 @@
- (GNCIPv4Address *)ipAddress {
if (!_ipAddress) {
_ipAddress = [GNCWiFiLANServerSocket lookupIpAddress];
_ipAddress = [GNCNWFrameworkServerSocket lookupIpAddress];
}
return _ipAddress;
}
- (GNCWiFiLANSocket *)acceptWithError:(NSError **)error {
- (GNCNWFrameworkSocket *)acceptWithError:(NSError **)error {
// Wait until we have a ready connection and listener is in a ready/invalid state.
[_condition lock];
nw_connection_t connection = [self.readyConnections lastObject];
@@ -99,7 +99,7 @@
if (connection == nil) {
return nil;
}
return [[GNCWiFiLANSocket alloc] initWithConnection:connection];
return [[GNCNWFrameworkSocket alloc] initWithConnection:connection];
}
- (void)close {
@@ -110,13 +110,13 @@
_listener = nil;
}
- (BOOL)startListeningWithError:(NSError **)error {
- (BOOL)startListeningWithError:(NSError **)error includePeerToPeer:(BOOL)includePeerToPeer {
// Create a parameters object configured to support TCP. TLS MUST be disabled for Nearby to
// function properly. This also is set to include peer-to-peer, but unsure if it's required.
// function properly.
nw_parameters_t parameters =
nw_parameters_create_secure_tcp(/*tls*/ NW_PARAMETERS_DISABLE_PROTOCOL,
/*tcp*/ NW_PARAMETERS_DEFAULT_CONFIGURATION);
nw_parameters_set_include_peer_to_peer(parameters, true);
nw_parameters_set_include_peer_to_peer(parameters, includePeerToPeer);
// If the server socket port is zero, a random port will be selected. The server socket port will
// be updated to reflect the port it's listening on, once the listener transitions to the ready
@@ -192,8 +192,8 @@
// We timed out waiting for the listener to transition into a state.
if (!didSignal) {
[self close];
_listenerError = [NSError errorWithDomain:GNCWiFiLANErrorDomain
code:GNCWiFiLANErrorTimedOut
_listenerError = [NSError errorWithDomain:GNCNWFrameworkErrorDomain
code:GNCNWFrameworkErrorTimedOut
userInfo:nil];
return NO;
}
@@ -1,4 +1,4 @@
// Copyright 2023 Google LLC
// Copyright 2025 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -15,7 +15,7 @@
#import <Foundation/Foundation.h>
#import <Network/Network.h>
@interface GNCWiFiLANSocket : NSObject
@interface GNCNWFrameworkSocket : NSObject
/**
* @remark init is not an available initializer.
@@ -1,4 +1,4 @@
// Copyright 2023 Google LLC
// Copyright 2025 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -12,20 +12,20 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#import "internal/platform/implementation/apple/Mediums/WiFiLAN/GNCWiFiLANSocket.h"
#import "internal/platform/implementation/apple/Mediums/NWFramework/GNCNWFrameworkSocket.h"
#import <Foundation/Foundation.h>
#import <Network/Network.h>
#import "GoogleToolboxForMac/GTMLogger.h"
@interface GNCWiFiLANSocket ()
@interface GNCNWFrameworkSocket ()
@property(nonatomic, readonly) nw_connection_t connection;
@end
@implementation GNCWiFiLANSocket {
@implementation GNCNWFrameworkSocket {
}
- (instancetype)initWithConnection:(nw_connection_t)connection {
@@ -17,7 +17,7 @@
#include <string>
#import "internal/platform/implementation/apple/Mediums/WiFiLAN/GNCIPv4Address.h"
#import "internal/platform/implementation/apple/Mediums/NWFramework/GNCIPv4Address.h"
@interface GNCIPv4AddressTest : XCTestCase
@end
@@ -26,9 +26,9 @@
@class GNCMBonjourBrowser;
@class GNCMBonjourService;
@class GNCWiFiLANMedium;
@class GNCWiFiLANServerSocket;
@class GNCWiFiLANSocket;
@class GNCNWFramework;
@class GNCNWFrameworkServerSocket;
@class GNCNWFrameworkSocket;
namespace nearby {
namespace apple {
@@ -38,14 +38,14 @@ namespace apple {
*/
class WifiLanInputStream : public InputStream {
public:
explicit WifiLanInputStream(GNCWiFiLANSocket* socket);
explicit WifiLanInputStream(GNCNWFrameworkSocket* socket);
~WifiLanInputStream() override = default;
ExceptionOr<ByteArray> Read(std::int64_t size) override;
Exception Close() override;
private:
GNCWiFiLANSocket* socket_;
GNCNWFrameworkSocket* socket_;
};
/**
@@ -53,7 +53,7 @@ class WifiLanInputStream : public InputStream {
*/
class WifiLanOutputStream : public OutputStream {
public:
explicit WifiLanOutputStream(GNCWiFiLANSocket* socket);
explicit WifiLanOutputStream(GNCNWFrameworkSocket* socket);
~WifiLanOutputStream() override = default;
Exception Write(const ByteArray& data) override;
@@ -61,7 +61,7 @@ class WifiLanOutputStream : public OutputStream {
Exception Close() override;
private:
GNCWiFiLANSocket* socket_;
GNCNWFrameworkSocket* socket_;
};
/**
@@ -69,7 +69,7 @@ class WifiLanOutputStream : public OutputStream {
*/
class WifiLanSocket : public api::WifiLanSocket {
public:
explicit WifiLanSocket(GNCWiFiLANSocket* socket);
explicit WifiLanSocket(GNCNWFrameworkSocket* socket);
~WifiLanSocket() override = default;
InputStream& GetInputStream() override;
@@ -77,7 +77,7 @@ class WifiLanSocket : public api::WifiLanSocket {
Exception Close() override;
private:
GNCWiFiLANSocket* socket_;
GNCNWFrameworkSocket* socket_;
std::unique_ptr<WifiLanInputStream> input_stream_;
std::unique_ptr<WifiLanOutputStream> output_stream_;
};
@@ -87,7 +87,7 @@ class WifiLanSocket : public api::WifiLanSocket {
*/
class WifiLanServerSocket : public api::WifiLanServerSocket {
public:
explicit WifiLanServerSocket(GNCWiFiLANServerSocket* server_socket);
explicit WifiLanServerSocket(GNCNWFrameworkServerSocket* server_socket);
~WifiLanServerSocket() override = default;
std::string GetIPAddress() const override;
@@ -96,7 +96,7 @@ class WifiLanServerSocket : public api::WifiLanServerSocket {
Exception Close() override;
private:
GNCWiFiLANServerSocket* server_socket_;
GNCNWFrameworkServerSocket* server_socket_;
};
/**
@@ -126,7 +126,7 @@ class WifiLanMedium : public api::WifiLanMedium {
std::unique_ptr<api::WifiLanServerSocket> ListenForService(int port) override;
private:
GNCWiFiLANMedium* medium_;
GNCNWFramework* medium_;
absl::AnyInvocable<void(NsdServiceInfo)> service_discovered_cb_;
absl::AnyInvocable<void(NsdServiceInfo)> service_lost_cb_;
};
@@ -19,10 +19,10 @@
#include <string>
#include <utility>
#import "internal/platform/implementation/apple/Mediums/WiFiLAN/GNCIPv4Address.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 "internal/platform/implementation/apple/Mediums/NWFramework/GNCIPv4Address.h"
#import "internal/platform/implementation/apple/Mediums/NWFramework/GNCNWFramework.h"
#import "internal/platform/implementation/apple/Mediums/NWFramework/GNCNWFrameworkServerSocket.h"
#import "internal/platform/implementation/apple/Mediums/NWFramework/GNCNWFrameworkSocket.h"
#import "GoogleToolboxForMac/GTMLogger.h"
namespace nearby {
@@ -30,7 +30,7 @@ namespace apple {
#pragma mark - WifiLanInputStream
WifiLanInputStream::WifiLanInputStream(GNCWiFiLANSocket* socket) : socket_(socket) {}
WifiLanInputStream::WifiLanInputStream(GNCNWFrameworkSocket* socket) : socket_(socket) {}
ExceptionOr<ByteArray> WifiLanInputStream::Read(std::int64_t size) {
NSError* error = nil;
@@ -50,7 +50,7 @@ Exception WifiLanInputStream::Close() {
#pragma mark - WifiLanOutputStream
WifiLanOutputStream::WifiLanOutputStream(GNCWiFiLANSocket* socket) : socket_(socket) {}
WifiLanOutputStream::WifiLanOutputStream(GNCNWFrameworkSocket* socket) : socket_(socket) {}
Exception WifiLanOutputStream::Write(const ByteArray& data) {
NSError* error = nil;
@@ -76,7 +76,7 @@ Exception WifiLanOutputStream::Close() {
#pragma mark - WifiLanSocket
WifiLanSocket::WifiLanSocket(GNCWiFiLANSocket* socket)
WifiLanSocket::WifiLanSocket(GNCNWFrameworkSocket* socket)
: socket_(socket),
input_stream_(std::make_unique<WifiLanInputStream>(socket)),
output_stream_(std::make_unique<WifiLanOutputStream>(socket)) {}
@@ -92,7 +92,7 @@ Exception WifiLanSocket::Close() {
#pragma mark - WifiLanServerSocket
WifiLanServerSocket::WifiLanServerSocket(GNCWiFiLANServerSocket* server_socket)
WifiLanServerSocket::WifiLanServerSocket(GNCNWFrameworkServerSocket* server_socket)
: server_socket_(server_socket) {}
std::string WifiLanServerSocket::GetIPAddress() const {
@@ -104,7 +104,7 @@ int WifiLanServerSocket::GetPort() const { return server_socket_.port; }
std::unique_ptr<api::WifiLanSocket> WifiLanServerSocket::Accept() {
NSError* error = nil;
GNCWiFiLANSocket* socket = [server_socket_ acceptWithError:&error];
GNCNWFrameworkSocket* socket = [server_socket_ acceptWithError:&error];
if (socket != nil) {
return std::make_unique<WifiLanSocket>(socket);
}
@@ -121,7 +121,7 @@ Exception WifiLanServerSocket::Close() {
#pragma mark - WifiLanMedium
WifiLanMedium::WifiLanMedium() : medium_([[GNCWiFiLANMedium alloc] init]) {}
WifiLanMedium::WifiLanMedium() : medium_([[GNCNWFramework alloc] init]) {}
bool WifiLanMedium::StartAdvertising(const NsdServiceInfo& nsd_service_info) {
NSInteger port = nsd_service_info.GetPort();
@@ -149,8 +149,10 @@ bool WifiLanMedium::StartDiscovery(const std::string& service_type,
__block NSString* serviceType = @(service_type.c_str());
__block DiscoveredServiceCallback client_callback = std::move(callback);
// Set `includePeerToPeer` to YES to support peer-to-peer connections for Apple devices.
NSError* error = nil;
BOOL result = [medium_ startDiscoveryForServiceType:serviceType
includePeerToPeer:YES
serviceFoundHandler:^(NSString* name, NSDictionary<NSString*, NSString*>* txtRecords) {
NsdServiceInfo nsd_service_info;
nsd_service_info.SetServiceType([serviceType UTF8String]);
@@ -188,12 +190,14 @@ bool WifiLanMedium::StopDiscovery(const std::string& service_type) {
std::unique_ptr<api::WifiLanSocket> WifiLanMedium::ConnectToService(
const NsdServiceInfo& remote_service_info, CancellationFlag* cancellation_flag) {
// Set `includePeerToPeer` to YES to support peer-to-peer connections for Apple devices.
NSError* error = nil;
NSString* serviceName = @(remote_service_info.GetServiceName().c_str());
NSString* serviceType = @(remote_service_info.GetServiceType().c_str());
GNCWiFiLANSocket* socket = [medium_ connectToServiceName:serviceName
serviceType:serviceType
error:&error];
GNCNWFrameworkSocket* socket = [medium_ connectToServiceName:serviceName
serviceType:serviceType
includePeerToPeer:YES
error:&error];
if (socket != nil) {
return std::make_unique<WifiLanSocket>(socket);
}
@@ -213,7 +217,7 @@ std::unique_ptr<api::WifiLanSocket> WifiLanMedium::ConnectToService(
}
NSData* hostData = [NSData dataWithBytes:ip_address.data() length:ip_address.size()];
GNCIPv4Address* host = [GNCIPv4Address addressFromData:hostData];
GNCWiFiLANSocket* socket = [medium_ connectToHost:host port:port error:&error];
GNCNWFrameworkSocket* socket = [medium_ connectToHost:host port:port error:&error];
if (socket != nil) {
return std::make_unique<WifiLanSocket>(socket);
}
@@ -225,7 +229,10 @@ std::unique_ptr<api::WifiLanSocket> WifiLanMedium::ConnectToService(
std::unique_ptr<api::WifiLanServerSocket> WifiLanMedium::ListenForService(int port) {
NSError* error = nil;
GNCWiFiLANServerSocket* serverSocket = [medium_ listenForServiceOnPort:port error:&error];
// Set `includePeerToPeer` to YES to support peer-to-peer connections for Apple devices.
GNCNWFrameworkServerSocket* serverSocket = [medium_ listenForServiceOnPort:port
includePeerToPeer:YES
error:&error];
if (serverSocket != nil) {
return std::make_unique<WifiLanServerSocket>(serverSocket);
}