OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "blimp/net/tcp_client_transport.h" | 5 #include "blimp/net/tcp_client_transport.h" |
6 | 6 |
7 #include "base/callback.h" | 7 #include "base/callback.h" |
8 #include "base/callback_helpers.h" | 8 #include "base/callback_helpers.h" |
9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
11 #include "blimp/net/stream_socket_connection.h" | 11 #include "blimp/net/stream_socket_connection.h" |
| 12 #include "net/socket/client_socket_factory.h" |
12 #include "net/socket/stream_socket.h" | 13 #include "net/socket/stream_socket.h" |
13 #include "net/socket/tcp_client_socket.h" | 14 #include "net/socket/tcp_client_socket.h" |
14 | 15 |
15 namespace blimp { | 16 namespace blimp { |
16 | 17 |
17 TCPClientTransport::TCPClientTransport(const net::AddressList& addresses, | 18 TCPClientTransport::TCPClientTransport(const net::IPEndPoint& ip_endpoint, |
18 net::NetLog* net_log) | 19 net::NetLog* net_log) |
19 : addresses_(addresses), net_log_(net_log) {} | 20 : ip_endpoint_(ip_endpoint), |
| 21 net_log_(net_log), |
| 22 socket_factory_(net::ClientSocketFactory::GetDefaultFactory()) {} |
20 | 23 |
21 TCPClientTransport::~TCPClientTransport() {} | 24 TCPClientTransport::~TCPClientTransport() {} |
22 | 25 |
| 26 void TCPClientTransport::SetClientSocketFactoryForTest( |
| 27 net::ClientSocketFactory* factory) { |
| 28 DCHECK(factory); |
| 29 socket_factory_ = factory; |
| 30 } |
| 31 |
23 void TCPClientTransport::Connect(const net::CompletionCallback& callback) { | 32 void TCPClientTransport::Connect(const net::CompletionCallback& callback) { |
24 DCHECK(!socket_); | 33 DCHECK(!socket_); |
25 DCHECK(!callback.is_null()); | 34 DCHECK(!callback.is_null()); |
26 | 35 |
27 socket_.reset( | 36 connect_callback_ = callback; |
28 new net::TCPClientSocket(addresses_, net_log_, net::NetLog::Source())); | 37 socket_ = socket_factory_->CreateTransportClientSocket( |
| 38 net::AddressList(ip_endpoint_), net_log_, net::NetLog::Source()); |
29 net::CompletionCallback completion_callback = base::Bind( | 39 net::CompletionCallback completion_callback = base::Bind( |
30 &TCPClientTransport::OnTCPConnectComplete, base::Unretained(this)); | 40 &TCPClientTransport::OnTCPConnectComplete, base::Unretained(this)); |
31 | 41 |
32 int result = socket_->Connect(completion_callback); | 42 int result = socket_->Connect(completion_callback); |
33 if (result == net::ERR_IO_PENDING) { | 43 if (result == net::ERR_IO_PENDING) { |
34 connect_callback_ = callback; | |
35 return; | 44 return; |
36 } | 45 } |
37 | 46 |
38 if (result != net::OK) { | 47 OnTCPConnectComplete(result); |
39 socket_ = nullptr; | |
40 } | |
41 | |
42 base::MessageLoop::current()->PostTask(FROM_HERE, | |
43 base::Bind(callback, result)); | |
44 } | 48 } |
45 | 49 |
46 scoped_ptr<BlimpConnection> TCPClientTransport::TakeConnection() { | 50 scoped_ptr<BlimpConnection> TCPClientTransport::TakeConnection() { |
47 DCHECK(connect_callback_.is_null()); | 51 DCHECK(connect_callback_.is_null()); |
48 DCHECK(socket_); | 52 DCHECK(socket_); |
49 return make_scoped_ptr(new StreamSocketConnection(std::move(socket_))); | 53 return make_scoped_ptr(new StreamSocketConnection(std::move(socket_))); |
50 } | 54 } |
51 | 55 |
52 const std::string TCPClientTransport::GetName() const { | 56 const char* TCPClientTransport::GetName() const { |
53 return "TCP"; | 57 return "TCP"; |
54 } | 58 } |
55 | 59 |
56 void TCPClientTransport::OnTCPConnectComplete(int result) { | 60 void TCPClientTransport::OnTCPConnectComplete(int result) { |
57 DCHECK_NE(net::ERR_IO_PENDING, result); | 61 DCHECK_NE(net::ERR_IO_PENDING, result); |
58 DCHECK(socket_); | 62 OnConnectComplete(result); |
| 63 } |
| 64 |
| 65 void TCPClientTransport::OnConnectComplete(int result) { |
59 if (result != net::OK) { | 66 if (result != net::OK) { |
60 socket_ = nullptr; | 67 socket_ = nullptr; |
61 } | 68 } |
62 base::ResetAndReturn(&connect_callback_).Run(result); | 69 base::ResetAndReturn(&connect_callback_).Run(result); |
63 } | 70 } |
64 | 71 |
| 72 scoped_ptr<net::StreamSocket> TCPClientTransport::TakeSocket() { |
| 73 return std::move(socket_); |
| 74 } |
| 75 |
| 76 void TCPClientTransport::SetSocket(scoped_ptr<net::StreamSocket> socket) { |
| 77 DCHECK(socket); |
| 78 socket_ = std::move(socket); |
| 79 } |
| 80 |
| 81 net::ClientSocketFactory* TCPClientTransport::socket_factory() const { |
| 82 return socket_factory_; |
| 83 } |
| 84 |
65 } // namespace blimp | 85 } // namespace blimp |
OLD | NEW |