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

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

Issue 234843003: Make server-sockets level triggered. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Fix platforms. Created 6 years, 8 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 | « runtime/bin/eventhandler_macos.cc ('k') | no next file » | 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 return _RawServerSocket.bind(address, port, backlog, v6Only);
(...skipping 394 matching lines...) Expand 10 before | Expand all | Expand 10 after
405 final int typeFlags; 405 final int typeFlags;
406 406
407 // Holds the port of the socket, 0 if not known. 407 // Holds the port of the socket, 0 if not known.
408 int localPort = 0; 408 int localPort = 0;
409 409
410 // Holds the address used to connect or bind the socket. 410 // Holds the address used to connect or bind the socket.
411 InternetAddress address; 411 InternetAddress address;
412 412
413 int available = 0; 413 int available = 0;
414 414
415 int returnTokens = 0; 415 int tokens = 0;
416 416
417 bool sendReadEvents = false; 417 bool sendReadEvents = false;
418 bool readEventIssued = false; 418 bool readEventIssued = false;
419 419
420 bool sendWriteEvents = false; 420 bool sendWriteEvents = false;
421 bool writeEventIssued = false; 421 bool writeEventIssued = false;
422 bool writeAvailable = false; 422 bool writeAvailable = false;
423 423
424 // Statistics. 424 // Statistics.
425 int totalRead = 0; 425 int totalRead = 0;
(...skipping 263 matching lines...) Expand 10 before | Expand all | Expand 10 after
689 if (result is OSError) { 689 if (result is OSError) {
690 scheduleMicrotask(() => reportError(result, "Send failed")); 690 scheduleMicrotask(() => reportError(result, "Send failed"));
691 result = 0; 691 result = 0;
692 } 692 }
693 return result; 693 return result;
694 } 694 }
695 695
696 _NativeSocket accept() { 696 _NativeSocket accept() {
697 // Don't issue accept if we're closing. 697 // Don't issue accept if we're closing.
698 if (isClosing || isClosed) return null; 698 if (isClosing || isClosed) return null;
699 assert(available > 0);
700 available--;
701 tokens++;
702 returnTokens();
699 var socket = new _NativeSocket.normal(); 703 var socket = new _NativeSocket.normal();
700 if (nativeAccept(socket) != true) return null; 704 if (nativeAccept(socket) != true) return null;
701 socket.localPort = localPort; 705 socket.localPort = localPort;
702 socket.address = address; 706 socket.address = address;
703 totalRead += 1; 707 totalRead += 1;
704 return socket; 708 return socket;
705 } 709 }
706 710
707 int get port { 711 int get port {
708 if (localPort != 0) return localPort; 712 if (localPort != 0) return localPort;
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
781 issueReadEvent(); 785 issueReadEvent();
782 continue; 786 continue;
783 } 787 }
784 788
785 if (i == WRITE_EVENT) { 789 if (i == WRITE_EVENT) {
786 writeAvailable = true; 790 writeAvailable = true;
787 issueWriteEvent(delayed: false); 791 issueWriteEvent(delayed: false);
788 continue; 792 continue;
789 } 793 }
790 794
791 if (i == READ_EVENT && !isListening) { 795 if (i == READ_EVENT) {
792 var avail = nativeAvailable(); 796 if (isListening) {
793 if (avail is int) { 797 available++;
794 available = avail;
795 } else { 798 } else {
796 // Available failed. Mark socket as having data, to ensure read 799 var avail = nativeAvailable();
797 // events, and thus reporting of this error. 800 if (avail is int) {
798 available = 1; 801 available = avail;
802 } else {
803 // Available failed. Mark socket as having data, to ensure read
804 // events, and thus reporting of this error.
805 available = 1;
806 }
807 issueReadEvent();
808 continue;
799 } 809 }
800 issueReadEvent();
801 continue;
802 } 810 }
803 811
804 var handler = eventHandlers[i]; 812 var handler = eventHandlers[i];
805 if (i == DESTROYED_EVENT) { 813 if (i == DESTROYED_EVENT) {
806 assert(!isClosed); 814 assert(!isClosed);
807 isClosed = true; 815 isClosed = true;
808 closeCompleter.complete(); 816 closeCompleter.complete();
809 disconnectFromEventHandler(); 817 disconnectFromEventHandler();
810 if (handler != null) handler(); 818 if (handler != null) handler();
811 continue; 819 continue;
812 } 820 }
813 821
814 if (i == ERROR_EVENT) { 822 if (i == ERROR_EVENT) {
815 if (!isClosing) { 823 if (!isClosing) {
816 reportError(nativeGetError(), ""); 824 reportError(nativeGetError(), "");
817 } 825 }
818 } else if (!isClosed) { 826 } else if (!isClosed) {
819 // If the connection is closed right after it's accepted, there's a 827 // If the connection is closed right after it's accepted, there's a
820 // chance the close-handler is not set. 828 // chance the close-handler is not set.
821 if (handler != null) handler(); 829 if (handler != null) handler();
822 } 830 }
823 } 831 }
824 } 832 }
825 if (eventPort != null && !isClosing && !isClosed && !isListening) { 833 if (!isListening) {
826 returnTokens++; 834 tokens++;
827 if (returnTokens == 8) { 835 returnTokens();
836 }
837 }
838
839 void returnTokens() {
840 if (eventPort != null && !isClosing && !isClosed) {
841 if (tokens == 8) {
828 // Return in batches of 8. 842 // Return in batches of 8.
829 assert(returnTokens < (1 << FIRST_COMMAND)); 843 assert(tokens < (1 << FIRST_COMMAND));
830 sendToEventHandler((1 << RETURN_TOKEN_COMMAND) | returnTokens); 844 sendToEventHandler((1 << RETURN_TOKEN_COMMAND) | tokens);
831 returnTokens = 0; 845 tokens = 0;
832 } 846 }
833 } 847 }
834 } 848 }
835 849
836 void setHandlers({read, write, error, closed, destroyed}) { 850 void setHandlers({read, write, error, closed, destroyed}) {
837 eventHandlers[READ_EVENT] = read; 851 eventHandlers[READ_EVENT] = read;
838 eventHandlers[WRITE_EVENT] = write; 852 eventHandlers[WRITE_EVENT] = write;
839 eventHandlers[ERROR_EVENT] = error; 853 eventHandlers[ERROR_EVENT] = error;
840 eventHandlers[CLOSED_EVENT] = closed; 854 eventHandlers[CLOSED_EVENT] = closed;
841 eventHandlers[DESTROYED_EVENT] = destroyed; 855 eventHandlers[DESTROYED_EVENT] = destroyed;
(...skipping 236 matching lines...) Expand 10 before | Expand all | Expand 10 after
1078 throw new StateError("Stream was already listened to"); 1092 throw new StateError("Stream was already listened to");
1079 } 1093 }
1080 var zone = Zone.current; 1094 var zone = Zone.current;
1081 _controller = new StreamController(sync: true, 1095 _controller = new StreamController(sync: true,
1082 onListen: _onSubscriptionStateChange, 1096 onListen: _onSubscriptionStateChange,
1083 onCancel: _onSubscriptionStateChange, 1097 onCancel: _onSubscriptionStateChange,
1084 onPause: _onPauseStateChange, 1098 onPause: _onPauseStateChange,
1085 onResume: _onPauseStateChange); 1099 onResume: _onPauseStateChange);
1086 _socket.setHandlers( 1100 _socket.setHandlers(
1087 read: zone.bindCallback(() { 1101 read: zone.bindCallback(() {
1088 do { 1102 while (_socket.available > 0) {
1089 var socket = _socket.accept(); 1103 var socket = _socket.accept();
1090 if (socket == null) return; 1104 if (socket == null) return;
1091 _controller.add(new _RawSocket(socket)); 1105 _controller.add(new _RawSocket(socket));
1092 } while (!_controller.isPaused); 1106 if (_controller.isPaused) return;
1107 }
1093 }), 1108 }),
1094 error: zone.bindUnaryCallback((e) { 1109 error: zone.bindUnaryCallback((e) {
1095 _controller.addError(e); 1110 _controller.addError(e);
1096 _controller.close(); 1111 _controller.close();
1097 }), 1112 }),
1098 destroyed: _controller.close); 1113 destroyed: _controller.close);
1099 return _controller.stream.listen( 1114 return _controller.stream.listen(
1100 onData, 1115 onData,
1101 onError: onError, 1116 onError: onError,
1102 onDone: onDone, 1117 onDone: onDone,
(...skipping 662 matching lines...) Expand 10 before | Expand all | Expand 10 after
1765 String address, 1780 String address,
1766 List<int> in_addr, 1781 List<int> in_addr,
1767 int port) { 1782 int port) {
1768 return new Datagram( 1783 return new Datagram(
1769 data, 1784 data,
1770 new _InternetAddress(address, null, in_addr), 1785 new _InternetAddress(address, null, in_addr),
1771 port); 1786 port);
1772 } 1787 }
1773 1788
1774 String _socketsStats() => _SocketsObservatory.toJSON(); 1789 String _socketsStats() => _SocketsObservatory.toJSON();
OLDNEW
« no previous file with comments | « runtime/bin/eventhandler_macos.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698