]> git.saurik.com Git - apple/cf.git/blob - Stream.subproj/CFStreamAbstract.h
CF-368.28.tar.gz
[apple/cf.git] / Stream.subproj / CFStreamAbstract.h
1 /*
2 * Copyright (c) 2005 Apple Computer, 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-2005, 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 #if defined(__cplusplus)
33 extern "C" {
34 #endif
35
36 typedef struct {
37 CFIndex version; /* == 1 */
38 void *(*create)(CFReadStreamRef stream, void *info);
39 void (*finalize)(CFReadStreamRef stream, void *info);
40 CFStringRef (*copyDescription)(CFReadStreamRef stream, void *info);
41 Boolean (*open)(CFReadStreamRef stream, CFStreamError *error, Boolean *openComplete, void *info);
42 Boolean (*openCompleted)(CFReadStreamRef stream, CFStreamError *error, void *info);
43 CFIndex (*read)(CFReadStreamRef stream, UInt8 *buffer, CFIndex bufferLength, CFStreamError *error, Boolean *atEOF, void *info);
44 const UInt8 *(*getBuffer)(CFReadStreamRef stream, CFIndex maxBytesToRead, CFIndex *numBytesRead, CFStreamError *error, Boolean *atEOF, void *info);
45 Boolean (*canRead)(CFReadStreamRef stream, void *info);
46 void (*close)(CFReadStreamRef stream, void *info);
47 CFTypeRef (*copyProperty)(CFReadStreamRef stream, CFStringRef propertyName, void *info);
48 Boolean (*setProperty)(CFReadStreamRef stream, CFStringRef propertyName, CFTypeRef propertyValue, void *info);
49 void (*requestEvents)(CFReadStreamRef stream, CFOptionFlags streamEvents, void *info);
50 void (*schedule)(CFReadStreamRef stream, CFRunLoopRef runLoop, CFStringRef runLoopMode, void *info);
51 void (*unschedule)(CFReadStreamRef stream, CFRunLoopRef runLoop, CFStringRef runLoopMode, void *info);
52 } CFReadStreamCallBacks;
53
54 typedef struct {
55 CFIndex version; /* == 1 */
56
57 void *(*create)(CFWriteStreamRef stream, void *info);
58 void (*finalize)(CFWriteStreamRef stream, void *info);
59 CFStringRef (*copyDescription)(CFWriteStreamRef stream, void *info);
60
61 Boolean (*open)(CFWriteStreamRef stream, CFStreamError *error, Boolean *openComplete, void *info);
62 Boolean (*openCompleted)(CFWriteStreamRef stream, CFStreamError *error, void *info);
63 CFIndex (*write)(CFWriteStreamRef stream, const UInt8 *buffer, CFIndex bufferLength, CFStreamError *error, void *info);
64 Boolean (*canWrite)(CFWriteStreamRef stream, void *info);
65 void (*close)(CFWriteStreamRef stream, void *info);
66 CFTypeRef (*copyProperty)(CFWriteStreamRef stream, CFStringRef propertyName, void *info);
67 Boolean (*setProperty)(CFWriteStreamRef stream, CFStringRef propertyName, CFTypeRef propertyValue, void *info);
68 void (*requestEvents)(CFWriteStreamRef stream, CFOptionFlags streamEvents, void *info);
69 void (*schedule)(CFWriteStreamRef stream, CFRunLoopRef runLoop, CFStringRef runLoopMode, void *info);
70 void (*unschedule)(CFWriteStreamRef stream, CFRunLoopRef runLoop, CFStringRef runLoopMode, void *info);
71 } CFWriteStreamCallBacks;
72
73 /* 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.
74
75 Expected semantics:
76 - 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.
77 - 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.
78 - 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.
79 - 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.
80 - 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.
81 - 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.
82 - 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.
83 - 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.
84 */
85
86 // Primitive creation mechanisms.
87 CF_EXPORT
88 CFReadStreamRef CFReadStreamCreate(CFAllocatorRef alloc, const CFReadStreamCallBacks *callbacks, void *info);
89 CF_EXPORT
90 CFWriteStreamRef CFWriteStreamCreate(CFAllocatorRef alloc, const CFWriteStreamCallBacks *callbacks, void *info);
91
92 /* All the functions below can only be called when you are sure the stream in question was created via
93 CFReadStreamCreate() or CFWriteStreamCreate(), above. They are NOT safe for toll-free bridged objects,
94 so the caller must be sure the argument passed is not such an object. */
95
96 // To be called by the concrete stream implementation (the callbacks) when an event occurs. error may be NULL if event != kCFStreamEventErrorOccurred
97 CF_EXPORT
98 void CFReadStreamSignalEvent(CFReadStreamRef stream, CFStreamEventType event, CFStreamError *error);
99 CF_EXPORT
100 void CFWriteStreamSignalEvent(CFWriteStreamRef stream, CFStreamEventType event, CFStreamError *error);
101 CF_EXPORT
102
103 // These require that the stream allow the run loop to run once before delivering the event to its client.
104 void _CFReadStreamSignalEventDelayed(CFReadStreamRef stream, CFStreamEventType event, CFStreamError *error);
105 CF_EXPORT
106 void _CFWriteStreamSignalEventDelayed(CFWriteStreamRef stream, CFStreamEventType event, CFStreamError *error);
107
108 // Convenience for concrete implementations to extract the info pointer given the stream.
109 CF_EXPORT
110 void *CFReadStreamGetInfoPointer(CFReadStreamRef stream);
111 CF_EXPORT
112 void *CFWriteStreamGetInfoPointer(CFWriteStreamRef stream);
113
114 // Returns the client info pointer currently set on the stream. These should probably be made public one day.
115 CF_EXPORT
116 void *_CFReadStreamGetClient(CFReadStreamRef readStream);
117 CF_EXPORT
118 void *_CFWriteStreamGetClient(CFWriteStreamRef writeStream);
119
120 // Returns an array of the runloops and modes on which the stream is currently scheduled
121 CF_EXPORT
122 CFArrayRef _CFReadStreamGetRunLoopsAndModes(CFReadStreamRef readStream);
123 CF_EXPORT
124 CFArrayRef _CFWriteStreamGetRunLoopsAndModes(CFWriteStreamRef writeStream);
125
126 /* Deprecated; here for backwards compatibility. Will be removed by Jaguar6D. */
127 typedef struct {
128 CFIndex version; /* == 0 */
129 Boolean (*open)(CFReadStreamRef stream, CFStreamError *error, Boolean *openComplete, void *info);
130 Boolean (*openCompleted)(CFReadStreamRef stream, CFStreamError *error, void *info);
131 CFIndex (*read)(CFReadStreamRef stream, UInt8 *buffer, CFIndex bufferLength, CFStreamError *error, Boolean *atEOF, void *info);
132 const UInt8 *(*getBuffer)(CFReadStreamRef stream, CFIndex maxBytesToRead, CFIndex *numBytesRead, CFStreamError *error, Boolean *atEOF, void *info);
133 Boolean (*canRead)(CFReadStreamRef stream, void *info);
134 void (*close)(CFReadStreamRef stream, void *info);
135 CFTypeRef (*copyProperty)(CFReadStreamRef stream, CFStringRef propertyName, void *info);
136 void (*schedule)(CFReadStreamRef stream, CFRunLoopRef runLoop, CFStringRef runLoopMode, void *info);
137 void (*unschedule)(CFReadStreamRef stream, CFRunLoopRef runLoop, CFStringRef runLoopMode, void *info);
138 } CFReadStreamCallBacksV0;
139
140 typedef struct {
141 CFIndex version; /* == 0 */
142 Boolean (*open)(CFWriteStreamRef stream, CFStreamError *error, Boolean *openComplete, void *info);
143 Boolean (*openCompleted)(CFWriteStreamRef stream, CFStreamError *error, void *info);
144 CFIndex (*write)(CFWriteStreamRef stream, const UInt8 *buffer, CFIndex bufferLength, CFStreamError *error, void *info);
145 Boolean (*canWrite)(CFWriteStreamRef stream, void *info);
146 void (*close)(CFWriteStreamRef stream, void *info);
147 CFTypeRef (*copyProperty)(CFWriteStreamRef stream, CFStringRef propertyName, void *info);
148 void (*schedule)(CFWriteStreamRef stream, CFRunLoopRef runLoop, CFStringRef runLoopMode, void *info);
149 void (*unschedule)(CFWriteStreamRef stream, CFRunLoopRef runLoop, CFStringRef runLoopMode, void *info);
150 } CFWriteStreamCallBacksV0;
151
152 #if defined(__cplusplus)
153 }
154 #endif
155
156 #endif /* ! __COREFOUNDATION_CFSTREAMABSTRACT__ */