OLD | NEW |
| (Empty) |
1 // Copyright (c) 2011, 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 stream read until functionality. | |
6 // | |
7 // VMOptions= | |
8 // VMOptions=--short_socket_read | |
9 // VMOptions=--short_socket_write | |
10 // VMOptions=--short_socket_read --short_socket_write | |
11 | |
12 main() { | |
13 EchoServerStreamReadUntilTest.testMain(); | |
14 } | |
15 | |
16 class EchoServerStreamReadUntilTest { | |
17 | |
18 static void testMain() { | |
19 EchoServerGame echoServerGame = new EchoServerGame.start(); | |
20 } | |
21 } | |
22 | |
23 class EchoServerGame { | |
24 | |
25 static final MSGSIZE = 10; | |
26 static final SERVERINIT = 0; | |
27 static final SERVERSHUTDOWN = -1; | |
28 static final MESSAGES = 200; | |
29 // Char "A". | |
30 static final FIRSTCHAR = 65; | |
31 | |
32 // First pattern is the third and second last character of the message. | |
33 static final List<int> PATTERN1 = | |
34 const [FIRSTCHAR + MSGSIZE - 3, FIRSTCHAR + MSGSIZE - 2]; | |
35 // Second pattern is the last character of the message. | |
36 static final List<int> PATTERN2 = const [FIRSTCHAR + MSGSIZE - 1]; | |
37 | |
38 EchoServerGame.start() | |
39 : _receivePort = new ReceivePort(), | |
40 _sendPort = null, | |
41 _buffer = new List<int>(MSGSIZE), | |
42 _messages = 0 { | |
43 for (int i = 0; i < MSGSIZE; i++) { | |
44 _buffer[i] = FIRSTCHAR + i; | |
45 } | |
46 new EchoServer().spawn().then((SendPort port) { | |
47 _sendPort = port; | |
48 start(); | |
49 }); | |
50 } | |
51 | |
52 void sendData() { | |
53 Socket _socket; | |
54 | |
55 void closeHandler() { | |
56 _socket.close(); | |
57 } | |
58 | |
59 void errorHandler() { | |
60 print("Socket error"); | |
61 Expect.equals(true, false); | |
62 _socket.close(); | |
63 } | |
64 | |
65 void connectHandler() { | |
66 SocketInputStream inputStream = _socket.inputStream; | |
67 SocketOutputStream outputStream = _socket.outputStream; | |
68 | |
69 void dataSent() { | |
70 | |
71 void dataReceived(List<int> buffer) { | |
72 if (buffer.length == MSGSIZE - 1) { | |
73 for (int i = 0; i < MSGSIZE - 1; i++) { | |
74 Expect.equals(FIRSTCHAR + i, _buffer[i]); | |
75 } | |
76 inputStream.readUntil(PATTERN2, dataReceived); | |
77 } else { | |
78 Expect.equals(1, buffer.length); | |
79 _messages++; | |
80 _socket.close(); | |
81 if (_messages < MESSAGES) { | |
82 sendData(); | |
83 } else { | |
84 shutdown(); | |
85 } | |
86 } | |
87 } | |
88 | |
89 // Write data and continue in dataSent. | |
90 inputStream.readUntil(PATTERN1, dataReceived); | |
91 } | |
92 | |
93 _socket.setCloseHandler(closeHandler); | |
94 _socket.setErrorHandler(errorHandler); | |
95 | |
96 // Write data and continue in dataSent. | |
97 bool written = outputStream.write(_buffer, 0, MSGSIZE, dataSent); | |
98 if (written) { | |
99 dataSent(); | |
100 } | |
101 } | |
102 | |
103 _socket = new Socket(EchoServer.HOST, _port); | |
104 if (_socket !== null) { | |
105 _socket.setConnectHandler(connectHandler); | |
106 } | |
107 } | |
108 | |
109 void start() { | |
110 _receivePort.receive((var message, SendPort replyTo) { | |
111 _port = message; | |
112 sendData(); | |
113 }); | |
114 _sendPort.send(SERVERINIT, _receivePort.toSendPort()); | |
115 } | |
116 | |
117 void shutdown() { | |
118 _sendPort.send(SERVERSHUTDOWN, _receivePort.toSendPort()); | |
119 _receivePort.close(); | |
120 } | |
121 | |
122 int _port; | |
123 ReceivePort _receivePort; | |
124 SendPort _sendPort; | |
125 List<int> _buffer; | |
126 int _messages; | |
127 } | |
128 | |
129 class EchoServer extends Isolate { | |
130 | |
131 static final HOST = "127.0.0.1"; | |
132 static final int MSGSIZE = EchoServerGame.MSGSIZE; | |
133 static final int FIRSTCHAR = EchoServerGame.FIRSTCHAR; | |
134 static final List<int> PATTERN1 = EchoServerGame.PATTERN1; | |
135 static final List<int> PATTERN2 = EchoServerGame.PATTERN2; | |
136 | |
137 void main() { | |
138 | |
139 void connectionHandler() { | |
140 Socket _client; | |
141 | |
142 void messageHandler() { | |
143 SocketInputStream inputStream = _client.inputStream; | |
144 SocketOutputStream outputStream = _client.outputStream; | |
145 | |
146 // Data is expected to arrive in two chunks. First all but the | |
147 // last character and second a single character. | |
148 void dataReceived(List<int> buffer) { | |
149 | |
150 if (buffer.length == MSGSIZE - 1) { | |
151 for (int i = 0; i < MSGSIZE - 1; i++) { | |
152 Expect.equals(EchoServerGame.FIRSTCHAR + i, buffer[i]); | |
153 } | |
154 void next() { | |
155 inputStream.readUntil(PATTERN2, dataReceived); | |
156 inputStream.readUntil(PATTERN2, dataReceived); | |
157 } | |
158 bool done = outputStream.write(buffer, 0, buffer.length, next); | |
159 if (done) { | |
160 next(); | |
161 } | |
162 } else { | |
163 Expect.equals(1, buffer.length); | |
164 outputStream.write(buffer, 0, buffer.length, null); | |
165 } | |
166 } | |
167 | |
168 void closeHandler() { | |
169 _client.close(); | |
170 } | |
171 | |
172 void errorHandler() { | |
173 print("Socket error"); | |
174 Expect.equals(true, false); | |
175 _client.close(); | |
176 } | |
177 | |
178 _client.setCloseHandler(closeHandler); | |
179 _client.setErrorHandler(errorHandler); | |
180 | |
181 inputStream.readUntil(PATTERN1, dataReceived); | |
182 } | |
183 | |
184 _client = _server.accept(); | |
185 if (_client !== null) { | |
186 messageHandler(); | |
187 } | |
188 } | |
189 | |
190 void errorHandlerServer() { | |
191 Logger.println("Server socket error"); | |
192 _server.close(); | |
193 } | |
194 | |
195 this.port.receive((message, SendPort replyTo) { | |
196 if (message == EchoServerGame.SERVERINIT) { | |
197 _server = new ServerSocket(HOST, 0, 10); | |
198 Expect.equals(true, _server !== null); | |
199 int port = _server.port; | |
200 _server.setConnectionHandler(connectionHandler); | |
201 _server.setErrorHandler(errorHandlerServer); | |
202 replyTo.send(port, null); | |
203 } else if (message == EchoServerGame.SERVERSHUTDOWN) { | |
204 _server.close(); | |
205 this.port.close(); | |
206 } | |
207 }); | |
208 } | |
209 | |
210 ServerSocket _server; | |
211 } | |
OLD | NEW |