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

Side by Side Diff: runtime/bin/socket_patch.dart

Issue 16514010: Add NetworkAdaptor to dart:io. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Add test file. Created 7 years, 6 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 | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2013, the Dart 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 file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 patch class RawServerSocket { 5 patch class RawServerSocket {
6 /* patch */ static Future<RawServerSocket> bind(address, 6 /* patch */ static Future<RawServerSocket> bind(address,
7 int port, 7 int port,
8 {int backlog: 0, 8 {int backlog: 0,
9 bool v6Only: false}) { 9 bool v6Only: false}) {
10 return _RawServerSocket.bind(address, port, backlog, v6Only); 10 return _RawServerSocket.bind(address, port, backlog, v6Only);
(...skipping 24 matching lines...) Expand all
35 /* patch */ static InternetAddress get ANY_IP_V6 { 35 /* patch */ static InternetAddress get ANY_IP_V6 {
36 return _InternetAddress.ANY_IP_V6; 36 return _InternetAddress.ANY_IP_V6;
37 } 37 }
38 38
39 /* patch */ static Future<List<InternetAddress>> lookup( 39 /* patch */ static Future<List<InternetAddress>> lookup(
40 String host, {InternetAddressType type: InternetAddressType.ANY}) { 40 String host, {InternetAddressType type: InternetAddressType.ANY}) {
41 return _NativeSocket.lookup(host, type: type); 41 return _NativeSocket.lookup(host, type: type);
42 } 42 }
43 } 43 }
44 44
45 patch class NetworkAdaptor {
46 /* patch */ static Future<List<NetworkAdaptor>> list({
47 bool includeLoopback: false,
48 InternetAddressType type: InternetAddressType.ANY}) {
49 return _NativeSocket.listAdaptors(includeLoopback: includeLoopback,
50 type: type);
51 }
52 }
53
45 class _InternetAddress implements InternetAddress { 54 class _InternetAddress implements InternetAddress {
46 static const int _ADDRESS_LOOPBACK_IP_V4 = 0; 55 static const int _ADDRESS_LOOPBACK_IP_V4 = 0;
47 static const int _ADDRESS_LOOPBACK_IP_V6 = 1; 56 static const int _ADDRESS_LOOPBACK_IP_V6 = 1;
48 static const int _ADDRESS_ANY_IP_V4 = 2; 57 static const int _ADDRESS_ANY_IP_V4 = 2;
49 static const int _ADDRESS_ANY_IP_V6 = 3; 58 static const int _ADDRESS_ANY_IP_V6 = 3;
50 59
51 static _InternetAddress LOOPBACK_IP_V4 = 60 static _InternetAddress LOOPBACK_IP_V4 =
52 new _InternetAddress.fixed(_ADDRESS_LOOPBACK_IP_V4); 61 new _InternetAddress.fixed(_ADDRESS_LOOPBACK_IP_V4);
53 static _InternetAddress LOOPBACK_IP_V6 = 62 static _InternetAddress LOOPBACK_IP_V6 =
54 new _InternetAddress.fixed(_ADDRESS_LOOPBACK_IP_V6); 63 new _InternetAddress.fixed(_ADDRESS_LOOPBACK_IP_V6);
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
88 } 97 }
89 } 98 }
90 99
91 String toString() { 100 String toString() {
92 return "InternetAddress('$address', ${type.name})"; 101 return "InternetAddress('$address', ${type.name})";
93 } 102 }
94 103
95 static Uint8List _fixed(int id) native "InternetAddress_Fixed"; 104 static Uint8List _fixed(int id) native "InternetAddress_Fixed";
96 } 105 }
97 106
107 class _NetworkAdaptor implements NetworkAdaptor {
108 final String name;
109 final List<InternetAddress> addresses;
110
111 _NetworkAdaptor(String this.name, List<InternetAddress> this.addresses);
112
113 String toString() {
114 return "NetworkAdaptor('$name', $addresses)";
115 }
116 }
117
98 118
99 // The _NativeSocket class encapsulates an OS socket. 119 // The _NativeSocket class encapsulates an OS socket.
100 class _NativeSocket extends NativeFieldWrapperClass1 { 120 class _NativeSocket extends NativeFieldWrapperClass1 {
101 // Bit flags used when communicating between the eventhandler and 121 // Bit flags used when communicating between the eventhandler and
102 // dart code. The EVENT flags are used to indicate events of 122 // dart code. The EVENT flags are used to indicate events of
103 // interest when sending a message from dart code to the 123 // interest when sending a message from dart code to the
104 // eventhandler. When receiving a message from the eventhandler the 124 // eventhandler. When receiving a message from the eventhandler the
105 // EVENT flags indicate the events that actually happened. The 125 // EVENT flags indicate the events that actually happened. The
106 // COMMAND flags are used to send commands from dart to the 126 // COMMAND flags are used to send commands from dart to the
107 // eventhandler. COMMAND flags are never received from the 127 // eventhandler. COMMAND flags are never received from the
(...skipping 16 matching lines...) Expand all
124 // Type flag send to the eventhandler providing additional 144 // Type flag send to the eventhandler providing additional
125 // information on the type of the file descriptor. 145 // information on the type of the file descriptor.
126 static const int LISTENING_SOCKET = 16; 146 static const int LISTENING_SOCKET = 16;
127 static const int PIPE_SOCKET = 17; 147 static const int PIPE_SOCKET = 17;
128 static const int TYPE_NORMAL_SOCKET = 0; 148 static const int TYPE_NORMAL_SOCKET = 0;
129 static const int TYPE_LISTENING_SOCKET = 1 << LISTENING_SOCKET; 149 static const int TYPE_LISTENING_SOCKET = 1 << LISTENING_SOCKET;
130 static const int TYPE_PIPE = 1 << PIPE_SOCKET; 150 static const int TYPE_PIPE = 1 << PIPE_SOCKET;
131 151
132 // Native port messages. 152 // Native port messages.
133 static const HOST_NAME_LOOKUP = 0; 153 static const HOST_NAME_LOOKUP = 0;
154 static const LIST_ADAPTORS = 1;
134 155
135 // Socket close state 156 // Socket close state
136 bool isClosed = false; 157 bool isClosed = false;
137 bool isClosedRead = false; 158 bool isClosedRead = false;
138 bool isClosedWrite = false; 159 bool isClosedWrite = false;
139 Completer closeCompleter = new Completer(); 160 Completer closeCompleter = new Completer();
140 161
141 // Handlers and receive port for socket events from the event handler. 162 // Handlers and receive port for socket events from the event handler.
142 int eventMask = 0; 163 int eventMask = 0;
143 List eventHandlers; 164 List eventHandlers;
(...skipping 23 matching lines...) Expand all
167 throw createError(response, "Failed host name lookup"); 188 throw createError(response, "Failed host name lookup");
168 } else { 189 } else {
169 return response.skip(1).map((result) { 190 return response.skip(1).map((result) {
170 var type = new InternetAddressType._from(result[0]); 191 var type = new InternetAddressType._from(result[0]);
171 return new _InternetAddress(type, result[1], host, result[2]); 192 return new _InternetAddress(type, result[1], host, result[2]);
172 }).toList(); 193 }).toList();
173 } 194 }
174 }); 195 });
175 } 196 }
176 197
198 static Future<List<NetworkAdaptor>> listAdaptors({
199 bool includeLoopback: false,
200 InternetAddressType type: InternetAddressType.ANY}) {
201 ensureSocketService();
202 return socketService.call([LIST_ADAPTORS, includeLoopback, type._value])
203 .then((response) {
204 if (isErrorResponse(response)) {
205 throw createError(response, "Failed listing adaptors");
206 } else {
207 var list = new List<NetworkAdaptor>();
208 var map = response.skip(1)
209 .fold(new Map<String, NetworkAdaptor>(), (map, result) {
210 var type = new InternetAddressType._from(result[0]);
211 var name = result[3];
212 map.putIfAbsent(name, () => new List<InternetAddress>());
213 map[name].add(
214 new _InternetAddress(type, result[1], "", result[2]));
215 return map;
216 })
217 .forEach((name, addresses) {
218 list.add(new _NetworkAdaptor(name, addresses));
219 });
220 return list;
221 }
222 });
223 }
224
177 static Future<_NativeSocket> connect(host, int port) { 225 static Future<_NativeSocket> connect(host, int port) {
178 return new Future.value(host) 226 return new Future.value(host)
179 .then((host) { 227 .then((host) {
180 if (host is _InternetAddress) return host; 228 if (host is _InternetAddress) return host;
181 return lookup(host) 229 return lookup(host)
182 .then((list) { 230 .then((list) {
183 if (list.length == 0) { 231 if (list.length == 0) {
184 throw createError(response, "Failed host name lookup"); 232 throw createError(response, "Failed host name lookup");
185 } 233 }
186 return list[0]; 234 return list[0];
(...skipping 878 matching lines...) Expand 10 before | Expand all | Expand 10 after
1065 if (_detachReady != null) { 1113 if (_detachReady != null) {
1066 _detachReady.complete(null); 1114 _detachReady.complete(null);
1067 } else { 1115 } else {
1068 if (_raw != null) { 1116 if (_raw != null) {
1069 _raw.shutdown(SocketDirection.SEND); 1117 _raw.shutdown(SocketDirection.SEND);
1070 _disableWriteEvent(); 1118 _disableWriteEvent();
1071 } 1119 }
1072 } 1120 }
1073 } 1121 }
1074 } 1122 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698