| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "blimp/net/tcp_client_transport.h" | |
| 6 | |
| 7 #include <memory> | |
| 8 #include <utility> | |
| 9 | |
| 10 #include "base/callback.h" | |
| 11 #include "base/callback_helpers.h" | |
| 12 #include "base/memory/ptr_util.h" | |
| 13 #include "base/message_loop/message_loop.h" | |
| 14 #include "blimp/net/message_port.h" | |
| 15 #include "blimp/net/tcp_connection.h" | |
| 16 #include "net/log/net_log_source.h" | |
| 17 #include "net/socket/client_socket_factory.h" | |
| 18 #include "net/socket/stream_socket.h" | |
| 19 #include "net/socket/tcp_client_socket.h" | |
| 20 | |
| 21 namespace blimp { | |
| 22 | |
| 23 TCPClientTransport::TCPClientTransport(const net::IPEndPoint& ip_endpoint, | |
| 24 net::NetLog* net_log) | |
| 25 : ip_endpoint_(ip_endpoint), | |
| 26 net_log_(net_log), | |
| 27 socket_factory_(net::ClientSocketFactory::GetDefaultFactory()) {} | |
| 28 | |
| 29 TCPClientTransport::~TCPClientTransport() {} | |
| 30 | |
| 31 void TCPClientTransport::SetClientSocketFactoryForTest( | |
| 32 net::ClientSocketFactory* factory) { | |
| 33 DCHECK(factory); | |
| 34 socket_factory_ = factory; | |
| 35 } | |
| 36 | |
| 37 void TCPClientTransport::Connect(const net::CompletionCallback& callback) { | |
| 38 DCHECK(!socket_); | |
| 39 DCHECK(!callback.is_null()); | |
| 40 | |
| 41 connect_callback_ = callback; | |
| 42 socket_ = socket_factory_->CreateTransportClientSocket( | |
| 43 net::AddressList(ip_endpoint_), nullptr, net_log_, net::NetLogSource()); | |
| 44 net::CompletionCallback completion_callback = base::Bind( | |
| 45 &TCPClientTransport::OnTCPConnectComplete, base::Unretained(this)); | |
| 46 | |
| 47 int result = socket_->Connect(completion_callback); | |
| 48 if (result == net::ERR_IO_PENDING) { | |
| 49 return; | |
| 50 } | |
| 51 | |
| 52 OnTCPConnectComplete(result); | |
| 53 } | |
| 54 | |
| 55 std::unique_ptr<MessagePort> TCPClientTransport::TakeMessagePort() { | |
| 56 DCHECK(connect_callback_.is_null()); | |
| 57 DCHECK(socket_); | |
| 58 return MessagePort::CreateForStreamSocketWithCompression(std::move(socket_)); | |
| 59 } | |
| 60 | |
| 61 std::unique_ptr<BlimpConnection> TCPClientTransport::MakeConnection() { | |
| 62 return base::MakeUnique<TCPConnection>(TakeMessagePort()); | |
| 63 } | |
| 64 | |
| 65 const char* TCPClientTransport::GetName() const { | |
| 66 return "TCP"; | |
| 67 } | |
| 68 | |
| 69 void TCPClientTransport::OnTCPConnectComplete(int result) { | |
| 70 DCHECK_NE(net::ERR_IO_PENDING, result); | |
| 71 OnConnectComplete(result); | |
| 72 } | |
| 73 | |
| 74 void TCPClientTransport::OnConnectComplete(int result) { | |
| 75 if (result != net::OK) { | |
| 76 socket_ = nullptr; | |
| 77 } | |
| 78 base::ResetAndReturn(&connect_callback_).Run(result); | |
| 79 } | |
| 80 | |
| 81 std::unique_ptr<net::StreamSocket> TCPClientTransport::TakeSocket() { | |
| 82 return std::move(socket_); | |
| 83 } | |
| 84 | |
| 85 void TCPClientTransport::SetSocket(std::unique_ptr<net::StreamSocket> socket) { | |
| 86 DCHECK(socket); | |
| 87 socket_ = std::move(socket); | |
| 88 } | |
| 89 | |
| 90 net::ClientSocketFactory* TCPClientTransport::socket_factory() const { | |
| 91 return socket_factory_; | |
| 92 } | |
| 93 | |
| 94 } // namespace blimp | |
| OLD | NEW |