OLD | NEW |
| (Empty) |
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 | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 // | |
5 // Echo server test program to test socket streams. | |
6 // | |
7 // VMOptions= | |
8 // VMOptions=--short_socket_read | |
9 // VMOptions=--short_socket_write | |
10 // VMOptions=--short_socket_read --short_socket_write | |
11 | |
12 #import("dart:io"); | |
13 #import("dart:isolate"); | |
14 #source("TestingServer.dart"); | |
15 | |
16 class EchoServerGame { | |
17 | |
18 static final MSGSIZE = 10; | |
19 static final MESSAGES = 100; | |
20 static final FIRSTCHAR = 65; | |
21 | |
22 EchoServerGame.start() | |
23 : _receivePort = new ReceivePort(), | |
24 _sendPort = null, | |
25 _buffer = new List<int>(MSGSIZE), | |
26 _messages = 0 { | |
27 for (int i = 0; i < MSGSIZE; i++) { | |
28 _buffer[i] = FIRSTCHAR + i; | |
29 } | |
30 new EchoServer().spawn().then((SendPort port) { | |
31 _sendPort = port; | |
32 start(); | |
33 }); | |
34 } | |
35 | |
36 void sendData() { | |
37 | |
38 void errorHandler(Exception e) { | |
39 Expect.fail("Socket error $e"); | |
40 } | |
41 | |
42 void connectHandler() { | |
43 | |
44 SocketOutputStream stream = _socket.outputStream; | |
45 | |
46 void dataSent() { | |
47 InputStream inputStream = _socket.inputStream; | |
48 int offset = 0; | |
49 List<int> data; | |
50 | |
51 void onClosed() { | |
52 Expect.equals(MSGSIZE, offset); | |
53 _messages++; | |
54 if (_messages < MESSAGES) { | |
55 sendData(); | |
56 } else { | |
57 shutdown(); | |
58 } | |
59 } | |
60 | |
61 void onData() { | |
62 // Test both read and readInto. | |
63 int bytesRead = 0; | |
64 if (_messages % 2 == 0) { | |
65 bytesRead = inputStream.readInto(data, offset, MSGSIZE - offset); | |
66 for (int i = 0; i < offset + bytesRead; i++) { | |
67 Expect.equals(FIRSTCHAR + i, data[i]); | |
68 } | |
69 } else { | |
70 data = inputStream.read(); | |
71 bytesRead = data.length; | |
72 for (int i = 0; i < data.length; i++) { | |
73 Expect.equals(FIRSTCHAR + i + offset, data[i]); | |
74 } | |
75 } | |
76 | |
77 offset += bytesRead; | |
78 } | |
79 | |
80 if (_messages % 2 == 0) data = new List<int>(MSGSIZE); | |
81 inputStream.onData = onData; | |
82 inputStream.onClosed = onClosed; | |
83 } | |
84 | |
85 _socket.onError = errorHandler; | |
86 | |
87 // Test both write and writeFrom in different forms. | |
88 switch (_messages % 4) { | |
89 case 0: | |
90 stream.write(_buffer); | |
91 break; | |
92 case 1: | |
93 stream.write(_buffer, false); | |
94 break; | |
95 case 2: | |
96 stream.writeFrom(_buffer); | |
97 break; | |
98 case 3: | |
99 Expect.equals(0, _buffer.length % 2); | |
100 stream.writeFrom(_buffer, len: _buffer.length ~/ 2); | |
101 stream.writeFrom(_buffer, _buffer.length ~/ 2); | |
102 break; | |
103 } | |
104 stream.close(); | |
105 dataSent(); | |
106 } | |
107 | |
108 _socket = new Socket(TestingServer.HOST, _port); | |
109 if (_socket !== null) { | |
110 _socket.onConnect = connectHandler; | |
111 } else { | |
112 Expect.fail("socket creation failed"); | |
113 } | |
114 } | |
115 | |
116 void start() { | |
117 _receivePort.receive((var message, SendPort replyTo) { | |
118 _port = message; | |
119 sendData(); | |
120 }); | |
121 _sendPort.send(TestingServer.INIT, _receivePort.toSendPort()); | |
122 } | |
123 | |
124 void shutdown() { | |
125 _sendPort.send(TestingServer.SHUTDOWN, _receivePort.toSendPort()); | |
126 _receivePort.close(); | |
127 } | |
128 | |
129 int _port; | |
130 ReceivePort _receivePort; | |
131 SendPort _sendPort; | |
132 Socket _socket; | |
133 List<int> _buffer; | |
134 int _messages; | |
135 } | |
136 | |
137 | |
138 class EchoServer extends TestingServer { | |
139 | |
140 static final int MSGSIZE = EchoServerGame.MSGSIZE; | |
141 | |
142 void onConnection(Socket connection) { | |
143 InputStream inputStream; | |
144 List<int> buffer = new List<int>(MSGSIZE); | |
145 int offset = 0; | |
146 | |
147 void dataReceived() { | |
148 SocketOutputStream outputStream; | |
149 int bytesRead; | |
150 outputStream = connection.outputStream; | |
151 bytesRead = inputStream.readInto(buffer, offset, MSGSIZE - offset); | |
152 if (bytesRead > 0) { | |
153 offset += bytesRead; | |
154 for (int i = 0; i < offset; i++) { | |
155 Expect.equals(EchoServerGame.FIRSTCHAR + i, buffer[i]); | |
156 } | |
157 if (offset == MSGSIZE) { | |
158 outputStream.write(buffer); | |
159 outputStream.close(); | |
160 } | |
161 } | |
162 } | |
163 | |
164 void errorHandler(Exception e) { | |
165 Expect.fail("Socket error $e"); | |
166 } | |
167 | |
168 inputStream = connection.inputStream; | |
169 inputStream.onData = dataReceived; | |
170 connection.onError = errorHandler; | |
171 } | |
172 } | |
173 | |
174 main() { | |
175 EchoServerGame echoServerGame = new EchoServerGame.start(); | |
176 } | |
OLD | NEW |