OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
84 _handler != null) { | 84 _handler != null) { |
85 _handler.close(); | 85 _handler.close(); |
86 _handler = null; | 86 _handler = null; |
87 } else { | 87 } else { |
88 _activateHandlers(); | 88 _activateHandlers(); |
89 } | 89 } |
90 } | 90 } |
91 | 91 |
92 void _getPort() native "Socket_GetPort"; | 92 void _getPort() native "Socket_GetPort"; |
93 | 93 |
94 void set errorHandler(void callback()) { | 94 void set onError(void callback()) { |
95 _setHandler(_ERROR_EVENT, callback); | 95 _setHandler(_ERROR_EVENT, callback); |
96 } | 96 } |
97 | 97 |
98 void _activateHandlers() { | 98 void _activateHandlers() { |
99 if (_canActivateHandlers && (_id >= 0)) { | 99 if (_canActivateHandlers && (_id >= 0)) { |
100 if (_handlerMask == 0) { | 100 if (_handlerMask == 0) { |
101 if (_handler != null) { | 101 if (_handler != null) { |
102 _handler.close(); | 102 _handler.close(); |
103 _handler = null; | 103 _handler = null; |
104 } | 104 } |
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
219 return socket; | 219 return socket; |
220 } | 220 } |
221 | 221 |
222 _ServerSocket._internal(); | 222 _ServerSocket._internal(); |
223 | 223 |
224 bool _accept(Socket socket) native "ServerSocket_Accept"; | 224 bool _accept(Socket socket) native "ServerSocket_Accept"; |
225 | 225 |
226 bool _createBindListen(String bindAddress, int port, int backlog) | 226 bool _createBindListen(String bindAddress, int port, int backlog) |
227 native "ServerSocket_CreateBindListen"; | 227 native "ServerSocket_CreateBindListen"; |
228 | 228 |
229 void set connectionHandler(void callback(Socket connection)) { | 229 void set onConnection(void callback(Socket connection)) { |
230 _clientConnectionHandler = callback; | 230 _clientConnectionHandler = callback; |
231 _setHandler(_IN_EVENT, | 231 _setHandler(_IN_EVENT, |
232 _clientConnectionHandler != null ? _connectionHandler : null); | 232 _clientConnectionHandler != null ? _connectionHandler : null); |
233 } | 233 } |
234 | 234 |
235 void _connectionHandler() { | 235 void _connectionHandler() { |
236 if (_id >= 0) { | 236 if (_id >= 0) { |
237 _Socket socket = new _Socket._internal(); | 237 _Socket socket = new _Socket._internal(); |
238 if (_accept(socket)) _clientConnectionHandler(socket); | 238 if (_accept(socket)) _clientConnectionHandler(socket); |
239 } | 239 } |
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
352 SocketIOException("Error: writeList failed - invalid socket handle"); | 352 SocketIOException("Error: writeList failed - invalid socket handle"); |
353 } | 353 } |
354 | 354 |
355 int _writeList(List<int> buffer, int offset, int bytes) | 355 int _writeList(List<int> buffer, int offset, int bytes) |
356 native "Socket_WriteList"; | 356 native "Socket_WriteList"; |
357 | 357 |
358 void _reportError() { | 358 void _reportError() { |
359 // For all errors we close the socket, call the error handler and | 359 // For all errors we close the socket, call the error handler and |
360 // disable further calls of the error handler. | 360 // disable further calls of the error handler. |
361 close(); | 361 close(); |
362 var errorHandler = _handlerMap[_ERROR_EVENT]; | 362 var onError = _handlerMap[_ERROR_EVENT]; |
363 if (errorHandler != null) { | 363 if (onError != null) { |
364 errorHandler(); | 364 onError(); |
365 _setHandler(_ERROR_EVENT, null); | 365 _setHandler(_ERROR_EVENT, null); |
366 } | 366 } |
367 } | 367 } |
368 | 368 |
369 bool _createConnect(String host, int port) native "Socket_CreateConnect"; | 369 bool _createConnect(String host, int port) native "Socket_CreateConnect"; |
370 | 370 |
371 void set writeHandler(void callback()) { | 371 void set onWrite(void callback()) { |
372 if (_outputStream != null) throw new StreamException( | 372 if (_outputStream != null) throw new StreamException( |
373 "Cannot set write handler when output stream is used"); | 373 "Cannot set write handler when output stream is used"); |
374 _clientWriteHandler = callback; | 374 _clientWriteHandler = callback; |
375 _updateOutHandler(); | 375 _updateOutHandler(); |
376 } | 376 } |
377 | 377 |
378 void set connectHandler(void callback()) { | 378 void set onConnect(void callback()) { |
379 if (_seenFirstOutEvent || _outputStream != null) { | 379 if (_seenFirstOutEvent || _outputStream != null) { |
380 throw new StreamException( | 380 throw new StreamException( |
381 "Cannot set connect handler when already connected"); | 381 "Cannot set connect handler when already connected"); |
382 } | 382 } |
383 if (_outputStream != null) { | 383 if (_outputStream != null) { |
384 throw new StreamException( | 384 throw new StreamException( |
385 "Cannot set connect handler when output stream is used"); | 385 "Cannot set connect handler when output stream is used"); |
386 } | 386 } |
387 _clientConnectHandler = callback; | 387 _clientConnectHandler = callback; |
388 _updateOutHandler(); | 388 _updateOutHandler(); |
389 } | 389 } |
390 | 390 |
391 void set dataHandler(void callback()) { | 391 void set onData(void callback()) { |
392 if (_inputStream != null) throw new StreamException( | 392 if (_inputStream != null) throw new StreamException( |
393 "Cannot set data handler when input stream is used"); | 393 "Cannot set data handler when input stream is used"); |
394 _dataHandler = callback; | 394 _onData = callback; |
395 } | 395 } |
396 | 396 |
397 void set closeHandler(void callback()) { | 397 void set onClosed(void callback()) { |
398 if (_inputStream != null) throw new StreamException( | 398 if (_inputStream != null) throw new StreamException( |
399 "Cannot set close handler when input stream is used"); | 399 "Cannot set close handler when input stream is used"); |
400 _closeHandler = callback; | 400 _onClosed = callback; |
401 } | 401 } |
402 | 402 |
403 bool _isListenSocket() => false; | 403 bool _isListenSocket() => false; |
404 | 404 |
405 bool _isPipe() => _pipe; | 405 bool _isPipe() => _pipe; |
406 | 406 |
407 InputStream get inputStream() { | 407 InputStream get inputStream() { |
408 if (_inputStream === null) { | 408 if (_inputStream === null) { |
409 if (_handlerMap[_IN_EVENT] !== null || | 409 if (_handlerMap[_IN_EVENT] !== null || |
410 _handlerMap[_CLOSE_EVENT] !== null) { | 410 _handlerMap[_CLOSE_EVENT] !== null) { |
411 throw new StreamException( | 411 throw new StreamException( |
412 "Cannot get input stream when socket handlers are used"); | 412 "Cannot get input stream when socket handlers are used"); |
413 } | 413 } |
414 _inputStream = new SocketInputStream(this); | 414 _inputStream = new SocketInputStream(this); |
415 } | 415 } |
416 return _inputStream; | 416 return _inputStream; |
417 } | 417 } |
418 | 418 |
419 OutputStream get outputStream() { | 419 OutputStream get outputStream() { |
420 if (_outputStream === null) { | 420 if (_outputStream === null) { |
421 if (_handlerMap[_OUT_EVENT] !== null) { | 421 if (_handlerMap[_OUT_EVENT] !== null) { |
422 throw new StreamException( | 422 throw new StreamException( |
423 "Cannot get input stream when socket handlers are used"); | 423 "Cannot get input stream when socket handlers are used"); |
424 } | 424 } |
425 _outputStream = new SocketOutputStream(this); | 425 _outputStream = new SocketOutputStream(this); |
426 } | 426 } |
427 return _outputStream; | 427 return _outputStream; |
428 } | 428 } |
429 | 429 |
430 void set _writeHandler(void callback()) { | 430 void set _onWrite(void callback()) { |
431 _setHandler(_OUT_EVENT, callback); | 431 _setHandler(_OUT_EVENT, callback); |
432 } | 432 } |
433 | 433 |
434 void set _dataHandler(void callback()) { | 434 void set _onData(void callback()) { |
435 _setHandler(_IN_EVENT, callback); | 435 _setHandler(_IN_EVENT, callback); |
436 } | 436 } |
437 | 437 |
438 void set _closeHandler(void callback()) { | 438 void set _onClosed(void callback()) { |
439 _setHandler(_CLOSE_EVENT, callback); | 439 _setHandler(_CLOSE_EVENT, callback); |
440 } | 440 } |
441 | 441 |
442 void _updateOutHandler() { | 442 void _updateOutHandler() { |
443 void firstWriteHandler() { | 443 void firstWriteHandler() { |
444 assert(!_seenFirstOutEvent); | 444 assert(!_seenFirstOutEvent); |
445 _seenFirstOutEvent = true; | 445 _seenFirstOutEvent = true; |
446 | 446 |
447 // From now on the write handler is only the client write | 447 // From now on the write handler is only the client write |
448 // handler (connect handler cannot be called again). Change this | 448 // handler (connect handler cannot be called again). Change this |
449 // before calling any handlers as handlers can change the | 449 // before calling any handlers as handlers can change the |
450 // handlers. | 450 // handlers. |
451 if (_clientWriteHandler === null) _writeHandler = _clientWriteHandler; | 451 if (_clientWriteHandler === null) _onWrite = _clientWriteHandler; |
452 | 452 |
453 // First out event is socket connected event. | 453 // First out event is socket connected event. |
454 if (_clientConnectHandler !== null) _clientConnectHandler(); | 454 if (_clientConnectHandler !== null) _clientConnectHandler(); |
455 _clientConnectHandler = null; | 455 _clientConnectHandler = null; |
456 | 456 |
457 // Always (even for the first out event) call the write handler. | 457 // Always (even for the first out event) call the write handler. |
458 if (_clientWriteHandler !== null) _clientWriteHandler(); | 458 if (_clientWriteHandler !== null) _clientWriteHandler(); |
459 } | 459 } |
460 | 460 |
461 if (_clientConnectHandler === null && _clientWriteHandler === null) { | 461 if (_clientConnectHandler === null && _clientWriteHandler === null) { |
462 _writeHandler = null; | 462 _onWrite = null; |
463 } else { | 463 } else { |
464 if (_seenFirstOutEvent) { | 464 if (_seenFirstOutEvent) { |
465 _writeHandler = _clientWriteHandler; | 465 _onWrite = _clientWriteHandler; |
466 } else { | 466 } else { |
467 _writeHandler = firstWriteHandler; | 467 _onWrite = firstWriteHandler; |
468 } | 468 } |
469 } | 469 } |
470 } | 470 } |
471 | 471 |
472 bool _seenFirstOutEvent = false; | 472 bool _seenFirstOutEvent = false; |
473 bool _closedRead = false; | 473 bool _closedRead = false; |
474 bool _closedWrite = false; | 474 bool _closedWrite = false; |
475 bool _pipe = false; | 475 bool _pipe = false; |
476 Function _clientConnectHandler; | 476 Function _clientConnectHandler; |
477 Function _clientWriteHandler; | 477 Function _clientWriteHandler; |
478 SocketInputStream _inputStream; | 478 SocketInputStream _inputStream; |
479 SocketOutputStream _outputStream; | 479 SocketOutputStream _outputStream; |
480 } | 480 } |
481 | 481 |
OLD | NEW |