Mercurial > projects > dstep
comparison dstep/foundation/NSStream.d @ 14:89f3c3ef1fd2
Added the Foundation framework
author | Jacob Carlborg <doob@me.com> |
---|---|
date | Mon, 03 Aug 2009 15:23:15 +0200 |
parents | |
children | 19885b43130e |
comparison
equal
deleted
inserted
replaced
13:4f583f7e242e | 14:89f3c3ef1fd2 |
---|---|
1 /** | |
2 * Copyright: Copyright (c) 2009 Jacob Carlborg. | |
3 * Authors: Jacob Carlborg | |
4 * Version: Initial created: Aug 3, 2009 | |
5 * License: $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost Software License 1.0) | |
6 */ | |
7 module dstep.foundation.NSStream; | |
8 | |
9 import dstep.foundation.NSData; | |
10 import dstep.foundation.NSDictionary; | |
11 import dstep.foundation.NSError; | |
12 import dstep.foundation.NSHost; | |
13 import dstep.foundation.NSInputStream; | |
14 import dstep.foundation.NSObject; | |
15 import dstep.foundation.NSOutputStream; | |
16 import dstep.foundation.NSRunLoop; | |
17 import dstep.foundation.NSString; | |
18 import dstep.objc.bridge.Bridge; | |
19 import dstep.objc.objc : id; | |
20 | |
21 import bindings = dstep.foundation.NSStream_bindings; | |
22 | |
23 alias NSUInteger NSStreamStatus; | |
24 alias NSUInteger NSStreamEvent; | |
25 | |
26 const NSString NSStreamSocketSecurityLevelKey; | |
27 const NSString NSStreamSocketSecurityLevelNone; | |
28 const NSString NSStreamSocketSecurityLevelSSLv2; | |
29 const NSString NSStreamSocketSecurityLevelSSLv3; | |
30 const NSString NSStreamSocketSecurityLevelTLSv1; | |
31 const NSString NSStreamSocketSecurityLevelNegotiatedSSL; | |
32 const NSString NSStreamSOCKSProxyConfigurationKey; | |
33 const NSString NSStreamSOCKSProxyHostKey; | |
34 const NSString NSStreamSOCKSProxyPortKey; | |
35 const NSString NSStreamSOCKSProxyVersionKey; | |
36 const NSString NSStreamSOCKSProxyUserKey; | |
37 const NSString NSStreamSOCKSProxyPasswordKey; | |
38 const NSString NSStreamSOCKSProxyVersion4; | |
39 const NSString NSStreamSOCKSProxyVersion5; | |
40 const NSString NSStreamDataWrittenToMemoryStreamKey; | |
41 const NSString NSStreamFileCurrentOffsetKey; | |
42 const NSString NSStreamSocketSSLErrorDomain; | |
43 const NSString NSStreamSOCKSErrorDomain; | |
44 | |
45 enum | |
46 { | |
47 NSStreamStatusNotOpen = 0, | |
48 NSStreamStatusOpening = 1, | |
49 NSStreamStatusOpen = 2, | |
50 NSStreamStatusReading = 3, | |
51 NSStreamStatusWriting = 4, | |
52 NSStreamStatusAtEnd = 5, | |
53 NSStreamStatusClosed = 6, | |
54 NSStreamStatusError = 7 | |
55 } | |
56 | |
57 enum | |
58 { | |
59 NSStreamEventNone = 0, | |
60 NSStreamEventOpenCompleted = 1 << 0, | |
61 NSStreamEventHasBytesAvailable = 1 << 1, | |
62 NSStreamEventHasSpaceAvailable = 1 << 2, | |
63 NSStreamEventErrorOccurred = 1 << 3, | |
64 NSStreamEventEndEncountered = 1 << 4 | |
65 } | |
66 | |
67 static this () | |
68 { | |
69 NSStreamSocketSecurityLevelKey = new NSString(bindings.NSStreamSocketSecurityLevelKey); | |
70 NSStreamSocketSecurityLevelNone = new NSString(bindings.NSStreamSocketSecurityLevelNone); | |
71 NSStreamSocketSecurityLevelSSLv2 = new NSString(bindings.NSStreamSocketSecurityLevelSSLv2); | |
72 NSStreamSocketSecurityLevelSSLv3 = new NSString(bindings.NSStreamSocketSecurityLevelSSLv3); | |
73 NSStreamSocketSecurityLevelTLSv1 = new NSString(bindings.NSStreamSocketSecurityLevelTLSv1); | |
74 NSStreamSocketSecurityLevelNegotiatedSSL = new NSString(bindings.NSStreamSocketSecurityLevelNegotiatedSSL); | |
75 NSStreamSOCKSProxyConfigurationKey = new NSString(bindings.NSStreamSOCKSProxyConfigurationKey); | |
76 NSStreamSOCKSProxyHostKey = new NSString(bindings.NSStreamSOCKSProxyHostKey); | |
77 NSStreamSOCKSProxyPortKey = new NSString(bindings.NSStreamSOCKSProxyPortKey); | |
78 NSStreamSOCKSProxyVersionKey = new NSString(bindings.NSStreamSOCKSProxyVersionKey); | |
79 NSStreamSOCKSProxyUserKey = new NSString(bindings.NSStreamSOCKSProxyUserKey); | |
80 NSStreamSOCKSProxyPasswordKey = new NSString(bindings.NSStreamSOCKSProxyPasswordKey); | |
81 NSStreamSOCKSProxyVersion4 = new NSString(bindings.NSStreamSOCKSProxyVersion4); | |
82 NSStreamSOCKSProxyVersion5 = new NSString(bindings.NSStreamSOCKSProxyVersion5); | |
83 NSStreamDataWrittenToMemoryStreamKey = new NSString(bindings.NSStreamDataWrittenToMemoryStreamKey); | |
84 NSStreamFileCurrentOffsetKey = new NSString(bindings.NSStreamFileCurrentOffsetKey); | |
85 NSStreamSocketSSLErrorDomain = new NSString(bindings.NSStreamSocketSSLErrorDomain); | |
86 NSStreamSOCKSErrorDomain = new NSString(bindings.NSStreamSOCKSErrorDomain); | |
87 } | |
88 | |
89 class NSStream : NSObject | |
90 { | |
91 mixin ObjcWrap; | |
92 mixin TNSSocketStreamCreationExtensions; | |
93 | |
94 void open () | |
95 { | |
96 return invokeObjcSelf!(void, "open"); | |
97 } | |
98 | |
99 void close () | |
100 { | |
101 return invokeObjcSelf!(void, "close"); | |
102 } | |
103 | |
104 Object delegate_ () | |
105 { | |
106 return invokeObjcSelf!(Object, "delegate"); | |
107 } | |
108 | |
109 void setDelegate (Object delegate_) | |
110 { | |
111 return invokeObjcSelf!(void, "setDelegate:", Object)(delegate_); | |
112 } | |
113 | |
114 Object propertyForKey (NSString key) | |
115 { | |
116 return invokeObjcSelf!(Object, "propertyForKey:", NSString)(key); | |
117 } | |
118 | |
119 bool setProperty (Object property, NSString key) | |
120 { | |
121 return invokeObjcSelf!(bool, "setProperty:forKey:", Object, NSString)(property, key); | |
122 } | |
123 | |
124 void scheduleInRunLoop (NSRunLoop aRunLoop, NSString mode) | |
125 { | |
126 return invokeObjcSelf!(void, "scheduleInRunLoop:forMode:", NSRunLoop, NSString)(aRunLoop, mode); | |
127 } | |
128 | |
129 void removeFromRunLoop (NSRunLoop aRunLoop, NSString mode) | |
130 { | |
131 return invokeObjcSelf!(void, "removeFromRunLoop:forMode:", NSRunLoop, NSString)(aRunLoop, mode); | |
132 } | |
133 | |
134 uint streamStatus () | |
135 { | |
136 return invokeObjcSelf!(uint, "streamStatus"); | |
137 } | |
138 | |
139 NSError streamError () | |
140 { | |
141 return invokeObjcSelf!(NSError, "streamError"); | |
142 } | |
143 } | |
144 | |
145 class NSOutputStream : NSStream | |
146 { | |
147 mixin ObjcWrap; | |
148 mixin TNSOutputStreamExtensions; | |
149 | |
150 NSInteger write (char* buffer, NSUInteger len) | |
151 { | |
152 return invokeObjcSelf!(NSInteger, "write:maxLength:", char*, NSUInteger)(buffer, len); | |
153 } | |
154 | |
155 bool hasSpaceAvailable () | |
156 { | |
157 return invokeObjcSelf!(bool, "hasSpaceAvailable"); | |
158 } | |
159 } | |
160 | |
161 class NSInputStream : NSStream | |
162 { | |
163 mixin ObjcWrap; | |
164 mixin TNSInputStreamExtensions; | |
165 | |
166 NSInteger read (char* buffer, NSUInteger len) | |
167 { | |
168 return invokeObjcSelf!(NSInteger, "read:maxLength:", char*, NSUInteger)(buffer, len); | |
169 } | |
170 | |
171 bool getBuffer (uint8_t** buffer, NSUInteger* len) | |
172 { | |
173 return invokeObjcSelf!(bool, "getBuffer:length:", uint8_t**, NSUInteger*)(buffer, len); | |
174 } | |
175 | |
176 bool hasBytesAvailable () | |
177 { | |
178 return invokeObjcSelf!(bool, "hasBytesAvailable"); | |
179 } | |
180 } | |
181 | |
182 template TNSStreamDelegateEventExtensions () | |
183 { | |
184 void stream (NSStream aStream, uint eventCode); | |
185 } | |
186 | |
187 template TNSOutputStreamExtensions () | |
188 { | |
189 Object initToMemory () | |
190 { | |
191 return invokeObjcSelf!(Object, "initToMemory"); | |
192 } | |
193 | |
194 this () | |
195 { | |
196 objcObject = Bridge.invokeObjcClassMethod!(id, "alloc")(objcClass); | |
197 id result = Bridge.invokeObjcMethod!(id, "initToMemory")(objcObject); | |
198 | |
199 if (result) | |
200 objcObject = ret; | |
201 | |
202 dObject = this; | |
203 } | |
204 | |
205 Object initToBuffer (char* buffer, NSUInteger capacity) | |
206 { | |
207 return invokeObjcSelf!(Object, "initToBuffer:capacity:", char*, NSUInteger)(buffer, capacity); | |
208 } | |
209 | |
210 this (char* buffer, NSUInteger capacity) | |
211 { | |
212 objcObject = Bridge.invokeObjcClassMethod!(id, "alloc")(objcClass); | |
213 id result = Bridge.invokeObjcMethod!(id, "initToBuffer:capacity:", char*, NSUInteger)(objcObject, buffer, capacity); | |
214 | |
215 if (result) | |
216 objcObject = ret; | |
217 | |
218 dObject = this; | |
219 } | |
220 | |
221 Object initToFileAtPath (NSString path, bool shouldAppend) | |
222 { | |
223 return invokeObjcSelf!(Object, "initToFileAtPath:append:", NSString, bool)(path, shouldAppend); | |
224 } | |
225 | |
226 this (NSString path, bool shouldAppend) | |
227 { | |
228 objcObject = Bridge.invokeObjcClassMethod!(id, "alloc")(objcClass); | |
229 id result = Bridge.invokeObjcMethod!(id, "initToFileAtPath:append:", NSString, bool)(objcObject, path, shouldAppend); | |
230 | |
231 if (result) | |
232 objcObject = ret; | |
233 | |
234 dObject = this; | |
235 } | |
236 | |
237 static Object outputStreamToMemory () | |
238 { | |
239 return invokeObjcSelfClass!(Object, "outputStreamToMemory"); | |
240 } | |
241 | |
242 static Object outputStreamToBuffer (char* buffer, NSUInteger capacity) | |
243 { | |
244 return invokeObjcSelfClass!(Object, "outputStreamToBuffer:capacity:", char*, NSUInteger)(buffer, capacity); | |
245 } | |
246 | |
247 static Object outputStreamToFileAtPath (NSString path, bool shouldAppend) | |
248 { | |
249 return invokeObjcSelfClass!(Object, "outputStreamToFileAtPath:append:", NSString, bool)(path, shouldAppend); | |
250 } | |
251 } | |
252 | |
253 template TNSSocketStreamCreationExtensions () | |
254 { | |
255 static void getStreamsToHost (NSHost host, NSInteger port, NSInputStream** inputStream, NSOutputStream** outputStream) | |
256 { | |
257 return invokeObjcSelfClass!(void, "getStreamsToHost:port:inputStream:outputStream:", NSHost, NSInteger, NSInputStream**, NSOutputStream**)(host, port, inputStream, outputStream); | |
258 } | |
259 } | |
260 | |
261 template TNSInputStreamExtensions () | |
262 { | |
263 Object initWithData (NSData data) | |
264 { | |
265 return invokeObjcSelf!(Object, "initWithData:", NSData)(data); | |
266 } | |
267 | |
268 this (NSData data) | |
269 { | |
270 objcObject = Bridge.invokeObjcClassMethod!(id, "alloc")(objcClass); | |
271 id result = Bridge.invokeObjcMethod!(id, "initWithData:", NSData)(objcObject, data); | |
272 | |
273 if (result) | |
274 objcObject = ret; | |
275 | |
276 dObject = this; | |
277 } | |
278 | |
279 Object initWithFileAtPath (NSString path) | |
280 { | |
281 return invokeObjcSelf!(Object, "initWithFileAtPath:", NSString)(path); | |
282 } | |
283 | |
284 this (NSString path) | |
285 { | |
286 objcObject = Bridge.invokeObjcClassMethod!(id, "alloc")(objcClass); | |
287 id result = Bridge.invokeObjcMethod!(id, "initWithFileAtPath:", NSString)(objcObject, path); | |
288 | |
289 if (result) | |
290 objcObject = ret; | |
291 | |
292 dObject = this; | |
293 } | |
294 | |
295 static Object inputStreamWithData (NSData data) | |
296 { | |
297 return invokeObjcSelfClass!(Object, "inputStreamWithData:", NSData)(data); | |
298 } | |
299 | |
300 static Object inputStreamWithFileAtPath (NSString path) | |
301 { | |
302 return invokeObjcSelfClass!(Object, "inputStreamWithFileAtPath:", NSString)(path); | |
303 } | |
304 } | |
305 |