OLD | NEW |
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 | 7 |
8 /* | 8 /* |
9 * Keep these constants in sync with the native poll event identifiers. | 9 * Keep these constants in sync with the native poll event identifiers. |
10 */ | 10 */ |
11 static final int _IN_EVENT = 0; | 11 static final int _IN_EVENT = 0; |
12 static final int _OUT_EVENT = 1; | 12 static final int _OUT_EVENT = 1; |
13 static final int _ERROR_EVENT = 2; | 13 static final int _ERROR_EVENT = 2; |
14 static final int _CLOSE_EVENT = 3; | 14 static final int _CLOSE_EVENT = 3; |
15 static final int _FIRST_EVENT = _IN_EVENT; | 15 static final int _FIRST_EVENT = _IN_EVENT; |
16 static final int _LAST_EVENT = _CLOSE_EVENT; | 16 static final int _LAST_EVENT = _CLOSE_EVENT; |
17 | 17 |
18 static final int _CLOSE_COMMAND = 4; | 18 static final int _CLOSE_COMMAND = 8; |
| 19 |
| 20 static final int _LISTEN_SOCKET = 16; |
19 | 21 |
20 _SocketBase () { | 22 _SocketBase () { |
21 _handler = new ReceivePort(); | 23 _handler = new ReceivePort(); |
22 _handlerMap = new List(_CLOSE_EVENT + 1); | 24 _handlerMap = new List(_CLOSE_EVENT + 1); |
23 _handlerMask = 0; | 25 _handlerMask = 0; |
24 _canActivateHandlers = true; | 26 _canActivateHandlers = true; |
25 _id = -1; | 27 _id = -1; |
26 _handler.receive((var message, ignored) { | 28 _handler.receive((var message, ignored) { |
27 _multiplex(message); | 29 _multiplex(message); |
28 }); | 30 }); |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
65 } | 67 } |
66 | 68 |
67 void _getPort() native "Socket_GetPort"; | 69 void _getPort() native "Socket_GetPort"; |
68 | 70 |
69 void setErrorHandler(void callback()) { | 71 void setErrorHandler(void callback()) { |
70 _setHandler(_ERROR_EVENT, callback); | 72 _setHandler(_ERROR_EVENT, callback); |
71 } | 73 } |
72 | 74 |
73 void _doActivateHandlers() { | 75 void _doActivateHandlers() { |
74 if (_canActivateHandlers && (_id >= 0)) { | 76 if (_canActivateHandlers && (_id >= 0)) { |
75 EventHandler._sendData(_id, _handler, _handlerMask); | 77 int data = _handlerMask; |
| 78 if (_isListenSocket()) data |= (1 << _LISTEN_SOCKET); |
| 79 EventHandler._sendData(_id, _handler, data); |
76 } | 80 } |
77 } | 81 } |
78 | 82 |
79 void _scheduleEvent(int event) { | 83 void _scheduleEvent(int event) { |
80 _handler.toSendPort().send([1 << event], null); | 84 _handler.toSendPort().send([1 << event], null); |
81 } | 85 } |
82 | 86 |
83 int get port() { | 87 int get port() { |
84 if (_port == null) { | 88 if (_port == null) { |
85 _port = _getPort(); | 89 _port = _getPort(); |
(...skipping 13 matching lines...) Expand all Loading... |
99 if (_handler == null) { | 103 if (_handler == null) { |
100 throw new | 104 throw new |
101 SocketIOException("Error: close failed - invalid socket handle"); | 105 SocketIOException("Error: close failed - invalid socket handle"); |
102 } else { | 106 } else { |
103 _handler.close(); | 107 _handler.close(); |
104 _handler = null; | 108 _handler = null; |
105 } | 109 } |
106 } | 110 } |
107 } | 111 } |
108 | 112 |
| 113 abstract bool _isListenSocket(); |
109 | 114 |
110 /* | 115 /* |
111 * Socket id is set from native. -1 indicates that the socket was closed. | 116 * Socket id is set from native. -1 indicates that the socket was closed. |
112 */ | 117 */ |
113 int _id; | 118 int _id; |
114 | 119 |
115 /* | 120 /* |
116 * Dedicated ReceivePort for socket events. | 121 * Dedicated ReceivePort for socket events. |
117 */ | 122 */ |
118 ReceivePort _handler; | 123 ReceivePort _handler; |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
173 } | 178 } |
174 | 179 |
175 bool _accept(Socket socket) native "ServerSocket_Accept"; | 180 bool _accept(Socket socket) native "ServerSocket_Accept"; |
176 | 181 |
177 bool _createBindListen(String bindAddress, int port, int backlog) | 182 bool _createBindListen(String bindAddress, int port, int backlog) |
178 native "ServerSocket_CreateBindListen"; | 183 native "ServerSocket_CreateBindListen"; |
179 | 184 |
180 void setConnectionHandler(void callback()) { | 185 void setConnectionHandler(void callback()) { |
181 _setHandler(_IN_EVENT, callback); | 186 _setHandler(_IN_EVENT, callback); |
182 } | 187 } |
| 188 |
| 189 bool _isListenSocket() => true; |
183 } | 190 } |
184 | 191 |
185 | 192 |
186 class _Socket extends _SocketBase implements Socket { | 193 class _Socket extends _SocketBase implements Socket { |
187 /* | 194 /* |
188 * Constructor for socket. First a socket object is allocated | 195 * Constructor for socket. First a socket object is allocated |
189 * in which the native socket is stored. After that _createConnect is | 196 * in which the native socket is stored. After that _createConnect is |
190 * called which creates a file discriptor and connects to the given | 197 * called which creates a file discriptor and connects to the given |
191 * host on the given port. Null is returned if file descriptor creation | 198 * host on the given port. Null is returned if file descriptor creation |
192 * or connect failsed | 199 * or connect failsed |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
268 } | 275 } |
269 | 276 |
270 void setDataHandler(void callback()) { | 277 void setDataHandler(void callback()) { |
271 _setHandler(_IN_EVENT, callback); | 278 _setHandler(_IN_EVENT, callback); |
272 } | 279 } |
273 | 280 |
274 void setCloseHandler(void callback()) { | 281 void setCloseHandler(void callback()) { |
275 _setHandler(_CLOSE_EVENT, callback); | 282 _setHandler(_CLOSE_EVENT, callback); |
276 } | 283 } |
277 | 284 |
| 285 bool _isListenSocket() => false; |
| 286 |
278 InputStream get inputStream() { | 287 InputStream get inputStream() { |
279 if (_inputStream == null) { | 288 if (_inputStream == null) { |
280 _inputStream = new SocketInputStream(this); | 289 _inputStream = new SocketInputStream(this); |
281 } | 290 } |
282 return _inputStream; | 291 return _inputStream; |
283 } | 292 } |
284 | 293 |
285 OutputStream get outputStream() { | 294 OutputStream get outputStream() { |
286 if (_outputStream == null) { | 295 if (_outputStream == null) { |
287 _outputStream = new SocketOutputStream(this); | 296 _outputStream = new SocketOutputStream(this); |
288 } | 297 } |
289 return _outputStream; | 298 return _outputStream; |
290 } | 299 } |
291 | 300 |
292 SocketInputStream _inputStream; | 301 SocketInputStream _inputStream; |
293 SocketOutputStream _outputStream; | 302 SocketOutputStream _outputStream; |
294 } | 303 } |
295 | 304 |
OLD | NEW |