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