OLD | NEW |
| (Empty) |
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 | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 abstract class ServerSocket { | |
6 /** | |
7 * Constructs a new server socket, binds it to a given address and port, | |
8 * and listens on it. | |
9 */ | |
10 factory ServerSocket(String bindAddress, int port, int backlog) { | |
11 return new _ServerSocket(bindAddress, port, backlog); | |
12 } | |
13 | |
14 /** | |
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 | |
25 /** | |
26 * Returns the port used by this socket. | |
27 */ | |
28 int get port; | |
29 | |
30 /** | |
31 * Closes the socket. | |
32 */ | |
33 void close(); | |
34 } | |
35 | |
36 | |
37 abstract class Socket { | |
38 /** | |
39 * Constructs a new socket and initiate connecting it to the given | |
40 * host on the given port. The returned socket is not yet connected | |
41 * but ready for registration of callbacks. | |
42 */ | |
43 factory Socket(String host, int port) => new _Socket(host, port); | |
44 | |
45 /** | |
46 * Returns the number of received and non-read bytes in the socket that | |
47 * can be read. | |
48 */ | |
49 int available(); | |
50 | |
51 /** | |
52 * Reads up to [count] bytes of data from the socket and stores them into | |
53 * buffer after buffer offset [offset]. The number of successfully read | |
54 * bytes is returned. This function is non-blocking and will only read data | |
55 * if data is available. | |
56 */ | |
57 int readList(List<int> buffer, int offset, int count); | |
58 | |
59 /** | |
60 * Writes up to [count] bytes of the buffer from [offset] buffer offset to | |
61 * the socket. The number of successfully written bytes is returned. This | |
62 * function is non-blocking and will only write data if buffer space is | |
63 * available in the socket. | |
64 */ | |
65 int writeList(List<int> buffer, int offset, int count); | |
66 | |
67 /** | |
68 * The connect handler gets called when connection to a given host | |
69 * succeeded. | |
70 */ | |
71 void set onConnect(void callback()); | |
72 | |
73 /** | |
74 * The data handler gets called when data becomes available at the socket. | |
75 */ | |
76 void set onData(void callback()); | |
77 | |
78 /** | |
79 * The write handler gets called once when the socket becomes | |
80 * available for writing. Then the handler is automatically reset to null. | |
81 * This handler is mainly used when writeList has reported an incomplete | |
82 * write, to schedule writing the remaining data to the socket. | |
83 */ | |
84 void set onWrite(void callback()); | |
85 | |
86 /** | |
87 * The close handler gets called when a the last byte have been read | |
88 * from a socket. At this point the socket might still be open for | |
89 * writing for sending more data. | |
90 */ | |
91 void set onClosed(void callback()); | |
92 | |
93 /** | |
94 * The error handler gets called when a socket error occurs. | |
95 */ | |
96 void set onError(void callback(e)); | |
97 | |
98 /** | |
99 * Returns input stream to the socket. | |
100 */ | |
101 InputStream get inputStream; | |
102 | |
103 /** | |
104 * Returns output stream of the socket. | |
105 */ | |
106 OutputStream get outputStream; | |
107 | |
108 /** | |
109 * Returns the port used by this socket. | |
110 */ | |
111 int get port; | |
112 | |
113 /** | |
114 * Returns the remote port connected to by this socket. | |
115 */ | |
116 int get remotePort; | |
117 | |
118 /** | |
119 * Returns the remote host connected to by this socket. | |
120 */ | |
121 String get remoteHost; | |
122 | |
123 /** | |
124 * Closes the socket. Calling [close] will never throw an exception | |
125 * and calling it several times is supported. If [halfClose] is true | |
126 * the socket will only be closed for writing and it might still be | |
127 * possible to read data. Calling [close] will not trigger a call to | |
128 * [onClosed]. | |
129 */ | |
130 void close([bool halfClose = false]); | |
131 } | |
132 | |
133 | |
134 class SocketIOException implements Exception { | |
135 const SocketIOException([String this.message = "", | |
136 OSError this.osError = null]); | |
137 String toString() { | |
138 StringBuffer sb = new StringBuffer(); | |
139 sb.add("SocketIOException"); | |
140 if (!message.isEmpty) { | |
141 sb.add(": $message"); | |
142 if (osError != null) { | |
143 sb.add(" ($osError)"); | |
144 } | |
145 } else if (osError != null) { | |
146 sb.add(": $osError"); | |
147 } | |
148 return sb.toString(); | |
149 } | |
150 final String message; | |
151 final OSError osError; | |
152 } | |
OLD | NEW |