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 // Echo server test program for testing sockets. | 5 // Echo server test program for testing sockets. |
6 // | 6 // |
7 // VMOptions= | 7 // VMOptions= |
8 // VMOptions=--short_socket_read | 8 // VMOptions=--short_socket_read |
9 // VMOptions=--short_socket_write | 9 // VMOptions=--short_socket_write |
10 // VMOptions=--short_socket_read --short_socket_write | 10 // VMOptions=--short_socket_read --short_socket_write |
11 | 11 |
12 #library("EchoServerTest.dart"); | 12 #library("EchoServerTest.dart"); |
13 | 13 |
14 class EchoServerTest { | 14 class EchoServerTest { |
15 | 15 |
16 static void testMain() { | 16 static void testMain() { |
17 EchoServerGame echoServerGame = new EchoServerGame.start(); | 17 EchoServerGame echoServerGame = new EchoServerGame.start(); |
18 } | 18 } |
19 } | 19 } |
20 | 20 |
21 class EchoServerGame { | 21 class EchoServerGame { |
22 | 22 |
23 static final MSGSIZE = 10; | 23 static final MSGSIZE = 10; |
24 static final SERVERINIT = 0; | 24 static final SERVERINIT = 0; |
25 static final SERVERSHUTDOWN = -1; | 25 static final SERVERSHUTDOWN = -1; |
26 static final MESSAGES = 200; | 26 static final MESSAGES = 100; |
27 static final FIRSTCHAR = 65; | 27 static final FIRSTCHAR = 65; |
28 | 28 |
29 EchoServerGame.start() | 29 EchoServerGame.start() |
30 : _receivePort = new ReceivePort(), | 30 : _receivePort = new ReceivePort(), |
31 _sendPort = null, | 31 _sendPort = null, |
32 _buffer = new List<int>(MSGSIZE), | 32 _buffer = new List<int>(MSGSIZE), |
33 _messages = 0 { | 33 _messages = 0 { |
34 for (int i = 0; i < MSGSIZE; i++) { | 34 for (int i = 0; i < MSGSIZE; i++) { |
35 _buffer[i] = FIRSTCHAR + i; | 35 _buffer[i] = FIRSTCHAR + i; |
36 } | 36 } |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
68 sendData(); | 68 sendData(); |
69 } else { | 69 } else { |
70 shutdown(); | 70 shutdown(); |
71 } | 71 } |
72 } | 72 } |
73 } | 73 } |
74 | 74 |
75 handleRead(); | 75 handleRead(); |
76 } | 76 } |
77 | 77 |
78 void closeHandler() { | |
79 _socket.close(); | |
80 } | |
81 | |
82 void errorHandler() { | 78 void errorHandler() { |
83 print("Socket error"); | 79 Expect.fail("Socket error"); |
84 _socket.close(); | |
85 } | 80 } |
86 | 81 |
87 void connectHandler() { | 82 void connectHandler() { |
88 | 83 |
89 void writeMessage() { | 84 void writeMessage() { |
90 int bytesWritten = 0; | 85 int bytesWritten = 0; |
91 | 86 |
92 void handleWrite() { | 87 void handleWrite() { |
93 bytesWritten += _socket.writeList( | 88 bytesWritten += _socket.writeList( |
94 _buffer, bytesWritten, MSGSIZE - bytesWritten); | 89 _buffer, bytesWritten, MSGSIZE - bytesWritten); |
95 if (bytesWritten < MSGSIZE) { | 90 if (bytesWritten < MSGSIZE) { |
96 _socket.writeHandler = handleWrite; | 91 _socket.writeHandler = handleWrite; |
97 } | 92 } |
98 } | 93 } |
99 | 94 |
100 handleWrite(); | 95 handleWrite(); |
101 } | 96 } |
102 | 97 |
103 _socket.dataHandler = messageHandler; | 98 _socket.dataHandler = messageHandler; |
104 _socket.closeHandler = closeHandler; | |
105 _socket.errorHandler = errorHandler; | 99 _socket.errorHandler = errorHandler; |
106 writeMessage(); | 100 writeMessage(); |
107 } | 101 } |
108 | 102 |
109 _socket = new Socket(EchoServer.HOST, _port); | 103 _socket = new Socket(EchoServer.HOST, _port); |
110 if (_socket !== null) { | 104 if (_socket !== null) { |
111 _socket.connectHandler = connectHandler; | 105 _socket.connectHandler = connectHandler; |
112 } else { | 106 } else { |
113 Expect.fail("socket creation failed"); | 107 Expect.fail("Socket creation failed"); |
114 } | 108 } |
115 } | 109 } |
116 | 110 |
117 void start() { | 111 void start() { |
118 _receivePort.receive((var message, SendPort replyTo) { | 112 _receivePort.receive((var message, SendPort replyTo) { |
119 _port = message; | 113 _port = message; |
120 sendData(); | 114 sendData(); |
121 }); | 115 }); |
122 _sendPort.send(SERVERINIT, _receivePort.toSendPort()); | 116 _sendPort.send(SERVERINIT, _receivePort.toSendPort()); |
123 } | 117 } |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
169 void writeMessage() { | 163 void writeMessage() { |
170 | 164 |
171 int bytesWritten = 0; | 165 int bytesWritten = 0; |
172 | 166 |
173 void handleWrite() { | 167 void handleWrite() { |
174 int written = _client.writeList( | 168 int written = _client.writeList( |
175 buffer, bytesWritten, msgSize - bytesWritten); | 169 buffer, bytesWritten, msgSize - bytesWritten); |
176 bytesWritten += written; | 170 bytesWritten += written; |
177 if (bytesWritten < msgSize) { | 171 if (bytesWritten < msgSize) { |
178 _client.writeHandler = handleWrite; | 172 _client.writeHandler = handleWrite; |
| 173 } else { |
| 174 _client.close(true); |
179 } | 175 } |
180 } | 176 } |
181 handleWrite(); | 177 handleWrite(); |
182 } | 178 } |
183 writeMessage(); | 179 writeMessage(); |
184 } | 180 } |
185 } | 181 } |
186 } | 182 } |
187 | 183 |
188 handleRead(); | 184 handleRead(); |
189 } | 185 } |
190 | 186 |
191 void closeHandler() { | 187 void closeHandler() { |
192 _client.close(); | 188 _client.close(); |
193 } | 189 } |
194 | 190 |
195 void errorHandler() { | 191 void errorHandler() { |
196 print("Socket error"); | 192 Expect.fail("Socket error"); |
197 _client.close(); | |
198 } | 193 } |
199 | 194 |
200 _client = _server.accept(); | 195 _client = _server.accept(); |
201 _client.dataHandler = messageHandler; | 196 _client.dataHandler = messageHandler; |
202 _client.closeHandler = closeHandler; | 197 _client.closeHandler = closeHandler; |
203 _client.errorHandler = errorHandler; | 198 _client.errorHandler = errorHandler; |
204 } | 199 } |
205 | 200 |
206 void errorHandlerServer() { | 201 void errorHandlerServer() { |
207 print("Server socket error"); | 202 Expect.fail("Server socket error"); |
208 _server.close(); | |
209 } | 203 } |
210 | 204 |
211 this.port.receive((message, SendPort replyTo) { | 205 this.port.receive((message, SendPort replyTo) { |
212 if (message == EchoServerGame.SERVERINIT) { | 206 if (message == EchoServerGame.SERVERINIT) { |
213 _server = new ServerSocket(HOST, 0, 10); | 207 _server = new ServerSocket(HOST, 0, 10); |
214 Expect.equals(true, _server !== null); | 208 Expect.equals(true, _server !== null); |
215 _server.connectionHandler = connectionHandler; | 209 _server.connectionHandler = connectionHandler; |
216 _server.errorHandler = errorHandlerServer; | 210 _server.errorHandler = errorHandlerServer; |
217 replyTo.send(_server.port, null); | 211 replyTo.send(_server.port, null); |
218 } else if (message == EchoServerGame.SERVERSHUTDOWN) { | 212 } else if (message == EchoServerGame.SERVERSHUTDOWN) { |
219 _server.close(); | 213 _server.close(); |
220 this.port.close(); | 214 this.port.close(); |
221 } | 215 } |
222 }); | 216 }); |
223 } | 217 } |
224 | 218 |
225 ServerSocket _server; | 219 ServerSocket _server; |
226 } | 220 } |
227 | 221 |
228 main() { | 222 main() { |
229 EchoServerTest.testMain(); | 223 EchoServerTest.testMain(); |
230 } | 224 } |
OLD | NEW |