| 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 "package:expect/expect.dart"; | 14 import "package:expect/expect.dart"; |
| 15 import "package:async_helper/async_helper.dart"; |
| 15 import "dart:async"; | 16 import "dart:async"; |
| 16 import "dart:io"; | 17 import "dart:io"; |
| 17 import "dart:isolate"; | 18 import "dart:isolate"; |
| 18 part "testing_server.dart"; | 19 part "testing_server.dart"; |
| 19 | 20 |
| 20 class EchoServerGame { | 21 class EchoServerGame { |
| 21 | 22 |
| 22 static const MSGSIZE = 10; | 23 static const MSGSIZE = 10; |
| 23 static const MESSAGES = 100; | 24 static const MESSAGES = 100; |
| 24 static const FIRSTCHAR = 65; | 25 static const FIRSTCHAR = 65; |
| 25 | 26 |
| 26 EchoServerGame.start() | 27 EchoServerGame.start() |
| 27 : _receivePort = new ReceivePort(), | 28 : _buffer = new List<int>(MSGSIZE), |
| 28 _sendPort = null, | |
| 29 _buffer = new List<int>(MSGSIZE), | |
| 30 _messages = 0 { | 29 _messages = 0 { |
| 31 for (int i = 0; i < MSGSIZE; i++) { | 30 for (int i = 0; i < MSGSIZE; i++) { |
| 32 _buffer[i] = FIRSTCHAR + i; | 31 _buffer[i] = FIRSTCHAR + i; |
| 33 } | 32 } |
| 34 _sendPort = spawnFunction(startEchoServer); | |
| 35 initialize(); | 33 initialize(); |
| 36 } | 34 } |
| 37 | 35 |
| 38 void sendData() { | 36 void sendData() { |
| 39 int offset = 0; | 37 int offset = 0; |
| 40 List<int> data; | 38 List<int> data; |
| 41 | 39 |
| 42 void onData(List<int> data) { | 40 void onData(List<int> data) { |
| 43 int bytesRead = data.length; | 41 int bytesRead = data.length; |
| 44 for (int i = 0; i < data.length; i++) { | 42 for (int i = 0; i < data.length; i++) { |
| (...skipping 28 matching lines...) Expand all Loading... |
| 73 data = new List<int>(MSGSIZE); | 71 data = new List<int>(MSGSIZE); |
| 74 } | 72 } |
| 75 | 73 |
| 76 Socket.connect(TestingServer.HOST, _port).then((s) { | 74 Socket.connect(TestingServer.HOST, _port).then((s) { |
| 77 _socket = s; | 75 _socket = s; |
| 78 connectHandler(); | 76 connectHandler(); |
| 79 }); | 77 }); |
| 80 } | 78 } |
| 81 | 79 |
| 82 void initialize() { | 80 void initialize() { |
| 83 _receivePort.receive((var message, SendPort replyTo) { | 81 var receivePort = new ReceivePort(); |
| 84 _port = message; | 82 var remote = Isolate.spawn(startEchoServer, receivePort.sendPort); |
| 83 receivePort.first.then((msg) { |
| 84 this._port = msg[0]; |
| 85 this._closeSendPort = msg[1]; |
| 85 sendData(); | 86 sendData(); |
| 86 }); | 87 }); |
| 87 _sendPort.send(TestingServer.INIT, _receivePort.toSendPort()); | |
| 88 } | 88 } |
| 89 | 89 |
| 90 void shutdown() { | 90 void shutdown() { |
| 91 _sendPort.send(TestingServer.SHUTDOWN, _receivePort.toSendPort()); | 91 _closeSendPort.send(null); |
| 92 _receivePort.close(); | 92 asyncEnd(); |
| 93 } | 93 } |
| 94 | 94 |
| 95 int _port; | 95 int _port; |
| 96 ReceivePort _receivePort; | 96 SendPort _closeSendPort; |
| 97 SendPort _sendPort; | |
| 98 Socket _socket; | 97 Socket _socket; |
| 99 List<int> _buffer; | 98 List<int> _buffer; |
| 100 int _messages; | 99 int _messages; |
| 101 } | 100 } |
| 102 | 101 |
| 103 | 102 |
| 104 void startEchoServer() { | 103 void startEchoServer(SendPort replyPort) { |
| 105 var server = new EchoServer(); | 104 var server = new EchoServer(); |
| 106 port.receive(server.dispatch); | 105 server.init().then((port) { |
| 106 replyPort.send([port, server.closeSendPort]); |
| 107 }); |
| 107 } | 108 } |
| 108 | 109 |
| 109 | 110 |
| 110 class EchoServer extends TestingServer { | 111 class EchoServer extends TestingServer { |
| 111 | 112 |
| 112 static const int MSGSIZE = EchoServerGame.MSGSIZE; | 113 static const int MSGSIZE = EchoServerGame.MSGSIZE; |
| 113 | 114 |
| 114 void onConnection(Socket connection) { | 115 void onConnection(Socket connection) { |
| 115 List<int> buffer = new List<int>(MSGSIZE); | 116 List<int> buffer = new List<int>(MSGSIZE); |
| 116 int offset = 0; | 117 int offset = 0; |
| (...skipping 19 matching lines...) Expand all Loading... |
| 136 var trace = getAttachedStackTrace(e); | 137 var trace = getAttachedStackTrace(e); |
| 137 if (trace != null) msg += "\nStackTrace: $trace"; | 138 if (trace != null) msg += "\nStackTrace: $trace"; |
| 138 Expect.fail(msg); | 139 Expect.fail(msg); |
| 139 } | 140 } |
| 140 | 141 |
| 141 connection.listen(dataReceived, onError: errorHandler); | 142 connection.listen(dataReceived, onError: errorHandler); |
| 142 } | 143 } |
| 143 } | 144 } |
| 144 | 145 |
| 145 main() { | 146 main() { |
| 147 asyncStart(); |
| 146 EchoServerGame echoServerGame = new EchoServerGame.start(); | 148 EchoServerGame echoServerGame = new EchoServerGame.start(); |
| 147 } | 149 } |
| OLD | NEW |