CFSocket.pas 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. { CFSocket.h
  2. Copyright (c) 1999-2005, Apple, Inc. All rights reserved.
  3. }
  4. { Pascal Translation: Peter N Lewis, <[email protected]>, 2004 }
  5. { Pascal Translation Updated: Peter N Lewis, <[email protected]>, September 2005 }
  6. {
  7. Modified for use with Free Pascal
  8. Version 200
  9. Please report any bugs to <[email protected]>
  10. }
  11. {$mode macpas}
  12. {$packenum 1}
  13. {$macro on}
  14. {$inline on}
  15. {$CALLING MWPASCAL}
  16. unit CFSocket;
  17. interface
  18. {$setc UNIVERSAL_INTERFACES_VERSION := $0342}
  19. {$setc GAP_INTERFACES_VERSION := $0200}
  20. {$ifc not defined USE_CFSTR_CONSTANT_MACROS}
  21. {$setc USE_CFSTR_CONSTANT_MACROS := TRUE}
  22. {$endc}
  23. {$ifc defined CPUPOWERPC and defined CPUI386}
  24. {$error Conflicting initial definitions for CPUPOWERPC and CPUI386}
  25. {$endc}
  26. {$ifc defined FPC_BIG_ENDIAN and defined FPC_LITTLE_ENDIAN}
  27. {$error Conflicting initial definitions for FPC_BIG_ENDIAN and FPC_LITTLE_ENDIAN}
  28. {$endc}
  29. {$ifc not defined __ppc__ and defined CPUPOWERPC}
  30. {$setc __ppc__ := 1}
  31. {$elsec}
  32. {$setc __ppc__ := 0}
  33. {$endc}
  34. {$ifc not defined __i386__ and defined CPUI386}
  35. {$setc __i386__ := 1}
  36. {$elsec}
  37. {$setc __i386__ := 0}
  38. {$endc}
  39. {$ifc defined __ppc__ and __ppc__ and defined __i386__ and __i386__}
  40. {$error Conflicting definitions for __ppc__ and __i386__}
  41. {$endc}
  42. {$ifc defined __ppc__ and __ppc__}
  43. {$setc TARGET_CPU_PPC := TRUE}
  44. {$setc TARGET_CPU_X86 := FALSE}
  45. {$elifc defined __i386__ and __i386__}
  46. {$setc TARGET_CPU_PPC := FALSE}
  47. {$setc TARGET_CPU_X86 := TRUE}
  48. {$elsec}
  49. {$error Neither __ppc__ nor __i386__ is defined.}
  50. {$endc}
  51. {$setc TARGET_CPU_PPC_64 := FALSE}
  52. {$ifc defined FPC_BIG_ENDIAN}
  53. {$setc TARGET_RT_BIG_ENDIAN := TRUE}
  54. {$setc TARGET_RT_LITTLE_ENDIAN := FALSE}
  55. {$elifc defined FPC_LITTLE_ENDIAN}
  56. {$setc TARGET_RT_BIG_ENDIAN := FALSE}
  57. {$setc TARGET_RT_LITTLE_ENDIAN := TRUE}
  58. {$elsec}
  59. {$error Neither FPC_BIG_ENDIAN nor FPC_LITTLE_ENDIAN are defined.}
  60. {$endc}
  61. {$setc ACCESSOR_CALLS_ARE_FUNCTIONS := TRUE}
  62. {$setc CALL_NOT_IN_CARBON := FALSE}
  63. {$setc OLDROUTINENAMES := FALSE}
  64. {$setc OPAQUE_TOOLBOX_STRUCTS := TRUE}
  65. {$setc OPAQUE_UPP_TYPES := TRUE}
  66. {$setc OTCARBONAPPLICATION := TRUE}
  67. {$setc OTKERNEL := FALSE}
  68. {$setc PM_USE_SESSION_APIS := TRUE}
  69. {$setc TARGET_API_MAC_CARBON := TRUE}
  70. {$setc TARGET_API_MAC_OS8 := FALSE}
  71. {$setc TARGET_API_MAC_OSX := TRUE}
  72. {$setc TARGET_CARBON := TRUE}
  73. {$setc TARGET_CPU_68K := FALSE}
  74. {$setc TARGET_CPU_MIPS := FALSE}
  75. {$setc TARGET_CPU_SPARC := FALSE}
  76. {$setc TARGET_OS_MAC := TRUE}
  77. {$setc TARGET_OS_UNIX := FALSE}
  78. {$setc TARGET_OS_WIN32 := FALSE}
  79. {$setc TARGET_RT_MAC_68881 := FALSE}
  80. {$setc TARGET_RT_MAC_CFM := FALSE}
  81. {$setc TARGET_RT_MAC_MACHO := TRUE}
  82. {$setc TYPED_FUNCTION_POINTERS := TRUE}
  83. {$setc TYPE_BOOL := FALSE}
  84. {$setc TYPE_EXTENDED := FALSE}
  85. {$setc TYPE_LONGLONG := TRUE}
  86. uses MacTypes,CFBase,CFData,CFString,CFRunLoop,CFDate;
  87. {$ALIGN POWER}
  88. type
  89. CFSocketNativeHandle = SInt32;
  90. type
  91. CFSocketRef = ^SInt32; { an opaque 32-bit type }
  92. { A CFSocket contains a native socket within a structure that can
  93. be used to read from the socket in the background and make the data
  94. thus read available using a runloop source. The callback used for
  95. this may be of three types, as specified by the callBackTypes
  96. argument when creating the CFSocket.
  97. If kCFSocketReadCallBack is used, then data will not be
  98. automatically read, but the callback will be called when data
  99. is available to be read, or a new child socket is waiting to be
  100. accepted.
  101. If kCFSocketAcceptCallBack is used, then new child sockets will be
  102. accepted and passed to the callback, with the data argument being
  103. a pointer to a CFSocketNativeHandle. This is usable only with
  104. connection rendezvous sockets.
  105. If kCFSocketDataCallBack is used, then data will be read in chunks
  106. in the background and passed to the callback, with the data argument
  107. being a CFDataRef.
  108. These three types are mutually exclusive, but any one of them may
  109. have kCFSocketConnectCallBack added to it, if the socket will be
  110. used to connect in the background. Connect in the background occurs
  111. if CFSocketConnectToAddress is called with a negative timeout
  112. value, in which case the call returns immediately, and a
  113. kCFSocketConnectCallBack is generated when the connect finishes.
  114. In this case the data argument is either NULL, or a pointer to
  115. an SInt32 error code if the connect failed. kCFSocketConnectCallBack
  116. will never be sent more than once for a given socket.
  117. The callback types may also have kCFSocketWriteCallBack added to
  118. them, if large amounts of data are to be sent rapidly over the
  119. socket and notification is desired when there is space in the
  120. kernel buffers so that the socket is writable again.
  121. With a connection-oriented socket, if the connection is broken from the
  122. other end, then one final kCFSocketReadCallBack or kCFSocketDataCallBack
  123. will occur. In the case of kCFSocketReadCallBack, the underlying socket
  124. will have 0 bytes available to read. In the case of kCFSocketDataCallBack,
  125. the data argument will be a CFDataRef of length 0.
  126. There are socket flags that may be set to control whether callbacks of
  127. a given type are automatically reenabled after they are triggered, and
  128. whether the underlying native socket will be closed when the CFSocket
  129. is invalidated. By default read, accept, and data callbacks are
  130. automatically reenabled; write callbacks are not, and connect callbacks
  131. may not be, since they are sent once only. Be careful about automatically
  132. reenabling read and write callbacks, since this implies that the
  133. callbacks will be sent repeatedly if the socket remains readable or
  134. writable respectively. Be sure to set these flags only for callbacks
  135. that your CFSocket actually possesses; the result of setting them for
  136. other callback types is undefined.
  137. Individual callbacks may also be enabled and disabled manually, whether
  138. they are automatically reenabled or not. If they are not automatically
  139. reenabled, then they will need to be manually reenabled when the callback
  140. is ready to be received again (and not sooner). Even if they are
  141. automatically reenabled, there may be occasions when it will be useful
  142. to be able to manually disable them temporarily and then reenable them.
  143. Be sure to enable and disable only callbacks that your CFSocket actually
  144. possesses; the result of enabling and disabling other callback types is
  145. undefined.
  146. By default the underlying native socket will be closed when the CFSocket
  147. is invalidated, but it will not be if kCFSocketCloseOnInvalidate is
  148. turned off. This can be useful in order to destroy a CFSocket but
  149. continue to use the underlying native socket. The CFSocket must
  150. still be invalidated when it will no longer be used. Do not in
  151. either case close the underlying native socket without invalidating
  152. the CFSocket.
  153. Addresses are stored as CFDatas containing a struct sockaddr
  154. appropriate for the protocol family; make sure that all fields are
  155. filled in properly when passing in an address.
  156. }
  157. type
  158. CFSocketError = SInt32;
  159. const
  160. kCFSocketSuccess = 0;
  161. kCFSocketError = -1;
  162. kCFSocketTimeout = -2;
  163. type
  164. CFSocketSignature = record
  165. protocolFamily: SInt32;
  166. socketType: SInt32;
  167. protocol: SInt32;
  168. address: CFDataRef;
  169. end;
  170. type
  171. CFSocketCallBackType = SInt32;
  172. const
  173. kCFSocketNoCallBack = 0;
  174. kCFSocketReadCallBack = 1;
  175. kCFSocketAcceptCallBack = 2;
  176. kCFSocketDataCallBack = 3;
  177. kCFSocketConnectCallBack = 4;
  178. {#if MAC_OS_X_VERSION_10_2 <= MAC_OS_X_VERSION_MAX_ALLOWED}
  179. kCFSocketWriteCallBack = 8;
  180. {#endif}
  181. {#if MAC_OS_X_VERSION_10_2 <= MAC_OS_X_VERSION_MAX_ALLOWED}
  182. { Socket flags }
  183. const
  184. kCFSocketAutomaticallyReenableReadCallBack = 1;
  185. kCFSocketAutomaticallyReenableAcceptCallBack = 2;
  186. kCFSocketAutomaticallyReenableDataCallBack = 3;
  187. kCFSocketAutomaticallyReenableWriteCallBack = 8;
  188. kCFSocketCloseOnInvalidate = 128;
  189. {#endif}
  190. type
  191. CFSocketCallBack = procedure( s: CFSocketRef; typ: CFSocketCallBackType; address: CFDataRef; data: {const} UnivPtr; info: UnivPtr );
  192. { If the callback wishes to keep hold of address or data after the point that it returns, then it must copy them. }
  193. type
  194. CFSocketContext = record
  195. version: CFIndex;
  196. info: UnivPtr;
  197. retain: function( info: {const} UnivPtr ): UnivPtr;
  198. release: procedure( info: {const} UnivPtr );
  199. copyDescription: function( info: {const} UnivPtr ): CFStringRef;
  200. end;
  201. function CFSocketGetTypeID: CFTypeID; external name '_CFSocketGetTypeID';
  202. function CFSocketCreate( allocator: CFAllocatorRef; protocolFamily: SInt32; socketType: SInt32; protocol: SInt32; callBackTypes: CFOptionFlags; callout: CFSocketCallBack; const (*var*) context: CFSocketContext ): CFSocketRef; external name '_CFSocketCreate';
  203. function CFSocketCreateWithNative( allocator: CFAllocatorRef; sock: CFSocketNativeHandle; callBackTypes: CFOptionFlags; callout: CFSocketCallBack; const (*var*) context: CFSocketContext ): CFSocketRef; external name '_CFSocketCreateWithNative';
  204. function CFSocketCreateWithSocketSignature( allocator: CFAllocatorRef; const (*var*) signature: CFSocketSignature; callBackTypes: CFOptionFlags; callout: CFSocketCallBack; const (*var*) context: CFSocketContext ): CFSocketRef; external name '_CFSocketCreateWithSocketSignature';
  205. function CFSocketCreateConnectedToSocketSignature( allocator: CFAllocatorRef; const (*var*) signature: CFSocketSignature; callBackTypes: CFOptionFlags; callout: CFSocketCallBack; const (*var*) context: CFSocketContext; timeout: CFTimeInterval ): CFSocketRef; external name '_CFSocketCreateConnectedToSocketSignature';
  206. { CFSocketCreateWithSignature creates a socket of the requested type and binds its address (using CFSocketSetAddress) to the requested address. If this fails, it returns NULL. CFSocketCreateConnectedToSignature creates a socket suitable for connecting to the requested type and address, and connects it (using CFSocketConnectToAddress). If this fails, it returns NULL. }
  207. function CFSocketSetAddress( s: CFSocketRef; address: CFDataRef ): CFSocketError; external name '_CFSocketSetAddress';
  208. function CFSocketConnectToAddress( s: CFSocketRef; address: CFDataRef; timeout: CFTimeInterval ): CFSocketError; external name '_CFSocketConnectToAddress';
  209. procedure CFSocketInvalidate( s: CFSocketRef ); external name '_CFSocketInvalidate';
  210. function CFSocketIsValid( s: CFSocketRef ): Boolean; external name '_CFSocketIsValid';
  211. function CFSocketCopyAddress( s: CFSocketRef ): CFDataRef; external name '_CFSocketCopyAddress';
  212. function CFSocketCopyPeerAddress( s: CFSocketRef ): CFDataRef; external name '_CFSocketCopyPeerAddress';
  213. procedure CFSocketGetContext( s: CFSocketRef; var context: CFSocketContext ); external name '_CFSocketGetContext';
  214. function CFSocketGetNative( s: CFSocketRef ): CFSocketNativeHandle; external name '_CFSocketGetNative';
  215. function CFSocketCreateRunLoopSource( allocator: CFAllocatorRef; s: CFSocketRef; order: CFIndex ): CFRunLoopSourceRef; external name '_CFSocketCreateRunLoopSource';
  216. {#if MAC_OS_X_VERSION_10_2 <= MAC_OS_X_VERSION_MAX_ALLOWED}
  217. function CFSocketGetSocketFlags( s: CFSocketRef ): CFOptionFlags; external name '_CFSocketGetSocketFlags';
  218. procedure CFSocketSetSocketFlags( s: CFSocketRef; flags: CFOptionFlags ); external name '_CFSocketSetSocketFlags';
  219. procedure CFSocketDisableCallBacks( s: CFSocketRef; callBackTypes: CFOptionFlags ); external name '_CFSocketDisableCallBacks';
  220. procedure CFSocketEnableCallBacks( s: CFSocketRef; callBackTypes: CFOptionFlags ); external name '_CFSocketEnableCallBacks';
  221. {#endif}
  222. { For convenience, a function is provided to send data using the socket with a timeout. The timeout will be used only if the specified value is positive. The address should be left NULL if the socket is already connected. }
  223. function CFSocketSendData( s: CFSocketRef; address: CFDataRef; data: CFDataRef; timeout: CFTimeInterval ): CFSocketError; external name '_CFSocketSendData';
  224. { Generic name registry functionality (CFSocketRegisterValue,
  225. CFSocketCopyRegisteredValue) allows the registration of any property
  226. list type. Functions specific to CFSockets (CFSocketRegisterSocketData,
  227. CFSocketCopyRegisteredSocketData) register a CFData containing the
  228. components of a socket signature (protocol family, socket type,
  229. protocol, and address). In each function the nameServerSignature
  230. may be NULL, or any component of it may be 0, to use default values
  231. (TCP, INADDR_LOOPBACK, port as set). Name registration servers might
  232. not allow registration with other than TCP and INADDR_LOOPBACK.
  233. The actual address of the server responding to a query may be obtained
  234. by using the nameServerAddress argument. This address, the address
  235. returned by CFSocketCopyRegisteredSocketSignature, and the value
  236. returned by CFSocketCopyRegisteredValue must (if non-null) be released
  237. by the caller. CFSocketUnregister removes any registration associated
  238. with the specified name.
  239. }
  240. function CFSocketRegisterValue( const (*var*) nameServerSignature: CFSocketSignature; timeout: CFTimeInterval; name: CFStringRef; value: CFPropertyListRef ): CFSocketError; external name '_CFSocketRegisterValue';
  241. function CFSocketCopyRegisteredValue( const (*var*) nameServerSignature: CFSocketSignature; timeout: CFTimeInterval; name: CFStringRef; var value: CFPropertyListRef; var nameServerAddress: CFDataRef ): CFSocketError; external name '_CFSocketCopyRegisteredValue';
  242. function CFSocketRegisterSocketSignature( const (*var*) nameServerSignature: CFSocketSignature; timeout: CFTimeInterval; name: CFStringRef; const (*var*) signature: CFSocketSignature ): CFSocketError; external name '_CFSocketRegisterSocketSignature';
  243. function CFSocketCopyRegisteredSocketSignature( const (*var*) nameServerSignature: CFSocketSignature; timeout: CFTimeInterval; name: CFStringRef; var signature: CFSocketSignature; var nameServerAddress: CFDataRef ): CFSocketError; external name '_CFSocketCopyRegisteredSocketSignature';
  244. function CFSocketUnregister( const (*var*) nameServerSignature: CFSocketSignature; timeout: CFTimeInterval; name: CFStringRef ): CFSocketError; external name '_CFSocketUnregister';
  245. procedure CFSocketSetDefaultNameRegistryPortNumber( port: UInt16 ); external name '_CFSocketSetDefaultNameRegistryPortNumber';
  246. function CFSocketGetDefaultNameRegistryPortNumber: UInt16; external name '_CFSocketGetDefaultNameRegistryPortNumber';
  247. { Constants used in name registry server communications }
  248. var kCFSocketCommandKey: CFStringRef; external name '_kCFSocketCommandKey'; (* attribute const *)
  249. var kCFSocketNameKey: CFStringRef; external name '_kCFSocketNameKey'; (* attribute const *)
  250. var kCFSocketValueKey: CFStringRef; external name '_kCFSocketValueKey'; (* attribute const *)
  251. var kCFSocketResultKey: CFStringRef; external name '_kCFSocketResultKey'; (* attribute const *)
  252. var kCFSocketErrorKey: CFStringRef; external name '_kCFSocketErrorKey'; (* attribute const *)
  253. var kCFSocketRegisterCommand: CFStringRef; external name '_kCFSocketRegisterCommand'; (* attribute const *)
  254. var kCFSocketRetrieveCommand: CFStringRef; external name '_kCFSocketRetrieveCommand'; (* attribute const *)
  255. end.