OLD | NEW |
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 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 | 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 class EchoServerStreamTest { | 12 class EchoServerStreamTest { |
13 | 13 |
14 static void testMain() { | 14 static void testMain() { |
15 EchoServerGame echoServerGame = new EchoServerGame.start(); | 15 EchoServerGame echoServerGame = new EchoServerGame.start(); |
16 } | 16 } |
17 } | 17 } |
18 | 18 |
19 class EchoServerGame { | 19 class EchoServerGame { |
20 | 20 |
21 static final MSGSIZE = 10; | 21 static final MSGSIZE = 10; |
22 static final SERVERINIT = 0; | 22 static final SERVERINIT = 0; |
23 static final SERVERSHUTDOWN = -1; | 23 static final SERVERSHUTDOWN = -1; |
24 static final MESSAGES = 200; | 24 static final MESSAGES = 100; |
25 static final FIRSTCHAR = 65; | 25 static final FIRSTCHAR = 65; |
26 | 26 |
27 EchoServerGame.start() | 27 EchoServerGame.start() |
28 : _receivePort = new ReceivePort(), | 28 : _receivePort = new ReceivePort(), |
29 _sendPort = null, | 29 _sendPort = null, |
30 _buffer = new List<int>(MSGSIZE), | 30 _buffer = new List<int>(MSGSIZE), |
31 _messages = 0 { | 31 _messages = 0 { |
32 for (int i = 0; i < MSGSIZE; i++) { | 32 for (int i = 0; i < MSGSIZE; i++) { |
33 _buffer[i] = FIRSTCHAR + i; | 33 _buffer[i] = FIRSTCHAR + i; |
34 } | 34 } |
35 new EchoServer().spawn().then((SendPort port) { | 35 new EchoServer().spawn().then((SendPort port) { |
36 _sendPort = port; | 36 _sendPort = port; |
37 start(); | 37 start(); |
38 }); | 38 }); |
39 } | 39 } |
40 | 40 |
41 void sendData() { | 41 void sendData() { |
42 | 42 |
43 void closeHandler() { | 43 void closeHandler() { |
44 _socket.close(); | 44 _socket.close(); |
45 } | 45 } |
46 | 46 |
47 void errorHandler() { | 47 void errorHandler() { |
48 print("Socket error"); | 48 Expect.fail("Socket error"); |
49 _socket.close(); | |
50 } | 49 } |
51 | 50 |
52 void connectHandler() { | 51 void connectHandler() { |
53 | 52 |
54 SocketOutputStream stream = _socket.outputStream; | 53 SocketOutputStream stream = _socket.outputStream; |
55 | 54 |
56 void dataSent() { | 55 void dataSent() { |
57 InputStream inputStream = _socket.inputStream; | 56 InputStream inputStream = _socket.inputStream; |
58 int offset = 0; | 57 int offset = 0; |
59 List<int> data; | 58 List<int> data; |
(...skipping 26 matching lines...) Expand all Loading... |
86 } | 85 } |
87 } | 86 } |
88 | 87 |
89 if (_messages % 2 == 0) data = new List<int>(MSGSIZE); | 88 if (_messages % 2 == 0) data = new List<int>(MSGSIZE); |
90 inputStream.dataHandler = dataReceived; | 89 inputStream.dataHandler = dataReceived; |
91 } | 90 } |
92 | 91 |
93 _socket.closeHandler = closeHandler; | 92 _socket.closeHandler = closeHandler; |
94 _socket.errorHandler = errorHandler; | 93 _socket.errorHandler = errorHandler; |
95 stream.write(_buffer); | 94 stream.write(_buffer); |
| 95 stream.close(); |
96 dataSent(); | 96 dataSent(); |
97 } | 97 } |
98 | 98 |
99 _socket = new Socket(EchoServer.HOST, _port); | 99 _socket = new Socket(EchoServer.HOST, _port); |
100 if (_socket !== null) { | 100 if (_socket !== null) { |
101 _socket.connectHandler = connectHandler; | 101 _socket.connectHandler = connectHandler; |
102 } else { | 102 } else { |
103 Expect.fail("socket creation failed"); | 103 Expect.fail("socket creation failed"); |
104 } | 104 } |
105 } | 105 } |
(...skipping 26 matching lines...) Expand all Loading... |
132 | 132 |
133 void main() { | 133 void main() { |
134 | 134 |
135 void connectionHandler() { | 135 void connectionHandler() { |
136 Socket _client; | 136 Socket _client; |
137 InputStream inputStream; | 137 InputStream inputStream; |
138 List<int> buffer = new List<int>(MSGSIZE); | 138 List<int> buffer = new List<int>(MSGSIZE); |
139 int offset = 0; | 139 int offset = 0; |
140 | 140 |
141 void dataReceived() { | 141 void dataReceived() { |
142 SocketOutputStream outputStream = _client.outputStream; | 142 SocketOutputStream outputStream; |
143 int bytesRead = inputStream.readInto(buffer, offset, MSGSIZE - offset); | 143 int bytesRead; |
| 144 outputStream = _client.outputStream; |
| 145 bytesRead = inputStream.readInto(buffer, offset, MSGSIZE - offset); |
144 if (bytesRead > 0) { | 146 if (bytesRead > 0) { |
145 offset += bytesRead; | 147 offset += bytesRead; |
146 for (int i = 0; i < offset; i++) { | 148 for (int i = 0; i < offset; i++) { |
147 Expect.equals(EchoServerGame.FIRSTCHAR + i, buffer[i]); | 149 Expect.equals(EchoServerGame.FIRSTCHAR + i, buffer[i]); |
148 } | 150 } |
149 if (offset == MSGSIZE) { | 151 if (offset == MSGSIZE) { |
150 outputStream.write(buffer); | 152 outputStream.write(buffer); |
| 153 outputStream.close(); |
151 } | 154 } |
152 } | 155 } |
153 } | 156 } |
154 | 157 |
155 void closeHandler() { | |
156 _client.close(); | |
157 } | |
158 | |
159 void errorHandler() { | 158 void errorHandler() { |
160 print("Socket error"); | 159 Expect.fail("Socket error"); |
161 _client.close(); | |
162 } | 160 } |
163 | 161 |
164 _client = _server.accept(); | 162 _client = _server.accept(); |
165 inputStream = _client.inputStream; | 163 inputStream = _client.inputStream; |
166 inputStream.dataHandler = dataReceived; | 164 inputStream.dataHandler = dataReceived; |
167 _client.closeHandler = closeHandler; | |
168 _client.errorHandler = errorHandler; | 165 _client.errorHandler = errorHandler; |
169 } | 166 } |
170 | 167 |
171 void errorHandlerServer() { | 168 void errorHandlerServer() { |
172 print("Server socket error"); | 169 Expect.fail("Server socket error"); |
173 _server.close(); | |
174 } | 170 } |
175 | 171 |
176 this.port.receive((message, SendPort replyTo) { | 172 this.port.receive((message, SendPort replyTo) { |
177 if (message == EchoServerGame.SERVERINIT) { | 173 if (message == EchoServerGame.SERVERINIT) { |
178 _server = new ServerSocket(HOST, 0, 10); | 174 _server = new ServerSocket(HOST, 0, 10); |
179 Expect.equals(true, _server !== null); | 175 Expect.equals(true, _server !== null); |
180 _server.connectionHandler = connectionHandler; | 176 _server.connectionHandler = connectionHandler; |
181 _server.errorHandler = errorHandlerServer; | 177 _server.errorHandler = errorHandlerServer; |
182 replyTo.send(_server.port, null); | 178 replyTo.send(_server.port, null); |
183 } else if (message == EchoServerGame.SERVERSHUTDOWN) { | 179 } else if (message == EchoServerGame.SERVERSHUTDOWN) { |
184 _server.close(); | 180 _server.close(); |
185 this.port.close(); | 181 this.port.close(); |
186 } | 182 } |
187 }); | 183 }); |
188 } | 184 } |
189 | 185 |
190 ServerSocket _server; | 186 ServerSocket _server; |
191 } | 187 } |
192 | 188 |
193 main() { | 189 main() { |
194 EchoServerStreamTest.testMain(); | 190 EchoServerStreamTest.testMain(); |
195 } | 191 } |
OLD | NEW |