]> git.saurik.com Git - apple/cf.git/blob - CFStreamAbstract.h
CF-476.17.tar.gz
[apple/cf.git] / CFStreamAbstract.h
1 /*
2 * Copyright (c) 2008 Apple Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23 /* CFStreamAbstract.h
24 Copyright (c) 2000-2007, Apple Inc. All rights reserved.
25 */
26
27 #if !defined(__COREFOUNDATION_CFSTREAMABSTRACT__)
28 #define __COREFOUNDATION_CFSTREAMABSTRACT__ 1
29
30 #include <CoreFoundation/CFStream.h>
31
32 CF_EXTERN_C_BEGIN
33
34 /* During a stream's lifetime, the open callback will be called once, followed by any number of openCompleted calls (until openCompleted returns TRUE). Then any number of read/canRead or write/canWrite calls, then a single close call. copyProperty can be called at any time. prepareAsynch will be called exactly once when the stream's client is first configured.
35
36 Expected semantics:
37 - open reserves any system resources that are needed. The stream may start the process of opening, returning TRUE immediately and setting openComplete to FALSE. When the open completes, _CFStreamSignalEvent should be called passing kCFStreamOpenCompletedEvent. openComplete should be set to TRUE only if the open operation completed in its entirety.
38 - openCompleted will only be called after open has been called, but before any kCFStreamOpenCompletedEvent has been received. Return TRUE, setting error.code to 0, if the open operation has completed. Return TRUE, setting error to the correct error code and domain if the open operation completed, but failed. Return FALSE if the open operation is still in-progress. If your open ever fails to complete (i.e. sets openComplete to FALSE), you must be implement the openCompleted callback.
39 - read should read into the given buffer, returning the number of bytes successfully read. read must block until at least one byte is available, but should not block until the entire buffer is filled; zero should only be returned if end-of-stream is encountered. atEOF should be set to true if the EOF is encountered, false otherwise. error.code should be set to zero if no error occurs; otherwise, error should be set to the appropriate values.
40 - getBuffer is an optimization to return an internal buffer of bytes read from the stream, and may return NULL. getBuffer itself may be NULL if the concrete implementation does not wish to provide an internal buffer. If implemented, it should set numBytesRead to the number of bytes available in the internal buffer (but should not exceed maxBytesToRead) and return a pointer to the base of the bytes.
41 - canRead will only be called once openCompleted reports that the stream has been successfully opened (or the initial open call succeeded). It should return whether there are bytes that can be read without blocking.
42 - write should write the bytes in the given buffer to the device, returning the number of bytes successfully written. write must block until at least one byte is written. error.code should be set to zero if no error occurs; otherwise, error should be set to the appropriate values.
43 - close should close the device, releasing any reserved system resources. close cannot fail (it may be called to abort the stream), and may be called at any time after open has been called. It will only be called once.
44 - copyProperty should return the value for the given property, or NULL if none exists. Composite streams (streams built on top of other streams) should take care to call CFStreamCopyProperty on the base stream if they do not recognize the property given, to give the underlying stream a chance to respond.
45
46 In all cases, errors returned by reference will be initialized to NULL by the caller, and if they are set to non-NULL, will
47 be released by the caller
48 */
49
50 typedef struct {
51 CFIndex version; /* == 2 */
52
53 void *(*create)(CFReadStreamRef stream, void *info);
54 void (*finalize)(CFReadStreamRef stream, void *info);
55 CFStringRef (*copyDescription)(CFReadStreamRef stream, void *info);
56
57 Boolean (*open)(CFReadStreamRef stream, CFErrorRef *error, Boolean *openComplete, void *info);
58 Boolean (*openCompleted)(CFReadStreamRef stream, CFErrorRef *error, void *info);
59 CFIndex (*read)(CFReadStreamRef stream, UInt8 *buffer, CFIndex bufferLength, CFErrorRef *error, Boolean *atEOF, void *info);
60 const UInt8 *(*getBuffer)(CFReadStreamRef stream, CFIndex maxBytesToRead, CFIndex *numBytesRead, CFErrorRef *error, Boolean *atEOF, void *info);
61 Boolean (*canRead)(CFReadStreamRef stream, CFErrorRef *error, void *info);
62 void (*close)(CFReadStreamRef stream, void *info);
63
64 CFTypeRef (*copyProperty)(CFReadStreamRef stream, CFStringRef propertyName, void *info);
65 Boolean (*setProperty)(CFReadStreamRef stream, CFStringRef propertyName, CFTypeRef propertyValue, void *info);
66
67 void (*requestEvents)(CFReadStreamRef stream, CFOptionFlags streamEvents, void *info);
68 void (*schedule)(CFReadStreamRef stream, CFRunLoopRef runLoop, CFStringRef runLoopMode, void *info);
69 void (*unschedule)(CFReadStreamRef stream, CFRunLoopRef runLoop, CFStringRef runLoopMode, void *info);
70 } CFReadStreamCallBacks;
71
72 typedef struct {
73 CFIndex version; /* == 2 */
74
75 void *(*create)(CFWriteStreamRef stream, void *info);
76 void (*finalize)(CFWriteStreamRef stream, void *info);
77 CFStringRef (*copyDescription)(CFWriteStreamRef stream, void *info);
78
79 Boolean (*open)(CFWriteStreamRef stream, CFErrorRef *error, Boolean *openComplete, void *info);
80 Boolean (*openCompleted)(CFWriteStreamRef stream, CFErrorRef *error, void *info);
81 CFIndex (*write)(CFWriteStreamRef stream, const UInt8 *buffer, CFIndex bufferLength, CFErrorRef *error, void *info);
82 Boolean (*canWrite)(CFWriteStreamRef stream, CFErrorRef *error, void *info);
83 void (*close)(CFWriteStreamRef stream, void *info);
84
85 CFTypeRef (*copyProperty)(CFWriteStreamRef stream, CFStringRef propertyName, void *info);
86 Boolean (*setProperty)(CFWriteStreamRef stream, CFStringRef propertyName, CFTypeRef propertyValue, void *info);
87
88 void (*requestEvents)(CFWriteStreamRef stream, CFOptionFlags streamEvents, void *info);
89 void (*schedule)(CFWriteStreamRef stream, CFRunLoopRef runLoop, CFStringRef runLoopMode, void *info);
90 void (*unschedule)(CFWriteStreamRef stream, CFRunLoopRef runLoop, CFStringRef runLoopMode, void *info);
91 } CFWriteStreamCallBacks;
92
93 // Primitive creation mechanisms.
94 CF_EXPORT
95 CFReadStreamRef CFReadStreamCreate(CFAllocatorRef alloc, const CFReadStreamCallBacks *callbacks, void *info);
96 CF_EXPORT
97 CFWriteStreamRef CFWriteStreamCreate(CFAllocatorRef alloc, const CFWriteStreamCallBacks *callbacks, void *info);
98
99 /* All the functions below can only be called when you are sure the stream in question was created via
100 CFReadStreamCreate() or CFWriteStreamCreate(), above. They are NOT safe for toll-free bridged objects,
101 so the caller must be sure the argument passed is not such an object. */
102
103 // To be called by the concrete stream implementation (the callbacks) when an event occurs. error may be NULL if event != kCFStreamEventErrorOccurred
104 // error should be a CFErrorRef if the callbacks are version 2 or later; otherwise it should be a (CFStreamError *).
105 CF_EXPORT
106 void CFReadStreamSignalEvent(CFReadStreamRef stream, CFStreamEventType event, const void *error);
107 CF_EXPORT
108 void CFWriteStreamSignalEvent(CFWriteStreamRef stream, CFStreamEventType event, const void *error);
109
110 // These require that the stream allow the run loop to run once before delivering the event to its client.
111 // See the comment above CFRead/WriteStreamSignalEvent for interpretation of the error argument.
112 CF_EXPORT
113 void _CFReadStreamSignalEventDelayed(CFReadStreamRef stream, CFStreamEventType event, const void *error);
114 CF_EXPORT
115 void _CFWriteStreamSignalEventDelayed(CFWriteStreamRef stream, CFStreamEventType event, const void *error);
116
117 CF_EXPORT
118 void _CFReadStreamClearEvent(CFReadStreamRef stream, CFStreamEventType event);
119 // Write variant not currently needed
120 //CF_EXPORT
121 //void _CFWriteStreamClearEvent(CFWriteStreamRef stream, CFStreamEventType event);
122
123 // Convenience for concrete implementations to extract the info pointer given the stream.
124 CF_EXPORT
125 void *CFReadStreamGetInfoPointer(CFReadStreamRef stream);
126 CF_EXPORT
127 void *CFWriteStreamGetInfoPointer(CFWriteStreamRef stream);
128
129 // Returns the client info pointer currently set on the stream. These should probably be made public one day.
130 CF_EXPORT
131 void *_CFReadStreamGetClient(CFReadStreamRef readStream);
132 CF_EXPORT
133 void *_CFWriteStreamGetClient(CFWriteStreamRef writeStream);
134
135 // Returns an array of the runloops and modes on which the stream is currently scheduled
136 CF_EXPORT
137 CFArrayRef _CFReadStreamGetRunLoopsAndModes(CFReadStreamRef readStream);
138 CF_EXPORT
139 CFArrayRef _CFWriteStreamGetRunLoopsAndModes(CFWriteStreamRef writeStream);
140
141 /* Deprecated versions; here for backwards compatibility. */
142 typedef struct {
143 CFIndex version; /* == 1 */
144 void *(*create)(CFReadStreamRef stream, void *info);
145 void (*finalize)(CFReadStreamRef stream, void *info);
146 CFStringRef (*copyDescription)(CFReadStreamRef stream, void *info);
147 Boolean (*open)(CFReadStreamRef stream, CFStreamError *error, Boolean *openComplete, void *info);
148 Boolean (*openCompleted)(CFReadStreamRef stream, CFStreamError *error, void *info);
149 CFIndex (*read)(CFReadStreamRef stream, UInt8 *buffer, CFIndex bufferLength, CFStreamError *error, Boolean *atEOF, void *info);
150 const UInt8 *(*getBuffer)(CFReadStreamRef stream, CFIndex maxBytesToRead, CFIndex *numBytesRead, CFStreamError *error, Boolean *atEOF, void *info);
151 Boolean (*canRead)(CFReadStreamRef stream, void *info);
152 void (*close)(CFReadStreamRef stream, void *info);
153 CFTypeRef (*copyProperty)(CFReadStreamRef stream, CFStringRef propertyName, void *info);
154 Boolean (*setProperty)(CFReadStreamRef stream, CFStringRef propertyName, CFTypeRef propertyValue, void *info);
155 void (*requestEvents)(CFReadStreamRef stream, CFOptionFlags streamEvents, void *info);
156 void (*schedule)(CFReadStreamRef stream, CFRunLoopRef runLoop, CFStringRef runLoopMode, void *info);
157 void (*unschedule)(CFReadStreamRef stream, CFRunLoopRef runLoop, CFStringRef runLoopMode, void *info);
158 } CFReadStreamCallBacksV1;
159
160 typedef struct {
161 CFIndex version; /* == 1 */
162 void *(*create)(CFWriteStreamRef stream, void *info);
163 void (*finalize)(CFWriteStreamRef stream, void *info);
164 CFStringRef (*copyDescription)(CFWriteStreamRef stream, void *info);
165 Boolean (*open)(CFWriteStreamRef stream, CFStreamError *error, Boolean *openComplete, void *info);
166 Boolean (*openCompleted)(CFWriteStreamRef stream, CFStreamError *error, void *info);
167 CFIndex (*write)(CFWriteStreamRef stream, const UInt8 *buffer, CFIndex bufferLength, CFStreamError *error, void *info);
168 Boolean (*canWrite)(CFWriteStreamRef stream, void *info);
169 void (*close)(CFWriteStreamRef stream, void *info);
170 CFTypeRef (*copyProperty)(CFWriteStreamRef stream, CFStringRef propertyName, void *info);
171 Boolean (*setProperty)(CFWriteStreamRef stream, CFStringRef propertyName, CFTypeRef propertyValue, void *info);
172 void (*requestEvents)(CFWriteStreamRef stream, CFOptionFlags streamEvents, void *info);
173 void (*schedule)(CFWriteStreamRef stream, CFRunLoopRef runLoop, CFStringRef runLoopMode, void *info);
174 void (*unschedule)(CFWriteStreamRef stream, CFRunLoopRef runLoop, CFStringRef runLoopMode, void *info);
175 } CFWriteStreamCallBacksV1;
176
177 typedef struct {
178 CFIndex version; /* == 0 */
179 Boolean (*open)(CFReadStreamRef stream, CFStreamError *error, Boolean *openComplete, void *info);
180 Boolean (*openCompleted)(CFReadStreamRef stream, CFStreamError *error, void *info);
181 CFIndex (*read)(CFReadStreamRef stream, UInt8 *buffer, CFIndex bufferLength, CFStreamError *error, Boolean *atEOF, void *info);
182 const UInt8 *(*getBuffer)(CFReadStreamRef stream, CFIndex maxBytesToRead, CFIndex *numBytesRead, CFStreamError *error, Boolean *atEOF, void *info);
183 Boolean (*canRead)(CFReadStreamRef stream, void *info);
184 void (*close)(CFReadStreamRef stream, void *info);
185 CFTypeRef (*copyProperty)(CFReadStreamRef stream, CFStringRef propertyName, void *info);
186 void (*schedule)(CFReadStreamRef stream, CFRunLoopRef runLoop, CFStringRef runLoopMode, void *info);
187 void (*unschedule)(CFReadStreamRef stream, CFRunLoopRef runLoop, CFStringRef runLoopMode, void *info);
188 } CFReadStreamCallBacksV0;
189
190 typedef struct {
191 CFIndex version; /* == 0 */
192 Boolean (*open)(CFWriteStreamRef stream, CFStreamError *error, Boolean *openComplete, void *info);
193 Boolean (*openCompleted)(CFWriteStreamRef stream, CFStreamError *error, void *info);
194 CFIndex (*write)(CFWriteStreamRef stream, const UInt8 *buffer, CFIndex bufferLength, CFStreamError *error, void *info);
195 Boolean (*canWrite)(CFWriteStreamRef stream, void *info);
196 void (*close)(CFWriteStreamRef stream, void *info);
197 CFTypeRef (*copyProperty)(CFWriteStreamRef stream, CFStringRef propertyName, void *info);
198 void (*schedule)(CFWriteStreamRef stream, CFRunLoopRef runLoop, CFStringRef runLoopMode, void *info);
199 void (*unschedule)(CFWriteStreamRef stream, CFRunLoopRef runLoop, CFStringRef runLoopMode, void *info);
200 } CFWriteStreamCallBacksV0;
201
202 CF_EXTERN_C_END
203
204 #endif /* ! __COREFOUNDATION_CFSTREAMABSTRACT__ */