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

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

Issue 8413034: New OutputStream interface (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Minor fix Created 9 years, 1 month 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) 2011, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2011, 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 5
6 class _SocketBase { 6 class _SocketBase {
7 // Bit flags used when communicating between the eventhandler and 7 // Bit flags used when communicating between the eventhandler and
8 // dart code. The EVENT flags are used to indicate events of 8 // dart code. The EVENT flags are used to indicate events of
9 // interest when sending a message from dart code to the 9 // interest when sending a message from dart code to the
10 // eventhandler. When receiving a message from the eventhandler the 10 // eventhandler. When receiving a message from the eventhandler the
(...skipping 29 matching lines...) Expand all
40 } 40 }
41 41
42 /* 42 /*
43 * Multiplexes socket events to the right socket handler. 43 * Multiplexes socket events to the right socket handler.
44 */ 44 */
45 void _multiplex(List<int> message) { 45 void _multiplex(List<int> message) {
46 assert(message.length == 1); 46 assert(message.length == 1);
47 _canActivateHandlers = false; 47 _canActivateHandlers = false;
48 int event_mask = message[0]; 48 int event_mask = message[0];
49 for (int i = _FIRST_EVENT; i <= _LAST_EVENT; i++) { 49 for (int i = _FIRST_EVENT; i <= _LAST_EVENT; i++) {
50 if (((event_mask & (1 << i)) != 0) && _handlerMap[i] !== null) { 50 if (((event_mask & (1 << i)) != 0) && _handlerMap[i] != null) {
51 var handleEvent = _handlerMap[i]; 51 var handleEvent = _handlerMap[i];
52 52
53 // Unregister the out handler before executing it. 53 // Unregister the out handler before executing it.
54 if (i == _OUT_EVENT) _setHandler(i, null); 54 if (i == _OUT_EVENT) _setHandler(i, null);
55 55
56 // Don't call the in handler if there is no data available 56 // Don't call the in handler if there is no data available
57 // after all. 57 // after all.
58 if (i == _IN_EVENT && this is _Socket && available() == 0) continue; 58 if (i == _IN_EVENT && this is _Socket && available() == 0) continue;
59 59
60 handleEvent(); 60 handleEvent();
61 } 61 }
62 } 62 }
63 _canActivateHandlers = true; 63 _canActivateHandlers = true;
64 _doActivateHandlers(); 64 _activateHandlers();
65 } 65 }
66 66
67 void _setHandler(int event, void callback()) { 67 void _setHandler(int event, void callback()) {
68 if (callback === null) { 68 if (callback == null) {
69 _handlerMask &= ~(1 << event); 69 _handlerMask &= ~(1 << event);
70 } else { 70 } else {
71 _handlerMask |= (1 << event); 71 _handlerMask |= (1 << event);
72 } 72 }
73 _handlerMap[event] = callback; 73 _handlerMap[event] = callback;
74 _doActivateHandlers(); 74 _activateHandlers();
75 } 75 }
76 76
77 void _getPort() native "Socket_GetPort"; 77 void _getPort() native "Socket_GetPort";
78 78
79 void setErrorHandler(void callback()) { 79 void setErrorHandler(void callback()) {
80 _setHandler(_ERROR_EVENT, callback); 80 _setHandler(_ERROR_EVENT, callback);
81 } 81 }
82 82
83 void _doActivateHandlers() { 83 void _activateHandlers() {
84 if (_canActivateHandlers && (_id >= 0)) { 84 if (_canActivateHandlers && (_id >= 0)) {
85 int data = _handlerMask; 85 int data = _handlerMask;
86 if (_isListenSocket()) data |= (1 << _LISTENING_SOCKET); 86 if (_isListenSocket()) data |= (1 << _LISTENING_SOCKET);
87 EventHandler._sendData(_id, _handler, data); 87 EventHandler._sendData(_id, _handler, data);
88 } 88 }
89 } 89 }
90 90
91 void _scheduleEvent(int event) { 91 void _scheduleEvent(int event) {
92 _handler.toSendPort().send([1 << event], null); 92 _handler.toSendPort().send([1 << event], null);
93 } 93 }
94 94
95 int get port() { 95 int get port() {
96 if (_port === null) { 96 if (_port === null) {
97 _port = _getPort(); 97 _port = _getPort();
98 } 98 }
99 return _port; 99 return _port;
100 } 100 }
101 101
102 void close() { 102 void close() {
103 if (_id >= 0) { 103 if (_id >= 0) {
104 EventHandler._sendData(_id, _handler, 1 << _CLOSE_COMMAND); 104 EventHandler._sendData(_id, _handler, 1 << _CLOSE_COMMAND);
105 _handler.close(); 105 _handler.close();
106 _handler = null; 106 _handler = null;
107 _id = -1; 107 _id = -1;
108 } else { 108 } else if (_handler != null) {
109 // This is to support closing sockets created but never assigned 109 // This is to support closing sockets created but never assigned
110 // any actual socket. 110 // any actual socket.
111 if (_handler === null) { 111 _handler.close();
112 throw new 112 _handler = null;
113 SocketIOException("Error: close failed - invalid socket handle");
114 } else {
115 _handler.close();
116 _handler = null;
117 }
118 } 113 }
114 // Don't throw exceptions when closing sockets more than once.
Mads Ager (google) 2011/10/31 09:56:48 Maybe remove this here and add it is a comment to
Søren Gjesse 2011/11/01 08:56:46 Done.
119 } 115 }
120 116
121 abstract bool _isListenSocket(); 117 abstract bool _isListenSocket();
122 118
123 /* 119 /*
124 * Socket id is set from native. -1 indicates that the socket was closed. 120 * Socket id is set from native. -1 indicates that the socket was closed.
125 */ 121 */
126 int _id; 122 int _id;
127 123
128 /* 124 /*
(...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after
303 if (_outputStream === null) { 299 if (_outputStream === null) {
304 _outputStream = new SocketOutputStream(this); 300 _outputStream = new SocketOutputStream(this);
305 } 301 }
306 return _outputStream; 302 return _outputStream;
307 } 303 }
308 304
309 SocketInputStream _inputStream; 305 SocketInputStream _inputStream;
310 SocketOutputStream _outputStream; 306 SocketOutputStream _outputStream;
311 } 307 }
312 308
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698