| OLD | NEW |
| 1 // Copyright (c) 2013, 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 // 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>.fixedLength(MSGSIZE), | 27 _buffer = new List<int>(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 int offset = 0; | 37 int offset = 0; |
| (...skipping 20 matching lines...) Expand all Loading... |
| 58 void errorHandler(e) { | 58 void errorHandler(e) { |
| 59 Expect.fail("Socket error $e"); | 59 Expect.fail("Socket error $e"); |
| 60 } | 60 } |
| 61 | 61 |
| 62 void connectHandler() { | 62 void connectHandler() { |
| 63 _socket.listen(onData, | 63 _socket.listen(onData, |
| 64 onError: errorHandler, | 64 onError: errorHandler, |
| 65 onDone: onClosed); | 65 onDone: onClosed); |
| 66 _socket.add(_buffer); | 66 _socket.add(_buffer); |
| 67 _socket.close(); | 67 _socket.close(); |
| 68 data = new List<int>.fixedLength(MSGSIZE); | 68 data = new List<int>(MSGSIZE); |
| 69 } | 69 } |
| 70 | 70 |
| 71 Socket.connect(TestingServer.HOST, _port).then((s) { | 71 Socket.connect(TestingServer.HOST, _port).then((s) { |
| 72 _socket = s; | 72 _socket = s; |
| 73 connectHandler(); | 73 connectHandler(); |
| 74 }); | 74 }); |
| 75 } | 75 } |
| 76 | 76 |
| 77 void initialize() { | 77 void initialize() { |
| 78 _receivePort.receive((var message, SendPort replyTo) { | 78 _receivePort.receive((var message, SendPort replyTo) { |
| (...skipping 21 matching lines...) Expand all Loading... |
| 100 var server = new EchoServer(); | 100 var server = new EchoServer(); |
| 101 port.receive(server.dispatch); | 101 port.receive(server.dispatch); |
| 102 } | 102 } |
| 103 | 103 |
| 104 | 104 |
| 105 class EchoServer extends TestingServer { | 105 class EchoServer extends TestingServer { |
| 106 | 106 |
| 107 static const int MSGSIZE = EchoServerGame.MSGSIZE; | 107 static const int MSGSIZE = EchoServerGame.MSGSIZE; |
| 108 | 108 |
| 109 void onConnection(Socket connection) { | 109 void onConnection(Socket connection) { |
| 110 List<int> buffer = new List<int>.fixedLength(MSGSIZE); | 110 List<int> buffer = new List<int>(MSGSIZE); |
| 111 int offset = 0; | 111 int offset = 0; |
| 112 | 112 |
| 113 void dataReceived(List<int> data) { | 113 void dataReceived(List<int> data) { |
| 114 int bytesRead; | 114 int bytesRead; |
| 115 bytesRead = data.length; | 115 bytesRead = data.length; |
| 116 if (bytesRead > 0) { | 116 if (bytesRead > 0) { |
| 117 buffer.setRange(offset, data.length, data); | 117 buffer.setRange(offset, data.length, data); |
| 118 offset += bytesRead; | 118 offset += bytesRead; |
| 119 for (int i = 0; i < offset; i++) { | 119 for (int i = 0; i < offset; i++) { |
| 120 Expect.equals(EchoServerGame.FIRSTCHAR + i, buffer[i]); | 120 Expect.equals(EchoServerGame.FIRSTCHAR + i, buffer[i]); |
| 121 } | 121 } |
| 122 if (offset == MSGSIZE) { | 122 if (offset == MSGSIZE) { |
| 123 connection.add(buffer); | 123 connection.add(buffer); |
| 124 connection.close(); | 124 connection.close(); |
| 125 } | 125 } |
| 126 } | 126 } |
| 127 } | 127 } |
| 128 | 128 |
| 129 void errorHandler(e) { | 129 void errorHandler(e) { |
| 130 Expect.fail("Socket error $e"); | 130 Expect.fail("Socket error $e"); |
| 131 } | 131 } |
| 132 | 132 |
| 133 connection.listen(dataReceived, onError: errorHandler); | 133 connection.listen(dataReceived, onError: errorHandler); |
| 134 } | 134 } |
| 135 } | 135 } |
| 136 | 136 |
| 137 main() { | 137 main() { |
| 138 EchoServerGame echoServerGame = new EchoServerGame.start(); | 138 EchoServerGame echoServerGame = new EchoServerGame.start(); |
| 139 } | 139 } |
| OLD | NEW |