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 for testing sockets. | |
6 // | |
7 // VMOptions= | |
8 // VMOptions=--short_socket_read | |
9 // VMOptions=--short_socket_write | |
10 // VMOptions=--short_socket_read --short_socket_write | |
11 | |
12 #library("EchoServerTest.dart"); | |
13 #import("dart:io"); | |
14 #import("dart:isolate"); | |
15 #source("TestingServer.dart"); | |
16 | |
17 class EchoServerTest { | |
18 | |
19 static void testMain() { | |
20 EchoServerGame echoServerGame = new EchoServerGame.start(); | |
21 } | |
22 } | |
23 | |
24 class EchoServerGame { | |
25 | |
26 static final MSGSIZE = 10; | |
27 static final MESSAGES = 100; | |
28 static final FIRSTCHAR = 65; | |
29 | |
30 EchoServerGame.start() | |
31 : _receivePort = new ReceivePort(), | |
32 _sendPort = null, | |
33 _buffer = new List<int>(MSGSIZE), | |
34 _messages = 0 { | |
35 for (int i = 0; i < MSGSIZE; i++) { | |
36 _buffer[i] = FIRSTCHAR + i; | |
37 } | |
38 new EchoServer().spawn().then((SendPort port) { | |
39 _sendPort = port; | |
40 start(); | |
41 }); | |
42 } | |
43 | |
44 void sendData() { | |
45 Socket _socket; | |
46 | |
47 void messageHandler() { | |
48 | |
49 List<int> bufferReceived = new List<int>(MSGSIZE); | |
50 int bytesRead = 0; | |
51 | |
52 void handleRead() { | |
53 bytesRead += _socket.readList( | |
54 bufferReceived, bytesRead, MSGSIZE - bytesRead); | |
55 if (bytesRead < MSGSIZE) { | |
56 // We check every time the whole buffer to verify data integrity. | |
57 for (int i = 0; i < bytesRead; i++) { | |
58 Expect.equals(FIRSTCHAR + i, bufferReceived[i]); | |
59 } | |
60 _socket.onData = handleRead; | |
61 } else { | |
62 // We check every time the whole buffer to verify data integrity. | |
63 for (int i = 0; i < MSGSIZE; i++) { | |
64 Expect.equals(FIRSTCHAR + i, bufferReceived[i]); | |
65 } | |
66 _messages++; | |
67 _socket.close(); | |
68 if (_messages < MESSAGES) { | |
69 sendData(); | |
70 } else { | |
71 shutdown(); | |
72 } | |
73 } | |
74 } | |
75 | |
76 handleRead(); | |
77 } | |
78 | |
79 void errorHandler(Exception e) { | |
80 Expect.fail("Socket error $e"); | |
81 } | |
82 | |
83 void connectHandler() { | |
84 | |
85 void writeMessage() { | |
86 int bytesWritten = 0; | |
87 | |
88 void handleWrite() { | |
89 bytesWritten += _socket.writeList( | |
90 _buffer, bytesWritten, MSGSIZE - bytesWritten); | |
91 if (bytesWritten < MSGSIZE) { | |
92 _socket.onWrite = handleWrite; | |
93 } | |
94 } | |
95 | |
96 handleWrite(); | |
97 } | |
98 | |
99 _socket.onData = messageHandler; | |
100 _socket.onError = errorHandler; | |
101 writeMessage(); | |
102 } | |
103 | |
104 _socket = new Socket(TestingServer.HOST, _port); | |
105 if (_socket !== null) { | |
106 _socket.onConnect = connectHandler; | |
107 } else { | |
108 Expect.fail("Socket creation failed"); | |
109 } | |
110 } | |
111 | |
112 void start() { | |
113 _receivePort.receive((var message, SendPort replyTo) { | |
114 _port = message; | |
115 sendData(); | |
116 }); | |
117 _sendPort.send(TestingServer.INIT, _receivePort.toSendPort()); | |
118 } | |
119 | |
120 void shutdown() { | |
121 _sendPort.send(TestingServer.SHUTDOWN, _receivePort.toSendPort()); | |
122 _receivePort.close(); | |
123 } | |
124 | |
125 int _port; | |
126 ReceivePort _receivePort; | |
127 SendPort _sendPort; | |
128 List<int> _buffer; | |
129 int _messages; | |
130 } | |
131 | |
132 class EchoServer extends TestingServer { | |
133 | |
134 static final msgSize = EchoServerGame.MSGSIZE; | |
135 | |
136 void onConnection(Socket connection) { | |
137 | |
138 void messageHandler() { | |
139 | |
140 List<int> buffer = new List<int>(msgSize); | |
141 int bytesRead = 0; | |
142 | |
143 void handleRead() { | |
144 int read = connection.readList(buffer, bytesRead, msgSize - bytesRead); | |
145 if (read > 0) { | |
146 bytesRead += read; | |
147 if (bytesRead < msgSize) { | |
148 // We check every time the whole buffer to verify data integrity. | |
149 for (int i = 0; i < bytesRead; i++) { | |
150 Expect.equals(EchoServerGame.FIRSTCHAR + i, buffer[i]); | |
151 } | |
152 connection.onData = handleRead; | |
153 } else { | |
154 // We check every time the whole buffer to verify data integrity. | |
155 for (int i = 0; i < msgSize; i++) { | |
156 Expect.equals(EchoServerGame.FIRSTCHAR + i, buffer[i]); | |
157 } | |
158 | |
159 void writeMessage() { | |
160 | |
161 int bytesWritten = 0; | |
162 | |
163 void handleWrite() { | |
164 int written = connection.writeList( | |
165 buffer, bytesWritten, msgSize - bytesWritten); | |
166 bytesWritten += written; | |
167 if (bytesWritten < msgSize) { | |
168 connection.onWrite = handleWrite; | |
169 } else { | |
170 connection.close(true); | |
171 } | |
172 } | |
173 handleWrite(); | |
174 } | |
175 writeMessage(); | |
176 } | |
177 } | |
178 } | |
179 | |
180 handleRead(); | |
181 } | |
182 | |
183 void closeHandler() { | |
184 connection.close(); | |
185 } | |
186 | |
187 void errorHandler(Exception e) { | |
188 Expect.fail("Socket error $e"); | |
189 } | |
190 | |
191 connection.onData = messageHandler; | |
192 connection.onClosed = closeHandler; | |
193 connection.onError = errorHandler; | |
194 } | |
195 } | |
196 | |
197 main() { | |
198 EchoServerTest.testMain(); | |
199 } | |
OLD | NEW |