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 |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
47 void errorHandler() { | 47 void errorHandler() { |
48 print("Socket error"); | 48 print("Socket error"); |
49 _socket.close(); | 49 _socket.close(); |
50 } | 50 } |
51 | 51 |
52 void connectHandler() { | 52 void connectHandler() { |
53 | 53 |
54 SocketOutputStream stream = _socket.outputStream; | 54 SocketOutputStream stream = _socket.outputStream; |
55 | 55 |
56 void dataSent() { | 56 void dataSent() { |
57 // Reset buffer | 57 InputStream inputStream = _socket.inputStream; |
58 for (int i = 0; i < MSGSIZE; i++) { | 58 int offset = 0; |
59 _buffer[i] = 1; | 59 List<int> data; |
60 } | |
61 SocketInputStream stream = _socket.inputStream; | |
62 | 60 |
63 void dataReceived() { | 61 void dataReceived() { |
64 for (int i = 0; i < MSGSIZE; i++) { | 62 // Test both read and readInto. |
65 Expect.equals(FIRSTCHAR + i, _buffer[i]); | 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 } |
66 } | 75 } |
67 _messages++; | 76 |
68 _socket.close(); | 77 offset += bytesRead; |
69 if (_messages < MESSAGES) { | 78 if (offset == MSGSIZE) { |
70 sendData(); | 79 _messages++; |
71 } else { | 80 _socket.close(); |
72 shutdown(); | 81 if (_messages < MESSAGES) { |
| 82 sendData(); |
| 83 } else { |
| 84 shutdown(); |
| 85 } |
73 } | 86 } |
74 } | 87 } |
75 | 88 |
76 bool read = stream.read(_buffer, 0, MSGSIZE, dataReceived); | 89 if (_messages % 2 == 0) data = new List<int>(MSGSIZE); |
77 if (read) { | 90 inputStream.dataHandler = dataReceived; |
78 dataReceived(); | |
79 } | |
80 } | 91 } |
81 | 92 |
82 _socket.setCloseHandler(closeHandler); | 93 _socket.setCloseHandler(closeHandler); |
83 _socket.setErrorHandler(errorHandler); | 94 _socket.setErrorHandler(errorHandler); |
84 bool written = stream.write(_buffer, 0, MSGSIZE, dataSent); | 95 bool written = stream.write(_buffer, 0, MSGSIZE, dataSent); |
85 if (written) { | 96 if (written) { |
86 dataSent(); | 97 dataSent(); |
87 } | 98 } |
88 } | 99 } |
89 | 100 |
(...skipping 28 matching lines...) Expand all Loading... |
118 | 129 |
119 class EchoServer extends Isolate { | 130 class EchoServer extends Isolate { |
120 | 131 |
121 static final HOST = "127.0.0.1"; | 132 static final HOST = "127.0.0.1"; |
122 static final int MSGSIZE = EchoServerGame.MSGSIZE; | 133 static final int MSGSIZE = EchoServerGame.MSGSIZE; |
123 | 134 |
124 void main() { | 135 void main() { |
125 | 136 |
126 void connectionHandler() { | 137 void connectionHandler() { |
127 Socket _client; | 138 Socket _client; |
| 139 InputStream inputStream; |
| 140 List<int> buffer = new List<int>(MSGSIZE); |
| 141 int offset = 0; |
128 | 142 |
129 void messageHandler() { | 143 void dataReceived() { |
130 | 144 SocketOutputStream outputStream = _client.outputStream; |
131 List<int> buffer = new List<int>(MSGSIZE); | 145 int bytesRead = inputStream.readInto(buffer, offset, MSGSIZE - offset); |
132 | 146 if (bytesRead > 0) { |
133 void dataReceived() { | 147 offset += bytesRead; |
134 | 148 for (int i = 0; i < offset; i++) { |
135 SocketOutputStream outputStream = _client.outputStream; | |
136 | |
137 for (int i = 0; i < MSGSIZE; i++) { | |
138 Expect.equals(EchoServerGame.FIRSTCHAR + i, buffer[i]); | 149 Expect.equals(EchoServerGame.FIRSTCHAR + i, buffer[i]); |
139 } | 150 } |
140 outputStream.write(buffer, 0, MSGSIZE, null); | 151 if (offset == MSGSIZE) { |
141 } | 152 outputStream.write(buffer, 0, buffer.length, null); |
142 | 153 } |
143 SocketInputStream inputStream = _client.inputStream; | |
144 bool read = inputStream.read(buffer, 0, MSGSIZE, dataReceived); | |
145 if (read) { | |
146 dataReceived(); | |
147 } | 154 } |
148 } | 155 } |
149 | 156 |
150 void closeHandler() { | 157 void closeHandler() { |
151 _client.close(); | 158 _client.close(); |
152 } | 159 } |
153 | 160 |
154 void errorHandler() { | 161 void errorHandler() { |
155 print("Socket error"); | 162 print("Socket error"); |
156 _client.close(); | 163 _client.close(); |
157 } | 164 } |
158 | 165 |
159 _client = _server.accept(); | 166 _client = _server.accept(); |
160 _client.setDataHandler(messageHandler); | 167 inputStream = _client.inputStream; |
| 168 inputStream.dataHandler = dataReceived; |
161 _client.setCloseHandler(closeHandler); | 169 _client.setCloseHandler(closeHandler); |
162 _client.setErrorHandler(errorHandler); | 170 _client.setErrorHandler(errorHandler); |
163 } | 171 } |
164 | 172 |
165 void errorHandlerServer() { | 173 void errorHandlerServer() { |
166 print("Server socket error"); | 174 print("Server socket error"); |
167 _server.close(); | 175 _server.close(); |
168 } | 176 } |
169 | 177 |
170 this.port.receive((message, SendPort replyTo) { | 178 this.port.receive((message, SendPort replyTo) { |
171 if (message == EchoServerGame.SERVERINIT) { | 179 if (message == EchoServerGame.SERVERINIT) { |
172 _server = new ServerSocket(HOST, 0, 10); | 180 _server = new ServerSocket(HOST, 0, 10); |
173 Expect.equals(true, _server !== null); | 181 Expect.equals(true, _server !== null); |
174 _server.setConnectionHandler(connectionHandler); | 182 _server.setConnectionHandler(connectionHandler); |
175 _server.setErrorHandler(errorHandlerServer); | 183 _server.setErrorHandler(errorHandlerServer); |
176 replyTo.send(_server.port, null); | 184 replyTo.send(_server.port, null); |
177 } else if (message == EchoServerGame.SERVERSHUTDOWN) { | 185 } else if (message == EchoServerGame.SERVERSHUTDOWN) { |
178 _server.close(); | 186 _server.close(); |
179 this.port.close(); | 187 this.port.close(); |
180 } | 188 } |
181 }); | 189 }); |
182 } | 190 } |
183 | 191 |
184 ServerSocket _server; | 192 ServerSocket _server; |
185 } | 193 } |
186 | 194 |
187 main() { | 195 main() { |
188 EchoServerStreamTest.testMain(); | 196 EchoServerStreamTest.testMain(); |
189 } | 197 } |
OLD | NEW |