Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(386)

Side by Side Diff: lib/io/system_posix.dart

Issue 1209033003: Work in progres, please take a look and give early feedback if this is the way we want to structure… (Closed) Base URL: git@github.com:dart-lang/fletch.git@master
Patch Set: indentation Created 5 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2014, the Fletch project authors. Please see the AUTHORS file 1 // Copyright (c) 2014, the Fletch project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE.md file. 3 // BSD-style license that can be found in the LICENSE.md file.
4 4
5 part of system; 5 part of system;
6 6
7 const int AF_INET = 2; 7 const int AF_INET = 2;
8 const int AF_INET6 = 10; 8 const int AF_INET6 = 10;
9 9
10 const int SOCK_STREAM = 1; 10 const int SOCK_STREAM = 1;
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
49 int get _addrlenOffset => 16; 49 int get _addrlenOffset => 16;
50 50
51 int get ai_flags => getInt32(0); 51 int get ai_flags => getInt32(0);
52 int get ai_family => getInt32(4); 52 int get ai_family => getInt32(4);
53 void set ai_family(int value) { setInt32(4, value); } 53 void set ai_family(int value) { setInt32(4, value); }
54 int get ai_socktype => getInt32(8); 54 int get ai_socktype => getInt32(8);
55 int get ai_protocol => getInt32(12); 55 int get ai_protocol => getInt32(12);
56 56
57 int get ai_addrlen => getInt32(_addrlenOffset); 57 int get ai_addrlen => getInt32(_addrlenOffset);
58 58
59 Foreign get ai_addr; 59 ForeignMemory get ai_addr;
60 get ai_canonname; 60 get ai_canonname;
61 AddrInfo get ai_next; 61 AddrInfo get ai_next;
62 } 62 }
63 63
64 abstract class PosixSystem implements System { 64 abstract class PosixSystem implements System {
65 static final Foreign _accept = Foreign.lookup("accept"); 65 static final ForeignFunction _accept =
66 static final Foreign _access = Foreign.lookup("access"); 66 ForeignLibrary.standard.lookup("accept");
67 static final Foreign _bind = Foreign.lookup("bind"); 67 static final ForeignFunction _access =
68 static final Foreign _close = Foreign.lookup("close"); 68 ForeignLibrary.standard.lookup("access");
69 static final Foreign _connect = Foreign.lookup("connect"); 69 static final ForeignFunction _bind = ForeignLibrary.standard.lookup("bind");
kasperl 2015/07/03 07:49:54 I'd prefer to break all the lines the same way to
ricow1 2015/07/03 08:57:25 Done.
70 static final Foreign _fcntl = Foreign.lookup("fcntl"); 70 static final ForeignFunction _close = ForeignLibrary.standard.lookup("close");
71 static final Foreign _freeaddrinfo = Foreign.lookup("freeaddrinfo"); 71 static final ForeignFunction _connect =
72 static final Foreign _getaddrinfo = Foreign.lookup("getaddrinfo"); 72 ForeignLibrary.standard.lookup("connect");
73 static final Foreign _getsockname = Foreign.lookup("getsockname"); 73 static final ForeignFunction _fcntl = ForeignLibrary.standard.lookup("fcntl");
74 static final Foreign _ioctl = Foreign.lookup("ioctl"); 74 static final ForeignFunction _freeaddrinfo =
75 static final Foreign _listen = Foreign.lookup("listen"); 75 ForeignLibrary.standard.lookup("freeaddrinfo");
76 static final Foreign _memcpy = Foreign.lookup("memcpy"); 76 static final ForeignFunction _getaddrinfo =
77 static final Foreign _mkstemp = Foreign.lookup("mkstemp"); 77 ForeignLibrary.standard.lookup("getaddrinfo");
78 static final Foreign _nanosleep = Foreign.lookup("nanosleep"); 78 static final ForeignFunction _getsockname =
79 static final Foreign _read = Foreign.lookup("read"); 79 ForeignLibrary.standard.lookup("getsockname");
80 static final Foreign _setsockopt = Foreign.lookup("setsockopt"); 80 static final ForeignFunction _ioctl = ForeignLibrary.standard.lookup("ioctl");
81 static final Foreign _shutdown = Foreign.lookup("shutdown"); 81 static final ForeignFunction _listen =
82 static final Foreign _socket = Foreign.lookup("socket"); 82 ForeignLibrary.standard.lookup("listen");
83 static final Foreign _unlink = Foreign.lookup("unlink"); 83 static final ForeignFunction _memcpy =
84 static final Foreign _write = Foreign.lookup("write"); 84 ForeignLibrary.standard.lookup("memcpy");
85 static final ForeignFunction _mkstemp =
86 ForeignLibrary.standard.lookup("mkstemp");
87 static final ForeignFunction _nanosleep =
88 ForeignLibrary.standard.lookup("nanosleep");
89 static final ForeignFunction _read = ForeignLibrary.standard.lookup("read");
90 static final ForeignFunction _setsockopt =
91 ForeignLibrary.standard.lookup("setsockopt");
92 static final ForeignFunction _shutdown =
93 ForeignLibrary.standard.lookup("shutdown");
94 static final ForeignFunction _socket =
95 ForeignLibrary.standard.lookup("socket");
96 static final ForeignFunction _unlink =
97 ForeignLibrary.standard.lookup("unlink");
98 static final ForeignFunction _write = ForeignLibrary.standard.lookup("write");
85 99
86 int get FIONREAD; 100 int get FIONREAD;
87 101
88 int get SOL_SOCKET; 102 int get SOL_SOCKET;
89 103
90 int get SO_REUSEADDR; 104 int get SO_REUSEADDR;
91 105
92 Foreign get _open; 106 ForeignFunction get _open;
93 Foreign get _lseek; 107 ForeignFunction get _lseek;
94 108
95 int socket() { 109 int socket() {
96 return _retry(() => _socket.icall$3(AF_INET, SOCK_STREAM, 0)); 110 return _retry(() => _socket.icall$3(AF_INET, SOCK_STREAM, 0));
97 } 111 }
98 112
99 InternetAddress lookup(String host) { 113 InternetAddress lookup(String host) {
100 Foreign node = new Foreign.fromString(host); 114 ForeignMemory node = new ForeignMemory.fromString(host);
101 // TODO(ajohnsen): Actually apply hints. 115 // TODO(ajohnsen): Actually apply hints.
102 AddrInfo hints = new AddrInfo(); 116 AddrInfo hints = new AddrInfo();
103 // TODO(ajohnsen): Allow IPv6 results. 117 // TODO(ajohnsen): Allow IPv6 results.
104 hints.ai_family = AF_INET; 118 hints.ai_family = AF_INET;
105 Struct result = new Struct(1); 119 Struct result = new Struct(1);
106 int status = _retry(() => _getaddrinfo.icall$4(node, 120 int status = _retry(() => _getaddrinfo.icall$4(node,
107 Foreign.NULL, 121 Foreign.NULL,
108 hints, 122 hints,
109 result)); 123 result));
110 AddrInfo start = new AddrInfo.fromAddress(result.getField(0)); 124 AddrInfo start = new AddrInfo.fromAddress(result.getField(0));
111 AddrInfo info = start; 125 AddrInfo info = start;
112 var address; 126 var address;
113 while (info.value != 0) { 127 while (info.address != 0) {
114 int length; 128 int length;
115 int offset; 129 int offset;
116 // Loop until we find the right type. 130 // Loop until we find the right type.
117 if (info.ai_family == AF_INET) { 131 if (info.ai_family == AF_INET) {
118 length = 4; 132 length = 4;
119 offset = 4; 133 offset = 4;
120 } else if (info.ai_family == AF_INET6) { 134 } else if (info.ai_family == AF_INET6) {
121 length = 16; 135 length = 16;
122 offset = 8; 136 offset = 8;
123 } else { 137 } else {
124 info = info.ai_next; 138 info = info.ai_next;
125 continue; 139 continue;
126 } 140 }
127 Foreign addr = info.ai_addr; 141 ForeignMemory addr = info.ai_addr;
128 List<int> bytes = new List<int>(length); 142 List<int> bytes = new List<int>(length);
129 addr.copyBytesToList(bytes, offset, offset + length, 0); 143 addr.copyBytesToList(bytes, offset, offset + length, 0);
130 address = new _InternetAddress(bytes); 144 address = new _InternetAddress(bytes);
131 break; 145 break;
132 } 146 }
133 _freeaddrinfo.icall$1(start); 147 _freeaddrinfo.icall$1(start);
134 node.free(); 148 node.free();
135 hints.free(); 149 hints.free();
136 result.free(); 150 result.free();
137 if (status != 0) throw "Failed to call 'getaddrinfo': ${errno()}"; 151 if (status != 0) throw "Failed to call 'getaddrinfo': ${errno()}";
138 return address; 152 return address;
139 } 153 }
140 154
141 int open(String path, bool write, bool append) { 155 int open(String path, bool write, bool append) {
142 int flags = O_RDONLY; 156 int flags = O_RDONLY;
143 if (write || append) { 157 if (write || append) {
144 flags = O_RDWR | O_CREAT; 158 flags = O_RDWR | O_CREAT;
145 if (append) flags = flags | O_TRUNC; 159 if (append) flags = flags | O_TRUNC;
146 } 160 }
147 flags |= O_CLOEXEC; 161 flags |= O_CLOEXEC;
148 Foreign cPath = new Foreign.fromString(path); 162 ForeignMemory cPath = new ForeignMemory.fromString(path);
149 int fd = _retry(() => _open.icall$2(cPath, flags)); 163 int fd = _retry(() => _open.icall$2(cPath, flags));
150 cPath.free(); 164 cPath.free();
151 return fd; 165 return fd;
152 } 166 }
153 167
154 int lseek(int fd, int offset, int whence) { 168 int lseek(int fd, int offset, int whence) {
155 return _retry(() => _lseek.Lcall$wLw(fd, offset, whence)); 169 return _retry(() => _lseek.Lcall$wLw(fd, offset, whence));
156 } 170 }
157 171
158 TempFile mkstemp(String path) { 172 TempFile mkstemp(String path) {
159 Foreign cPath = new Foreign.fromString(path + "XXXXXX"); 173 ForeignMemory cPath = new ForeignMemory.fromString(path + "XXXXXX");
160 int result = _retry(() => _mkstemp.icall$1(cPath)); 174 int result = _retry(() => _mkstemp.icall$1(cPath));
161 if (result != -1) { 175 if (result != -1) {
162 var bytes = new List(cPath.length - 1); 176 var bytes = new List(cPath.length - 1);
163 cPath.copyBytesToList(bytes, 0, cPath.length - 1, 0); 177 cPath.copyBytesToList(bytes, 0, cPath.length - 1, 0);
164 path = new String.fromCharCodes(bytes); 178 path = new String.fromCharCodes(bytes);
165 } 179 }
166 cPath.free(); 180 cPath.free();
167 return new TempFile(result, path); 181 return new TempFile(result, path);
168 } 182 }
169 183
170 int access(String path) { 184 int access(String path) {
171 Foreign cPath = new Foreign.fromString(path); 185 ForeignMemory cPath = new ForeignMemory.fromString(path);
172 int result = _retry(() => _access.icall$2(cPath, 0)); 186 int result = _retry(() => _access.icall$2(cPath, 0));
173 cPath.free(); 187 cPath.free();
174 return result; 188 return result;
175 } 189 }
176 190
177 int unlink(String path) { 191 int unlink(String path) {
178 Foreign cPath = new Foreign.fromString(path); 192 ForeignMemory cPath = new ForeignMemory.fromString(path);
179 int result = _retry(() => _unlink.icall$1(cPath)); 193 int result = _retry(() => _unlink.icall$1(cPath));
180 cPath.free(); 194 cPath.free();
181 return result; 195 return result;
182 } 196 }
183 197
184 int bind(int fd, _InternetAddress address, int port) { 198 int bind(int fd, _InternetAddress address, int port) {
185 Foreign sockaddr = _createSocketAddress(address, port); 199 ForeignMemory sockaddr = _createSocketAddress(address, port);
186 int status = _retry(() => _bind.icall$3(fd, sockaddr, sockaddr.length)); 200 int status = _retry(() => _bind.icall$3(fd, sockaddr, sockaddr.length));
187 sockaddr.free(); 201 sockaddr.free();
188 return status; 202 return status;
189 } 203 }
190 204
191 int listen(int fd) { 205 int listen(int fd) {
192 return _retry(() => _listen.icall$2(fd, 128)); 206 return _retry(() => _listen.icall$2(fd, 128));
193 } 207 }
194 208
195 int setsockopt(int fd, int level, int optname, int value) { 209 int setsockopt(int fd, int level, int optname, int value) {
196 Struct32 opt = new Struct32(1); 210 Struct32 opt = new Struct32(1);
197 opt.setField(0, value); 211 opt.setField(0, value);
198 int result = _retry(() => _setsockopt.icall$5(fd, 212 int result = _retry(() => _setsockopt.icall$5(fd,
199 level, 213 level,
200 optname, 214 optname,
201 opt, 215 opt,
202 opt.length)); 216 opt.length));
203 opt.free(); 217 opt.free();
204 return result; 218 return result;
205 } 219 }
206 220
207 int accept(int fd) { 221 int accept(int fd) {
208 return _retry(() => _accept.icall$3(fd, Foreign.NULL, Foreign.NULL)); 222 return _retry(() => _accept.icall$3(fd, Foreign.NULL, Foreign.NULL));
209 } 223 }
210 224
211 int port(int fd) { 225 int port(int fd) {
212 const int LENGTH = 28; 226 const int LENGTH = 28;
213 var sockaddr = new Foreign.allocated(LENGTH); 227 var sockaddr = new ForeignMemory.allocated(LENGTH);
214 var addrlen = new Foreign.allocated(4); 228 var addrlen = new ForeignMemory.allocated(4);
215 addrlen.setInt32(0, LENGTH); 229 addrlen.setInt32(0, LENGTH);
216 int status = _retry(() => _getsockname.icall$3(fd, sockaddr, addrlen)); 230 int status = _retry(() => _getsockname.icall$3(fd, sockaddr, addrlen));
217 int port = -1; 231 int port = -1;
218 if (status == 0) { 232 if (status == 0) {
219 port = sockaddr.getUint8(2) << 8; 233 port = sockaddr.getUint8(2) << 8;
220 port |= sockaddr.getUint8(3); 234 port |= sockaddr.getUint8(3);
221 } 235 }
222 sockaddr.free(); 236 sockaddr.free();
223 addrlen.free(); 237 addrlen.free();
224 return port; 238 return port;
225 } 239 }
226 240
227 int connect(int fd, _InternetAddress address, int port) { 241 int connect(int fd, _InternetAddress address, int port) {
228 Foreign sockaddr = _createSocketAddress(address, port); 242 ForeignMemory sockaddr = _createSocketAddress(address, port);
229 int status = _retry(() => _connect.icall$3(fd, sockaddr, sockaddr.length)); 243 int status = _retry(() => _connect.icall$3(fd, sockaddr, sockaddr.length));
230 sockaddr.free(); 244 sockaddr.free();
231 return status; 245 return status;
232 } 246 }
233 247
234 int setBlocking(int fd, bool blocking) { 248 int setBlocking(int fd, bool blocking) {
235 int flags = _retry(() => _fcntl.icall$3(fd, F_GETFL, 0)); 249 int flags = _retry(() => _fcntl.icall$3(fd, F_GETFL, 0));
236 if (flags == -1) return -1; 250 if (flags == -1) return -1;
237 if (blocking) { 251 if (blocking) {
238 flags &= ~O_NONBLOCK; 252 flags &= ~O_NONBLOCK;
(...skipping 11 matching lines...) Expand all
250 Struct result = new Struct(1); 264 Struct result = new Struct(1);
251 int status = _retry(() => _ioctl.icall$3(fd, FIONREAD, result)); 265 int status = _retry(() => _ioctl.icall$3(fd, FIONREAD, result));
252 int available = result.getWord(0); 266 int available = result.getWord(0);
253 result.free(); 267 result.free();
254 if (status == -1) return status; 268 if (status == -1) return status;
255 return available; 269 return available;
256 } 270 }
257 271
258 int read(int fd, var buffer, int offset, int length) { 272 int read(int fd, var buffer, int offset, int length) {
259 _rangeCheck(buffer, offset, length); 273 _rangeCheck(buffer, offset, length);
260 var address = buffer.getForeign().value + offset; 274 var address = buffer.getForeign().address + offset;
261 return _retry(() => _read.icall$3(fd, address, length)); 275 return _retry(() => _read.icall$3(fd, address, length));
262 } 276 }
263 277
264 int write(int fd, var buffer, int offset, int length) { 278 int write(int fd, var buffer, int offset, int length) {
265 _rangeCheck(buffer, offset, length); 279 _rangeCheck(buffer, offset, length);
266 var address = buffer.getForeign().value + offset; 280 var address = buffer.getForeign().address + offset;
267 return _retry(() => _write.icall$3(fd, address, length)); 281 return _retry(() => _write.icall$3(fd, address, length));
268 } 282 }
269 283
270 void memcpy(var dest, 284 void memcpy(var dest,
271 int destOffset, 285 int destOffset,
272 var src, 286 var src,
273 int srcOffset, 287 int srcOffset,
274 int length) { 288 int length) {
275 var destAddress = dest.getForeign().value + destOffset; 289 var destAddress = dest.getForeign().address + destOffset;
276 var srcAddress = src.getForeign().value + srcOffset; 290 var srcAddress = src.getForeign().address + srcOffset;
277 _memcpy.icall$3(destAddress, srcAddress, length); 291 _memcpy.icall$3(destAddress, srcAddress, length);
278 } 292 }
279 293
280 int shutdown(int fd, int how) { 294 int shutdown(int fd, int how) {
281 return _retry(() => _shutdown.icall$2(fd, how)); 295 return _retry(() => _shutdown.icall$2(fd, how));
282 } 296 }
283 297
284 int close(int fd) { 298 int close(int fd) {
285 return _retry(() => _close.icall$1(fd)); 299 return _retry(() => _close.icall$1(fd));
286 } 300 }
(...skipping 10 matching lines...) Expand all
297 Errno errno() { 311 Errno errno() {
298 return Errno.from(Foreign.errno); 312 return Errno.from(Foreign.errno);
299 } 313 }
300 314
301 static void _rangeCheck(ByteBuffer buffer, int offset, int length) { 315 static void _rangeCheck(ByteBuffer buffer, int offset, int length) {
302 if (buffer.lengthInBytes < offset + length) { 316 if (buffer.lengthInBytes < offset + length) {
303 throw new IndexError(length, buffer); 317 throw new IndexError(length, buffer);
304 } 318 }
305 } 319 }
306 320
307 Foreign _createSocketAddress(_InternetAddress address, int port) { 321 ForeignMemory _createSocketAddress(_InternetAddress address, int port) {
308 var bytes = address._bytes; 322 var bytes = address._bytes;
309 Foreign sockaddr; 323 ForeignMemory sockaddr;
310 int length; 324 int length;
311 if (bytes.length == 4) { 325 if (bytes.length == 4) {
312 length = 16; 326 length = 16;
313 sockaddr = new Foreign.allocated(length); 327 sockaddr = new ForeignMemory.allocated(length);
314 sockaddr.setUint16(0, AF_INET); 328 sockaddr.setUint16(0, AF_INET);
315 sockaddr.copyBytesFromList(bytes, 4, 8, 0); 329 sockaddr.copyBytesFromList(bytes, 4, 8, 0);
316 } else if (bytes.length == 16) { 330 } else if (bytes.length == 16) {
317 length = 28; 331 length = 28;
318 sockaddr = new Foreign.allocated(length); 332 sockaddr = new ForeignMemory.allocated(length);
319 sockaddr.setUint16(0, AF_INET6); 333 sockaddr.setUint16(0, AF_INET6);
320 sockaddr.copyBytesFromList(bytes, 8, 24, 0); 334 sockaddr.copyBytesFromList(bytes, 8, 24, 0);
321 } else { 335 } else {
322 throw "Invalid InternetAddress"; 336 throw "Invalid InternetAddress";
323 } 337 }
324 // Set port in Network Byte Order. 338 // Set port in Network Byte Order.
325 sockaddr.setUint8(2, port >> 8); 339 sockaddr.setUint8(2, port >> 8);
326 sockaddr.setUint8(3, port & 0xFF); 340 sockaddr.setUint8(3, port & 0xFF);
327 return sockaddr; 341 return sockaddr;
328 } 342 }
329 343
330 int _retry(Function f) { 344 int _retry(Function f) {
331 int value; 345 int value;
332 while ((value = f()) == -1) { 346 while ((value = f()) == -1) {
333 if (Foreign.errno != Errno.EINTR) break; 347 if (Foreign.errno != Errno.EINTR) break;
334 } 348 }
335 return value; 349 return value;
336 } 350 }
337 } 351 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698