| CustomiOSApi 1.09

GCDAsyncUdpSocket.h
1//
2// GCDAsyncUdpSocket
3//
4// This class is in the public domain.
5// Originally created by Robbie Hanson of Deusty LLC.
6// Updated and maintained by Deusty LLC and the Apple development community.
7//
8// https://github.com/robbiehanson/CocoaAsyncSocket
9//
10
11#import <Foundation/Foundation.h>
12#import <dispatch/dispatch.h>
13
14//Hide it in the documentation
15// @cond PRIVATE
16
17 extern NSString *const GCDAsyncUdpSocketException;
18 extern NSString *const GCDAsyncUdpSocketErrorDomain;
19
20 extern NSString *const GCDAsyncUdpSocketQueueName;
21 extern NSString *const GCDAsyncUdpSocketThreadName;
22
23 enum GCDAsyncUdpSocketError
24{
25 GCDAsyncUdpSocketNoError = 0, // Never used
26 GCDAsyncUdpSocketBadConfigError, // Invalid configuration
27 GCDAsyncUdpSocketBadParamError, // Invalid parameter was passed
28 GCDAsyncUdpSocketSendTimeoutError, // A send operation timed out
29 GCDAsyncUdpSocketClosedError, // The socket was closed
30 GCDAsyncUdpSocketOtherError, // Description provided in userInfo
31};
32 typedef enum GCDAsyncUdpSocketError GCDAsyncUdpSocketError;
33
82 typedef BOOL (^GCDAsyncUdpSocketReceiveFilterBlock)(NSData *data, NSData *address, id *context);
83
110 typedef BOOL (^GCDAsyncUdpSocketSendFilterBlock)(NSData *data, NSData *address, long tag);
111
112
113@interface GCDAsyncUdpSocket : NSObject
114
130- (id)init;
131- (id)initWithSocketQueue:(dispatch_queue_t)sq;
132- (id)initWithDelegate:(id)aDelegate delegateQueue:(dispatch_queue_t)dq;
133- (id)initWithDelegate:(id)aDelegate delegateQueue:(dispatch_queue_t)dq socketQueue:(dispatch_queue_t)sq;
134
135#pragma mark Configuration
136
137- (id)delegate;
138- (void)setDelegate:(id)delegate;
139- (void)synchronouslySetDelegate:(id)delegate;
140
141- (dispatch_queue_t)delegateQueue;
142- (void)setDelegateQueue:(dispatch_queue_t)delegateQueue;
143- (void)synchronouslySetDelegateQueue:(dispatch_queue_t)delegateQueue;
144
145- (void)getDelegate:(id *)delegatePtr delegateQueue:(dispatch_queue_t *)delegateQueuePtr;
146- (void)setDelegate:(id)delegate delegateQueue:(dispatch_queue_t)delegateQueue;
147- (void)synchronouslySetDelegate:(id)delegate delegateQueue:(dispatch_queue_t)delegateQueue;
148
167- (BOOL)isIPv4Enabled;
168- (void)setIPv4Enabled:(BOOL)flag;
169
170- (BOOL)isIPv6Enabled;
171- (void)setIPv6Enabled:(BOOL)flag;
172
173- (BOOL)isIPv4Preferred;
174- (BOOL)isIPv6Preferred;
175- (BOOL)isIPVersionNeutral;
176
177- (void)setPreferIPv4;
178- (void)setPreferIPv6;
179- (void)setIPVersionNeutral;
180
197- (uint16_t)maxReceiveIPv4BufferSize;
198- (void)setMaxReceiveIPv4BufferSize:(uint16_t)max;
199
200- (uint32_t)maxReceiveIPv6BufferSize;
201- (void)setMaxReceiveIPv6BufferSize:(uint32_t)max;
202
207- (id)userData;
208- (void)setUserData:(id)arbitraryUserData;
209
210#pragma mark Diagnostics
211
221- (NSData *)localAddress;
222- (NSString *)localHost;
223- (uint16_t)localPort;
224
225- (NSData *)localAddress_IPv4;
226- (NSString *)localHost_IPv4;
227- (uint16_t)localPort_IPv4;
228
229- (NSData *)localAddress_IPv6;
230- (NSString *)localHost_IPv6;
231- (uint16_t)localPort_IPv6;
232
243- (NSData *)connectedAddress;
244- (NSString *)connectedHost;
245- (uint16_t)connectedPort;
246
252- (BOOL)isConnected;
253
258- (BOOL)isClosed;
259
268- (BOOL)isIPv4;
269
281- (BOOL)isIPv6;
282
283#pragma mark Binding
284
301- (BOOL)bindToPort:(uint16_t)port error:(NSError **)errPtr;
302
323- (BOOL)bindToPort:(uint16_t)port interface:(NSString *)interface error:(NSError **)errPtr;
324
343- (BOOL)bindToAddress:(NSData *)localAddr error:(NSError **)errPtr;
344
345#pragma mark Connecting
346
370- (BOOL)connectToHost:(NSString *)host onPort:(uint16_t)port error:(NSError **)errPtr;
371
401- (BOOL)connectToAddress:(NSData *)remoteAddr error:(NSError **)errPtr;
402
403#pragma mark Multicast
404
412- (BOOL)joinMulticastGroup:(NSString *)group error:(NSError **)errPtr;
413
422- (BOOL)joinMulticastGroup:(NSString *)group onInterface:(NSString *)interface error:(NSError **)errPtr;
423
424- (BOOL)leaveMulticastGroup:(NSString *)group error:(NSError **)errPtr;
425- (BOOL)leaveMulticastGroup:(NSString *)group onInterface:(NSString *)interface error:(NSError **)errPtr;
426
427#pragma mark Broadcast
428
438- (BOOL)enableBroadcast:(BOOL)flag error:(NSError **)errPtr;
439
440#pragma mark Sending
441
481- (void)sendData:(NSData *)data withTimeout:(NSTimeInterval)timeout tag:(long)tag;
482
530- (void)sendData:(NSData *)data
531 toHost:(NSString *)host
532 port:(uint16_t)port
533 withTimeout:(NSTimeInterval)timeout
534 tag:(long)tag;
535
578- (void)sendData:(NSData *)data toAddress:(NSData *)remoteAddr withTimeout:(NSTimeInterval)timeout tag:(long)tag;
579
601- (void)setSendFilter:(GCDAsyncUdpSocketSendFilterBlock)filterBlock withQueue:(dispatch_queue_t)filterQueue;
602
616- (void)setSendFilter:(GCDAsyncUdpSocketSendFilterBlock)filterBlock
617 withQueue:(dispatch_queue_t)filterQueue
618 isAsynchronous:(BOOL)isAsynchronous;
619
620#pragma mark Receiving
621
646- (BOOL)receiveOnce:(NSError **)errPtr;
647
672- (BOOL)beginReceiving:(NSError **)errPtr;
673
686- (void)pauseReceiving;
687
733- (void)setReceiveFilter:(GCDAsyncUdpSocketReceiveFilterBlock)filterBlock withQueue:(dispatch_queue_t)filterQueue;
734
748- (void)setReceiveFilter:(GCDAsyncUdpSocketReceiveFilterBlock)filterBlock
749 withQueue:(dispatch_queue_t)filterQueue
750 isAsynchronous:(BOOL)isAsynchronous;
751
752#pragma mark Closing
753
761- (void)close;
762
769- (void)closeAfterSending;
770
771#pragma mark Advanced
844- (void)markSocketQueueTargetQueue:(dispatch_queue_t)socketQueuesPreConfiguredTargetQueue;
845- (void)unmarkSocketQueueTargetQueue:(dispatch_queue_t)socketQueuesPreviouslyConfiguredTargetQueue;
846
875- (void)performBlock:(dispatch_block_t)block;
876
885- (int)socketFD;
886- (int)socket4FD;
887- (int)socket6FD;
888
889#if TARGET_OS_IPHONE
890
901- (CFReadStreamRef)readStream;
902- (CFWriteStreamRef)writeStream;
903
925//- (BOOL)enableBackgroundingOnSockets;
926
927#endif
928
929#pragma mark Utilities
930
935+ (NSString *)hostFromAddress:(NSData *)address;
936+ (uint16_t)portFromAddress:(NSData *)address;
937+ (int)familyFromAddress:(NSData *)address;
938
939+ (BOOL)isIPv4Address:(NSData *)address;
940+ (BOOL)isIPv6Address:(NSData *)address;
941
942+ (BOOL)getHost:(NSString **)hostPtr port:(uint16_t *)portPtr fromAddress:(NSData *)address;
943+ (BOOL)getHost:(NSString **)hostPtr port:(uint16_t *)portPtr family:(int *)afPtr fromAddress:(NSData *)address;
944
945@end
946
948#pragma mark -
950
951@protocol GCDAsyncUdpSocketDelegate
952@optional
953
961- (void)udpSocket:(GCDAsyncUdpSocket *)sock didConnectToAddress:(NSData *)address;
962
971- (void)udpSocket:(GCDAsyncUdpSocket *)sock didNotConnect:(NSError *)error;
972
976- (void)udpSocket:(GCDAsyncUdpSocket *)sock didSendDataWithTag:(long)tag;
977
982- (void)udpSocket:(GCDAsyncUdpSocket *)sock didNotSendDataWithTag:(long)tag dueToError:(NSError *)error;
983
987- (void)udpSocket:(GCDAsyncUdpSocket *)sock didReceiveData:(NSData *)data
988 fromAddress:(NSData *)address
989 withFilterContext:(id)filterContext;
990
994- (void)udpSocketDidClose:(GCDAsyncUdpSocket *)sock withError:(NSError *)error;
995
996@end
997