OLD | NEW |
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 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 | 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 |
(...skipping 30 matching lines...) Expand all Loading... |
42 | 42 |
43 void connectHandler() { | 43 void connectHandler() { |
44 | 44 |
45 SocketOutputStream stream = _socket.outputStream; | 45 SocketOutputStream stream = _socket.outputStream; |
46 | 46 |
47 void dataSent() { | 47 void dataSent() { |
48 InputStream inputStream = _socket.inputStream; | 48 InputStream inputStream = _socket.inputStream; |
49 int offset = 0; | 49 int offset = 0; |
50 List<int> data; | 50 List<int> data; |
51 | 51 |
52 void onClose() { | 52 void onClosed() { |
53 Expect.equals(MSGSIZE, offset); | 53 Expect.equals(MSGSIZE, offset); |
54 _messages++; | 54 _messages++; |
55 if (_messages < MESSAGES) { | 55 if (_messages < MESSAGES) { |
56 sendData(); | 56 sendData(); |
57 } else { | 57 } else { |
58 shutdown(); | 58 shutdown(); |
59 } | 59 } |
60 } | 60 } |
61 | 61 |
62 void onData() { | 62 void onData() { |
63 // Test both read and readInto. | 63 // Test both read and readInto. |
64 int bytesRead = 0; | 64 int bytesRead = 0; |
65 if (_messages % 2 == 0) { | 65 if (_messages % 2 == 0) { |
66 bytesRead = inputStream.readInto(data, offset, MSGSIZE - offset); | 66 bytesRead = inputStream.readInto(data, offset, MSGSIZE - offset); |
67 for (int i = 0; i < offset + bytesRead; i++) { | 67 for (int i = 0; i < offset + bytesRead; i++) { |
68 Expect.equals(FIRSTCHAR + i, data[i]); | 68 Expect.equals(FIRSTCHAR + i, data[i]); |
69 } | 69 } |
70 } else { | 70 } else { |
71 data = inputStream.read(); | 71 data = inputStream.read(); |
72 bytesRead = data.length; | 72 bytesRead = data.length; |
73 for (int i = 0; i < data.length; i++) { | 73 for (int i = 0; i < data.length; i++) { |
74 Expect.equals(FIRSTCHAR + i + offset, data[i]); | 74 Expect.equals(FIRSTCHAR + i + offset, data[i]); |
75 } | 75 } |
76 } | 76 } |
77 | 77 |
78 offset += bytesRead; | 78 offset += bytesRead; |
79 } | 79 } |
80 | 80 |
81 if (_messages % 2 == 0) data = new List<int>(MSGSIZE); | 81 if (_messages % 2 == 0) data = new List<int>(MSGSIZE); |
82 inputStream.dataHandler = onData; | 82 inputStream.onData = onData; |
83 inputStream.closeHandler = onClose; | 83 inputStream.onClosed = onClosed; |
84 } | 84 } |
85 | 85 |
86 _socket.errorHandler = errorHandler; | 86 _socket.onError = errorHandler; |
87 | 87 |
88 // Test both write and writeFrom in different forms. | 88 // Test both write and writeFrom in different forms. |
89 switch (_messages % 4) { | 89 switch (_messages % 4) { |
90 case 0: | 90 case 0: |
91 stream.write(_buffer); | 91 stream.write(_buffer); |
92 break; | 92 break; |
93 case 1: | 93 case 1: |
94 stream.write(_buffer, false); | 94 stream.write(_buffer, false); |
95 break; | 95 break; |
96 case 2: | 96 case 2: |
97 stream.writeFrom(_buffer); | 97 stream.writeFrom(_buffer); |
98 break; | 98 break; |
99 case 3: | 99 case 3: |
100 Expect.equals(0, _buffer.length % 2); | 100 Expect.equals(0, _buffer.length % 2); |
101 stream.writeFrom(_buffer, len: _buffer.length ~/ 2); | 101 stream.writeFrom(_buffer, len: _buffer.length ~/ 2); |
102 stream.writeFrom(_buffer, _buffer.length ~/ 2); | 102 stream.writeFrom(_buffer, _buffer.length ~/ 2); |
103 break; | 103 break; |
104 } | 104 } |
105 stream.close(); | 105 stream.close(); |
106 dataSent(); | 106 dataSent(); |
107 } | 107 } |
108 | 108 |
109 _socket = new Socket(TestingServer.HOST, _port); | 109 _socket = new Socket(TestingServer.HOST, _port); |
110 if (_socket !== null) { | 110 if (_socket !== null) { |
111 _socket.connectHandler = connectHandler; | 111 _socket.onConnect = connectHandler; |
112 } else { | 112 } else { |
113 Expect.fail("socket creation failed"); | 113 Expect.fail("socket creation failed"); |
114 } | 114 } |
115 } | 115 } |
116 | 116 |
117 void start() { | 117 void start() { |
118 _receivePort.receive((var message, SendPort replyTo) { | 118 _receivePort.receive((var message, SendPort replyTo) { |
119 _port = message; | 119 _port = message; |
120 sendData(); | 120 sendData(); |
121 }); | 121 }); |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
160 outputStream.close(); | 160 outputStream.close(); |
161 } | 161 } |
162 } | 162 } |
163 } | 163 } |
164 | 164 |
165 void errorHandler() { | 165 void errorHandler() { |
166 Expect.fail("Socket error"); | 166 Expect.fail("Socket error"); |
167 } | 167 } |
168 | 168 |
169 inputStream = connection.inputStream; | 169 inputStream = connection.inputStream; |
170 inputStream.dataHandler = dataReceived; | 170 inputStream.onData = dataReceived; |
171 connection.errorHandler = errorHandler; | 171 connection.onError = errorHandler; |
172 } | 172 } |
173 } | 173 } |
174 | 174 |
175 main() { | 175 main() { |
176 EchoServerGame echoServerGame = new EchoServerGame.start(); | 176 EchoServerGame echoServerGame = new EchoServerGame.start(); |
177 } | 177 } |
OLD | NEW |