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_engine_transport.h" | 5 #include "blimp/net/tcp_engine_transport.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 #include <utility> | 8 #include <utility> |
9 | 9 |
10 #include "base/callback.h" | 10 #include "base/callback.h" |
11 #include "base/callback_helpers.h" | 11 #include "base/callback_helpers.h" |
12 #include "base/location.h" | 12 #include "base/location.h" |
13 #include "base/memory/ptr_util.h" | 13 #include "base/memory/ptr_util.h" |
14 #include "base/single_thread_task_runner.h" | |
15 #include "base/threading/thread_task_runner_handle.h" | 14 #include "base/threading/thread_task_runner_handle.h" |
16 #include "blimp/net/stream_socket_connection.h" | 15 #include "blimp/net/message_port.h" |
| 16 #include "blimp/net/message_port_util.h" |
17 #include "net/socket/stream_socket.h" | 17 #include "net/socket/stream_socket.h" |
18 #include "net/socket/tcp_server_socket.h" | 18 #include "net/socket/tcp_server_socket.h" |
19 | 19 |
20 namespace blimp { | 20 namespace blimp { |
21 | 21 |
22 TCPEngineTransport::TCPEngineTransport(const net::IPEndPoint& address, | 22 TCPEngineTransport::TCPEngineTransport(const net::IPEndPoint& address, |
23 net::NetLog* net_log) | 23 net::NetLog* net_log) |
24 : address_(address), net_log_(net_log) {} | 24 : address_(address), net_log_(net_log) {} |
25 | 25 |
26 TCPEngineTransport::~TCPEngineTransport() {} | 26 TCPEngineTransport::~TCPEngineTransport() {} |
(...skipping 17 matching lines...) Expand all Loading... |
44 net::CompletionCallback accept_callback = base::Bind( | 44 net::CompletionCallback accept_callback = base::Bind( |
45 &TCPEngineTransport::OnTCPConnectAccepted, base::Unretained(this)); | 45 &TCPEngineTransport::OnTCPConnectAccepted, base::Unretained(this)); |
46 | 46 |
47 int result = server_socket_->Accept(&accepted_socket_, accept_callback); | 47 int result = server_socket_->Accept(&accepted_socket_, accept_callback); |
48 if (result == net::ERR_IO_PENDING) { | 48 if (result == net::ERR_IO_PENDING) { |
49 connect_callback_ = callback; | 49 connect_callback_ = callback; |
50 return; | 50 return; |
51 } | 51 } |
52 | 52 |
53 if (result != net::OK) { | 53 if (result != net::OK) { |
54 // TODO(haibinlu): investigate when we can keep using this server socket. | |
55 server_socket_.reset(); | 54 server_socket_.reset(); |
56 } | 55 } |
57 | 56 |
58 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, | 57 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
59 base::Bind(callback, result)); | 58 base::Bind(callback, result)); |
60 } | 59 } |
61 | 60 |
62 std::unique_ptr<BlimpConnection> TCPEngineTransport::TakeConnection() { | 61 std::unique_ptr<MessagePort> TCPEngineTransport::TakeMessagePort() { |
63 DCHECK(connect_callback_.is_null()); | 62 DCHECK(connect_callback_.is_null()); |
64 DCHECK(accepted_socket_); | 63 DCHECK(accepted_socket_); |
65 return base::WrapUnique( | 64 return CreateMessagePortForStreamSocket(std::move(accepted_socket_)); |
66 new StreamSocketConnection(std::move(accepted_socket_))); | |
67 } | 65 } |
68 | 66 |
69 const char* TCPEngineTransport::GetName() const { | 67 const char* TCPEngineTransport::GetName() const { |
70 return "TCP"; | 68 return "TCP"; |
71 } | 69 } |
72 | 70 |
73 int TCPEngineTransport::GetLocalAddress(net::IPEndPoint* address) const { | 71 int TCPEngineTransport::GetLocalAddress(net::IPEndPoint* address) const { |
74 DCHECK(server_socket_); | 72 DCHECK(server_socket_); |
75 return server_socket_->GetLocalAddress(address); | 73 return server_socket_->GetLocalAddress(address); |
76 } | 74 } |
77 | 75 |
78 void TCPEngineTransport::OnTCPConnectAccepted(int result) { | 76 void TCPEngineTransport::OnTCPConnectAccepted(int result) { |
79 DCHECK_NE(net::ERR_IO_PENDING, result); | 77 DCHECK_NE(net::ERR_IO_PENDING, result); |
80 DCHECK(accepted_socket_); | 78 DCHECK(accepted_socket_); |
81 if (result != net::OK) { | 79 if (result != net::OK) { |
82 accepted_socket_.reset(); | 80 accepted_socket_.reset(); |
83 } | 81 } |
84 base::ResetAndReturn(&connect_callback_).Run(result); | 82 base::ResetAndReturn(&connect_callback_).Run(result); |
85 } | 83 } |
86 | 84 |
87 } // namespace blimp | 85 } // namespace blimp |
OLD | NEW |