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 interface ServerSocket default _ServerSocket { | 5 abstract class ServerSocket { |
6 /** | 6 /** |
7 * Constructs a new server socket, binds it to a given address and port, | 7 * Constructs a new server socket, binds it to a given address and port, |
8 * and listens on it. | 8 * and listens on it. |
9 */ | 9 */ |
10 ServerSocket(String bindAddress, int port, int backlog); | 10 factory ServerSocket(String bindAddress, int port, int backlog) { |
| 11 return new _ServerSocket(bindAddress, port, backlog); |
| 12 } |
11 | 13 |
12 /** | 14 /** |
13 * The connection handler gets called when there is a new incoming | 15 * The connection handler gets called when there is a new incoming |
14 * connection on the socket. | 16 * connection on the socket. |
15 */ | 17 */ |
16 void set onConnection(void callback(Socket connection)); | 18 void set onConnection(void callback(Socket connection)); |
17 | 19 |
18 /** | 20 /** |
19 * The error handler gets called when a socket error occurs. | 21 * The error handler gets called when a socket error occurs. |
20 */ | 22 */ |
21 void set onError(void callback(e)); | 23 void set onError(void callback(e)); |
22 | 24 |
23 /** | 25 /** |
24 * Returns the port used by this socket. | 26 * Returns the port used by this socket. |
25 */ | 27 */ |
26 int get port; | 28 int get port; |
27 | 29 |
28 /** | 30 /** |
29 * Closes the socket. | 31 * Closes the socket. |
30 */ | 32 */ |
31 void close(); | 33 void close(); |
32 } | 34 } |
33 | 35 |
34 | 36 |
35 interface Socket extends Hashable default _Socket { | 37 abstract class Socket implements Hashable { |
36 /** | 38 /** |
37 * Constructs a new socket and initiate connecting it to the given | 39 * Constructs a new socket and initiate connecting it to the given |
38 * host on the given port. The returned socket is not yet connected | 40 * host on the given port. The returned socket is not yet connected |
39 * but ready for registration of callbacks. | 41 * but ready for registration of callbacks. |
40 */ | 42 */ |
41 Socket(String host, int port); | 43 factory Socket(String host, int port) => new _Socket(host, port); |
42 | 44 |
43 /** | 45 /** |
44 * Returns the number of received and non-read bytes in the socket that | 46 * Returns the number of received and non-read bytes in the socket that |
45 * can be read. | 47 * can be read. |
46 */ | 48 */ |
47 int available(); | 49 int available(); |
48 | 50 |
49 /** | 51 /** |
50 * Reads up to [count] bytes of data from the socket and stores them into | 52 * Reads up to [count] bytes of data from the socket and stores them into |
51 * buffer after buffer offset [offset]. The number of successfully read | 53 * buffer after buffer offset [offset]. The number of successfully read |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
118 */ | 120 */ |
119 String get remoteHost; | 121 String get remoteHost; |
120 | 122 |
121 /** | 123 /** |
122 * Closes the socket. Calling [close] will never throw an exception | 124 * Closes the socket. Calling [close] will never throw an exception |
123 * and calling it several times is supported. If [halfClose] is true | 125 * and calling it several times is supported. If [halfClose] is true |
124 * the socket will only be closed for writing and it might still be | 126 * the socket will only be closed for writing and it might still be |
125 * possible to read data. Calling [close] will not trigger a call to | 127 * possible to read data. Calling [close] will not trigger a call to |
126 * [onClosed]. | 128 * [onClosed]. |
127 */ | 129 */ |
128 void close([bool halfClose]); | 130 void close([bool halfClose = false]); |
129 | 131 |
130 /** | 132 /** |
131 * Socket is hashable. | 133 * Socket is hashable. |
132 */ | 134 */ |
133 int hashCode(); | 135 int hashCode(); |
134 } | 136 } |
135 | 137 |
136 | 138 |
137 class SocketIOException implements Exception { | 139 class SocketIOException implements Exception { |
138 const SocketIOException([String this.message = "", | 140 const SocketIOException([String this.message = "", |
139 OSError this.osError = null]); | 141 OSError this.osError = null]); |
140 String toString() { | 142 String toString() { |
141 StringBuffer sb = new StringBuffer(); | 143 StringBuffer sb = new StringBuffer(); |
142 sb.add("SocketIOException"); | 144 sb.add("SocketIOException"); |
143 if (!message.isEmpty()) { | 145 if (!message.isEmpty()) { |
144 sb.add(": $message"); | 146 sb.add(": $message"); |
145 if (osError != null) { | 147 if (osError != null) { |
146 sb.add(" ($osError)"); | 148 sb.add(" ($osError)"); |
147 } | 149 } |
148 } else if (osError != null) { | 150 } else if (osError != null) { |
149 sb.add(": $osError"); | 151 sb.add(": $osError"); |
150 } | 152 } |
151 return sb.toString(); | 153 return sb.toString(); |
152 } | 154 } |
153 final String message; | 155 final String message; |
154 final OSError osError; | 156 final OSError osError; |
155 } | 157 } |
OLD | NEW |