core_foundation_sys/
stream.rs

1// Copyright 2023 The Servo Project Developers. See the COPYRIGHT
2// file at the top-level directory of this distribution.
3//
4// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7// option. This file may not be copied, modified, or distributed
8// except according to those terms.
9
10use std::os::raw::{c_int, c_void};
11
12use crate::base::{
13    Boolean, CFAllocatorRef, CFIndex, CFOptionFlags, CFTypeID, CFTypeRef, SInt32, UInt32, UInt8,
14};
15use crate::error::CFErrorRef;
16use crate::runloop::CFRunLoopRef;
17use crate::socket::{CFSocketNativeHandle, CFSocketSignature};
18use crate::string::CFStringRef;
19use crate::url::CFURLRef;
20
21#[repr(C)]
22pub struct __CFReadStream(c_void);
23
24#[repr(C)]
25pub struct __CFWriteStream(c_void);
26
27pub type CFReadStreamRef = *mut __CFReadStream;
28pub type CFWriteStreamRef = *mut __CFWriteStream;
29pub type CFStreamPropertyKey = CFStringRef;
30pub type CFStreamStatus = CFIndex;
31pub type CFStreamEventType = CFOptionFlags;
32pub type CFStreamErrorDomain = CFIndex;
33
34#[repr(C)]
35#[derive(Debug, Clone, Copy, PartialEq, Eq)]
36pub struct CFStreamError {
37    pub domain: CFIndex,
38    pub error: SInt32,
39}
40
41/* CFStreamStatus: Constants that describe the status of a stream */
42pub const kCFStreamStatusNotOpen: CFStreamStatus = 0;
43pub const kCFStreamStatusOpening: CFStreamStatus = 1;
44pub const kCFStreamStatusOpen: CFStreamStatus = 2;
45pub const kCFStreamStatusReading: CFStreamStatus = 3;
46pub const kCFStreamStatusWriting: CFStreamStatus = 4;
47pub const kCFStreamStatusAtEnd: CFStreamStatus = 5;
48pub const kCFStreamStatusClosed: CFStreamStatus = 6;
49pub const kCFStreamStatusError: CFStreamStatus = 7;
50
51// deprecated
52pub const kCFStreamErrorDomainCustom: CFStreamErrorDomain = -1;
53pub const kCFStreamErrorDomainPOSIX: CFStreamErrorDomain = 1;
54pub const kCFStreamErrorDomainMacOSStatus: CFStreamErrorDomain = 2;
55
56/* CFStreamEventType: Defines constants for stream-related events */
57pub const kCFStreamEventNone: CFStreamEventType = 0;
58pub const kCFStreamEventOpenCompleted: CFStreamEventType = 1;
59pub const kCFStreamEventHasBytesAvailable: CFStreamEventType = 2;
60pub const kCFStreamEventCanAcceptBytes: CFStreamEventType = 4;
61pub const kCFStreamEventErrorOccurred: CFStreamEventType = 8;
62pub const kCFStreamEventEndEncountered: CFStreamEventType = 16;
63
64#[repr(C)]
65#[derive(Debug, Clone, Copy)]
66pub struct CFStreamClientContext {
67    pub version: CFIndex,
68    pub info: *mut c_void,
69    pub retain: extern "C" fn(info: *const c_void) -> *const c_void,
70    pub release: extern "C" fn(info: *const c_void),
71    pub copyDescription: extern "C" fn(info: *const c_void) -> CFStringRef,
72}
73
74pub type CFReadStreamClientCallBack = extern "C" fn(
75    stream: CFReadStreamRef,
76    _type: CFStreamEventType,
77    clientCallBackInfo: *mut c_void,
78);
79pub type CFWriteStreamClientCallBack = extern "C" fn(
80    stream: CFWriteStreamRef,
81    _type: CFStreamEventType,
82    clientCallBackInfo: *mut c_void,
83);
84
85extern "C" {
86    /*
87     * CFStream.h
88     */
89
90    /* Stream Properties */
91    pub static kCFStreamPropertyAppendToFile: CFStreamPropertyKey;
92    pub static kCFStreamPropertyDataWritten: CFStreamPropertyKey;
93    pub static kCFStreamPropertyFileCurrentOffset: CFStreamPropertyKey;
94    pub static kCFStreamPropertySocketNativeHandle: CFStreamPropertyKey;
95    pub static kCFStreamPropertySocketRemoteHostName: CFStreamPropertyKey;
96    pub static kCFStreamPropertySocketRemotePortNumber: CFStreamPropertyKey;
97    pub static kCFStreamPropertyShouldCloseNativeSocket: CFStringRef;
98    pub static kCFStreamPropertySocketSecurityLevel: CFStringRef;
99
100    /* CFStream Socket Security Level Constants */
101    pub static kCFStreamSocketSecurityLevelNone: CFStringRef;
102    pub static kCFStreamSocketSecurityLevelSSLv2: CFStringRef;
103    pub static kCFStreamSocketSecurityLevelSSLv3: CFStringRef;
104    pub static kCFStreamSocketSecurityLevelTLSv1: CFStringRef;
105    pub static kCFStreamSocketSecurityLevelNegotiatedSSL: CFStringRef;
106
107    /* CFStream SOCKS Proxy Key Constants */
108    pub static kCFStreamPropertySOCKSProxy: CFStringRef;
109    pub static kCFStreamPropertySOCKSProxyHost: CFStringRef;
110    pub static kCFStreamPropertySOCKSProxyPort: CFStringRef;
111    pub static kCFStreamPropertySOCKSVersion: CFStringRef;
112    pub static kCFStreamSocketSOCKSVersion4: CFStringRef;
113    pub static kCFStreamSocketSOCKSVersion5: CFStringRef;
114    pub static kCFStreamPropertySOCKSUser: CFStringRef;
115    pub static kCFStreamPropertySOCKSPassword: CFStringRef;
116
117    /* CFStream Error Domain Constants (CFHost) */
118    pub static kCFStreamErrorDomainSOCKS: c_int;
119    pub static kCFStreamErrorDomainSSL: c_int;
120
121    /* CFStream: Creating Streams */
122    pub fn CFStreamCreatePairWithPeerSocketSignature(
123        alloc: CFAllocatorRef,
124        signature: *const CFSocketSignature,
125        readStream: *mut CFReadStreamRef,
126        writeStream: *mut CFWriteStreamRef,
127    ); // deprecated
128    pub fn CFStreamCreatePairWithSocketToHost(
129        alloc: CFAllocatorRef,
130        host: CFStringRef,
131        port: UInt32,
132        readStream: *mut CFReadStreamRef,
133        writeStream: *mut CFWriteStreamRef,
134    ); // deprecated
135    pub fn CFStreamCreatePairWithSocket(
136        alloc: CFAllocatorRef,
137        sock: CFSocketNativeHandle,
138        readStream: *mut CFReadStreamRef,
139        writeStream: *mut CFWriteStreamRef,
140    ); // deprecated
141    pub fn CFStreamCreateBoundPair(
142        alloc: CFAllocatorRef,
143        readStream: *mut CFReadStreamRef,
144        writeStream: *mut CFWriteStreamRef,
145        transferBufferSize: CFIndex,
146    );
147
148    //pub fn CFReadStreamSetDispatchQueue(stream: CFReadStreamRef, q: dispatch_queue_t); // macos(10.9)+
149    //pub fn CFWriteStreamSetDispatchQueue(stream: CFWriteStreamRef, q: dispatch_queue_t); // macos(10.9)+
150    //pub fn CFReadStreamCopyDispatchQueue(stream: CFReadStreamRef) -> dispatch_queue_t; // macos(10.9)+
151    //pub fn CFWriteStreamCopyDispatchQueue(stream: CFReadStreamRef) -> dispatch_queue_t; // macos(10.9)+
152
153    /* CFReadStream */
154    /* Creating a Read Stream */
155    pub fn CFReadStreamCreateWithBytesNoCopy(
156        alloc: CFAllocatorRef,
157        bytes: *const UInt8,
158        length: CFIndex,
159        bytesDeallocator: CFAllocatorRef,
160    ) -> CFReadStreamRef;
161    pub fn CFReadStreamCreateWithFile(alloc: CFAllocatorRef, fileURL: CFURLRef) -> CFReadStreamRef;
162
163    /* Opening and Closing a Read Stream */
164    pub fn CFReadStreamClose(stream: CFReadStreamRef);
165    pub fn CFReadStreamOpen(stream: CFReadStreamRef) -> Boolean;
166
167    /* Reading from a Stream */
168    pub fn CFReadStreamRead(
169        stream: CFReadStreamRef,
170        buffer: *mut UInt8,
171        bufferLength: CFIndex,
172    ) -> CFIndex;
173
174    /* Scheduling a Read Stream */
175    pub fn CFReadStreamScheduleWithRunLoop(
176        stream: CFReadStreamRef,
177        runLoop: CFRunLoopRef,
178        runLoopMode: CFStringRef,
179    );
180    pub fn CFReadStreamUnscheduleFromRunLoop(
181        stream: CFReadStreamRef,
182        runLoop: CFRunLoopRef,
183        runLoopMode: CFStringRef,
184    );
185
186    /* Examining Stream Properties */
187    pub fn CFReadStreamCopyProperty(
188        stream: CFReadStreamRef,
189        propertyName: CFStreamPropertyKey,
190    ) -> CFTypeRef;
191    pub fn CFReadStreamGetBuffer(
192        stream: CFReadStreamRef,
193        maxBytesToRead: CFIndex,
194        numBytesRead: *mut CFIndex,
195    ) -> *const UInt8;
196    pub fn CFReadStreamCopyError(stream: CFReadStreamRef) -> CFErrorRef;
197    pub fn CFReadStreamGetError(stream: CFReadStreamRef) -> CFStreamError; // deprecated
198    pub fn CFReadStreamGetStatus(stream: CFReadStreamRef) -> CFStreamStatus;
199    pub fn CFReadStreamHasBytesAvailable(stream: CFReadStreamRef) -> Boolean;
200
201    /* Setting Stream Properties */
202    pub fn CFReadStreamSetClient(
203        stream: CFReadStreamRef,
204        streamEvents: CFOptionFlags,
205        clientCB: CFReadStreamClientCallBack,
206        clientContext: *mut CFStreamClientContext,
207    ) -> Boolean;
208    pub fn CFReadStreamSetProperty(
209        stream: CFReadStreamRef,
210        propertyName: CFStreamPropertyKey,
211        propertyValue: CFTypeRef,
212    ) -> Boolean;
213
214    /* Getting the CFReadStream Type ID */
215    pub fn CFReadStreamGetTypeID() -> CFTypeID;
216
217    /* CFWriteStream */
218    /* Creating a Write Stream */
219    pub fn CFWriteStreamCreateWithAllocatedBuffers(
220        alloc: CFAllocatorRef,
221        bufferAllocator: CFAllocatorRef,
222    ) -> CFWriteStreamRef;
223    pub fn CFWriteStreamCreateWithBuffer(
224        alloc: CFAllocatorRef,
225        buffer: *mut UInt8,
226        bufferCapacity: CFIndex,
227    ) -> CFWriteStreamRef;
228    pub fn CFWriteStreamCreateWithFile(
229        alloc: CFAllocatorRef,
230        fileURL: CFURLRef,
231    ) -> CFWriteStreamRef;
232
233    /* Opening and Closing a Stream */
234    pub fn CFWriteStreamClose(stream: CFWriteStreamRef);
235    pub fn CFWriteStreamOpen(stream: CFWriteStreamRef) -> Boolean;
236
237    /* Writing to a Stream */
238    pub fn CFWriteStreamWrite(
239        stream: CFWriteStreamRef,
240        buffer: *const UInt8,
241        bufferLength: CFIndex,
242    ) -> CFIndex;
243
244    /* Scheduling a Write Stream */
245    pub fn CFWriteStreamScheduleWithRunLoop(
246        stream: CFWriteStreamRef,
247        runLoop: CFRunLoopRef,
248        runLoopMode: CFStringRef,
249    );
250    pub fn CFWriteStreamUnscheduleFromRunLoop(
251        stream: CFWriteStreamRef,
252        runLoop: CFRunLoopRef,
253        runLoopMode: CFStringRef,
254    );
255
256    /* Examining Stream Properties */
257    pub fn CFWriteStreamCanAcceptBytes(stream: CFWriteStreamRef) -> Boolean;
258    pub fn CFWriteStreamCopyProperty(
259        stream: CFWriteStreamRef,
260        propertyName: CFStreamPropertyKey,
261    ) -> CFTypeRef;
262    pub fn CFWriteStreamCopyError(stream: CFWriteStreamRef) -> CFErrorRef;
263    pub fn CFWriteStreamGetError(stream: CFWriteStreamRef) -> CFStreamError; // deprecated
264    pub fn CFWriteStreamGetStatus(stream: CFWriteStreamRef) -> CFStreamStatus;
265
266    /* Setting Stream Properties */
267    pub fn CFWriteStreamSetClient(
268        stream: CFWriteStreamRef,
269        streamEvents: CFOptionFlags,
270        clientCB: CFWriteStreamClientCallBack,
271        clientContext: *mut CFStreamClientContext,
272    ) -> Boolean;
273    pub fn CFWriteStreamSetProperty(
274        stream: CFWriteStreamRef,
275        propertyName: CFStreamPropertyKey,
276        propertyValue: CFTypeRef,
277    ) -> Boolean;
278
279    /* Getting the CFWriteStream Type ID */
280    pub fn CFWriteStreamGetTypeID() -> CFTypeID;
281}