| 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 // |
| 7 // VMOptions= |
| 8 // VMOptions=--short_socket_read |
| 9 // VMOptions=--short_socket_write |
| 10 // VMOptions=--short_socket_read --short_socket_write |
| 6 | 11 |
| 7 class EchoServerTest { | 12 class EchoServerTest { |
| 8 | 13 |
| 9 static void testMain() { | 14 static void testMain() { |
| 10 EchoServerGame echoServerGame = new EchoServerGame.start(); | 15 EchoServerGame echoServerGame = new EchoServerGame.start(); |
| 11 } | 16 } |
| 12 } | 17 } |
| 13 | 18 |
| 14 class EchoServerGame { | 19 class EchoServerGame { |
| 15 | 20 |
| (...skipping 11 matching lines...) Expand all Loading... |
| 27 for (int i = 0; i < MSGSIZE; i++) { | 32 for (int i = 0; i < MSGSIZE; i++) { |
| 28 _buffer[i] = FIRSTCHAR + i; | 33 _buffer[i] = FIRSTCHAR + i; |
| 29 } | 34 } |
| 30 new EchoServer().spawn().then((SendPort port) { | 35 new EchoServer().spawn().then((SendPort port) { |
| 31 _sendPort = port; | 36 _sendPort = port; |
| 32 start(); | 37 start(); |
| 33 }); | 38 }); |
| 34 } | 39 } |
| 35 | 40 |
| 36 void sendData() { | 41 void sendData() { |
| 42 Socket _socket; |
| 37 | 43 |
| 38 void messageHandler() { | 44 void messageHandler() { |
| 39 | 45 |
| 40 List<int> bufferReceived = new List<int>(MSGSIZE); | 46 List<int> bufferReceived = new List<int>(MSGSIZE); |
| 41 int bytesRead = 0; | 47 int bytesRead = 0; |
| 42 | 48 |
| 43 void handleRead() { | 49 void handleRead() { |
| 44 | 50 bytesRead += _socket.readList( |
| 45 if (_socket.available() > 0) { | 51 bufferReceived, bytesRead, MSGSIZE - bytesRead); |
| 46 bytesRead += _socket.readList( | |
| 47 bufferReceived, bytesRead, MSGSIZE - bytesRead); | |
| 48 } | |
| 49 if (bytesRead < MSGSIZE) { | 52 if (bytesRead < MSGSIZE) { |
| 50 /* | 53 // We check every time the whole buffer to verify data integrity. |
| 51 * We check every time the whole buffer to verify data integrity. | |
| 52 */ | |
| 53 for (int i = 0; i < bytesRead; i++) { | 54 for (int i = 0; i < bytesRead; i++) { |
| 54 Expect.equals(FIRSTCHAR + i, bufferReceived[i]); | 55 Expect.equals(FIRSTCHAR + i, bufferReceived[i]); |
| 55 } | 56 } |
| 56 _socket.setDataHandler(handleRead); | 57 _socket.setDataHandler(handleRead); |
| 57 } | 58 } else { |
| 58 else { | 59 // We check every time the whole buffer to verify data integrity. |
| 59 /* | |
| 60 * We check every time the whole buffer to verify data integrity. | |
| 61 */ | |
| 62 for (int i = 0; i < MSGSIZE; i++) { | 60 for (int i = 0; i < MSGSIZE; i++) { |
| 63 Expect.equals(FIRSTCHAR + i, bufferReceived[i]); | 61 Expect.equals(FIRSTCHAR + i, bufferReceived[i]); |
| 64 } | 62 } |
| 65 _messages++; | 63 _messages++; |
| 66 _socket.close(); | 64 _socket.close(); |
| 67 if (_messages < MESSAGES) { | 65 if (_messages < MESSAGES) { |
| 68 sendData(); | 66 sendData(); |
| 69 } else { | 67 } else { |
| 70 shutdown(); | 68 shutdown(); |
| 71 } | 69 } |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 123 } | 121 } |
| 124 | 122 |
| 125 void shutdown() { | 123 void shutdown() { |
| 126 _sendPort.send(SERVERSHUTDOWN, _receivePort.toSendPort()); | 124 _sendPort.send(SERVERSHUTDOWN, _receivePort.toSendPort()); |
| 127 _receivePort.close(); | 125 _receivePort.close(); |
| 128 } | 126 } |
| 129 | 127 |
| 130 int _port; | 128 int _port; |
| 131 ReceivePort _receivePort; | 129 ReceivePort _receivePort; |
| 132 SendPort _sendPort; | 130 SendPort _sendPort; |
| 133 Socket _socket; | |
| 134 List<int> _buffer; | 131 List<int> _buffer; |
| 135 int _messages; | 132 int _messages; |
| 136 } | 133 } |
| 137 | 134 |
| 138 class EchoServer extends Isolate { | 135 class EchoServer extends Isolate { |
| 139 | 136 |
| 140 static final HOST = "127.0.0.1"; | 137 static final HOST = "127.0.0.1"; |
| 141 static final msgSize = EchoServerGame.MSGSIZE; | 138 static final msgSize = EchoServerGame.MSGSIZE; |
| 142 | 139 |
| 143 | 140 |
| 144 void main() { | 141 void main() { |
| 145 | 142 |
| 146 void connectionHandler() { | 143 void connectionHandler() { |
| 147 Socket _client; | 144 Socket _client; |
| 148 | 145 |
| 149 void messageHandler() { | 146 void messageHandler() { |
| 150 | 147 |
| 151 List<int> buffer = new List<int>(msgSize); | 148 List<int> buffer = new List<int>(msgSize); |
| 152 int bytesRead = 0; | 149 int bytesRead = 0; |
| 153 | 150 |
| 154 void handleRead() { | 151 void handleRead() { |
| 155 if (_client.available() > 0) { | 152 int read = _client.readList(buffer, bytesRead, msgSize - bytesRead); |
| 156 bytesRead += _client.readList(buffer, bytesRead, msgSize - bytesRead
); | 153 if (read > 0) { |
| 157 } | 154 bytesRead += read; |
| 158 if (bytesRead < msgSize) { | 155 if (bytesRead < msgSize) { |
| 159 /* | 156 // We check every time the whole buffer to verify data integrity. |
| 160 * We check every time the whole buffer to verify data integrity. | 157 for (int i = 0; i < bytesRead; i++) { |
| 161 */ | 158 Expect.equals(EchoServerGame.FIRSTCHAR + i, buffer[i]); |
| 162 for (int i = 0; i < bytesRead; i++) { | 159 } |
| 163 Expect.equals(EchoServerGame.FIRSTCHAR + i, buffer[i]); | 160 _client.setDataHandler(handleRead); |
| 161 } else { |
| 162 // We check every time the whole buffer to verify data integrity. |
| 163 for (int i = 0; i < msgSize; i++) { |
| 164 Expect.equals(EchoServerGame.FIRSTCHAR + i, buffer[i]); |
| 165 } |
| 166 |
| 167 void writeMessage() { |
| 168 |
| 169 int bytesWritten = 0; |
| 170 |
| 171 void handleWrite() { |
| 172 int written = _client.writeList( |
| 173 buffer, bytesWritten, msgSize - bytesWritten); |
| 174 bytesWritten += written; |
| 175 if (bytesWritten < msgSize) { |
| 176 _client.setWriteHandler(handleWrite); |
| 177 } |
| 178 } |
| 179 handleWrite(); |
| 180 } |
| 181 writeMessage(); |
| 164 } | 182 } |
| 165 _client.setDataHandler(handleRead); | |
| 166 } | |
| 167 else { | |
| 168 /* | |
| 169 * We check every time the whole buffer to verify data integrity. | |
| 170 */ | |
| 171 for (int i = 0; i < msgSize; i++) { | |
| 172 Expect.equals(EchoServerGame.FIRSTCHAR + i, buffer[i]); | |
| 173 } | |
| 174 | |
| 175 void writeMessage() { | |
| 176 | |
| 177 int bytesWritten = 0; | |
| 178 | |
| 179 void handleWrite() { | |
| 180 bytesWritten += _client.writeList( | |
| 181 buffer, bytesWritten, msgSize - bytesWritten); | |
| 182 if (bytesWritten < msgSize) { | |
| 183 _client.setWriteHandler(handleWrite); | |
| 184 } | |
| 185 } | |
| 186 handleWrite(); | |
| 187 } | |
| 188 writeMessage(); | |
| 189 } | 183 } |
| 190 } | 184 } |
| 191 | 185 |
| 192 handleRead(); | 186 handleRead(); |
| 193 } | 187 } |
| 194 | 188 |
| 195 void closeHandler() { | 189 void closeHandler() { |
| 196 _client.close(); | 190 _client.close(); |
| 197 } | 191 } |
| 198 | 192 |
| (...skipping 26 matching lines...) Expand all Loading... |
| 225 } | 219 } |
| 226 }); | 220 }); |
| 227 } | 221 } |
| 228 | 222 |
| 229 ServerSocket _server; | 223 ServerSocket _server; |
| 230 } | 224 } |
| 231 | 225 |
| 232 main() { | 226 main() { |
| 233 EchoServerTest.testMain(); | 227 EchoServerTest.testMain(); |
| 234 } | 228 } |
| OLD | NEW |