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_engine_transport.h" | |
6 | |
7 #include <memory> | |
8 #include <utility> | |
9 | |
10 #include "base/callback.h" | |
11 #include "base/callback_helpers.h" | |
12 #include "base/location.h" | |
13 #include "base/memory/ptr_util.h" | |
14 #include "base/threading/thread_task_runner_handle.h" | |
15 #include "blimp/net/message_port.h" | |
16 #include "blimp/net/tcp_connection.h" | |
17 #include "net/log/net_log_source.h" | |
18 #include "net/socket/stream_socket.h" | |
19 #include "net/socket/tcp_server_socket.h" | |
20 | |
21 namespace blimp { | |
22 | |
23 TCPEngineTransport::TCPEngineTransport(const net::IPEndPoint& address, | |
24 net::NetLog* net_log) | |
25 : address_(address), net_log_(net_log), weak_factory_(this) {} | |
26 | |
27 TCPEngineTransport::~TCPEngineTransport() {} | |
28 | |
29 void TCPEngineTransport::Connect(const net::CompletionCallback& callback) { | |
30 DCHECK(!accepted_socket_); | |
31 DCHECK(!callback.is_null()); | |
32 | |
33 if (!server_socket_) { | |
34 server_socket_.reset( | |
35 new net::TCPServerSocket(net_log_, net::NetLogSource())); | |
36 int result = server_socket_->Listen(address_, 5); | |
37 if (result != net::OK) { | |
38 server_socket_.reset(); | |
39 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
40 FROM_HERE, base::Bind(callback, result)); | |
41 return; | |
42 } | |
43 } | |
44 | |
45 net::CompletionCallback accept_callback = base::Bind( | |
46 &TCPEngineTransport::OnTCPConnectAccepted, weak_factory_.GetWeakPtr()); | |
47 | |
48 connect_callback_ = callback; | |
49 int result = server_socket_->Accept(&accepted_socket_, accept_callback); | |
50 if (result == net::ERR_IO_PENDING) { | |
51 return; | |
52 } | |
53 | |
54 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
55 FROM_HERE, base::Bind(&TCPEngineTransport::OnTCPConnectAccepted, | |
56 weak_factory_.GetWeakPtr(), result)); | |
57 } | |
58 | |
59 std::unique_ptr<MessagePort> TCPEngineTransport::TakeMessagePort() { | |
60 DCHECK(connect_callback_.is_null()); | |
61 DCHECK(accepted_socket_); | |
62 return MessagePort::CreateForStreamSocketWithCompression( | |
63 std::move(accepted_socket_)); | |
64 } | |
65 | |
66 std::unique_ptr<BlimpConnection> TCPEngineTransport::MakeConnection() { | |
67 return base::MakeUnique<TCPConnection>(TakeMessagePort()); | |
68 } | |
69 | |
70 const char* TCPEngineTransport::GetName() const { | |
71 return "TCP"; | |
72 } | |
73 | |
74 void TCPEngineTransport::GetLocalAddress(net::IPEndPoint* address) const { | |
75 DCHECK(server_socket_); | |
76 server_socket_->GetLocalAddress(address); | |
77 } | |
78 | |
79 void TCPEngineTransport::OnTCPConnectAccepted(int result) { | |
80 DCHECK_NE(net::ERR_IO_PENDING, result); | |
81 DCHECK(accepted_socket_); | |
82 if (result != net::OK) { | |
83 accepted_socket_.reset(); | |
84 } | |
85 base::ResetAndReturn(&connect_callback_).Run(result); | |
86 } | |
87 | |
88 } // namespace blimp | |
OLD | NEW |