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

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

Issue 879353003: Introduce optional 'bool shared' parameter to ServerSocket.bind() ... (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge
Patch Set: Addressed comments Created 5 years, 10 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
« no previous file with comments | « dart/runtime/bin/socket.cc ('k') | dart/sdk/lib/_internal/compiler/js_lib/io_patch.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 bool shared: false}) {
11 return _RawServerSocket.bind(address, port, backlog, v6Only, shared);
11 } 12 }
12 } 13 }
13 14
14 15
15 patch class RawSocket { 16 patch class RawSocket {
16 /* patch */ static Future<RawSocket> connect(host, int port) { 17 /* patch */ static Future<RawSocket> connect(host, int port) {
17 return _RawSocket.connect(host, port); 18 return _RawSocket.connect(host, port);
18 } 19 }
19 } 20 }
20 21
(...skipping 431 matching lines...) Expand 10 before | Expand all | Expand 10 after
452 } 453 }
453 } 454 }
454 connectNext(); 455 connectNext();
455 return completer.future; 456 return completer.future;
456 }); 457 });
457 } 458 }
458 459
459 static Future<_NativeSocket> bind(host, 460 static Future<_NativeSocket> bind(host,
460 int port, 461 int port,
461 int backlog, 462 int backlog,
462 bool v6Only) { 463 bool v6Only,
464 bool shared) {
463 return new Future.value(host) 465 return new Future.value(host)
464 .then((host) { 466 .then((host) {
465 if (host is _InternetAddress) return host; 467 if (host is _InternetAddress) return host;
466 return lookup(host) 468 return lookup(host)
467 .then((list) { 469 .then((list) {
468 if (list.length == 0) { 470 if (list.length == 0) {
469 throw createError(response, "Failed host lookup: '$host'"); 471 throw createError(response, "Failed host lookup: '$host'");
470 } 472 }
471 return list[0]; 473 return list[0];
472 }); 474 });
473 }) 475 })
474 .then((address) { 476 .then((address) {
475 var socket = new _NativeSocket.listen(); 477 var socket = new _NativeSocket.listen();
476 socket.address = address; 478 socket.address = address;
479
477 var result = socket.nativeCreateBindListen(address._in_addr, 480 var result = socket.nativeCreateBindListen(address._in_addr,
478 port, 481 port,
479 backlog, 482 backlog,
480 v6Only); 483 v6Only,
484 shared);
481 if (result is OSError) { 485 if (result is OSError) {
482 throw new SocketException("Failed to create server socket", 486 throw new SocketException("Failed to create server socket",
483 osError: result, 487 osError: result,
484 address: address, 488 address: address,
485 port: port); 489 port: port);
486 } 490 }
487 if (port != 0) socket.localPort = port; 491 if (port != 0) socket.localPort = port;
488 socket.connectToEventHandler(); 492 socket.connectToEventHandler();
489 return socket; 493 return socket;
490 }); 494 });
(...skipping 25 matching lines...) Expand all
516 if (port != 0) socket.localPort = port; 520 if (port != 0) socket.localPort = port;
517 return socket; 521 return socket;
518 }); 522 });
519 } 523 }
520 524
521 _NativeSocket.datagram(this.address) 525 _NativeSocket.datagram(this.address)
522 : typeFlags = TYPE_NORMAL_SOCKET | TYPE_UDP_SOCKET; 526 : typeFlags = TYPE_NORMAL_SOCKET | TYPE_UDP_SOCKET;
523 527
524 _NativeSocket.normal() : typeFlags = TYPE_NORMAL_SOCKET | TYPE_TCP_SOCKET; 528 _NativeSocket.normal() : typeFlags = TYPE_NORMAL_SOCKET | TYPE_TCP_SOCKET;
525 529
526 _NativeSocket.listen() : typeFlags = TYPE_LISTENING_SOCKET | TYPE_TCP_SOCKET; 530 _NativeSocket.listen() : typeFlags = TYPE_LISTENING_SOCKET | TYPE_TCP_SOCKET {
531 isClosedWrite = true;
532 }
527 533
528 _NativeSocket.pipe() : typeFlags = TYPE_PIPE; 534 _NativeSocket.pipe() : typeFlags = TYPE_PIPE;
529 535
530 _NativeSocket.watch(int id) 536 _NativeSocket.watch(int id)
531 : typeFlags = TYPE_NORMAL_SOCKET | TYPE_INTERNAL_SOCKET { 537 : typeFlags = TYPE_NORMAL_SOCKET | TYPE_INTERNAL_SOCKET {
532 isClosedWrite = true; 538 isClosedWrite = true;
533 nativeSetSocketId(id); 539 nativeSetSocketId(id);
534 } 540 }
535 541
536 bool get isListening => (typeFlags & TYPE_LISTENING_SOCKET) != 0; 542 bool get isListening => (typeFlags & TYPE_LISTENING_SOCKET) != 0;
(...skipping 569 matching lines...) Expand 10 before | Expand all | Expand 10 after
1106 nativeAvailable() native "Socket_Available"; 1112 nativeAvailable() native "Socket_Available";
1107 nativeRead(int len) native "Socket_Read"; 1113 nativeRead(int len) native "Socket_Read";
1108 nativeRecvFrom() native "Socket_RecvFrom"; 1114 nativeRecvFrom() native "Socket_RecvFrom";
1109 nativeWrite(List<int> buffer, int offset, int bytes) 1115 nativeWrite(List<int> buffer, int offset, int bytes)
1110 native "Socket_WriteList"; 1116 native "Socket_WriteList";
1111 nativeSendTo(List<int> buffer, int offset, int bytes, 1117 nativeSendTo(List<int> buffer, int offset, int bytes,
1112 List<int> address, int port) 1118 List<int> address, int port)
1113 native "Socket_SendTo"; 1119 native "Socket_SendTo";
1114 nativeCreateConnect(List<int> addr, 1120 nativeCreateConnect(List<int> addr,
1115 int port) native "Socket_CreateConnect"; 1121 int port) native "Socket_CreateConnect";
1116 nativeCreateBindListen(List<int> addr, int port, int backlog, bool v6Only) 1122 nativeCreateBindListen(List<int> addr, int port, int backlog, bool v6Only,
1123 bool shared)
1117 native "ServerSocket_CreateBindListen"; 1124 native "ServerSocket_CreateBindListen";
1118 nativeCreateBindDatagram(List<int> addr, int port, bool reuseAddress) 1125 nativeCreateBindDatagram(List<int> addr, int port, bool reuseAddress)
1119 native "Socket_CreateBindDatagram"; 1126 native "Socket_CreateBindDatagram";
1120 nativeAccept(_NativeSocket socket) native "ServerSocket_Accept"; 1127 nativeAccept(_NativeSocket socket) native "ServerSocket_Accept";
1121 int nativeGetPort() native "Socket_GetPort"; 1128 int nativeGetPort() native "Socket_GetPort";
1122 List nativeGetRemotePeer() native "Socket_GetRemotePeer"; 1129 List nativeGetRemotePeer() native "Socket_GetRemotePeer";
1123 int nativeGetSocketId() native "Socket_GetSocketId"; 1130 int nativeGetSocketId() native "Socket_GetSocketId";
1124 OSError nativeGetError() native "Socket_GetError"; 1131 OSError nativeGetError() native "Socket_GetError";
1125 nativeGetOption(int option, int protocol) native "Socket_GetOption"; 1132 nativeGetOption(int option, int protocol) native "Socket_GetOption";
1126 bool nativeSetOption(int option, int protocol, value) 1133 bool nativeSetOption(int option, int protocol, value)
1127 native "Socket_SetOption"; 1134 native "Socket_SetOption";
1128 bool nativeJoinMulticast( 1135 bool nativeJoinMulticast(
1129 List<int> addr, List<int> interfaceAddr, int interfaceIndex) 1136 List<int> addr, List<int> interfaceAddr, int interfaceIndex)
1130 native "Socket_JoinMulticast"; 1137 native "Socket_JoinMulticast";
1131 bool nativeLeaveMulticast( 1138 bool nativeLeaveMulticast(
1132 List<int> addr, List<int> interfaceAddr, int interfaceIndex) 1139 List<int> addr, List<int> interfaceAddr, int interfaceIndex)
1133 native "Socket_LeaveMulticast"; 1140 native "Socket_LeaveMulticast";
1141 bool _nativeMarkSocketAsSharedHack()
1142 native "Socket_MarkSocketAsSharedHack";
1134 } 1143 }
1135 1144
1136 1145
1137 class _RawServerSocket extends Stream<RawSocket> 1146 class _RawServerSocket extends Stream<RawSocket>
1138 implements RawServerSocket { 1147 implements RawServerSocket {
1139 final _NativeSocket _socket; 1148 final _NativeSocket _socket;
1140 StreamController<RawSocket> _controller; 1149 StreamController<RawSocket> _controller;
1141 ReceivePort _referencePort; 1150 ReceivePort _referencePort;
1151 bool _v6Only;
1142 1152
1143 static Future<_RawServerSocket> bind(address, 1153 static Future<_RawServerSocket> bind(address,
1144 int port, 1154 int port,
1145 int backlog, 1155 int backlog,
1146 bool v6Only) { 1156 bool v6Only,
1157 bool shared) {
1147 if (port < 0 || port > 0xFFFF) 1158 if (port < 0 || port > 0xFFFF)
1148 throw new ArgumentError("Invalid port $port"); 1159 throw new ArgumentError("Invalid port $port");
1149 if (backlog < 0) throw new ArgumentError("Invalid backlog $backlog"); 1160 if (backlog < 0) throw new ArgumentError("Invalid backlog $backlog");
1150 return _NativeSocket.bind(address, port, backlog, v6Only) 1161 return _NativeSocket.bind(address, port, backlog, v6Only, shared)
1151 .then((socket) => new _RawServerSocket(socket)); 1162 .then((socket) => new _RawServerSocket(socket, v6Only));
1152 } 1163 }
1153 1164
1154 _RawServerSocket(this._socket); 1165 _RawServerSocket(this._socket, this._v6Only);
1155 1166
1156 StreamSubscription<RawSocket> listen(void onData(RawSocket event), 1167 StreamSubscription<RawSocket> listen(void onData(RawSocket event),
1157 {Function onError, 1168 {Function onError,
1158 void onDone(), 1169 void onDone(),
1159 bool cancelOnError}) { 1170 bool cancelOnError}) {
1160 if (_controller != null) { 1171 if (_controller != null) {
1161 throw new StateError("Stream was already listened to"); 1172 throw new StateError("Stream was already listened to");
1162 } 1173 }
1163 var zone = Zone.current; 1174 var zone = Zone.current;
1164 _controller = new StreamController(sync: true, 1175 _controller = new StreamController(sync: true,
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
1226 void _onPauseStateChange() { 1237 void _onPauseStateChange() {
1227 if (_controller.isPaused) { 1238 if (_controller.isPaused) {
1228 _pause(); 1239 _pause();
1229 } else { 1240 } else {
1230 _resume(); 1241 _resume();
1231 } 1242 }
1232 } 1243 }
1233 1244
1234 RawServerSocketReference get reference { 1245 RawServerSocketReference get reference {
1235 if (_referencePort == null) { 1246 if (_referencePort == null) {
1247 bool successfull = _socket._nativeMarkSocketAsSharedHack();
1236 _referencePort = new ReceivePort(); 1248 _referencePort = new ReceivePort();
1237 _referencePort.listen((sendPort) { 1249 _referencePort.listen((sendPort) {
1238 sendPort.send( 1250 sendPort.send(
1239 [_socket.nativeGetSocketId(), 1251 [_socket.address,
1240 _socket.address, 1252 _socket.port,
1241 _socket.localPort]); 1253 _v6Only]);
1242 }); 1254 });
1243 } 1255 }
1244 return new _RawServerSocketReference(_referencePort.sendPort); 1256 return new _RawServerSocketReference(_referencePort.sendPort);
1245 } 1257 }
1246 1258
1247 Map _toJSON(bool ref) => _socket._toJSON(ref); 1259 Map _toJSON(bool ref) => _socket._toJSON(ref);
1260
1248 void set _owner(owner) { _socket.owner = owner; } 1261 void set _owner(owner) { _socket.owner = owner; }
1249 } 1262 }
1250 1263
1251 1264
1252 class _RawServerSocketReference implements RawServerSocketReference { 1265 class _RawServerSocketReference implements RawServerSocketReference {
1253 final SendPort _sendPort; 1266 final SendPort _sendPort;
1254 1267
1255 _RawServerSocketReference(this._sendPort); 1268 _RawServerSocketReference(this._sendPort);
1256 1269
1257 Future<RawServerSocket> create() { 1270 Future<RawServerSocket> create() {
1258 var port = new ReceivePort(); 1271 var port = new ReceivePort();
1259 _sendPort.send(port.sendPort); 1272 _sendPort.send(port.sendPort);
1260 return port.first.then((args) { 1273 return port.first.then((List args) {
1261 port.close(); 1274 port.close();
1262 var native = new _NativeSocket.listen(); 1275
1263 native.nativeSetSocketId(args[0]); 1276 InternetAddress address = args[0];
1264 native.address = args[1]; 1277 int tcpPort = args[1];
1265 native.localPort = args[2]; 1278 bool v6Only = args[2];
1266 return new _RawServerSocket(native); 1279 return
1280 RawServerSocket.bind(address, tcpPort, v6Only: v6Only, shared: true);
1267 }); 1281 });
1268 } 1282 }
1269 1283
1270 int get hashCode => _sendPort.hashCode; 1284 int get hashCode => _sendPort.hashCode;
1271 1285
1272 bool operator==(Object other) 1286 bool operator==(Object other)
1273 => other is _RawServerSocketReference && _sendPort == other._sendPort; 1287 => other is _RawServerSocketReference && _sendPort == other._sendPort;
1274 } 1288 }
1275 1289
1276 1290
1277 class _RawSocket extends Stream<RawSocketEvent> 1291 class _RawSocket extends Stream<RawSocketEvent>
1278 implements RawSocket { 1292 implements RawSocket {
1279 final _NativeSocket _socket; 1293 final _NativeSocket _socket;
1280 StreamController<RawSocketEvent> _controller; 1294 StreamController<RawSocketEvent> _controller;
1281 bool _readEventsEnabled = true; 1295 bool _readEventsEnabled = true;
1282 bool _writeEventsEnabled = true; 1296 bool _writeEventsEnabled = true;
1283 1297
(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after
1422 1436
1423 Map _toJSON(bool ref) => _socket._toJSON(ref); 1437 Map _toJSON(bool ref) => _socket._toJSON(ref);
1424 void set _owner(owner) { _socket.owner = owner; } 1438 void set _owner(owner) { _socket.owner = owner; }
1425 } 1439 }
1426 1440
1427 1441
1428 patch class ServerSocket { 1442 patch class ServerSocket {
1429 /* patch */ static Future<ServerSocket> bind(address, 1443 /* patch */ static Future<ServerSocket> bind(address,
1430 int port, 1444 int port,
1431 {int backlog: 0, 1445 {int backlog: 0,
1432 bool v6Only: false}) { 1446 bool v6Only: false,
1433 return _ServerSocket.bind(address, port, backlog, v6Only); 1447 bool shared: false}) {
1448 return _ServerSocket.bind(address, port, backlog, v6Only, shared);
1434 } 1449 }
1435 } 1450 }
1436 1451
1437 1452
1438 class _ServerSocketReference implements ServerSocketReference { 1453 class _ServerSocketReference implements ServerSocketReference {
1439 final RawServerSocketReference _rawReference; 1454 final RawServerSocketReference _rawReference;
1440 1455
1441 _ServerSocketReference(this._rawReference); 1456 _ServerSocketReference(this._rawReference);
1442 1457
1443 Future<ServerSocket> create() { 1458 Future<ServerSocket> create() {
1444 return _rawReference.create().then((raw) => new _ServerSocket(raw)); 1459 return _rawReference.create().then((raw) => new _ServerSocket(raw));
1445 } 1460 }
1446 } 1461 }
1447 1462
1448 1463
1449 class _ServerSocket extends Stream<Socket> 1464 class _ServerSocket extends Stream<Socket>
1450 implements ServerSocket { 1465 implements ServerSocket {
1451 final _socket; 1466 final _socket;
1452 1467
1453 static Future<_ServerSocket> bind(address, 1468 static Future<_ServerSocket> bind(address,
1454 int port, 1469 int port,
1455 int backlog, 1470 int backlog,
1456 bool v6Only) { 1471 bool v6Only,
1457 return _RawServerSocket.bind(address, port, backlog, v6Only) 1472 bool shared) {
1473 return _RawServerSocket.bind(address, port, backlog, v6Only, shared)
1458 .then((socket) => new _ServerSocket(socket)); 1474 .then((socket) => new _ServerSocket(socket));
1459 } 1475 }
1460 1476
1461 _ServerSocket(this._socket); 1477 _ServerSocket(this._socket);
1462 1478
1463 StreamSubscription<Socket> listen(void onData(Socket event), 1479 StreamSubscription<Socket> listen(void onData(Socket event),
1464 {Function onError, 1480 {Function onError,
1465 void onDone(), 1481 void onDone(),
1466 bool cancelOnError}) { 1482 bool cancelOnError}) {
1467 return _socket.map((rawSocket) => new _Socket(rawSocket)).listen( 1483 return _socket.map((rawSocket) => new _Socket(rawSocket)).listen(
1468 onData, 1484 onData,
1469 onError: onError, 1485 onError: onError,
1470 onDone: onDone, 1486 onDone: onDone,
1471 cancelOnError: cancelOnError); 1487 cancelOnError: cancelOnError);
1472 } 1488 }
1473 1489
1474 int get port => _socket.port; 1490 int get port => _socket.port;
1475 1491
1476 InternetAddress get address => _socket.address; 1492 InternetAddress get address => _socket.address;
1477 1493
1478 Future close() => _socket.close().then((_) => this); 1494 Future close() => _socket.close().then((_) => this);
1479 1495
1480 ServerSocketReference get reference { 1496 ServerSocketReference get reference {
1481 return new _ServerSocketReference(_socket.reference); 1497 return new _ServerSocketReference(_socket.reference);
1482 } 1498 }
1483 1499
1484 Map _toJSON(bool ref) => _socket._toJSON(ref); 1500 Map _toJSON(bool ref) => _socket._toJSON(ref);
1501
1485 void set _owner(owner) { _socket._owner = owner; } 1502 void set _owner(owner) { _socket._owner = owner; }
1486 } 1503 }
1487 1504
1488 1505
1489 patch class Socket { 1506 patch class Socket {
1490 /* patch */ static Future<Socket> connect(host, int port) { 1507 /* patch */ static Future<Socket> connect(host, int port) {
1491 return RawSocket.connect(host, port).then( 1508 return RawSocket.connect(host, port).then(
1492 (socket) => new _Socket(socket)); 1509 (socket) => new _Socket(socket));
1493 } 1510 }
1494 } 1511 }
(...skipping 435 matching lines...) Expand 10 before | Expand all | Expand 10 after
1930 String address, 1947 String address,
1931 List<int> in_addr, 1948 List<int> in_addr,
1932 int port) { 1949 int port) {
1933 return new Datagram( 1950 return new Datagram(
1934 data, 1951 data,
1935 new _InternetAddress(address, null, in_addr), 1952 new _InternetAddress(address, null, in_addr),
1936 port); 1953 port);
1937 } 1954 }
1938 1955
1939 String _socketsStats() => _SocketsObservatory.toJSON(); 1956 String _socketsStats() => _SocketsObservatory.toJSON();
OLDNEW
« no previous file with comments | « dart/runtime/bin/socket.cc ('k') | dart/sdk/lib/_internal/compiler/js_lib/io_patch.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698