|
| 1 | +// |
| 2 | +// AsyncUdpSocket.h |
| 3 | +// |
| 4 | +// This class is in the public domain. |
| 5 | +// Originally created by Robbie Hanson on Wed Oct 01 2008. |
| 6 | +// Updated and maintained by Deusty Designs and the Mac development community. |
| 7 | +// |
| 8 | +// http://code.google.com/p/cocoaasyncsocket/ |
| 9 | +// |
| 10 | + |
| 11 | +#import <Foundation/Foundation.h> |
| 12 | + |
| 13 | +@class AsyncSendPacket; |
| 14 | +@class AsyncReceivePacket; |
| 15 | + |
| 16 | +extern NSString *const AsyncUdpSocketException; |
| 17 | +extern NSString *const AsyncUdpSocketErrorDomain; |
| 18 | + |
| 19 | +typedef NS_ENUM(NSInteger, AsyncUdpSocketError) { |
| 20 | + AsyncUdpSocketCFSocketError = kCFSocketError, // From CFSocketError enum |
| 21 | + AsyncUdpSocketNoError = 0, // Never used |
| 22 | + AsyncUdpSocketBadParameter, // Used if given a bad parameter (such as an improper address) |
| 23 | + AsyncUdpSocketIPv4Unavailable, // Used if you bind/connect using IPv6 only |
| 24 | + AsyncUdpSocketIPv6Unavailable, // Used if you bind/connect using IPv4 only (or iPhone) |
| 25 | + AsyncUdpSocketSendTimeoutError, |
| 26 | + AsyncUdpSocketReceiveTimeoutError |
| 27 | +}; |
| 28 | + |
| 29 | +__deprecated_msg("The RunLoop versions of CocoaAsyncSocket are deprecated and will be removed in a future release. Please migrate to GCDAsyncUdpSocket.") |
| 30 | +@interface AsyncUdpSocket : NSObject |
| 31 | +{ |
| 32 | + CFSocketRef theSocket4; // IPv4 socket |
| 33 | + CFSocketRef theSocket6; // IPv6 socket |
| 34 | + |
| 35 | + CFRunLoopSourceRef theSource4; // For theSocket4 |
| 36 | + CFRunLoopSourceRef theSource6; // For theSocket6 |
| 37 | + CFRunLoopRef theRunLoop; |
| 38 | + CFSocketContext theContext; |
| 39 | + NSArray *theRunLoopModes; |
| 40 | + |
| 41 | + NSMutableArray *theSendQueue; |
| 42 | + AsyncSendPacket *theCurrentSend; |
| 43 | + NSTimer *theSendTimer; |
| 44 | + |
| 45 | + NSMutableArray *theReceiveQueue; |
| 46 | + AsyncReceivePacket *theCurrentReceive; |
| 47 | + NSTimer *theReceiveTimer; |
| 48 | + |
| 49 | + id theDelegate; |
| 50 | + UInt16 theFlags; |
| 51 | + |
| 52 | + long theUserData; |
| 53 | + |
| 54 | + NSString *cachedLocalHost; |
| 55 | + UInt16 cachedLocalPort; |
| 56 | + |
| 57 | + NSString *cachedConnectedHost; |
| 58 | + UInt16 cachedConnectedPort; |
| 59 | + |
| 60 | + UInt32 maxReceiveBufferSize; |
| 61 | +} |
| 62 | + |
| 63 | +/** |
| 64 | + * Creates new instances of AsyncUdpSocket. |
| 65 | +**/ |
| 66 | +- (id)init; |
| 67 | +- (id)initWithDelegate:(id)delegate; |
| 68 | +- (id)initWithDelegate:(id)delegate userData:(long)userData; |
| 69 | + |
| 70 | +/** |
| 71 | + * Creates new instances of AsyncUdpSocket that support only IPv4 or IPv6. |
| 72 | + * The other init methods will support both, unless specifically binded or connected to one protocol. |
| 73 | + * If you know you'll only be using one protocol, these init methods may be a bit more efficient. |
| 74 | +**/ |
| 75 | +- (id)initIPv4; |
| 76 | +- (id)initIPv6; |
| 77 | + |
| 78 | +- (id)delegate; |
| 79 | +- (void)setDelegate:(id)delegate; |
| 80 | + |
| 81 | +- (long)userData; |
| 82 | +- (void)setUserData:(long)userData; |
| 83 | + |
| 84 | +/** |
| 85 | + * Returns the local address info for the socket. |
| 86 | + * |
| 87 | + * Note: Address info may not be available until after the socket has been bind'ed, |
| 88 | + * or until after data has been sent. |
| 89 | +**/ |
| 90 | +- (NSString *)localHost; |
| 91 | +- (UInt16)localPort; |
| 92 | + |
| 93 | +/** |
| 94 | + * Returns the remote address info for the socket. |
| 95 | + * |
| 96 | + * Note: Since UDP is connectionless by design, connected address info |
| 97 | + * will not be available unless the socket is explicitly connected to a remote host/port |
| 98 | +**/ |
| 99 | +- (NSString *)connectedHost; |
| 100 | +- (UInt16)connectedPort; |
| 101 | + |
| 102 | +/** |
| 103 | + * Returns whether or not this socket has been connected to a single host. |
| 104 | + * By design, UDP is a connectionless protocol, and connecting is not needed. |
| 105 | + * If connected, the socket will only be able to send/receive data to/from the connected host. |
| 106 | +**/ |
| 107 | +- (BOOL)isConnected; |
| 108 | + |
| 109 | +/** |
| 110 | + * Returns whether or not this socket has been closed. |
| 111 | + * The only way a socket can be closed is if you explicitly call one of the close methods. |
| 112 | +**/ |
| 113 | +- (BOOL)isClosed; |
| 114 | + |
| 115 | +/** |
| 116 | + * Returns whether or not this socket supports IPv4. |
| 117 | + * By default this will be true, unless the socket is specifically initialized as IPv6 only, |
| 118 | + * or is binded or connected to an IPv6 address. |
| 119 | +**/ |
| 120 | +- (BOOL)isIPv4; |
| 121 | + |
| 122 | +/** |
| 123 | + * Returns whether or not this socket supports IPv6. |
| 124 | + * By default this will be true, unless the socket is specifically initialized as IPv4 only, |
| 125 | + * or is binded or connected to an IPv4 address. |
| 126 | + * |
| 127 | + * This method will also return false on platforms that do not support IPv6. |
| 128 | + * Note: The iPhone does not currently support IPv6. |
| 129 | +**/ |
| 130 | +- (BOOL)isIPv6; |
| 131 | + |
| 132 | +/** |
| 133 | + * Returns the mtu of the socket. |
| 134 | + * If unknown, returns zero. |
| 135 | + * |
| 136 | + * Sending data larger than this may result in an error. |
| 137 | + * This is an advanced topic, and one should understand the wide range of mtu's on networks and the internet. |
| 138 | + * Therefore this method is only for reference and may be of little use in many situations. |
| 139 | +**/ |
| 140 | +- (unsigned int)maximumTransmissionUnit; |
| 141 | + |
| 142 | +/** |
| 143 | + * Binds the UDP socket to the given port and optional address. |
| 144 | + * Binding should be done for server sockets that receive data prior to sending it. |
| 145 | + * Client sockets can skip binding, |
| 146 | + * as the OS will automatically assign the socket an available port when it starts sending data. |
| 147 | + * |
| 148 | + * You cannot bind a socket after its been connected. |
| 149 | + * You can only bind a socket once. |
| 150 | + * You can still connect a socket (if desired) after binding. |
| 151 | + * |
| 152 | + * On success, returns YES. |
| 153 | + * Otherwise returns NO, and sets errPtr. If you don't care about the error, you can pass nil for errPtr. |
| 154 | +**/ |
| 155 | +- (BOOL)bindToPort:(UInt16)port error:(NSError **)errPtr; |
| 156 | +- (BOOL)bindToAddress:(NSString *)localAddr port:(UInt16)port error:(NSError **)errPtr; |
| 157 | + |
| 158 | +/** |
| 159 | + * Connects the UDP socket to the given host and port. |
| 160 | + * By design, UDP is a connectionless protocol, and connecting is not needed. |
| 161 | + * |
| 162 | + * Choosing to connect to a specific host/port has the following effect: |
| 163 | + * - You will only be able to send data to the connected host/port. |
| 164 | + * - You will only be able to receive data from the connected host/port. |
| 165 | + * - You will receive ICMP messages that come from the connected host/port, such as "connection refused". |
| 166 | + * |
| 167 | + * Connecting a UDP socket does not result in any communication on the socket. |
| 168 | + * It simply changes the internal state of the socket. |
| 169 | + * |
| 170 | + * You cannot bind a socket after its been connected. |
| 171 | + * You can only connect a socket once. |
| 172 | + * |
| 173 | + * On success, returns YES. |
| 174 | + * Otherwise returns NO, and sets errPtr. If you don't care about the error, you can pass nil for errPtr. |
| 175 | +**/ |
| 176 | +- (BOOL)connectToHost:(NSString *)host onPort:(UInt16)port error:(NSError **)errPtr; |
| 177 | +- (BOOL)connectToAddress:(NSData *)remoteAddr error:(NSError **)errPtr; |
| 178 | + |
| 179 | +/** |
| 180 | + * Join multicast group |
| 181 | + * |
| 182 | + * Group should be an IP address (eg @"225.228.0.1") |
| 183 | +**/ |
| 184 | +- (BOOL)joinMulticastGroup:(NSString *)group error:(NSError **)errPtr; |
| 185 | +- (BOOL)joinMulticastGroup:(NSString *)group withAddress:(NSString *)interface error:(NSError **)errPtr; |
| 186 | + |
| 187 | +/** |
| 188 | + * By default, the underlying socket in the OS will not allow you to send broadcast messages. |
| 189 | + * In order to send broadcast messages, you need to enable this functionality in the socket. |
| 190 | + * |
| 191 | + * A broadcast is a UDP message to addresses like "192.168.255.255" or "255.255.255.255" that is |
| 192 | + * delivered to every host on the network. |
| 193 | + * The reason this is generally disabled by default is to prevent |
| 194 | + * accidental broadcast messages from flooding the network. |
| 195 | +**/ |
| 196 | +- (BOOL)enableBroadcast:(BOOL)flag error:(NSError **)errPtr; |
| 197 | + |
| 198 | +/** |
| 199 | + * Asynchronously sends the given data, with the given timeout and tag. |
| 200 | + * |
| 201 | + * This method may only be used with a connected socket. |
| 202 | + * |
| 203 | + * If data is nil or zero-length, this method does nothing and immediately returns NO. |
| 204 | + * If the socket is not connected, this method does nothing and immediately returns NO. |
| 205 | +**/ |
| 206 | +- (BOOL)sendData:(NSData *)data withTimeout:(NSTimeInterval)timeout tag:(long)tag; |
| 207 | + |
| 208 | +/** |
| 209 | + * Asynchronously sends the given data, with the given timeout and tag, to the given host and port. |
| 210 | + * |
| 211 | + * This method cannot be used with a connected socket. |
| 212 | + * |
| 213 | + * If data is nil or zero-length, this method does nothing and immediately returns NO. |
| 214 | + * If the socket is connected, this method does nothing and immediately returns NO. |
| 215 | + * If unable to resolve host to a valid IPv4 or IPv6 address, this method returns NO. |
| 216 | +**/ |
| 217 | +- (BOOL)sendData:(NSData *)data toHost:(NSString *)host port:(UInt16)port withTimeout:(NSTimeInterval)timeout tag:(long)tag; |
| 218 | + |
| 219 | +/** |
| 220 | + * Asynchronously sends the given data, with the given timeout and tag, to the given address. |
| 221 | + * |
| 222 | + * This method cannot be used with a connected socket. |
| 223 | + * |
| 224 | + * If data is nil or zero-length, this method does nothing and immediately returns NO. |
| 225 | + * If the socket is connected, this method does nothing and immediately returns NO. |
| 226 | +**/ |
| 227 | +- (BOOL)sendData:(NSData *)data toAddress:(NSData *)remoteAddr withTimeout:(NSTimeInterval)timeout tag:(long)tag; |
| 228 | + |
| 229 | +/** |
| 230 | + * Asynchronously receives a single datagram packet. |
| 231 | + * |
| 232 | + * If the receive succeeds, the onUdpSocket:didReceiveData:fromHost:port:tag delegate method will be called. |
| 233 | + * Otherwise, a timeout will occur, and the onUdpSocket:didNotReceiveDataWithTag: delegate method will be called. |
| 234 | +**/ |
| 235 | +- (void)receiveWithTimeout:(NSTimeInterval)timeout tag:(long)tag; |
| 236 | + |
| 237 | +/** |
| 238 | + * Closes the socket immediately. Any pending send or receive operations are dropped. |
| 239 | +**/ |
| 240 | +- (void)close; |
| 241 | + |
| 242 | +/** |
| 243 | + * Closes after all pending send operations have completed. |
| 244 | + * After calling this, the sendData: and receive: methods will do nothing. |
| 245 | + * In other words, you won't be able to add any more send or receive operations to the queue. |
| 246 | + * The socket will close even if there are still pending receive operations. |
| 247 | +**/ |
| 248 | +- (void)closeAfterSending; |
| 249 | + |
| 250 | +/** |
| 251 | + * Closes after all pending receive operations have completed. |
| 252 | + * After calling this, the sendData: and receive: methods will do nothing. |
| 253 | + * In other words, you won't be able to add any more send or receive operations to the queue. |
| 254 | + * The socket will close even if there are still pending send operations. |
| 255 | +**/ |
| 256 | +- (void)closeAfterReceiving; |
| 257 | + |
| 258 | +/** |
| 259 | + * Closes after all pending send and receive operations have completed. |
| 260 | + * After calling this, the sendData: and receive: methods will do nothing. |
| 261 | + * In other words, you won't be able to add any more send or receive operations to the queue. |
| 262 | +**/ |
| 263 | +- (void)closeAfterSendingAndReceiving; |
| 264 | + |
| 265 | +/** |
| 266 | + * Gets/Sets the maximum size of the buffer that will be allocated for receive operations. |
| 267 | + * The default size is 9216 bytes. |
| 268 | + * |
| 269 | + * The theoretical maximum size of any IPv4 UDP packet is UINT16_MAX = 65535. |
| 270 | + * The theoretical maximum size of any IPv6 UDP packet is UINT32_MAX = 4294967295. |
| 271 | + * |
| 272 | + * In practice, however, the size of UDP packets will be much smaller. |
| 273 | + * Indeed most protocols will send and receive packets of only a few bytes, |
| 274 | + * or will set a limit on the size of packets to prevent fragmentation in the IP layer. |
| 275 | + * |
| 276 | + * If you set the buffer size too small, the sockets API in the OS will silently discard |
| 277 | + * any extra data, and you will not be notified of the error. |
| 278 | +**/ |
| 279 | +- (UInt32)maxReceiveBufferSize; |
| 280 | +- (void)setMaxReceiveBufferSize:(UInt32)max; |
| 281 | + |
| 282 | +/** |
| 283 | + * When you create an AsyncUdpSocket, it is added to the runloop of the current thread. |
| 284 | + * So it is easiest to simply create the socket on the thread you intend to use it. |
| 285 | + * |
| 286 | + * If, however, you need to move the socket to a separate thread at a later time, this |
| 287 | + * method may be used to accomplish the task. |
| 288 | + * |
| 289 | + * This method must be called from the thread/runloop the socket is currently running on. |
| 290 | + * |
| 291 | + * Note: After calling this method, all further method calls to this object should be done from the given runloop. |
| 292 | + * Also, all delegate calls will be sent on the given runloop. |
| 293 | +**/ |
| 294 | +- (BOOL)moveToRunLoop:(NSRunLoop *)runLoop; |
| 295 | + |
| 296 | +/** |
| 297 | + * Allows you to configure which run loop modes the socket uses. |
| 298 | + * The default set of run loop modes is NSDefaultRunLoopMode. |
| 299 | + * |
| 300 | + * If you'd like your socket to continue operation during other modes, you may want to add modes such as |
| 301 | + * NSModalPanelRunLoopMode or NSEventTrackingRunLoopMode. Or you may simply want to use NSRunLoopCommonModes. |
| 302 | + * |
| 303 | + * Note: NSRunLoopCommonModes is defined in 10.5. For previous versions one can use kCFRunLoopCommonModes. |
| 304 | +**/ |
| 305 | +- (BOOL)setRunLoopModes:(NSArray *)runLoopModes; |
| 306 | + |
| 307 | +/** |
| 308 | + * Returns the current run loop modes the AsyncSocket instance is operating in. |
| 309 | + * The default set of run loop modes is NSDefaultRunLoopMode. |
| 310 | +**/ |
| 311 | +- (NSArray *)runLoopModes; |
| 312 | + |
| 313 | +@end |
| 314 | + |
| 315 | +//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 316 | +#pragma mark - |
| 317 | +//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 318 | + |
| 319 | +__deprecated_msg("The RunLoop versions of CocoaAsyncSocket are deprecated and will be removed in a future release. Please migrate to GCDAsyncUdpSocket.") |
| 320 | +@protocol AsyncUdpSocketDelegate |
| 321 | +@optional |
| 322 | + |
| 323 | +/** |
| 324 | + * Called when the datagram with the given tag has been sent. |
| 325 | +**/ |
| 326 | +- (void)onUdpSocket:(AsyncUdpSocket *)sock didSendDataWithTag:(long)tag; |
| 327 | + |
| 328 | +/** |
| 329 | + * Called if an error occurs while trying to send a datagram. |
| 330 | + * This could be due to a timeout, or something more serious such as the data being too large to fit in a sigle packet. |
| 331 | +**/ |
| 332 | +- (void)onUdpSocket:(AsyncUdpSocket *)sock didNotSendDataWithTag:(long)tag dueToError:(NSError *)error; |
| 333 | + |
| 334 | +/** |
| 335 | + * Called when the socket has received the requested datagram. |
| 336 | + * |
| 337 | + * Due to the nature of UDP, you may occasionally receive undesired packets. |
| 338 | + * These may be rogue UDP packets from unknown hosts, |
| 339 | + * or they may be delayed packets arriving after retransmissions have already occurred. |
| 340 | + * It's important these packets are properly ignored, while not interfering with the flow of your implementation. |
| 341 | + * As an aid, this delegate method has a boolean return value. |
| 342 | + * If you ever need to ignore a received packet, simply return NO, |
| 343 | + * and AsyncUdpSocket will continue as if the packet never arrived. |
| 344 | + * That is, the original receive request will still be queued, and will still timeout as usual if a timeout was set. |
| 345 | + * For example, say you requested to receive data, and you set a timeout of 500 milliseconds, using a tag of 15. |
| 346 | + * If rogue data arrives after 250 milliseconds, this delegate method would be invoked, and you could simply return NO. |
| 347 | + * If the expected data then arrives within the next 250 milliseconds, |
| 348 | + * this delegate method will be invoked, with a tag of 15, just as if the rogue data never appeared. |
| 349 | + * |
| 350 | + * Under normal circumstances, you simply return YES from this method. |
| 351 | +**/ |
| 352 | +- (BOOL)onUdpSocket:(AsyncUdpSocket *)sock |
| 353 | + didReceiveData:(NSData *)data |
| 354 | + withTag:(long)tag |
| 355 | + fromHost:(NSString *)host |
| 356 | + port:(UInt16)port; |
| 357 | + |
| 358 | +/** |
| 359 | + * Called if an error occurs while trying to receive a requested datagram. |
| 360 | + * This is generally due to a timeout, but could potentially be something else if some kind of OS error occurred. |
| 361 | +**/ |
| 362 | +- (void)onUdpSocket:(AsyncUdpSocket *)sock didNotReceiveDataWithTag:(long)tag dueToError:(NSError *)error; |
| 363 | + |
| 364 | +/** |
| 365 | + * Called when the socket is closed. |
| 366 | + * A socket is only closed if you explicitly call one of the close methods. |
| 367 | +**/ |
| 368 | +- (void)onUdpSocketDidClose:(AsyncUdpSocket *)sock; |
| 369 | + |
| 370 | +@end |
0 commit comments