| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "net/curvecp/test_client.h" | 5 #include "net/curvecp/test_client.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 #include <deque> | 8 #include <deque> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "base/message_loop.h" | 12 #include "base/message_loop.h" |
| 13 #include "net/base/address_list.h" | 13 #include "net/base/address_list.h" |
| 14 #include "net/base/host_resolver.h" | 14 #include "net/base/host_resolver.h" |
| 15 #include "net/base/io_buffer.h" | 15 #include "net/base/io_buffer.h" |
| 16 #include "net/base/net_errors.h" | 16 #include "net/base/net_errors.h" |
| 17 #include "net/base/net_log.h" | 17 #include "net/base/net_log.h" |
| 18 #include "net/base/single_request_host_resolver.h" | 18 #include "net/base/single_request_host_resolver.h" |
| 19 #include "net/curvecp/curvecp_client_socket.h" | 19 #include "net/curvecp/curvecp_client_socket.h" |
| 20 | 20 |
| 21 namespace net { | 21 namespace net { |
| 22 | 22 |
| 23 TestClient::TestClient() | 23 TestClient::TestClient() |
| 24 : socket_(NULL), | 24 : socket_(NULL), |
| 25 errors_(0), | 25 errors_(0), |
| 26 bytes_to_send_(0), | 26 bytes_to_send_(0) { |
| 27 ALLOW_THIS_IN_INITIALIZER_LIST( | |
| 28 connect_callback_(this, &TestClient::OnConnectComplete)), | |
| 29 ALLOW_THIS_IN_INITIALIZER_LIST( | |
| 30 read_callback_(this, &TestClient::OnReadComplete)), | |
| 31 ALLOW_THIS_IN_INITIALIZER_LIST( | |
| 32 write_callback_(this, &TestClient::OnWriteComplete)), | |
| 33 finished_callback_(NULL) { | |
| 34 } | 27 } |
| 35 | 28 |
| 36 TestClient::~TestClient() { | 29 TestClient::~TestClient() { |
| 37 if (socket_) { | 30 if (socket_) { |
| 38 // TODO(mbelshe): The CurveCPClientSocket has a method called Disconnect. | 31 // TODO(mbelshe): The CurveCPClientSocket has a method called Disconnect. |
| 39 // The CurveCPServerSocket has a method called Close. | 32 // The CurveCPServerSocket has a method called Close. |
| 40 // Unify them into either Close or Disconnect!!! | 33 // Unify them into either Close or Disconnect!!! |
| 41 socket_->Disconnect(); | 34 socket_->Disconnect(); |
| 42 socket_ = NULL; | 35 socket_ = NULL; |
| 43 } | 36 } |
| 44 } | 37 } |
| 45 | 38 |
| 46 bool TestClient::Start(const HostPortPair& server_host_port_pair, | 39 bool TestClient::Start(const HostPortPair& server_host_port_pair, |
| 47 int bytes_to_send, | 40 int bytes_to_send, |
| 48 OldCompletionCallback* callback) { | 41 const CompletionCallback& callback) { |
| 49 DCHECK(!socket_); | 42 DCHECK(!socket_); |
| 50 DCHECK(!finished_callback_); | 43 DCHECK(finished_callback_.is_null()); |
| 51 | 44 |
| 52 finished_callback_ = callback; | 45 finished_callback_ = callback; |
| 53 bytes_to_read_ = bytes_to_send_ = bytes_to_send; | 46 bytes_to_read_ = bytes_to_send_ = bytes_to_send; |
| 54 | 47 |
| 55 scoped_ptr<HostResolver> system_host_resolver( | 48 scoped_ptr<HostResolver> system_host_resolver( |
| 56 CreateSystemHostResolver(1, 0, NULL)); | 49 CreateSystemHostResolver(1, 0, NULL)); |
| 57 SingleRequestHostResolver host_resolver(system_host_resolver.get()); | 50 SingleRequestHostResolver host_resolver(system_host_resolver.get()); |
| 58 HostResolver::RequestInfo request(server_host_port_pair); | 51 HostResolver::RequestInfo request(server_host_port_pair); |
| 59 AddressList addresses; | 52 AddressList addresses; |
| 60 int rv = host_resolver.Resolve(request, &addresses, CompletionCallback(), | 53 int rv = host_resolver.Resolve(request, &addresses, CompletionCallback(), |
| 61 BoundNetLog()); | 54 BoundNetLog()); |
| 62 if (rv != OK) { | 55 if (rv != OK) { |
| 63 LOG(ERROR) << "Could not resolve host"; | 56 LOG(ERROR) << "Could not resolve host"; |
| 64 return false; | 57 return false; |
| 65 } | 58 } |
| 66 | 59 |
| 67 socket_ = new CurveCPClientSocket(addresses, NULL, NetLog::Source()); | 60 socket_ = new CurveCPClientSocket(addresses, NULL, NetLog::Source()); |
| 68 rv = socket_->Connect(&connect_callback_); | 61 rv = socket_->Connect( |
| 62 base::Bind(&TestClient::OnConnectComplete, base::Unretained(this))); |
| 69 if (rv == ERR_IO_PENDING) | 63 if (rv == ERR_IO_PENDING) |
| 70 return true; | 64 return true; |
| 71 OnConnectComplete(rv); | 65 OnConnectComplete(rv); |
| 72 return rv == OK; | 66 return rv == OK; |
| 73 } | 67 } |
| 74 | 68 |
| 75 void TestClient::OnConnectComplete(int result) { | 69 void TestClient::OnConnectComplete(int result) { |
| 76 LOG(ERROR) << "Connect complete"; | 70 LOG(ERROR) << "Connect complete"; |
| 77 if (result < 0) { | 71 if (result < 0) { |
| 78 LOG(ERROR) << "Connect failure: " << result; | 72 LOG(ERROR) << "Connect failure: " << result; |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 129 if (bytes_to_send_) | 123 if (bytes_to_send_) |
| 130 SendData(); | 124 SendData(); |
| 131 } | 125 } |
| 132 | 126 |
| 133 void TestClient::ReadData() { | 127 void TestClient::ReadData() { |
| 134 DCHECK(!read_buffer_.get()); | 128 DCHECK(!read_buffer_.get()); |
| 135 read_buffer_ = new IOBuffer(kMaxMessage); | 129 read_buffer_ = new IOBuffer(kMaxMessage); |
| 136 | 130 |
| 137 int rv; | 131 int rv; |
| 138 do { | 132 do { |
| 139 rv = socket_->Read(read_buffer_, kMaxMessage, &read_callback_); | 133 rv = socket_->Read(read_buffer_, kMaxMessage, |
| 134 base::Bind(&TestClient::OnReadComplete, |
| 135 base::Unretained(this))); |
| 140 if (rv == ERR_IO_PENDING) | 136 if (rv == ERR_IO_PENDING) |
| 141 return; | 137 return; |
| 142 OnReadComplete(rv); // Complete the read manually | 138 OnReadComplete(rv); // Complete the read manually |
| 143 } while (rv > 0); | 139 } while (rv > 0); |
| 144 } | 140 } |
| 145 | 141 |
| 146 void TestClient::SendData() { | 142 void TestClient::SendData() { |
| 147 DCHECK(bytes_to_send_); // We should have data to send. | 143 DCHECK(bytes_to_send_); // We should have data to send. |
| 148 const int kWriteChunkSize = 777; // 777 is more abusive | 144 const int kWriteChunkSize = 777; // 777 is more abusive |
| 149 | 145 |
| 150 do { | 146 do { |
| 151 if (!write_buffer_.get()) { | 147 if (!write_buffer_.get()) { |
| 152 int bytes_to_send = std::min(kWriteChunkSize, bytes_to_send_); | 148 int bytes_to_send = std::min(kWriteChunkSize, bytes_to_send_); |
| 153 scoped_refptr<IOBuffer> buffer(new IOBuffer(bytes_to_send)); | 149 scoped_refptr<IOBuffer> buffer(new IOBuffer(bytes_to_send)); |
| 154 sent_stream_.GetBytes(buffer->data(), bytes_to_send); | 150 sent_stream_.GetBytes(buffer->data(), bytes_to_send); |
| 155 write_buffer_ = new DrainableIOBuffer(buffer, bytes_to_send); | 151 write_buffer_ = new DrainableIOBuffer(buffer, bytes_to_send); |
| 156 } | 152 } |
| 157 | 153 |
| 158 int rv = socket_->Write(write_buffer_, | 154 int rv = socket_->Write(write_buffer_, |
| 159 write_buffer_->BytesRemaining(), | 155 write_buffer_->BytesRemaining(), |
| 160 &write_callback_); | 156 base::Bind(&TestClient::OnWriteComplete, |
| 157 base::Unretained(this))); |
| 161 if (rv == ERR_IO_PENDING) | 158 if (rv == ERR_IO_PENDING) |
| 162 return; | 159 return; |
| 163 | 160 |
| 164 write_buffer_->DidConsume(rv); | 161 write_buffer_->DidConsume(rv); |
| 165 bytes_to_send_ -= rv; | 162 bytes_to_send_ -= rv; |
| 166 if (!write_buffer_->BytesRemaining()) | 163 if (!write_buffer_->BytesRemaining()) |
| 167 write_buffer_ = NULL; | 164 write_buffer_ = NULL; |
| 168 } while (bytes_to_send_); | 165 } while (bytes_to_send_); |
| 169 } | 166 } |
| 170 | 167 |
| 171 void TestClient::Finish(int result) { | 168 void TestClient::Finish(int result) { |
| 172 DCHECK(finished_callback_); | 169 DCHECK(!finished_callback_.is_null()); |
| 173 | 170 |
| 174 LOG(ERROR) << "TestClient Done!"; | 171 LOG(ERROR) << "TestClient Done!"; |
| 175 OldCompletionCallback* callback = finished_callback_; | 172 CompletionCallback callback = finished_callback_; |
| 176 finished_callback_ = NULL; | 173 finished_callback_.Reset(); |
| 177 callback->Run(result); | 174 callback.Run(result); |
| 178 } | 175 } |
| 179 | 176 |
| 180 } // namespace net | 177 } // namespace net |
| OLD | NEW |