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

Side by Side Diff: sdk/lib/io/socket.dart

Issue 12316036: Merge IO v2 branch to bleeding edge (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Rebased to r18818 Created 7 years, 10 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 | « sdk/lib/io/secure_socket.dart ('k') | sdk/lib/io/socket_stream_impl.dart » ('j') | 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) 2012, 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 part of dart.io; 5 part of dart.io;
6 6
7 abstract class ServerSocket { 7 /**
8 * The RawServerSocket is a server socket, providing a stream of low-level
9 * [RawSocket]s.
10 *
11 * See [RawSocket] for more info.
12 */
13 abstract class RawServerSocket implements Stream<RawSocket> {
8 /** 14 /**
9 * Constructs a new server socket, binds it to a given address and port, 15 * Returns a future for a [:RawServerSocket:]. When the future
10 * and listens on it. 16 * completes the server socket is bound to the given [address] and
17 * [port] and has started listening on it.
18 *
19 * If [port] has the value [:0:] (the default) an ephemeral port will
20 * be chosen by the system. The actual port used can be retrieved
21 * using the [:port:] getter.
22 *
23 * If [backlog] has the value of [:0:] a reasonable value will be
24 * chosen by the system.
11 */ 25 */
12 external factory ServerSocket(String bindAddress, int port, int backlog); 26 external static Future<RawServerSocket> bind([String address = "127.0.0.1",
13 27 int port = 0,
14 /** 28 int backlog = 0]);
15 * The connection handler gets called when there is a new incoming
16 * connection on the socket.
17 */
18 void set onConnection(void callback(Socket connection));
19
20 /**
21 * The error handler gets called when a socket error occurs.
22 */
23 void set onError(void callback(e));
24 29
25 /** 30 /**
26 * Returns the port used by this socket. 31 * Returns the port used by this socket.
32 */
33 int get port;
34
35 /**
36 * Closes the socket.
37 */
38 void close();
39 }
40
41
42 /**
43 * The [ServerSocket] is server socket, providing a stream of high-level
44 * [Socket]s.
45 *
46 * See [Socket] for more info.
47 */
48 abstract class ServerSocket implements Stream<Socket> {
49 /**
50 * Returns a future for a [:ServerSocket:]. When the future
51 * completes the server socket is bound to the given [address] and
52 * [port] and has started listening on it.
53 *
54 * If [port] has the value [:0:] (the default) an ephemeral port will
55 * be chosen by the system. The actual port used can be retrieved
56 * using the [port] getter.
57 *
58 * If [backlog] has the value of [:0:] a reasonable value will be
59 * chosen by the system.
60 */
61 external static Future<ServerSocket> bind([String address = "127.0.0.1",
62 int port = 0,
63 int backlog = 0]);
64
65 /**
66 * Returns the port used by this socket.
27 */ 67 */
28 int get port; 68 int get port;
29 69
30 /** 70 /**
31 * Closes the socket. 71 * Closes the socket.
32 */ 72 */
33 void close(); 73 void close();
34 } 74 }
35 75
76 /**
77 * The [SocketDirection] is used as a parameter to [Socket.close] and
78 * [RawSocket.close] to close a socket in the specified direction(s).
79 */
80 class SocketDirection {
81 static const SocketDirection RECEIVE = const SocketDirection._(0);
82 static const SocketDirection SEND = const SocketDirection._(1);
83 static const SocketDirection BOTH = const SocketDirection._(2);
84 const SocketDirection._(this._value);
85 final _value;
86 }
36 87
37 abstract class Socket { 88 /**
89 * Events for the [RawSocket].
90 */
91 class RawSocketEvent {
92 static const RawSocketEvent READ = const RawSocketEvent._(0);
93 static const RawSocketEvent WRITE = const RawSocketEvent._(1);
94 static const RawSocketEvent READ_CLOSED = const RawSocketEvent._(2);
95 const RawSocketEvent._(this._value);
96 final int _value;
97 String toString() {
98 return ['RawSocketEvent:READ',
99 'RawSocketEvent:WRITE',
100 'RawSocketEvent:READ_CLOSED'][_value];
101 }
102 }
103
104 /**
105 * The [RawSocket] is a low-level interface to a socket, exposing the raw
106 * events signaled by the system. It's a [Stream] of [RawSocketEvent]s.
107 */
108 abstract class RawSocket implements Stream<RawSocketEvent> {
38 /** 109 /**
39 * Constructs a new socket and initiate connecting it to the given 110 * Creats a new socket connection to the host and port and returns a [Future]
40 * host on the given port. The returned socket is not yet connected 111 * that will complete with either a [RawSocket] once connected or an error
41 * but ready for registration of callbacks. 112 * if the host-lookup or connection failed.
42 */ 113 */
43 external factory Socket(String host, int port); 114 external static Future<RawSocket> connect(String host, int port);
44 115
45 /** 116 /**
46 * Returns the number of received and non-read bytes in the socket that 117 * Returns the number of received and non-read bytes in the socket that
47 * can be read. 118 * can be read.
48 */ 119 */
49 int available(); 120 int available();
50 121
51 /** 122 /**
52 * Read up to [len] bytes from the socket. This function is 123 * Read up to [len] bytes from the socket. This function is
53 * non-blocking and will only return data if data is available. The 124 * non-blocking and will only return data if data is available. The
54 * number of bytes read can be less then [len] if fewer bytes are 125 * number of bytes read can be less then [len] if fewer bytes are
55 * available for immediate reading. If no data is available [null] 126 * available for immediate reading. If no data is available [null]
56 * is returned. 127 * is returned.
57 */ 128 */
58 List<int> read([int len]); 129 List<int> read([int len]);
59 130
60 /** 131 /**
61 * Reads up to [count] bytes of data from the socket and stores them into
62 * buffer after buffer offset [offset]. The number of successfully read
63 * bytes is returned. This function is non-blocking and will only read data
64 * if data is available.
65 */
66 int readList(List<int> buffer, int offset, int count);
67
68 /**
69 * Writes up to [count] bytes of the buffer from [offset] buffer offset to 132 * Writes up to [count] bytes of the buffer from [offset] buffer offset to
70 * the socket. The number of successfully written bytes is returned. This 133 * the socket. The number of successfully written bytes is returned. This
71 * function is non-blocking and will only write data if buffer space is 134 * function is non-blocking and will only write data if buffer space is
72 * available in the socket. 135 * available in the socket.
73 */ 136 */
74 int writeList(List<int> buffer, int offset, int count); 137 int write(List<int> buffer, [int offset, int count]);
75
76 /**
77 * The connect handler gets called when connection to a given host
78 * succeeded.
79 */
80 void set onConnect(void callback());
81
82 /**
83 * The data handler gets called when data becomes available at the socket.
84 */
85 void set onData(void callback());
86
87 /**
88 * The write handler gets called once when the socket becomes
89 * available for writing. Then the handler is automatically reset to null.
90 * This handler is mainly used when writeList has reported an incomplete
91 * write, to schedule writing the remaining data to the socket.
92 */
93 void set onWrite(void callback());
94
95 /**
96 * The close handler gets called when a the last byte have been read
97 * from a socket. At this point the socket might still be open for
98 * writing for sending more data.
99 */
100 void set onClosed(void callback());
101
102 /**
103 * The error handler gets called when a socket error occurs.
104 */
105 void set onError(void callback(e));
106
107 /**
108 * Returns input stream to the socket.
109 */
110 InputStream get inputStream;
111
112 /**
113 * Returns output stream of the socket.
114 */
115 OutputStream get outputStream;
116 138
117 /** 139 /**
118 * Returns the port used by this socket. 140 * Returns the port used by this socket.
119 */ 141 */
120 int get port; 142 int get port;
121 143
122 /** 144 /**
123 * Returns the remote port connected to by this socket. 145 * Returns the remote port connected to by this socket.
124 */ 146 */
125 int get remotePort; 147 int get remotePort;
126 148
127 /** 149 /**
128 * Returns the remote host connected to by this socket. 150 * Returns the remote host connected to by this socket.
129 */ 151 */
130 String get remoteHost; 152 String get remoteHost;
131 153
132 /** 154 /**
133 * Closes the socket. Calling [close] will never throw an exception 155 * Closes the socket. Calling [close] will never throw an exception
134 * and calling it several times is supported. If [halfClose] is true 156 * and calling it several times is supported. Calling [close] can result in
135 * the socket will only be closed for writing and it might still be 157 * a [RawSocketEvent.READ_CLOSED] event.
136 * possible to read data. Calling [close] will not trigger a call to
137 * [onClosed].
138 */ 158 */
139 void close([bool halfClose = false]); 159 void close();
160
161 /**
162 * Shutdown the socket in the [direction]. Calling [shutdown] will never
163 * throw an exception and calling it several times is supported. Calling
164 * shutdown with either [SocketDirection.BOTH] or [SocketDirection.RECEIVE]
165 * can result in a [RawSocketEvent.READ_CLOSED] event.
166 */
167 void shutdown(SocketDirection direction);
168
169 /**
170 * Set or get, if the [RawSocket] should listen for [RawSocketEvent.READ]
171 * events. Default is [true].
172 */
173 bool readEventsEnabled;
174
175 /**
176 * Set or get, if the [RawSocket] should listen for [RawSocketEvent.WRITE]
177 * events. Default is [true].
178 * This is a one-shot listener, and writeEventsEnabled must be set
179 * to true again to receive another write event.
180 */
181 bool writeEventsEnabled;
182 }
183
184 /**
185 * A high-level class for communicating over a TCP socket. The [Socket] exposes
186 * both a [Stream] and a [IOSink] interface, making it ideal for
187 * using together with other [Stream]s.
188 */
189 abstract class Socket implements Stream<List<int>>,
190 IOSink<Socket> {
191 /**
192 * Creats a new socket connection to the host and port and returns a [Future]
193 * that will complete with either a [RawSocket] once connected or an error
194 * if the host-lookup or connection failed.
195 */
196 external static Future<Socket> connect(String host, int port);
197
198 /**
199 * Destroy the socket in both directions. Calling [destroy] will make the
200 * send a close event on the stream and will no longer react on data being
201 * piped to it.
202 *
203 * Call [close](inherited by [IOSink]) to only close the [Socket]
204 * for sending data.
205 */
206 void destroy();
207
208 int get port;
209 String get remoteHost;
210 int get remotePort;
140 } 211 }
141 212
142 213
143 class SocketIOException implements Exception { 214 class SocketIOException implements Exception {
144 const SocketIOException([String this.message = "", 215 const SocketIOException([String this.message = "",
145 OSError this.osError = null]); 216 OSError this.osError = null]);
146 String toString() { 217 String toString() {
147 StringBuffer sb = new StringBuffer(); 218 StringBuffer sb = new StringBuffer();
148 sb.add("SocketIOException"); 219 sb.add("SocketIOException");
149 if (!message.isEmpty) { 220 if (!message.isEmpty) {
150 sb.add(": $message"); 221 sb.add(": $message");
151 if (osError != null) { 222 if (osError != null) {
152 sb.add(" ($osError)"); 223 sb.add(" ($osError)");
153 } 224 }
154 } else if (osError != null) { 225 } else if (osError != null) {
155 sb.add(": $osError"); 226 sb.add(": $osError");
156 } 227 }
157 return sb.toString(); 228 return sb.toString();
158 } 229 }
159 final String message; 230 final String message;
160 final OSError osError; 231 final OSError osError;
161 } 232 }
OLDNEW
« no previous file with comments | « sdk/lib/io/secure_socket.dart ('k') | sdk/lib/io/socket_stream_impl.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698