PageRenderTime 48ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/ep-020/test_longpress_pinch/build/iphone/Classes/AsyncUdpSocket.h

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