更新socket库
This commit is contained in:
parent
cce27928e8
commit
85f50867ae
|
|
@ -12,52 +12,16 @@
|
|||
#import <Security/Security.h>
|
||||
#import <Security/SecureTransport.h>
|
||||
#import <dispatch/dispatch.h>
|
||||
#import <Availability.h>
|
||||
|
||||
#include <sys/socket.h> // AF_INET, AF_INET6
|
||||
|
||||
@class GCDAsyncReadPacket;
|
||||
@class GCDAsyncWritePacket;
|
||||
@class GCDAsyncSocketPreBuffer;
|
||||
@protocol GCDAsyncSocketDelegate;
|
||||
|
||||
#if TARGET_OS_IPHONE
|
||||
|
||||
// Compiling for iOS
|
||||
|
||||
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 50000 // iOS 5.0 supported
|
||||
|
||||
#if __IPHONE_OS_VERSION_MIN_REQUIRED >= 50000 // iOS 5.0 supported and required
|
||||
|
||||
#define IS_SECURE_TRANSPORT_AVAILABLE YES
|
||||
#define SECURE_TRANSPORT_MAYBE_AVAILABLE 1
|
||||
#define SECURE_TRANSPORT_MAYBE_UNAVAILABLE 0
|
||||
|
||||
#else // iOS 5.0 supported but not required
|
||||
|
||||
#ifndef NSFoundationVersionNumber_iPhoneOS_5_0
|
||||
#define NSFoundationVersionNumber_iPhoneOS_5_0 881.00
|
||||
#endif
|
||||
|
||||
#define IS_SECURE_TRANSPORT_AVAILABLE (NSFoundationVersionNumber >= NSFoundationVersionNumber_iPhoneOS_5_0)
|
||||
#define SECURE_TRANSPORT_MAYBE_AVAILABLE 1
|
||||
#define SECURE_TRANSPORT_MAYBE_UNAVAILABLE 1
|
||||
|
||||
#endif
|
||||
|
||||
#else // iOS 5.0 not supported
|
||||
|
||||
#define IS_SECURE_TRANSPORT_AVAILABLE NO
|
||||
#define SECURE_TRANSPORT_MAYBE_AVAILABLE 0
|
||||
#define SECURE_TRANSPORT_MAYBE_UNAVAILABLE 1
|
||||
|
||||
#endif
|
||||
|
||||
#else
|
||||
|
||||
// Compiling for Mac OS X
|
||||
|
||||
#define IS_SECURE_TRANSPORT_AVAILABLE YES
|
||||
#define SECURE_TRANSPORT_MAYBE_AVAILABLE 1
|
||||
#define SECURE_TRANSPORT_MAYBE_UNAVAILABLE 0
|
||||
|
||||
#endif
|
||||
NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
extern NSString *const GCDAsyncSocketException;
|
||||
extern NSString *const GCDAsyncSocketErrorDomain;
|
||||
|
|
@ -65,18 +29,28 @@ extern NSString *const GCDAsyncSocketErrorDomain;
|
|||
extern NSString *const GCDAsyncSocketQueueName;
|
||||
extern NSString *const GCDAsyncSocketThreadName;
|
||||
|
||||
#if SECURE_TRANSPORT_MAYBE_AVAILABLE
|
||||
extern NSString *const GCDAsyncSocketSSLCipherSuites;
|
||||
extern NSString *const GCDAsyncSocketManuallyEvaluateTrust;
|
||||
#if TARGET_OS_IPHONE
|
||||
extern NSString *const GCDAsyncSocketUseCFStreamForTLS;
|
||||
#endif
|
||||
#define GCDAsyncSocketSSLPeerName (NSString *)kCFStreamSSLPeerName
|
||||
#define GCDAsyncSocketSSLCertificates (NSString *)kCFStreamSSLCertificates
|
||||
#define GCDAsyncSocketSSLIsServer (NSString *)kCFStreamSSLIsServer
|
||||
extern NSString *const GCDAsyncSocketSSLPeerID;
|
||||
extern NSString *const GCDAsyncSocketSSLProtocolVersionMin;
|
||||
extern NSString *const GCDAsyncSocketSSLProtocolVersionMax;
|
||||
#else
|
||||
extern NSString *const GCDAsyncSocketSSLSessionOptionFalseStart;
|
||||
extern NSString *const GCDAsyncSocketSSLSessionOptionSendOneByteRecord;
|
||||
extern NSString *const GCDAsyncSocketSSLCipherSuites;
|
||||
extern NSString *const GCDAsyncSocketSSLALPN;
|
||||
#if !TARGET_OS_IPHONE
|
||||
extern NSString *const GCDAsyncSocketSSLDiffieHellmanParameters;
|
||||
#endif
|
||||
#endif
|
||||
|
||||
enum GCDAsyncSocketError
|
||||
{
|
||||
#define GCDAsyncSocketLoggingContext 65535
|
||||
|
||||
|
||||
typedef NS_ERROR_ENUM(GCDAsyncSocketErrorDomain, GCDAsyncSocketError) {
|
||||
GCDAsyncSocketNoError = 0, // Never used
|
||||
GCDAsyncSocketBadConfigError, // Invalid configuration
|
||||
GCDAsyncSocketBadParamError, // Invalid parameter was passed
|
||||
|
|
@ -87,12 +61,12 @@ enum GCDAsyncSocketError
|
|||
GCDAsyncSocketClosedError, // The remote peer closed the connection
|
||||
GCDAsyncSocketOtherError, // Description provided in userInfo
|
||||
};
|
||||
typedef enum GCDAsyncSocketError GCDAsyncSocketError;
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
#pragma mark -
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
|
||||
@interface GCDAsyncSocket : NSObject
|
||||
|
||||
/**
|
||||
|
|
@ -111,24 +85,39 @@ typedef enum GCDAsyncSocketError GCDAsyncSocketError;
|
|||
*
|
||||
* The delegate queue and socket queue can optionally be the same.
|
||||
**/
|
||||
- (id)init;
|
||||
- (id)initWithSocketQueue:(dispatch_queue_t)sq;
|
||||
- (id)initWithDelegate:(id)aDelegate delegateQueue:(dispatch_queue_t)dq;
|
||||
- (id)initWithDelegate:(id)aDelegate delegateQueue:(dispatch_queue_t)dq socketQueue:(dispatch_queue_t)sq;
|
||||
- (instancetype)init;
|
||||
- (instancetype)initWithSocketQueue:(nullable dispatch_queue_t)sq;
|
||||
- (instancetype)initWithDelegate:(nullable id<GCDAsyncSocketDelegate>)aDelegate delegateQueue:(nullable dispatch_queue_t)dq;
|
||||
- (instancetype)initWithDelegate:(nullable id<GCDAsyncSocketDelegate>)aDelegate delegateQueue:(nullable dispatch_queue_t)dq socketQueue:(nullable dispatch_queue_t)sq NS_DESIGNATED_INITIALIZER;
|
||||
|
||||
/**
|
||||
* Create GCDAsyncSocket from already connect BSD socket file descriptor
|
||||
**/
|
||||
+ (nullable instancetype)socketFromConnectedSocketFD:(int)socketFD socketQueue:(nullable dispatch_queue_t)sq error:(NSError**)error;
|
||||
|
||||
+ (nullable instancetype)socketFromConnectedSocketFD:(int)socketFD delegate:(nullable id<GCDAsyncSocketDelegate>)aDelegate delegateQueue:(nullable dispatch_queue_t)dq error:(NSError**)error;
|
||||
|
||||
+ (nullable instancetype)socketFromConnectedSocketFD:(int)socketFD delegate:(nullable id<GCDAsyncSocketDelegate>)aDelegate delegateQueue:(nullable dispatch_queue_t)dq socketQueue:(nullable dispatch_queue_t)sq error:(NSError **)error;
|
||||
|
||||
#pragma mark Configuration
|
||||
|
||||
- (id)delegate;
|
||||
- (void)setDelegate:(id)delegate;
|
||||
- (void)synchronouslySetDelegate:(id)delegate;
|
||||
@property (atomic, weak, readwrite, nullable) id<GCDAsyncSocketDelegate> delegate;
|
||||
#if OS_OBJECT_USE_OBJC
|
||||
@property (atomic, strong, readwrite, nullable) dispatch_queue_t delegateQueue;
|
||||
#else
|
||||
@property (atomic, assign, readwrite, nullable) dispatch_queue_t delegateQueue;
|
||||
#endif
|
||||
|
||||
- (dispatch_queue_t)delegateQueue;
|
||||
- (void)setDelegateQueue:(dispatch_queue_t)delegateQueue;
|
||||
- (void)synchronouslySetDelegateQueue:(dispatch_queue_t)delegateQueue;
|
||||
- (void)getDelegate:(id<GCDAsyncSocketDelegate> __nullable * __nullable)delegatePtr delegateQueue:(dispatch_queue_t __nullable * __nullable)delegateQueuePtr;
|
||||
- (void)setDelegate:(nullable id<GCDAsyncSocketDelegate>)delegate delegateQueue:(nullable dispatch_queue_t)delegateQueue;
|
||||
|
||||
- (void)getDelegate:(id *)delegatePtr delegateQueue:(dispatch_queue_t *)delegateQueuePtr;
|
||||
- (void)setDelegate:(id)delegate delegateQueue:(dispatch_queue_t)delegateQueue;
|
||||
- (void)synchronouslySetDelegate:(id)delegate delegateQueue:(dispatch_queue_t)delegateQueue;
|
||||
/**
|
||||
* If you are setting the delegate to nil within the delegate's dealloc method,
|
||||
* you may need to use the synchronous versions below.
|
||||
**/
|
||||
- (void)synchronouslySetDelegate:(nullable id<GCDAsyncSocketDelegate>)delegate;
|
||||
- (void)synchronouslySetDelegateQueue:(nullable dispatch_queue_t)delegateQueue;
|
||||
- (void)synchronouslySetDelegate:(nullable id<GCDAsyncSocketDelegate>)delegate delegateQueue:(nullable dispatch_queue_t)delegateQueue;
|
||||
|
||||
/**
|
||||
* By default, both IPv4 and IPv6 are enabled.
|
||||
|
|
@ -142,21 +131,25 @@ typedef enum GCDAsyncSocketError GCDAsyncSocketError;
|
|||
* If a DNS lookup returns both IPv4 and IPv6 results, the preferred protocol will be chosen.
|
||||
* By default, the preferred protocol is IPv4, but may be configured as desired.
|
||||
**/
|
||||
- (BOOL)isIPv4Enabled;
|
||||
- (void)setIPv4Enabled:(BOOL)flag;
|
||||
|
||||
- (BOOL)isIPv6Enabled;
|
||||
- (void)setIPv6Enabled:(BOOL)flag;
|
||||
@property (atomic, assign, readwrite, getter=isIPv4Enabled) BOOL IPv4Enabled;
|
||||
@property (atomic, assign, readwrite, getter=isIPv6Enabled) BOOL IPv6Enabled;
|
||||
|
||||
- (BOOL)isIPv4PreferredOverIPv6;
|
||||
- (void)setPreferIPv4OverIPv6:(BOOL)flag;
|
||||
@property (atomic, assign, readwrite, getter=isIPv4PreferredOverIPv6) BOOL IPv4PreferredOverIPv6;
|
||||
|
||||
/**
|
||||
* When connecting to both IPv4 and IPv6 using Happy Eyeballs (RFC 6555) https://tools.ietf.org/html/rfc6555
|
||||
* this is the delay between connecting to the preferred protocol and the fallback protocol.
|
||||
*
|
||||
* Defaults to 300ms.
|
||||
**/
|
||||
@property (atomic, assign, readwrite) NSTimeInterval alternateAddressDelay;
|
||||
|
||||
/**
|
||||
* User data allows you to associate arbitrary information with the socket.
|
||||
* This data is not used internally by socket in any way.
|
||||
**/
|
||||
- (id)userData;
|
||||
- (void)setUserData:(id)arbitraryUserData;
|
||||
@property (atomic, strong, readwrite, nullable) id userData;
|
||||
|
||||
#pragma mark Accepting
|
||||
|
||||
|
|
@ -185,7 +178,16 @@ typedef enum GCDAsyncSocketError GCDAsyncSocketError;
|
|||
*
|
||||
* To accept connections on any interface pass nil, or simply use the acceptOnPort:error: method.
|
||||
**/
|
||||
- (BOOL)acceptOnInterface:(NSString *)interface port:(uint16_t)port error:(NSError **)errPtr;
|
||||
- (BOOL)acceptOnInterface:(nullable NSString *)interface port:(uint16_t)port error:(NSError **)errPtr;
|
||||
|
||||
/**
|
||||
* Tells the socket to begin listening and accepting connections on the unix domain at the given url.
|
||||
* When a connection is accepted, a new instance of GCDAsyncSocket will be spawned to handle it,
|
||||
* and the socket:didAcceptNewSocket: delegate method will be invoked.
|
||||
*
|
||||
* The socket will listen on all available interfaces (e.g. wifi, ethernet, etc)
|
||||
**/
|
||||
- (BOOL)acceptOnUrl:(NSURL *)url error:(NSError **)errPtr;
|
||||
|
||||
#pragma mark Connecting
|
||||
|
||||
|
|
@ -241,7 +243,7 @@ typedef enum GCDAsyncSocketError GCDAsyncSocketError;
|
|||
**/
|
||||
- (BOOL)connectToHost:(NSString *)host
|
||||
onPort:(uint16_t)port
|
||||
viaInterface:(NSString *)interface
|
||||
viaInterface:(nullable NSString *)interface
|
||||
withTimeout:(NSTimeInterval)timeout
|
||||
error:(NSError **)errPtr;
|
||||
|
||||
|
|
@ -253,7 +255,7 @@ typedef enum GCDAsyncSocketError GCDAsyncSocketError;
|
|||
* struct sockaddr sa -> NSData *dsa = [NSData dataWithBytes:&remoteAddr length:remoteAddr.sa_len];
|
||||
* struct sockaddr *sa -> NSData *dsa = [NSData dataWithBytes:remoteAddr length:remoteAddr->sa_len];
|
||||
*
|
||||
* This method invokes connectToAdd
|
||||
* This method invokes connectToAddress:remoteAddr viaInterface:nil withTimeout:-1 error:errPtr.
|
||||
**/
|
||||
- (BOOL)connectToAddress:(NSData *)remoteAddr error:(NSError **)errPtr;
|
||||
|
||||
|
|
@ -299,9 +301,19 @@ typedef enum GCDAsyncSocketError GCDAsyncSocketError;
|
|||
* This feature is here for networking professionals using very advanced techniques.
|
||||
**/
|
||||
- (BOOL)connectToAddress:(NSData *)remoteAddr
|
||||
viaInterface:(NSString *)interface
|
||||
viaInterface:(nullable NSString *)interface
|
||||
withTimeout:(NSTimeInterval)timeout
|
||||
error:(NSError **)errPtr;
|
||||
/**
|
||||
* Connects to the unix domain socket at the given url, using the specified timeout.
|
||||
*/
|
||||
- (BOOL)connectToUrl:(NSURL *)url withTimeout:(NSTimeInterval)timeout error:(NSError **)errPtr;
|
||||
|
||||
/**
|
||||
* Iterates over the given NetService's addresses in order, and invokes connectToAddress:error:. Stops at the
|
||||
* first invocation that succeeds and returns YES; otherwise returns NO.
|
||||
*/
|
||||
- (BOOL)connectToNetService:(NSNetService *)netService error:(NSError **)errPtr;
|
||||
|
||||
#pragma mark Disconnecting
|
||||
|
||||
|
|
@ -352,45 +364,49 @@ typedef enum GCDAsyncSocketError GCDAsyncSocketError;
|
|||
* Returns whether the socket is disconnected or connected.
|
||||
*
|
||||
* A disconnected socket may be recycled.
|
||||
* That is, it can used again for connecting or listening.
|
||||
* That is, it can be used again for connecting or listening.
|
||||
*
|
||||
* If a socket is in the process of connecting, it may be neither disconnected nor connected.
|
||||
**/
|
||||
- (BOOL)isDisconnected;
|
||||
- (BOOL)isConnected;
|
||||
@property (atomic, readonly) BOOL isDisconnected;
|
||||
@property (atomic, readonly) BOOL isConnected;
|
||||
|
||||
/**
|
||||
* Returns the local or remote host and port to which this socket is connected, or nil and 0 if not connected.
|
||||
* The host will be an IP address.
|
||||
**/
|
||||
- (NSString *)connectedHost;
|
||||
- (uint16_t)connectedPort;
|
||||
@property (atomic, readonly, nullable) NSString *connectedHost;
|
||||
@property (atomic, readonly) uint16_t connectedPort;
|
||||
@property (atomic, readonly, nullable) NSURL *connectedUrl;
|
||||
|
||||
- (NSString *)localHost;
|
||||
- (uint16_t)localPort;
|
||||
@property (atomic, readonly, nullable) NSString *localHost;
|
||||
@property (atomic, readonly) uint16_t localPort;
|
||||
|
||||
/**
|
||||
* Returns the local or remote address to which this socket is connected,
|
||||
* specified as a sockaddr structure wrapped in a NSData object.
|
||||
*
|
||||
* See also the connectedHost, connectedPort, localHost and localPort methods.
|
||||
* @seealso connectedHost
|
||||
* @seealso connectedPort
|
||||
* @seealso localHost
|
||||
* @seealso localPort
|
||||
**/
|
||||
- (NSData *)connectedAddress;
|
||||
- (NSData *)localAddress;
|
||||
@property (atomic, readonly, nullable) NSData *connectedAddress;
|
||||
@property (atomic, readonly, nullable) NSData *localAddress;
|
||||
|
||||
/**
|
||||
* Returns whether the socket is IPv4 or IPv6.
|
||||
* An accepting socket may be both.
|
||||
**/
|
||||
- (BOOL)isIPv4;
|
||||
- (BOOL)isIPv6;
|
||||
@property (atomic, readonly) BOOL isIPv4;
|
||||
@property (atomic, readonly) BOOL isIPv6;
|
||||
|
||||
/**
|
||||
* Returns whether or not the socket has been secured via SSL/TLS.
|
||||
*
|
||||
* See also the startTLS method.
|
||||
**/
|
||||
- (BOOL)isSecure;
|
||||
@property (atomic, readonly) BOOL isSecure;
|
||||
|
||||
#pragma mark Reading
|
||||
|
||||
|
|
@ -420,7 +436,7 @@ typedef enum GCDAsyncSocketError GCDAsyncSocketError;
|
|||
* The given buffer will automatically be increased in size if needed.
|
||||
*
|
||||
* If the timeout value is negative, the read operation will not use a timeout.
|
||||
* If the buffer if nil, the socket will create a buffer for you.
|
||||
* If the buffer is nil, the socket will create a buffer for you.
|
||||
*
|
||||
* If the bufferOffset is greater than the length of the given buffer,
|
||||
* the method will do nothing, and the delegate will not be called.
|
||||
|
|
@ -431,7 +447,7 @@ typedef enum GCDAsyncSocketError GCDAsyncSocketError;
|
|||
* the method [NSData dataWithBytesNoCopy:length:freeWhenDone:NO].
|
||||
**/
|
||||
- (void)readDataWithTimeout:(NSTimeInterval)timeout
|
||||
buffer:(NSMutableData *)buffer
|
||||
buffer:(nullable NSMutableData *)buffer
|
||||
bufferOffset:(NSUInteger)offset
|
||||
tag:(long)tag;
|
||||
|
||||
|
|
@ -442,7 +458,7 @@ typedef enum GCDAsyncSocketError GCDAsyncSocketError;
|
|||
* A maximum of length bytes will be read.
|
||||
*
|
||||
* If the timeout value is negative, the read operation will not use a timeout.
|
||||
* If the buffer if nil, a buffer will automatically be created for you.
|
||||
* If the buffer is nil, a buffer will automatically be created for you.
|
||||
* If maxLength is zero, no length restriction is enforced.
|
||||
*
|
||||
* If the bufferOffset is greater than the length of the given buffer,
|
||||
|
|
@ -454,7 +470,7 @@ typedef enum GCDAsyncSocketError GCDAsyncSocketError;
|
|||
* the method [NSData dataWithBytesNoCopy:length:freeWhenDone:NO].
|
||||
**/
|
||||
- (void)readDataWithTimeout:(NSTimeInterval)timeout
|
||||
buffer:(NSMutableData *)buffer
|
||||
buffer:(nullable NSMutableData *)buffer
|
||||
bufferOffset:(NSUInteger)offset
|
||||
maxLength:(NSUInteger)length
|
||||
tag:(long)tag;
|
||||
|
|
@ -474,7 +490,7 @@ typedef enum GCDAsyncSocketError GCDAsyncSocketError;
|
|||
* The given buffer will automatically be increased in size if needed.
|
||||
*
|
||||
* If the timeout value is negative, the read operation will not use a timeout.
|
||||
* If the buffer if nil, a buffer will automatically be created for you.
|
||||
* If the buffer is nil, a buffer will automatically be created for you.
|
||||
*
|
||||
* If the length is 0, this method does nothing and the delegate is not called.
|
||||
* If the bufferOffset is greater than the length of the given buffer,
|
||||
|
|
@ -487,7 +503,7 @@ typedef enum GCDAsyncSocketError GCDAsyncSocketError;
|
|||
**/
|
||||
- (void)readDataToLength:(NSUInteger)length
|
||||
withTimeout:(NSTimeInterval)timeout
|
||||
buffer:(NSMutableData *)buffer
|
||||
buffer:(nullable NSMutableData *)buffer
|
||||
bufferOffset:(NSUInteger)offset
|
||||
tag:(long)tag;
|
||||
|
||||
|
|
@ -512,7 +528,7 @@ typedef enum GCDAsyncSocketError GCDAsyncSocketError;
|
|||
* For performance reasons, the socket will retain it, not copy it.
|
||||
* So if it is immutable, don't modify it while the socket is using it.
|
||||
**/
|
||||
- (void)readDataToData:(NSData *)data withTimeout:(NSTimeInterval)timeout tag:(long)tag;
|
||||
- (void)readDataToData:(nullable NSData *)data withTimeout:(NSTimeInterval)timeout tag:(long)tag;
|
||||
|
||||
/**
|
||||
* Reads bytes until (and including) the passed "data" parameter, which acts as a separator.
|
||||
|
|
@ -520,7 +536,7 @@ typedef enum GCDAsyncSocketError GCDAsyncSocketError;
|
|||
* The given buffer will automatically be increased in size if needed.
|
||||
*
|
||||
* If the timeout value is negative, the read operation will not use a timeout.
|
||||
* If the buffer if nil, a buffer will automatically be created for you.
|
||||
* If the buffer is nil, a buffer will automatically be created for you.
|
||||
*
|
||||
* If the bufferOffset is greater than the length of the given buffer,
|
||||
* the method will do nothing (except maybe print a warning), and the delegate will not be called.
|
||||
|
|
@ -545,7 +561,7 @@ typedef enum GCDAsyncSocketError GCDAsyncSocketError;
|
|||
**/
|
||||
- (void)readDataToData:(NSData *)data
|
||||
withTimeout:(NSTimeInterval)timeout
|
||||
buffer:(NSMutableData *)buffer
|
||||
buffer:(nullable NSMutableData *)buffer
|
||||
bufferOffset:(NSUInteger)offset
|
||||
tag:(long)tag;
|
||||
|
||||
|
|
@ -585,7 +601,7 @@ typedef enum GCDAsyncSocketError GCDAsyncSocketError;
|
|||
* The given buffer will automatically be increased in size if needed.
|
||||
*
|
||||
* If the timeout value is negative, the read operation will not use a timeout.
|
||||
* If the buffer if nil, a buffer will automatically be created for you.
|
||||
* If the buffer is nil, a buffer will automatically be created for you.
|
||||
*
|
||||
* If maxLength is zero, no length restriction is enforced.
|
||||
* Otherwise if maxLength bytes are read without completing the read,
|
||||
|
|
@ -617,7 +633,7 @@ typedef enum GCDAsyncSocketError GCDAsyncSocketError;
|
|||
**/
|
||||
- (void)readDataToData:(NSData *)data
|
||||
withTimeout:(NSTimeInterval)timeout
|
||||
buffer:(NSMutableData *)buffer
|
||||
buffer:(nullable NSMutableData *)buffer
|
||||
bufferOffset:(NSUInteger)offset
|
||||
maxLength:(NSUInteger)length
|
||||
tag:(long)tag;
|
||||
|
|
@ -626,7 +642,7 @@ typedef enum GCDAsyncSocketError GCDAsyncSocketError;
|
|||
* Returns progress of the current read, from 0.0 to 1.0, or NaN if no current read (use isnan() to check).
|
||||
* The parameters "tag", "done" and "total" will be filled in if they aren't NULL.
|
||||
**/
|
||||
- (float)progressOfReadReturningTag:(long *)tagPtr bytesDone:(NSUInteger *)donePtr total:(NSUInteger *)totalPtr;
|
||||
- (float)progressOfReadReturningTag:(nullable long *)tagPtr bytesDone:(nullable NSUInteger *)donePtr total:(nullable NSUInteger *)totalPtr;
|
||||
|
||||
#pragma mark Writing
|
||||
|
||||
|
|
@ -647,13 +663,13 @@ typedef enum GCDAsyncSocketError GCDAsyncSocketError;
|
|||
* completes writing the bytes (which is NOT immediately after this method returns, but rather at a later time
|
||||
* when the delegate method notifies you), then you should first copy the bytes, and pass the copy to this method.
|
||||
**/
|
||||
- (void)writeData:(NSData *)data withTimeout:(NSTimeInterval)timeout tag:(long)tag;
|
||||
- (void)writeData:(nullable NSData *)data withTimeout:(NSTimeInterval)timeout tag:(long)tag;
|
||||
|
||||
/**
|
||||
* Returns progress of the current write, from 0.0 to 1.0, or NaN if no current write (use isnan() to check).
|
||||
* The parameters "tag", "done" and "total" will be filled in if they aren't NULL.
|
||||
**/
|
||||
- (float)progressOfWriteReturningTag:(long *)tagPtr bytesDone:(NSUInteger *)donePtr total:(NSUInteger *)totalPtr;
|
||||
- (float)progressOfWriteReturningTag:(nullable long *)tagPtr bytesDone:(nullable NSUInteger *)donePtr total:(nullable NSUInteger *)totalPtr;
|
||||
|
||||
#pragma mark Security
|
||||
|
||||
|
|
@ -664,35 +680,116 @@ typedef enum GCDAsyncSocketError GCDAsyncSocketError;
|
|||
* are finished. This allows one the option of sending a protocol dependent StartTLS message, and queuing
|
||||
* the upgrade to TLS at the same time, without having to wait for the write to finish.
|
||||
* Any reads or writes scheduled after this method is called will occur over the secured connection.
|
||||
*
|
||||
* ==== The available TOP-LEVEL KEYS are:
|
||||
*
|
||||
* The possible keys and values for the TLS settings are well documented.
|
||||
* Standard keys are:
|
||||
*
|
||||
* - kCFStreamSSLLevel
|
||||
* - kCFStreamSSLAllowsExpiredCertificates
|
||||
* - kCFStreamSSLAllowsExpiredRoots
|
||||
* - kCFStreamSSLAllowsAnyRoot
|
||||
* - kCFStreamSSLValidatesCertificateChain
|
||||
* - GCDAsyncSocketManuallyEvaluateTrust
|
||||
* The value must be of type NSNumber, encapsulating a BOOL value.
|
||||
* If you set this to YES, then the underlying SecureTransport system will not evaluate the SecTrustRef of the peer.
|
||||
* Instead it will pause at the moment evaulation would typically occur,
|
||||
* and allow us to handle the security evaluation however we see fit.
|
||||
* So GCDAsyncSocket will invoke the delegate method socket:shouldTrustPeer: passing the SecTrustRef.
|
||||
*
|
||||
* Note that if you set this option, then all other configuration keys are ignored.
|
||||
* Evaluation will be completely up to you during the socket:didReceiveTrust:completionHandler: delegate method.
|
||||
*
|
||||
* For more information on trust evaluation see:
|
||||
* Apple's Technical Note TN2232 - HTTPS Server Trust Evaluation
|
||||
* https://developer.apple.com/library/ios/technotes/tn2232/_index.html
|
||||
*
|
||||
* If unspecified, the default value is NO.
|
||||
*
|
||||
* - GCDAsyncSocketUseCFStreamForTLS (iOS only)
|
||||
* The value must be of type NSNumber, encapsulating a BOOL value.
|
||||
* By default GCDAsyncSocket will use the SecureTransport layer to perform encryption.
|
||||
* This gives us more control over the security protocol (many more configuration options),
|
||||
* plus it allows us to optimize things like sys calls and buffer allocation.
|
||||
*
|
||||
* However, if you absolutely must, you can instruct GCDAsyncSocket to use the old-fashioned encryption
|
||||
* technique by going through the CFStream instead. So instead of using SecureTransport, GCDAsyncSocket
|
||||
* will instead setup a CFRead/CFWriteStream. And then set the kCFStreamPropertySSLSettings property
|
||||
* (via CFReadStreamSetProperty / CFWriteStreamSetProperty) and will pass the given options to this method.
|
||||
*
|
||||
* Thus all the other keys in the given dictionary will be ignored by GCDAsyncSocket,
|
||||
* and will passed directly CFReadStreamSetProperty / CFWriteStreamSetProperty.
|
||||
* For more infomation on these keys, please see the documentation for kCFStreamPropertySSLSettings.
|
||||
*
|
||||
* If unspecified, the default value is NO.
|
||||
*
|
||||
* ==== The available CONFIGURATION KEYS are:
|
||||
*
|
||||
* - kCFStreamSSLPeerName
|
||||
* The value must be of type NSString.
|
||||
* It should match the name in the X.509 certificate given by the remote party.
|
||||
* See Apple's documentation for SSLSetPeerDomainName.
|
||||
*
|
||||
* - kCFStreamSSLCertificates
|
||||
* The value must be of type NSArray.
|
||||
* See Apple's documentation for SSLSetCertificate.
|
||||
*
|
||||
* - kCFStreamSSLIsServer
|
||||
*
|
||||
* If SecureTransport is available on iOS:
|
||||
*
|
||||
* - GCDAsyncSocketSSLCipherSuites
|
||||
* The value must be of type NSNumber, encapsulationg a BOOL value.
|
||||
* See Apple's documentation for SSLCreateContext for iOS.
|
||||
* This is optional for iOS. If not supplied, a NO value is the default.
|
||||
* This is not needed for Mac OS X, and the value is ignored.
|
||||
*
|
||||
* - GCDAsyncSocketSSLPeerID
|
||||
* The value must be of type NSData.
|
||||
* You must set this value if you want to use TLS session resumption.
|
||||
* See Apple's documentation for SSLSetPeerID.
|
||||
*
|
||||
* - GCDAsyncSocketSSLProtocolVersionMin
|
||||
* - GCDAsyncSocketSSLProtocolVersionMax
|
||||
* The value(s) must be of type NSNumber, encapsulting a SSLProtocol value.
|
||||
* See Apple's documentation for SSLSetProtocolVersionMin & SSLSetProtocolVersionMax.
|
||||
* See also the SSLProtocol typedef.
|
||||
*
|
||||
* If SecureTransport is available on Mac OS X:
|
||||
* - GCDAsyncSocketSSLSessionOptionFalseStart
|
||||
* The value must be of type NSNumber, encapsulating a BOOL value.
|
||||
* See Apple's documentation for kSSLSessionOptionFalseStart.
|
||||
*
|
||||
* - GCDAsyncSocketSSLSessionOptionSendOneByteRecord
|
||||
* The value must be of type NSNumber, encapsulating a BOOL value.
|
||||
* See Apple's documentation for kSSLSessionOptionSendOneByteRecord.
|
||||
*
|
||||
* - GCDAsyncSocketSSLCipherSuites
|
||||
* - GCDAsyncSocketSSLDiffieHellmanParameters;
|
||||
* The values must be of type NSArray.
|
||||
* Each item within the array must be a NSNumber, encapsulating an SSLCipherSuite.
|
||||
* See Apple's documentation for SSLSetEnabledCiphers.
|
||||
* See also the SSLCipherSuite typedef.
|
||||
*
|
||||
* - GCDAsyncSocketSSLDiffieHellmanParameters (Mac OS X only)
|
||||
* The value must be of type NSData.
|
||||
* See Apple's documentation for SSLSetDiffieHellmanParams.
|
||||
*
|
||||
* ==== The following UNAVAILABLE KEYS are: (with throw an exception)
|
||||
*
|
||||
* Please refer to Apple's documentation for associated values, as well as other possible keys.
|
||||
* - kCFStreamSSLAllowsAnyRoot (UNAVAILABLE)
|
||||
* You MUST use manual trust evaluation instead (see GCDAsyncSocketManuallyEvaluateTrust).
|
||||
* Corresponding deprecated method: SSLSetAllowsAnyRoot
|
||||
*
|
||||
* - kCFStreamSSLAllowsExpiredRoots (UNAVAILABLE)
|
||||
* You MUST use manual trust evaluation instead (see GCDAsyncSocketManuallyEvaluateTrust).
|
||||
* Corresponding deprecated method: SSLSetAllowsExpiredRoots
|
||||
*
|
||||
* - kCFStreamSSLAllowsExpiredCertificates (UNAVAILABLE)
|
||||
* You MUST use manual trust evaluation instead (see GCDAsyncSocketManuallyEvaluateTrust).
|
||||
* Corresponding deprecated method: SSLSetAllowsExpiredCerts
|
||||
*
|
||||
* - kCFStreamSSLValidatesCertificateChain (UNAVAILABLE)
|
||||
* You MUST use manual trust evaluation instead (see GCDAsyncSocketManuallyEvaluateTrust).
|
||||
* Corresponding deprecated method: SSLSetEnableCertVerify
|
||||
*
|
||||
* - kCFStreamSSLLevel (UNAVAILABLE)
|
||||
* You MUST use GCDAsyncSocketSSLProtocolVersionMin & GCDAsyncSocketSSLProtocolVersionMin instead.
|
||||
* Corresponding deprecated method: SSLSetProtocolVersionEnabled
|
||||
*
|
||||
*
|
||||
* Please refer to Apple's documentation for corresponding SSLFunctions.
|
||||
*
|
||||
* If you pass in nil or an empty dictionary, the default settings will be used.
|
||||
*
|
||||
* IMPORTANT SECURITY NOTE:
|
||||
* The default settings will check to make sure the remote party's certificate is signed by a
|
||||
* trusted 3rd party certificate agency (e.g. verisign) and that the certificate is not expired.
|
||||
* However it will not verify the name on the certificate unless you
|
||||
|
|
@ -704,12 +801,10 @@ typedef enum GCDAsyncSocketError GCDAsyncSocketError;
|
|||
* the default settings will not detect any problems since the certificate is valid.
|
||||
* To properly secure your connection in this particular scenario you
|
||||
* should set the kCFStreamSSLPeerName property to "MySecureServer.com".
|
||||
* If you do not know the peer name of the remote host in advance (for example, you're not sure
|
||||
* if it will be "domain.com" or "www.domain.com"), then you can use the default settings to validate the
|
||||
* certificate, and then use the X509Certificate class to verify the issuer after the socket has been secured.
|
||||
* The X509Certificate class is part of the CocoaAsyncSocket open source project.
|
||||
**/
|
||||
- (void)startTLS:(NSDictionary *)tlsSettings;
|
||||
*
|
||||
* You can also perform additional validation in socketDidSecure.
|
||||
**/
|
||||
- (void)startTLS:(nullable NSDictionary <NSString*,NSObject*>*)tlsSettings;
|
||||
|
||||
#pragma mark Advanced
|
||||
|
||||
|
|
@ -743,8 +838,7 @@ typedef enum GCDAsyncSocketError GCDAsyncSocketError;
|
|||
*
|
||||
* The default value is YES.
|
||||
**/
|
||||
- (BOOL)autoDisconnectOnClosedReadStream;
|
||||
- (void)setAutoDisconnectOnClosedReadStream:(BOOL)flag;
|
||||
@property (atomic, assign, readwrite) BOOL autoDisconnectOnClosedReadStream;
|
||||
|
||||
/**
|
||||
* GCDAsyncSocket maintains thread safety by using an internal serial dispatch_queue.
|
||||
|
|
@ -884,8 +978,8 @@ typedef enum GCDAsyncSocketError GCDAsyncSocketError;
|
|||
*
|
||||
* See also: (BOOL)enableBackgroundingOnSocket
|
||||
**/
|
||||
- (CFReadStreamRef)readStream;
|
||||
- (CFWriteStreamRef)writeStream;
|
||||
- (nullable CFReadStreamRef)readStream;
|
||||
- (nullable CFWriteStreamRef)writeStream;
|
||||
|
||||
/**
|
||||
* This method is only available from within the context of a performBlock: invocation.
|
||||
|
|
@ -916,26 +1010,42 @@ typedef enum GCDAsyncSocketError GCDAsyncSocketError;
|
|||
|
||||
#endif
|
||||
|
||||
#if SECURE_TRANSPORT_MAYBE_AVAILABLE
|
||||
|
||||
/**
|
||||
* This method is only available from within the context of a performBlock: invocation.
|
||||
* See the documentation for the performBlock: method above.
|
||||
*
|
||||
* Provides access to the socket's SSLContext, if SSL/TLS has been started on the socket.
|
||||
**/
|
||||
- (SSLContextRef)sslContext;
|
||||
|
||||
#endif
|
||||
- (nullable SSLContextRef)sslContext;
|
||||
|
||||
#pragma mark Utilities
|
||||
|
||||
/**
|
||||
* The address lookup utility used by the class.
|
||||
* This method is synchronous, so it's recommended you use it on a background thread/queue.
|
||||
*
|
||||
* The special strings "localhost" and "loopback" return the loopback address for IPv4 and IPv6.
|
||||
*
|
||||
* @returns
|
||||
* A mutable array with all IPv4 and IPv6 addresses returned by getaddrinfo.
|
||||
* The addresses are specifically for TCP connections.
|
||||
* You can filter the addresses, if needed, using the other utility methods provided by the class.
|
||||
**/
|
||||
+ (nullable NSMutableArray *)lookupHost:(NSString *)host port:(uint16_t)port error:(NSError **)errPtr;
|
||||
|
||||
/**
|
||||
* Extracting host and port information from raw address data.
|
||||
**/
|
||||
+ (NSString *)hostFromAddress:(NSData *)address;
|
||||
|
||||
+ (nullable NSString *)hostFromAddress:(NSData *)address;
|
||||
+ (uint16_t)portFromAddress:(NSData *)address;
|
||||
+ (BOOL)getHost:(NSString **)hostPtr port:(uint16_t *)portPtr fromAddress:(NSData *)address;
|
||||
|
||||
+ (BOOL)isIPv4Address:(NSData *)address;
|
||||
+ (BOOL)isIPv6Address:(NSData *)address;
|
||||
|
||||
+ (BOOL)getHost:( NSString * __nullable * __nullable)hostPtr port:(nullable uint16_t *)portPtr fromAddress:(NSData *)address;
|
||||
|
||||
+ (BOOL)getHost:(NSString * __nullable * __nullable)hostPtr port:(nullable uint16_t *)portPtr family:(nullable sa_family_t *)afPtr fromAddress:(NSData *)address;
|
||||
|
||||
/**
|
||||
* A few common line separators, for use with the readDataToData:... methods.
|
||||
|
|
@ -951,7 +1061,7 @@ typedef enum GCDAsyncSocketError GCDAsyncSocketError;
|
|||
#pragma mark -
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
@protocol GCDAsyncSocketDelegate
|
||||
@protocol GCDAsyncSocketDelegate <NSObject>
|
||||
@optional
|
||||
|
||||
/**
|
||||
|
|
@ -972,7 +1082,7 @@ typedef enum GCDAsyncSocketError GCDAsyncSocketError;
|
|||
* dispatch_retain(myExistingQueue);
|
||||
* return myExistingQueue;
|
||||
**/
|
||||
- (dispatch_queue_t)newSocketQueueForConnectionFromAddress:(NSData *)address onSocket:(GCDAsyncSocket *)sock;
|
||||
- (nullable dispatch_queue_t)newSocketQueueForConnectionFromAddress:(NSData *)address onSocket:(GCDAsyncSocket *)sock;
|
||||
|
||||
/**
|
||||
* Called when a socket accepts a connection.
|
||||
|
|
@ -992,6 +1102,12 @@ typedef enum GCDAsyncSocketError GCDAsyncSocketError;
|
|||
**/
|
||||
- (void)socket:(GCDAsyncSocket *)sock didConnectToHost:(NSString *)host port:(uint16_t)port;
|
||||
|
||||
/**
|
||||
* Called when a socket connects and is ready for reading and writing.
|
||||
* The host parameter will be an IP address, not a DNS name.
|
||||
**/
|
||||
- (void)socket:(GCDAsyncSocket *)sock didConnectToUrl:(NSURL *)url;
|
||||
|
||||
/**
|
||||
* Called when a socket has completed reading the requested data into memory.
|
||||
* Not called if there is an error.
|
||||
|
|
@ -1001,7 +1117,7 @@ typedef enum GCDAsyncSocketError GCDAsyncSocketError;
|
|||
/**
|
||||
* Called when a socket has read in data, but has not yet completed the read.
|
||||
* This would occur if using readToData: or readToLength: methods.
|
||||
* It may be used to for things such as updating progress bars.
|
||||
* It may be used for things such as updating progress bars.
|
||||
**/
|
||||
- (void)socket:(GCDAsyncSocket *)sock didReadPartialDataOfLength:(NSUInteger)partialLength tag:(long)tag;
|
||||
|
||||
|
|
@ -1012,7 +1128,7 @@ typedef enum GCDAsyncSocketError GCDAsyncSocketError;
|
|||
|
||||
/**
|
||||
* Called when a socket has written some data, but has not yet completed the entire write.
|
||||
* It may be used to for things such as updating progress bars.
|
||||
* It may be used for things such as updating progress bars.
|
||||
**/
|
||||
- (void)socket:(GCDAsyncSocket *)sock didWritePartialDataOfLength:(NSUInteger)partialLength tag:(long)tag;
|
||||
|
||||
|
|
@ -1058,9 +1174,24 @@ typedef enum GCDAsyncSocketError GCDAsyncSocketError;
|
|||
* Called when a socket disconnects with or without error.
|
||||
*
|
||||
* If you call the disconnect method, and the socket wasn't already disconnected,
|
||||
* this delegate method will be called before the disconnect method returns.
|
||||
* then an invocation of this delegate method will be enqueued on the delegateQueue
|
||||
* before the disconnect method returns.
|
||||
*
|
||||
* Note: If the GCDAsyncSocket instance is deallocated while it is still connected,
|
||||
* and the delegate is not also deallocated, then this method will be invoked,
|
||||
* but the sock parameter will be nil. (It must necessarily be nil since it is no longer available.)
|
||||
* This is a generally rare, but is possible if one writes code like this:
|
||||
*
|
||||
* asyncSocket = nil; // I'm implicitly disconnecting the socket
|
||||
*
|
||||
* In this case it may preferrable to nil the delegate beforehand, like this:
|
||||
*
|
||||
* asyncSocket.delegate = nil; // Don't invoke my delegate method
|
||||
* asyncSocket = nil; // I'm implicitly disconnecting the socket
|
||||
*
|
||||
* Of course, this depends on how your state machine is configured.
|
||||
**/
|
||||
- (void)socketDidDisconnect:(GCDAsyncSocket *)sock withError:(NSError *)err;
|
||||
- (void)socketDidDisconnect:(GCDAsyncSocket *)sock withError:(nullable NSError *)err;
|
||||
|
||||
/**
|
||||
* Called after the socket has successfully completed SSL/TLS negotiation.
|
||||
|
|
@ -1071,4 +1202,25 @@ typedef enum GCDAsyncSocketError GCDAsyncSocketError;
|
|||
**/
|
||||
- (void)socketDidSecure:(GCDAsyncSocket *)sock;
|
||||
|
||||
/**
|
||||
* Allows a socket delegate to hook into the TLS handshake and manually validate the peer it's connecting to.
|
||||
*
|
||||
* This is only called if startTLS is invoked with options that include:
|
||||
* - GCDAsyncSocketManuallyEvaluateTrust == YES
|
||||
*
|
||||
* Typically the delegate will use SecTrustEvaluate (and related functions) to properly validate the peer.
|
||||
*
|
||||
* Note from Apple's documentation:
|
||||
* Because [SecTrustEvaluate] might look on the network for certificates in the certificate chain,
|
||||
* [it] might block while attempting network access. You should never call it from your main thread;
|
||||
* call it only from within a function running on a dispatch queue or on a separate thread.
|
||||
*
|
||||
* Thus this method uses a completionHandler block rather than a normal return value.
|
||||
* The completionHandler block is thread-safe, and may be invoked from a background queue/thread.
|
||||
* It is safe to invoke the completionHandler block even if the socket has been closed.
|
||||
**/
|
||||
- (void)socket:(GCDAsyncSocket *)sock didReceiveTrust:(SecTrustRef)trust
|
||||
completionHandler:(void (^)(BOOL shouldTrustPeer))completionHandler;
|
||||
|
||||
@end
|
||||
NS_ASSUME_NONNULL_END
|
||||
|
|
|
|||
File diff suppressed because it is too large
Load Diff
|
|
@ -10,16 +10,17 @@
|
|||
|
||||
#import <Foundation/Foundation.h>
|
||||
#import <dispatch/dispatch.h>
|
||||
#import <TargetConditionals.h>
|
||||
#import <Availability.h>
|
||||
|
||||
|
||||
NS_ASSUME_NONNULL_BEGIN
|
||||
extern NSString *const GCDAsyncUdpSocketException;
|
||||
extern NSString *const GCDAsyncUdpSocketErrorDomain;
|
||||
|
||||
extern NSString *const GCDAsyncUdpSocketQueueName;
|
||||
extern NSString *const GCDAsyncUdpSocketThreadName;
|
||||
|
||||
enum GCDAsyncUdpSocketError
|
||||
{
|
||||
typedef NS_ERROR_ENUM(GCDAsyncUdpSocketErrorDomain, GCDAsyncUdpSocketError) {
|
||||
GCDAsyncUdpSocketNoError = 0, // Never used
|
||||
GCDAsyncUdpSocketBadConfigError, // Invalid configuration
|
||||
GCDAsyncUdpSocketBadParamError, // Invalid parameter was passed
|
||||
|
|
@ -27,7 +28,59 @@ enum GCDAsyncUdpSocketError
|
|||
GCDAsyncUdpSocketClosedError, // The socket was closed
|
||||
GCDAsyncUdpSocketOtherError, // Description provided in userInfo
|
||||
};
|
||||
typedef enum GCDAsyncUdpSocketError GCDAsyncUdpSocketError;
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
#pragma mark -
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
@class GCDAsyncUdpSocket;
|
||||
|
||||
@protocol GCDAsyncUdpSocketDelegate <NSObject>
|
||||
@optional
|
||||
|
||||
/**
|
||||
* By design, UDP is a connectionless protocol, and connecting is not needed.
|
||||
* However, you may optionally choose to connect to a particular host for reasons
|
||||
* outlined in the documentation for the various connect methods listed above.
|
||||
*
|
||||
* This method is called if one of the connect methods are invoked, and the connection is successful.
|
||||
**/
|
||||
- (void)udpSocket:(GCDAsyncUdpSocket *)sock didConnectToAddress:(NSData *)address;
|
||||
|
||||
/**
|
||||
* By design, UDP is a connectionless protocol, and connecting is not needed.
|
||||
* However, you may optionally choose to connect to a particular host for reasons
|
||||
* outlined in the documentation for the various connect methods listed above.
|
||||
*
|
||||
* This method is called if one of the connect methods are invoked, and the connection fails.
|
||||
* This may happen, for example, if a domain name is given for the host and the domain name is unable to be resolved.
|
||||
**/
|
||||
- (void)udpSocket:(GCDAsyncUdpSocket *)sock didNotConnect:(NSError * _Nullable)error;
|
||||
|
||||
/**
|
||||
* Called when the datagram with the given tag has been sent.
|
||||
**/
|
||||
- (void)udpSocket:(GCDAsyncUdpSocket *)sock didSendDataWithTag:(long)tag;
|
||||
|
||||
/**
|
||||
* Called if an error occurs while trying to send a datagram.
|
||||
* This could be due to a timeout, or something more serious such as the data being too large to fit in a sigle packet.
|
||||
**/
|
||||
- (void)udpSocket:(GCDAsyncUdpSocket *)sock didNotSendDataWithTag:(long)tag dueToError:(NSError * _Nullable)error;
|
||||
|
||||
/**
|
||||
* Called when the socket has received the requested datagram.
|
||||
**/
|
||||
- (void)udpSocket:(GCDAsyncUdpSocket *)sock didReceiveData:(NSData *)data
|
||||
fromAddress:(NSData *)address
|
||||
withFilterContext:(nullable id)filterContext;
|
||||
|
||||
/**
|
||||
* Called when the socket is closed.
|
||||
**/
|
||||
- (void)udpSocketDidClose:(GCDAsyncUdpSocket *)sock withError:(NSError * _Nullable)error;
|
||||
|
||||
@end
|
||||
|
||||
/**
|
||||
* You may optionally set a receive filter for the socket.
|
||||
|
|
@ -77,7 +130,7 @@ typedef enum GCDAsyncUdpSocketError GCDAsyncUdpSocketError;
|
|||
* [udpSocket setReceiveFilter:filter withQueue:myParsingQueue];
|
||||
*
|
||||
**/
|
||||
typedef BOOL (^GCDAsyncUdpSocketReceiveFilterBlock)(NSData *data, NSData *address, id *context);
|
||||
typedef BOOL (^GCDAsyncUdpSocketReceiveFilterBlock)(NSData *data, NSData *address, id __nullable * __nonnull context);
|
||||
|
||||
/**
|
||||
* You may optionally set a send filter for the socket.
|
||||
|
|
@ -125,24 +178,24 @@ typedef BOOL (^GCDAsyncUdpSocketSendFilterBlock)(NSData *data, NSData *address,
|
|||
*
|
||||
* The delegate queue and socket queue can optionally be the same.
|
||||
**/
|
||||
- (id)init;
|
||||
- (id)initWithSocketQueue:(dispatch_queue_t)sq;
|
||||
- (id)initWithDelegate:(id)aDelegate delegateQueue:(dispatch_queue_t)dq;
|
||||
- (id)initWithDelegate:(id)aDelegate delegateQueue:(dispatch_queue_t)dq socketQueue:(dispatch_queue_t)sq;
|
||||
- (instancetype)init;
|
||||
- (instancetype)initWithSocketQueue:(nullable dispatch_queue_t)sq;
|
||||
- (instancetype)initWithDelegate:(nullable id<GCDAsyncUdpSocketDelegate>)aDelegate delegateQueue:(nullable dispatch_queue_t)dq;
|
||||
- (instancetype)initWithDelegate:(nullable id<GCDAsyncUdpSocketDelegate>)aDelegate delegateQueue:(nullable dispatch_queue_t)dq socketQueue:(nullable dispatch_queue_t)sq NS_DESIGNATED_INITIALIZER;
|
||||
|
||||
#pragma mark Configuration
|
||||
|
||||
- (id)delegate;
|
||||
- (void)setDelegate:(id)delegate;
|
||||
- (void)synchronouslySetDelegate:(id)delegate;
|
||||
- (nullable id<GCDAsyncUdpSocketDelegate>)delegate;
|
||||
- (void)setDelegate:(nullable id<GCDAsyncUdpSocketDelegate>)delegate;
|
||||
- (void)synchronouslySetDelegate:(nullable id<GCDAsyncUdpSocketDelegate>)delegate;
|
||||
|
||||
- (dispatch_queue_t)delegateQueue;
|
||||
- (void)setDelegateQueue:(dispatch_queue_t)delegateQueue;
|
||||
- (void)synchronouslySetDelegateQueue:(dispatch_queue_t)delegateQueue;
|
||||
- (nullable dispatch_queue_t)delegateQueue;
|
||||
- (void)setDelegateQueue:(nullable dispatch_queue_t)delegateQueue;
|
||||
- (void)synchronouslySetDelegateQueue:(nullable dispatch_queue_t)delegateQueue;
|
||||
|
||||
- (void)getDelegate:(id *)delegatePtr delegateQueue:(dispatch_queue_t *)delegateQueuePtr;
|
||||
- (void)setDelegate:(id)delegate delegateQueue:(dispatch_queue_t)delegateQueue;
|
||||
- (void)synchronouslySetDelegate:(id)delegate delegateQueue:(dispatch_queue_t)delegateQueue;
|
||||
- (void)getDelegate:(id<GCDAsyncUdpSocketDelegate> __nullable * __nullable)delegatePtr delegateQueue:(dispatch_queue_t __nullable * __nullable)delegateQueuePtr;
|
||||
- (void)setDelegate:(nullable id<GCDAsyncUdpSocketDelegate>)delegate delegateQueue:(nullable dispatch_queue_t)delegateQueue;
|
||||
- (void)synchronouslySetDelegate:(nullable id<GCDAsyncUdpSocketDelegate>)delegate delegateQueue:(nullable dispatch_queue_t)delegateQueue;
|
||||
|
||||
/**
|
||||
* By default, both IPv4 and IPv6 are enabled.
|
||||
|
|
@ -178,7 +231,7 @@ typedef BOOL (^GCDAsyncUdpSocketSendFilterBlock)(NSData *data, NSData *address,
|
|||
|
||||
/**
|
||||
* Gets/Sets the maximum size of the buffer that will be allocated for receive operations.
|
||||
* The default maximum size is 9216 bytes.
|
||||
* The default maximum size is 65535 bytes.
|
||||
*
|
||||
* The theoretical maximum size of any IPv4 UDP packet is UINT16_MAX = 65535.
|
||||
* The theoretical maximum size of any IPv6 UDP packet is UINT32_MAX = 4294967295.
|
||||
|
|
@ -198,12 +251,27 @@ typedef BOOL (^GCDAsyncUdpSocketSendFilterBlock)(NSData *data, NSData *address,
|
|||
- (uint32_t)maxReceiveIPv6BufferSize;
|
||||
- (void)setMaxReceiveIPv6BufferSize:(uint32_t)max;
|
||||
|
||||
/**
|
||||
* Gets/Sets the maximum size of the buffer that will be allocated for send operations.
|
||||
* The default maximum size is 65535 bytes.
|
||||
*
|
||||
* Given that a typical link MTU is 1500 bytes, a large UDP datagram will have to be
|
||||
* fragmented, and that’s both expensive and risky (if one fragment goes missing, the
|
||||
* entire datagram is lost). You are much better off sending a large number of smaller
|
||||
* UDP datagrams, preferably using a path MTU algorithm to avoid fragmentation.
|
||||
*
|
||||
* You must set it before the sockt is created otherwise it won't work.
|
||||
*
|
||||
**/
|
||||
- (uint16_t)maxSendBufferSize;
|
||||
- (void)setMaxSendBufferSize:(uint16_t)max;
|
||||
|
||||
/**
|
||||
* User data allows you to associate arbitrary information with the socket.
|
||||
* This data is not used internally in any way.
|
||||
**/
|
||||
- (id)userData;
|
||||
- (void)setUserData:(id)arbitraryUserData;
|
||||
- (nullable id)userData;
|
||||
- (void)setUserData:(nullable id)arbitraryUserData;
|
||||
|
||||
#pragma mark Diagnostics
|
||||
|
||||
|
|
@ -216,16 +284,16 @@ typedef BOOL (^GCDAsyncUdpSocketSendFilterBlock)(NSData *data, NSData *address,
|
|||
* Note: Address info may not be available until after the socket has been binded, connected
|
||||
* or until after data has been sent.
|
||||
**/
|
||||
- (NSData *)localAddress;
|
||||
- (NSString *)localHost;
|
||||
- (nullable NSData *)localAddress;
|
||||
- (nullable NSString *)localHost;
|
||||
- (uint16_t)localPort;
|
||||
|
||||
- (NSData *)localAddress_IPv4;
|
||||
- (NSString *)localHost_IPv4;
|
||||
- (nullable NSData *)localAddress_IPv4;
|
||||
- (nullable NSString *)localHost_IPv4;
|
||||
- (uint16_t)localPort_IPv4;
|
||||
|
||||
- (NSData *)localAddress_IPv6;
|
||||
- (NSString *)localHost_IPv6;
|
||||
- (nullable NSData *)localAddress_IPv6;
|
||||
- (nullable NSString *)localHost_IPv6;
|
||||
- (uint16_t)localPort_IPv6;
|
||||
|
||||
/**
|
||||
|
|
@ -238,8 +306,8 @@ typedef BOOL (^GCDAsyncUdpSocketSendFilterBlock)(NSData *data, NSData *address,
|
|||
* will not be available unless the socket is explicitly connected to a remote host/port.
|
||||
* If the socket is not connected, these methods will return nil / 0.
|
||||
**/
|
||||
- (NSData *)connectedAddress;
|
||||
- (NSString *)connectedHost;
|
||||
- (nullable NSData *)connectedAddress;
|
||||
- (nullable NSString *)connectedHost;
|
||||
- (uint16_t)connectedPort;
|
||||
|
||||
/**
|
||||
|
|
@ -318,7 +386,7 @@ typedef BOOL (^GCDAsyncUdpSocketSendFilterBlock)(NSData *data, NSData *address,
|
|||
* On success, returns YES.
|
||||
* Otherwise returns NO, and sets errPtr. If you don't care about the error, you can pass NULL for errPtr.
|
||||
**/
|
||||
- (BOOL)bindToPort:(uint16_t)port interface:(NSString *)interface error:(NSError **)errPtr;
|
||||
- (BOOL)bindToPort:(uint16_t)port interface:(nullable NSString *)interface error:(NSError **)errPtr;
|
||||
|
||||
/**
|
||||
* Binds the UDP socket to the given address, specified as a sockaddr structure wrapped in a NSData object.
|
||||
|
|
@ -417,10 +485,33 @@ typedef BOOL (^GCDAsyncUdpSocketSendFilterBlock)(NSData *data, NSData *address,
|
|||
* On success, returns YES.
|
||||
* Otherwise returns NO, and sets errPtr. If you don't care about the error, you can pass nil for errPtr.
|
||||
**/
|
||||
- (BOOL)joinMulticastGroup:(NSString *)group onInterface:(NSString *)interface error:(NSError **)errPtr;
|
||||
- (BOOL)joinMulticastGroup:(NSString *)group onInterface:(nullable NSString *)interface error:(NSError **)errPtr;
|
||||
|
||||
- (BOOL)leaveMulticastGroup:(NSString *)group error:(NSError **)errPtr;
|
||||
- (BOOL)leaveMulticastGroup:(NSString *)group onInterface:(NSString *)interface error:(NSError **)errPtr;
|
||||
- (BOOL)leaveMulticastGroup:(NSString *)group onInterface:(nullable NSString *)interface error:(NSError **)errPtr;
|
||||
|
||||
/**
|
||||
* Send multicast on a specified interface.
|
||||
* For IPv4, interface should be the the IP address of the interface (eg @"192.168.10.1").
|
||||
* For IPv6, interface should be the a network interface name (eg @"en0").
|
||||
*
|
||||
* On success, returns YES.
|
||||
* Otherwise returns NO, and sets errPtr. If you don't care about the error, you can pass nil for errPtr.
|
||||
**/
|
||||
|
||||
- (BOOL)sendIPv4MulticastOnInterface:(NSString*)interface error:(NSError **)errPtr;
|
||||
- (BOOL)sendIPv6MulticastOnInterface:(NSString*)interface error:(NSError **)errPtr;
|
||||
|
||||
#pragma mark Reuse Port
|
||||
|
||||
/**
|
||||
* By default, only one socket can be bound to a given IP address + port at a time.
|
||||
* To enable multiple processes to simultaneously bind to the same address+port,
|
||||
* you need to enable this functionality in the socket. All processes that wish to
|
||||
* use the address+port simultaneously must all enable reuse port on the socket
|
||||
* bound to that port.
|
||||
**/
|
||||
- (BOOL)enableReusePort:(BOOL)flag error:(NSError **)errPtr;
|
||||
|
||||
#pragma mark Broadcast
|
||||
|
||||
|
|
@ -545,7 +636,7 @@ typedef BOOL (^GCDAsyncUdpSocketSendFilterBlock)(NSData *data, NSData *address,
|
|||
* If data is nil or zero-length, this method does nothing.
|
||||
* If passing NSMutableData, please read the thread-safety notice below.
|
||||
*
|
||||
* @param address
|
||||
* @param remoteAddr
|
||||
* The address to send the data to (specified as a sockaddr structure wrapped in a NSData object).
|
||||
*
|
||||
* @param timeout
|
||||
|
|
@ -596,7 +687,7 @@ typedef BOOL (^GCDAsyncUdpSocketSendFilterBlock)(NSData *data, NSData *address,
|
|||
* Note: This method invokes setSendFilter:withQueue:isAsynchronous: (documented below),
|
||||
* passing YES for the isAsynchronous parameter.
|
||||
**/
|
||||
- (void)setSendFilter:(GCDAsyncUdpSocketSendFilterBlock)filterBlock withQueue:(dispatch_queue_t)filterQueue;
|
||||
- (void)setSendFilter:(nullable GCDAsyncUdpSocketSendFilterBlock)filterBlock withQueue:(nullable dispatch_queue_t)filterQueue;
|
||||
|
||||
/**
|
||||
* The receive filter can be run via dispatch_async or dispatch_sync.
|
||||
|
|
@ -611,8 +702,8 @@ typedef BOOL (^GCDAsyncUdpSocketSendFilterBlock)(NSData *data, NSData *address,
|
|||
* then you cannot perform any tasks which may invoke dispatch_sync on the socket queue.
|
||||
* For example, you can't query properties on the socket.
|
||||
**/
|
||||
- (void)setSendFilter:(GCDAsyncUdpSocketSendFilterBlock)filterBlock
|
||||
withQueue:(dispatch_queue_t)filterQueue
|
||||
- (void)setSendFilter:(nullable GCDAsyncUdpSocketSendFilterBlock)filterBlock
|
||||
withQueue:(nullable dispatch_queue_t)filterQueue
|
||||
isAsynchronous:(BOOL)isAsynchronous;
|
||||
|
||||
#pragma mark Receiving
|
||||
|
|
@ -728,7 +819,7 @@ typedef BOOL (^GCDAsyncUdpSocketSendFilterBlock)(NSData *data, NSData *address,
|
|||
* Note: This method invokes setReceiveFilter:withQueue:isAsynchronous: (documented below),
|
||||
* passing YES for the isAsynchronous parameter.
|
||||
**/
|
||||
- (void)setReceiveFilter:(GCDAsyncUdpSocketReceiveFilterBlock)filterBlock withQueue:(dispatch_queue_t)filterQueue;
|
||||
- (void)setReceiveFilter:(nullable GCDAsyncUdpSocketReceiveFilterBlock)filterBlock withQueue:(nullable dispatch_queue_t)filterQueue;
|
||||
|
||||
/**
|
||||
* The receive filter can be run via dispatch_async or dispatch_sync.
|
||||
|
|
@ -743,8 +834,8 @@ typedef BOOL (^GCDAsyncUdpSocketSendFilterBlock)(NSData *data, NSData *address,
|
|||
* then you cannot perform any tasks which may invoke dispatch_sync on the socket queue.
|
||||
* For example, you can't query properties on the socket.
|
||||
**/
|
||||
- (void)setReceiveFilter:(GCDAsyncUdpSocketReceiveFilterBlock)filterBlock
|
||||
withQueue:(dispatch_queue_t)filterQueue
|
||||
- (void)setReceiveFilter:(nullable GCDAsyncUdpSocketReceiveFilterBlock)filterBlock
|
||||
withQueue:(nullable dispatch_queue_t)filterQueue
|
||||
isAsynchronous:(BOOL)isAsynchronous;
|
||||
|
||||
#pragma mark Closing
|
||||
|
|
@ -896,8 +987,8 @@ typedef BOOL (^GCDAsyncUdpSocketSendFilterBlock)(NSData *data, NSData *address,
|
|||
* However, if you need one for any reason,
|
||||
* these methods are a convenient way to get access to a safe instance of one.
|
||||
**/
|
||||
- (CFReadStreamRef)readStream;
|
||||
- (CFWriteStreamRef)writeStream;
|
||||
- (nullable CFReadStreamRef)readStream;
|
||||
- (nullable CFWriteStreamRef)writeStream;
|
||||
|
||||
/**
|
||||
* This method is only available from within the context of a performBlock: invocation.
|
||||
|
|
@ -930,66 +1021,16 @@ typedef BOOL (^GCDAsyncUdpSocketSendFilterBlock)(NSData *data, NSData *address,
|
|||
* Extracting host/port/family information from raw address data.
|
||||
**/
|
||||
|
||||
+ (NSString *)hostFromAddress:(NSData *)address;
|
||||
+ (nullable NSString *)hostFromAddress:(NSData *)address;
|
||||
+ (uint16_t)portFromAddress:(NSData *)address;
|
||||
+ (int)familyFromAddress:(NSData *)address;
|
||||
|
||||
+ (BOOL)isIPv4Address:(NSData *)address;
|
||||
+ (BOOL)isIPv6Address:(NSData *)address;
|
||||
|
||||
+ (BOOL)getHost:(NSString **)hostPtr port:(uint16_t *)portPtr fromAddress:(NSData *)address;
|
||||
+ (BOOL)getHost:(NSString **)hostPtr port:(uint16_t *)portPtr family:(int *)afPtr fromAddress:(NSData *)address;
|
||||
|
||||
@end
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
#pragma mark -
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
@protocol GCDAsyncUdpSocketDelegate
|
||||
@optional
|
||||
|
||||
/**
|
||||
* By design, UDP is a connectionless protocol, and connecting is not needed.
|
||||
* However, you may optionally choose to connect to a particular host for reasons
|
||||
* outlined in the documentation for the various connect methods listed above.
|
||||
*
|
||||
* This method is called if one of the connect methods are invoked, and the connection is successful.
|
||||
**/
|
||||
- (void)udpSocket:(GCDAsyncUdpSocket *)sock didConnectToAddress:(NSData *)address;
|
||||
|
||||
/**
|
||||
* By design, UDP is a connectionless protocol, and connecting is not needed.
|
||||
* However, you may optionally choose to connect to a particular host for reasons
|
||||
* outlined in the documentation for the various connect methods listed above.
|
||||
*
|
||||
* This method is called if one of the connect methods are invoked, and the connection fails.
|
||||
* This may happen, for example, if a domain name is given for the host and the domain name is unable to be resolved.
|
||||
**/
|
||||
- (void)udpSocket:(GCDAsyncUdpSocket *)sock didNotConnect:(NSError *)error;
|
||||
|
||||
/**
|
||||
* Called when the datagram with the given tag has been sent.
|
||||
**/
|
||||
- (void)udpSocket:(GCDAsyncUdpSocket *)sock didSendDataWithTag:(long)tag;
|
||||
|
||||
/**
|
||||
* Called if an error occurs while trying to send a datagram.
|
||||
* This could be due to a timeout, or something more serious such as the data being too large to fit in a sigle packet.
|
||||
**/
|
||||
- (void)udpSocket:(GCDAsyncUdpSocket *)sock didNotSendDataWithTag:(long)tag dueToError:(NSError *)error;
|
||||
|
||||
/**
|
||||
* Called when the socket has received the requested datagram.
|
||||
**/
|
||||
- (void)udpSocket:(GCDAsyncUdpSocket *)sock didReceiveData:(NSData *)data
|
||||
fromAddress:(NSData *)address
|
||||
withFilterContext:(id)filterContext;
|
||||
|
||||
/**
|
||||
* Called when the socket is closed.
|
||||
**/
|
||||
- (void)udpSocketDidClose:(GCDAsyncUdpSocket *)sock withError:(NSError *)error;
|
||||
+ (BOOL)getHost:(NSString * __nullable * __nullable)hostPtr port:(uint16_t * __nullable)portPtr fromAddress:(NSData *)address;
|
||||
+ (BOOL)getHost:(NSString * __nullable * __nullable)hostPtr port:(uint16_t * __nullable)portPtr family:(int * __nullable)afPtr fromAddress:(NSData *)address;
|
||||
|
||||
@end
|
||||
|
||||
NS_ASSUME_NONNULL_END
|
||||
|
|
|
|||
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue