| 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 // Echo server test program to test socket streams. | 5 // Echo server test program to test socket streams. |
| 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 ServerTest; | 12 library ServerTest; |
| 13 | 13 |
| 14 import "dart:io"; | 14 import "dart:io"; |
| 15 import "dart:isolate"; | 15 import "dart:isolate"; |
| 16 part "testing_server.dart"; | 16 part "testing_server.dart"; |
| 17 | 17 |
| 18 class EchoServerGame { | 18 class EchoServerGame { |
| 19 | 19 |
| 20 static const MSGSIZE = 10; | 20 static const MSGSIZE = 10; |
| 21 static const MESSAGES = 100; | 21 static const MESSAGES = 100; |
| 22 static const FIRSTCHAR = 65; | 22 static const FIRSTCHAR = 65; |
| 23 | 23 |
| 24 EchoServerGame.start() | 24 EchoServerGame.start() |
| 25 : _receivePort = new ReceivePort(), | 25 : _receivePort = new ReceivePort(), |
| 26 _sendPort = null, | 26 _sendPort = null, |
| 27 _buffer = new List<int>(MSGSIZE), | 27 _buffer = new List<int>.fixedLength(MSGSIZE), |
| 28 _messages = 0 { | 28 _messages = 0 { |
| 29 for (int i = 0; i < MSGSIZE; i++) { | 29 for (int i = 0; i < MSGSIZE; i++) { |
| 30 _buffer[i] = FIRSTCHAR + i; | 30 _buffer[i] = FIRSTCHAR + i; |
| 31 } | 31 } |
| 32 _sendPort = spawnFunction(startEchoServer); | 32 _sendPort = spawnFunction(startEchoServer); |
| 33 initialize(); | 33 initialize(); |
| 34 } | 34 } |
| 35 | 35 |
| 36 void sendData() { | 36 void sendData() { |
| 37 | 37 |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 70 data = inputStream.read(); | 70 data = inputStream.read(); |
| 71 bytesRead = data.length; | 71 bytesRead = data.length; |
| 72 for (int i = 0; i < data.length; i++) { | 72 for (int i = 0; i < data.length; i++) { |
| 73 Expect.equals(FIRSTCHAR + i + offset, data[i]); | 73 Expect.equals(FIRSTCHAR + i + offset, data[i]); |
| 74 } | 74 } |
| 75 } | 75 } |
| 76 | 76 |
| 77 offset += bytesRead; | 77 offset += bytesRead; |
| 78 } | 78 } |
| 79 | 79 |
| 80 if (_messages % 2 == 0) data = new List<int>(MSGSIZE); | 80 if (_messages % 2 == 0) data = new List<int>.fixedLength(MSGSIZE); |
| 81 inputStream.onData = onData; | 81 inputStream.onData = onData; |
| 82 inputStream.onClosed = onClosed; | 82 inputStream.onClosed = onClosed; |
| 83 } | 83 } |
| 84 | 84 |
| 85 _socket.onError = errorHandler; | 85 _socket.onError = errorHandler; |
| 86 | 86 |
| 87 // Test both write and writeFrom in different forms. | 87 // Test both write and writeFrom in different forms. |
| 88 switch (_messages % 4) { | 88 switch (_messages % 4) { |
| 89 case 0: | 89 case 0: |
| 90 stream.write(_buffer); | 90 stream.write(_buffer); |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 140 port.receive(server.dispatch); | 140 port.receive(server.dispatch); |
| 141 } | 141 } |
| 142 | 142 |
| 143 | 143 |
| 144 class EchoServer extends TestingServer { | 144 class EchoServer extends TestingServer { |
| 145 | 145 |
| 146 static const int MSGSIZE = EchoServerGame.MSGSIZE; | 146 static const int MSGSIZE = EchoServerGame.MSGSIZE; |
| 147 | 147 |
| 148 void onConnection(Socket connection) { | 148 void onConnection(Socket connection) { |
| 149 InputStream inputStream; | 149 InputStream inputStream; |
| 150 List<int> buffer = new List<int>(MSGSIZE); | 150 List<int> buffer = new List<int>.fixedLength(MSGSIZE); |
| 151 int offset = 0; | 151 int offset = 0; |
| 152 | 152 |
| 153 void dataReceived() { | 153 void dataReceived() { |
| 154 int bytesRead; | 154 int bytesRead; |
| 155 OutputStream outputStream = connection.outputStream; | 155 OutputStream outputStream = connection.outputStream; |
| 156 bytesRead = inputStream.readInto(buffer, offset, MSGSIZE - offset); | 156 bytesRead = inputStream.readInto(buffer, offset, MSGSIZE - offset); |
| 157 if (bytesRead > 0) { | 157 if (bytesRead > 0) { |
| 158 offset += bytesRead; | 158 offset += bytesRead; |
| 159 for (int i = 0; i < offset; i++) { | 159 for (int i = 0; i < offset; i++) { |
| 160 Expect.equals(EchoServerGame.FIRSTCHAR + i, buffer[i]); | 160 Expect.equals(EchoServerGame.FIRSTCHAR + i, buffer[i]); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 172 | 172 |
| 173 inputStream = connection.inputStream; | 173 inputStream = connection.inputStream; |
| 174 inputStream.onData = dataReceived; | 174 inputStream.onData = dataReceived; |
| 175 connection.onError = errorHandler; | 175 connection.onError = errorHandler; |
| 176 } | 176 } |
| 177 } | 177 } |
| 178 | 178 |
| 179 main() { | 179 main() { |
| 180 EchoServerGame echoServerGame = new EchoServerGame.start(); | 180 EchoServerGame echoServerGame = new EchoServerGame.start(); |
| 181 } | 181 } |
| OLD | NEW |