OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "remoting/protocol/connection_tester.h" | 5 #include "remoting/protocol/connection_tester.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/message_loop.h" | 8 #include "base/message_loop.h" |
9 #include "net/base/io_buffer.h" | 9 #include "net/base/io_buffer.h" |
10 #include "net/base/net_errors.h" | 10 #include "net/base/net_errors.h" |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
67 | 67 |
68 void StreamConnectionTester::DoWrite() { | 68 void StreamConnectionTester::DoWrite() { |
69 int result = 1; | 69 int result = 1; |
70 while (result > 0) { | 70 while (result > 0) { |
71 if (output_buffer_->BytesRemaining() == 0) | 71 if (output_buffer_->BytesRemaining() == 0) |
72 break; | 72 break; |
73 | 73 |
74 int bytes_to_write = std::min(output_buffer_->BytesRemaining(), | 74 int bytes_to_write = std::min(output_buffer_->BytesRemaining(), |
75 message_size_); | 75 message_size_); |
76 result = client_socket_->Write( | 76 result = client_socket_->Write( |
77 output_buffer_, bytes_to_write, | 77 output_buffer_.get(), |
| 78 bytes_to_write, |
78 base::Bind(&StreamConnectionTester::OnWritten, base::Unretained(this))); | 79 base::Bind(&StreamConnectionTester::OnWritten, base::Unretained(this))); |
79 HandleWriteResult(result); | 80 HandleWriteResult(result); |
80 } | 81 } |
81 } | 82 } |
82 | 83 |
83 void StreamConnectionTester::OnWritten(int result) { | 84 void StreamConnectionTester::OnWritten(int result) { |
84 HandleWriteResult(result); | 85 HandleWriteResult(result); |
85 DoWrite(); | 86 DoWrite(); |
86 } | 87 } |
87 | 88 |
88 void StreamConnectionTester::HandleWriteResult(int result) { | 89 void StreamConnectionTester::HandleWriteResult(int result) { |
89 if (result <= 0 && result != net::ERR_IO_PENDING) { | 90 if (result <= 0 && result != net::ERR_IO_PENDING) { |
90 LOG(ERROR) << "Received error " << result << " when trying to write"; | 91 LOG(ERROR) << "Received error " << result << " when trying to write"; |
91 write_errors_++; | 92 write_errors_++; |
92 Done(); | 93 Done(); |
93 } else if (result > 0) { | 94 } else if (result > 0) { |
94 output_buffer_->DidConsume(result); | 95 output_buffer_->DidConsume(result); |
95 } | 96 } |
96 } | 97 } |
97 | 98 |
98 void StreamConnectionTester::DoRead() { | 99 void StreamConnectionTester::DoRead() { |
99 int result = 1; | 100 int result = 1; |
100 while (result > 0) { | 101 while (result > 0) { |
101 input_buffer_->SetCapacity(input_buffer_->offset() + message_size_); | 102 input_buffer_->SetCapacity(input_buffer_->offset() + message_size_); |
102 result = host_socket_->Read( | 103 result = host_socket_->Read( |
103 input_buffer_, message_size_, | 104 input_buffer_.get(), |
| 105 message_size_, |
104 base::Bind(&StreamConnectionTester::OnRead, base::Unretained(this))); | 106 base::Bind(&StreamConnectionTester::OnRead, base::Unretained(this))); |
105 HandleReadResult(result); | 107 HandleReadResult(result); |
106 }; | 108 }; |
107 } | 109 } |
108 | 110 |
109 void StreamConnectionTester::OnRead(int result) { | 111 void StreamConnectionTester::OnRead(int result) { |
110 HandleReadResult(result); | 112 HandleReadResult(result); |
111 if (!done_) | 113 if (!done_) |
112 DoRead(); // Don't try to read again when we are done reading. | 114 DoRead(); // Don't try to read again when we are done reading. |
113 } | 115 } |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
178 | 180 |
179 scoped_refptr<net::IOBuffer> packet(new net::IOBuffer(message_size_)); | 181 scoped_refptr<net::IOBuffer> packet(new net::IOBuffer(message_size_)); |
180 for (int i = 0; i < message_size_; ++i) { | 182 for (int i = 0; i < message_size_; ++i) { |
181 packet->data()[i] = static_cast<char>(i); | 183 packet->data()[i] = static_cast<char>(i); |
182 } | 184 } |
183 sent_packets_[packets_sent_] = packet; | 185 sent_packets_[packets_sent_] = packet; |
184 // Put index of this packet in the beginning of the packet body. | 186 // Put index of this packet in the beginning of the packet body. |
185 memcpy(packet->data(), &packets_sent_, sizeof(packets_sent_)); | 187 memcpy(packet->data(), &packets_sent_, sizeof(packets_sent_)); |
186 | 188 |
187 int result = client_socket_->Write( | 189 int result = client_socket_->Write( |
188 packet, message_size_, | 190 packet.get(), |
| 191 message_size_, |
189 base::Bind(&DatagramConnectionTester::OnWritten, base::Unretained(this))); | 192 base::Bind(&DatagramConnectionTester::OnWritten, base::Unretained(this))); |
190 HandleWriteResult(result); | 193 HandleWriteResult(result); |
191 } | 194 } |
192 | 195 |
193 void DatagramConnectionTester::OnWritten(int result) { | 196 void DatagramConnectionTester::OnWritten(int result) { |
194 HandleWriteResult(result); | 197 HandleWriteResult(result); |
195 } | 198 } |
196 | 199 |
197 void DatagramConnectionTester::HandleWriteResult(int result) { | 200 void DatagramConnectionTester::HandleWriteResult(int result) { |
198 if (result <= 0 && result != net::ERR_IO_PENDING) { | 201 if (result <= 0 && result != net::ERR_IO_PENDING) { |
(...skipping 10 matching lines...) Expand all Loading... |
209 } | 212 } |
210 } | 213 } |
211 | 214 |
212 void DatagramConnectionTester::DoRead() { | 215 void DatagramConnectionTester::DoRead() { |
213 int result = 1; | 216 int result = 1; |
214 while (result > 0) { | 217 while (result > 0) { |
215 int kReadSize = message_size_ * 2; | 218 int kReadSize = message_size_ * 2; |
216 read_buffer_ = new net::IOBuffer(kReadSize); | 219 read_buffer_ = new net::IOBuffer(kReadSize); |
217 | 220 |
218 result = host_socket_->Read( | 221 result = host_socket_->Read( |
219 read_buffer_, kReadSize, | 222 read_buffer_.get(), |
| 223 kReadSize, |
220 base::Bind(&DatagramConnectionTester::OnRead, base::Unretained(this))); | 224 base::Bind(&DatagramConnectionTester::OnRead, base::Unretained(this))); |
221 HandleReadResult(result); | 225 HandleReadResult(result); |
222 }; | 226 }; |
223 } | 227 } |
224 | 228 |
225 void DatagramConnectionTester::OnRead(int result) { | 229 void DatagramConnectionTester::OnRead(int result) { |
226 HandleReadResult(result); | 230 HandleReadResult(result); |
227 DoRead(); | 231 DoRead(); |
228 } | 232 } |
229 | 233 |
(...skipping 18 matching lines...) Expand all Loading... |
248 if (memcmp(read_buffer_->data(), sent_packets_[packet_id]->data(), | 252 if (memcmp(read_buffer_->data(), sent_packets_[packet_id]->data(), |
249 message_size_) != 0) | 253 message_size_) != 0) |
250 bad_packets_received_++; | 254 bad_packets_received_++; |
251 } | 255 } |
252 } | 256 } |
253 } | 257 } |
254 } | 258 } |
255 | 259 |
256 } // namespace protocol | 260 } // namespace protocol |
257 } // namespace remoting | 261 } // namespace remoting |
OLD | NEW |