CFStream.pas 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. { CFStream.h
  2. Copyright (c) 2000-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]>, November 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 CFStream;
  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,CFString,CFDictionary,CFURL,CFRunLoop,CFSocket;
  87. {$ALIGN POWER}
  88. type
  89. CFStreamStatus = SInt32;
  90. const
  91. kCFStreamStatusNotOpen = 0;
  92. kCFStreamStatusOpening = 1; { open is in-progress }
  93. kCFStreamStatusOpen = 2;
  94. kCFStreamStatusReading = 3;
  95. kCFStreamStatusWriting = 4;
  96. kCFStreamStatusAtEnd = 5; { no further bytes can be read/written }
  97. kCFStreamStatusClosed = 6;
  98. kCFStreamStatusError = 7;
  99. type
  100. CFStreamErrorDomain = SInt32;
  101. const
  102. kCFStreamErrorDomainCustom = -1; { custom to the kind of stream in question }
  103. kCFStreamErrorDomainPOSIX = 1; { POSIX errno; interpret using <sys/errno.h> }
  104. kCFStreamErrorDomainMacOSStatus = 2; { OSStatus type from Carbon APIs; interpret using <MacTypes.h> }
  105. type
  106. CFStreamError = record
  107. domain: CFStreamErrorDomain;
  108. error: SInt32;
  109. end;
  110. CFStreamErrorPtr = ^CFStreamError;
  111. type
  112. CFStreamEventType = SInt32;
  113. const
  114. kCFStreamEventNone = 0;
  115. kCFStreamEventOpenCompleted = 1;
  116. kCFStreamEventHasBytesAvailable = 2;
  117. kCFStreamEventCanAcceptBytes = 4;
  118. kCFStreamEventErrorOccurred = 8;
  119. kCFStreamEventEndEncountered = 16;
  120. type
  121. CFStreamClientContext = record
  122. version: CFIndex;
  123. info: UnivPtr;
  124. retain: function( info: {const} UnivPtr ): UnivPtr;
  125. release: procedure( info: {const} UnivPtr );
  126. copyDescription: function( info: {const} UnivPtr ): CFStringRef;
  127. end;
  128. type
  129. CFReadStreamRef = ^SInt32; { an opaque 32-bit type }
  130. CFReadStreamRefPtr = ^CFReadStreamRef;
  131. CFWriteStreamRef = ^SInt32; { an opaque 32-bit type }
  132. CFWriteStreamRefPtr = ^CFWriteStreamRef;
  133. type
  134. CFReadStreamClientCallBack = procedure( stream: CFReadStreamRef; typ: CFStreamEventType; clientCallBackInfo: UnivPtr );
  135. CFWriteStreamClientCallBack = procedure( stream: CFWriteStreamRef; typ: CFStreamEventType; clientCallBackInfo: UnivPtr );
  136. function CFReadStreamGetTypeID: CFTypeID; external name '_CFReadStreamGetTypeID';
  137. function CFWriteStreamGetTypeID: CFTypeID; external name '_CFWriteStreamGetTypeID';
  138. { Memory streams }
  139. { Value will be a CFData containing all bytes thusfar written; used to recover the data written to a memory write stream. }
  140. var kCFStreamPropertyDataWritten: CFStringRef; external name '_kCFStreamPropertyDataWritten'; (* attribute const *)
  141. { Pass kCFAllocatorNull for bytesDeallocator to prevent CFReadStream from deallocating bytes; otherwise, CFReadStream will deallocate bytes when the stream is destroyed }
  142. function CFReadStreamCreateWithBytesNoCopy( alloc: CFAllocatorRef; bytes: UnivPtr; length: CFIndex; bytesDeallocator: CFAllocatorRef ): CFReadStreamRef; external name '_CFReadStreamCreateWithBytesNoCopy';
  143. { The stream writes into the buffer given; when bufferCapacity is exhausted, the stream is exhausted (status becomes kCFStreamStatusAtEnd) }
  144. function CFWriteStreamCreateWithBuffer( alloc: CFAllocatorRef; buffer: UnivPtr; bufferCapacity: CFIndex ): CFWriteStreamRef; external name '_CFWriteStreamCreateWithBuffer';
  145. { New buffers are allocated from bufferAllocator as bytes are written to the stream. At any point, you can recover the bytes thusfar written by asking for the property kCFStreamPropertyDataWritten, above }
  146. function CFWriteStreamCreateWithAllocatedBuffers( alloc: CFAllocatorRef; bufferAllocator: CFAllocatorRef ): CFWriteStreamRef; external name '_CFWriteStreamCreateWithAllocatedBuffers';
  147. { File streams }
  148. function CFReadStreamCreateWithFile( alloc: CFAllocatorRef; fileURL: CFURLRef ): CFReadStreamRef; external name '_CFReadStreamCreateWithFile';
  149. function CFWriteStreamCreateWithFile( alloc: CFAllocatorRef; fileURL: CFURLRef ): CFWriteStreamRef; external name '_CFWriteStreamCreateWithFile';
  150. {#if MAC_OS_X_VERSION_10_2 <= MAC_OS_X_VERSION_MAX_ALLOWED}
  151. { Property for file write streams; value should be a CFBoolean. Set to TRUE to append to a file, rather than to replace its contents }
  152. var kCFStreamPropertyAppendToFile: CFStringRef; external name '_kCFStreamPropertyAppendToFile'; (* attribute const *)
  153. {#endif}
  154. var kCFStreamPropertyFileCurrentOffset: CFStringRef; external name '_kCFStreamPropertyFileCurrentOffset'; (* attribute const *)
  155. (* AVAILABLE_MAC_OS_X_VERSION_10_3_AND_LATER *)
  156. { Socket stream properties }
  157. { Value will be a CFData containing the native handle }
  158. var kCFStreamPropertySocketNativeHandle: CFStringRef; external name '_kCFStreamPropertySocketNativeHandle'; (* attribute const *)
  159. { Value will be a CFString, or NULL if unknown }
  160. var kCFStreamPropertySocketRemoteHostName: CFStringRef; external name '_kCFStreamPropertySocketRemoteHostName'; (* attribute const *)
  161. { Value will be a CFNumber, or NULL if unknown }
  162. var kCFStreamPropertySocketRemotePortNumber: CFStringRef; external name '_kCFStreamPropertySocketRemotePortNumber'; (* attribute const *)
  163. { Socket streams; the returned streams are paired such that they use the same socket; pass NULL if you want only the read stream or the write stream }
  164. procedure CFStreamCreatePairWithSocket( alloc: CFAllocatorRef; sock: CFSocketNativeHandle; var readStream: CFReadStreamRef; var writeStream: CFWriteStreamRef ); external name '_CFStreamCreatePairWithSocket';
  165. procedure CFStreamCreatePairWithSocketToHost( alloc: CFAllocatorRef; host: CFStringRef; port: UInt32; var readStream: CFReadStreamRef; var writeStream: CFWriteStreamRef ); external name '_CFStreamCreatePairWithSocketToHost';
  166. {#if MAC_OS_X_VERSION_10_2 <= MAC_OS_X_VERSION_MAX_ALLOWED}
  167. procedure CFStreamCreatePairWithPeerSocketSignature( alloc: CFAllocatorRef; const (*var*) signature: CFSocketSignature; var readStream: CFReadStreamRef; var writeStream: CFWriteStreamRef ); external name '_CFStreamCreatePairWithPeerSocketSignature';
  168. {#endif}
  169. { Returns the current state of the stream }
  170. function CFReadStreamGetStatus( stream: CFReadStreamRef ): CFStreamStatus; external name '_CFReadStreamGetStatus';
  171. function CFWriteStreamGetStatus( stream: CFWriteStreamRef ): CFStreamStatus; external name '_CFWriteStreamGetStatus';
  172. { 0 is returned if no error has occurred. errorDomain specifies the domain
  173. in which the error code should be interpretted; pass NULL if you are not
  174. interested. }
  175. function CFReadStreamGetError( stream: CFReadStreamRef ): CFStreamError; external name '_CFReadStreamGetError';
  176. function CFWriteStreamGetError( stream: CFWriteStreamRef ): CFStreamError; external name '_CFWriteStreamGetError';
  177. { Returns success/failure. Opening a stream causes it to reserve all the system
  178. resources it requires. If the stream can open non-blocking, this will always
  179. return TRUE; listen to the run loop source to find out when the open completes
  180. and whether it was successful, or poll using CFRead/WriteStreamGetStatus(), waiting
  181. for a status of kCFStreamStatusOpen or kCFStreamStatusError. }
  182. function CFReadStreamOpen( stream: CFReadStreamRef ): Boolean; external name '_CFReadStreamOpen';
  183. function CFWriteStreamOpen( stream: CFWriteStreamRef ): Boolean; external name '_CFWriteStreamOpen';
  184. { Terminates the flow of bytes; releases any system resources required by the
  185. stream. The stream may not fail to close. You may call CFStreamClose() to
  186. effectively abort a stream. }
  187. procedure CFReadStreamClose( stream: CFReadStreamRef ); external name '_CFReadStreamClose';
  188. procedure CFWriteStreamClose( stream: CFWriteStreamRef ); external name '_CFWriteStreamClose';
  189. { Whether there is data currently available for reading; returns TRUE if it's
  190. impossible to tell without trying }
  191. function CFReadStreamHasBytesAvailable( stream: CFReadStreamRef ): Boolean; external name '_CFReadStreamHasBytesAvailable';
  192. { Returns the number of bytes read, or -1 if an error occurs preventing any
  193. bytes from being read, or 0 if the stream's end was encountered.
  194. It is an error to try and read from a stream that hasn't been opened first.
  195. This call will block until at least one byte is available; it will NOT block
  196. until the entire buffer can be filled. To avoid blocking, either poll using
  197. CFReadStreamHasBytesAvailable() or use the run loop and listen for the
  198. kCFStreamCanRead event for notification of data available. }
  199. function CFReadStreamRead( stream: CFReadStreamRef; buffer: UnivPtr; bufferLength: CFIndex ): CFIndex; external name '_CFReadStreamRead';
  200. { Returns a pointer to an internal buffer if possible (setting *numBytesRead
  201. to the length of the returned buffer), otherwise returns NULL; guaranteed
  202. to return in O(1). Bytes returned in the buffer are considered read from
  203. the stream; if maxBytesToRead is greater than 0, not more than maxBytesToRead
  204. will be returned. If maxBytesToRead is less than or equal to zero, as many bytes
  205. as are readily available will be returned. The returned buffer is good only
  206. until the next stream operation called on the stream. Caller should neither
  207. change the contents of the returned buffer nor attempt to deallocate the buffer;
  208. it is still owned by the stream. }
  209. function CFReadStreamGetBuffer( stream: CFReadStreamRef; maxBytesToRead: CFIndex; var numBytesRead: CFIndex ): UInt8Ptr; external name '_CFReadStreamGetBuffer';
  210. { Whether the stream can currently be written to without blocking;
  211. returns TRUE if it's impossible to tell without trying }
  212. function CFWriteStreamCanAcceptBytes( stream: CFWriteStreamRef ): Boolean; external name '_CFWriteStreamCanAcceptBytes';
  213. { Returns the number of bytes successfully written, -1 if an error has
  214. occurred, or 0 if the stream has been filled to capacity (for fixed-length
  215. streams). If the stream is not full, this call will block until at least
  216. one byte is written. To avoid blocking, either poll via CFWriteStreamCanAcceptBytes
  217. or use the run loop and listen for the kCFStreamCanWrite event. }
  218. function CFWriteStreamWrite( stream: CFWriteStreamRef; buffer: UnivPtr; bufferLength: CFIndex ): CFIndex; external name '_CFWriteStreamWrite';
  219. { Particular streams can name properties and assign meanings to them; you
  220. access these properties through the following calls. A property is any interesting
  221. information about the stream other than the data being transmitted itself.
  222. Examples include the headers from an HTTP transmission, or the expected
  223. number of bytes, or permission information, etc. Properties that can be set
  224. configure the behavior of the stream, and may only be settable at particular times
  225. (like before the stream has been opened). See the documentation for particular
  226. properties to determine their get- and set-ability. }
  227. function CFReadStreamCopyProperty( stream: CFReadStreamRef; propertyName: CFStringRef ): CFTypeRef; external name '_CFReadStreamCopyProperty';
  228. function CFWriteStreamCopyProperty( stream: CFWriteStreamRef; propertyName: CFStringRef ): CFTypeRef; external name '_CFWriteStreamCopyProperty';
  229. {#if MAC_OS_X_VERSION_10_2 <= MAC_OS_X_VERSION_MAX_ALLOWED}
  230. { Returns TRUE if the stream recognizes and accepts the given property-value pair;
  231. FALSE otherwise. }
  232. function CFReadStreamSetProperty( stream: CFReadStreamRef; propertyName: CFStringRef; propertyValue: CFTypeRef ): Boolean; external name '_CFReadStreamSetProperty';
  233. function CFWriteStreamSetProperty( stream: CFWriteStreamRef; propertyName: CFStringRef; propertyValue: CFTypeRef ): Boolean; external name '_CFWriteStreamSetProperty';
  234. {#endif}
  235. { Asynchronous processing - If you wish to neither poll nor block, you may register
  236. a client to hear about interesting events that occur on a stream. Only one client
  237. per stream is allowed; registering a new client replaces the previous one.
  238. Once you have set a client, you need to schedule a run loop on which that client
  239. can be notified. You may schedule multiple run loops (for instance, if you are
  240. using a thread pool). The client callback will be triggered via one of the scheduled
  241. run loops; It is the caller's responsibility to ensure that at least one of the
  242. scheduled run loops is being run.
  243. NOTE: not all streams provide these notifications. If a stream does not support
  244. asynchronous notification, CFStreamSetClient() will return NO; typically, such
  245. streams will never block for device I/O (e.g. a stream on memory)
  246. }
  247. function CFReadStreamSetClient( stream: CFReadStreamRef; streamEvents: CFOptionFlags; clientCB: CFReadStreamClientCallBack; var clientContext: CFStreamClientContext ): Boolean; external name '_CFReadStreamSetClient';
  248. function CFWriteStreamSetClient( stream: CFWriteStreamRef; streamEvents: CFOptionFlags; clientCB: CFWriteStreamClientCallBack; var clientContext: CFStreamClientContext ): Boolean; external name '_CFWriteStreamSetClient';
  249. procedure CFReadStreamScheduleWithRunLoop( stream: CFReadStreamRef; runLoop: CFRunLoopRef; runLoopMode: CFStringRef ); external name '_CFReadStreamScheduleWithRunLoop';
  250. procedure CFWriteStreamScheduleWithRunLoop( stream: CFWriteStreamRef; runLoop: CFRunLoopRef; runLoopMode: CFStringRef ); external name '_CFWriteStreamScheduleWithRunLoop';
  251. procedure CFReadStreamUnscheduleFromRunLoop( stream: CFReadStreamRef; runLoop: CFRunLoopRef; runLoopMode: CFStringRef ); external name '_CFReadStreamUnscheduleFromRunLoop';
  252. procedure CFWriteStreamUnscheduleFromRunLoop( stream: CFWriteStreamRef; runLoop: CFRunLoopRef; runLoopMode: CFStringRef ); external name '_CFWriteStreamUnscheduleFromRunLoop';
  253. end.