OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "content/browser/renderer_host/p2p/socket_host_tcp_server.h" | 5 #include "content/browser/renderer_host/p2p/socket_host_tcp_server.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
9 #include "base/stl_util.h" | |
10 #include "content/browser/renderer_host/p2p/socket_host_tcp.h" | 9 #include "content/browser/renderer_host/p2p/socket_host_tcp.h" |
11 #include "content/common/p2p_messages.h" | 10 #include "content/common/p2p_messages.h" |
12 #include "net/base/address_list.h" | 11 #include "net/base/address_list.h" |
13 #include "net/base/net_errors.h" | 12 #include "net/base/net_errors.h" |
14 #include "net/socket/stream_socket.h" | 13 #include "net/socket/stream_socket.h" |
15 | 14 |
16 namespace { | 15 namespace { |
17 const int kListenBacklog = 5; | 16 const int kListenBacklog = 5; |
18 } // namespace | 17 } // namespace |
19 | 18 |
20 namespace content { | 19 namespace content { |
21 | 20 |
22 P2PSocketHostTcpServer::P2PSocketHostTcpServer(IPC::Sender* message_sender, | 21 P2PSocketHostTcpServer::P2PSocketHostTcpServer(IPC::Sender* message_sender, |
23 int socket_id, | 22 int socket_id, |
24 P2PSocketType client_type) | 23 P2PSocketType client_type) |
25 : P2PSocketHost(message_sender, socket_id, P2PSocketHost::TCP), | 24 : P2PSocketHost(message_sender, socket_id, P2PSocketHost::TCP), |
26 client_type_(client_type), | 25 client_type_(client_type), |
27 socket_(new net::TCPServerSocket(NULL, net::NetLog::Source())), | 26 socket_(new net::TCPServerSocket(nullptr, net::NetLog::Source())), |
28 accept_callback_(base::Bind(&P2PSocketHostTcpServer::OnAccepted, | 27 accept_callback_(base::Bind(&P2PSocketHostTcpServer::OnAccepted, |
29 base::Unretained(this))) { | 28 base::Unretained(this))) { |
30 } | 29 } |
31 | 30 |
32 P2PSocketHostTcpServer::~P2PSocketHostTcpServer() { | 31 P2PSocketHostTcpServer::~P2PSocketHostTcpServer() { |
33 base::STLDeleteContainerPairSecondPointers(accepted_sockets_.begin(), | |
34 accepted_sockets_.end()); | |
35 | |
36 if (state_ == STATE_OPEN) { | 32 if (state_ == STATE_OPEN) { |
37 DCHECK(socket_.get()); | 33 DCHECK(socket_.get()); |
38 socket_.reset(); | 34 socket_.reset(); |
39 } | 35 } |
40 } | 36 } |
41 | 37 |
42 // TODO(guidou): Add support for port range. | 38 // TODO(guidou): Add support for port range. |
43 bool P2PSocketHostTcpServer::Init(const net::IPEndPoint& local_address, | 39 bool P2PSocketHostTcpServer::Init(const net::IPEndPoint& local_address, |
44 uint16_t min_port, | 40 uint16_t min_port, |
45 uint16_t max_port, | 41 uint16_t max_port, |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
97 OnError(); | 93 OnError(); |
98 return; | 94 return; |
99 } | 95 } |
100 | 96 |
101 net::IPEndPoint address; | 97 net::IPEndPoint address; |
102 if (accept_socket_->GetPeerAddress(&address) != net::OK) { | 98 if (accept_socket_->GetPeerAddress(&address) != net::OK) { |
103 LOG(ERROR) << "Failed to get address of an accepted socket."; | 99 LOG(ERROR) << "Failed to get address of an accepted socket."; |
104 accept_socket_.reset(); | 100 accept_socket_.reset(); |
105 return; | 101 return; |
106 } | 102 } |
107 AcceptedSocketsMap::iterator it = accepted_sockets_.find(address); | 103 accepted_sockets_[address] = std::move(accept_socket_); |
108 if (it != accepted_sockets_.end()) | |
109 delete it->second; | |
110 | |
111 accepted_sockets_[address] = accept_socket_.release(); | |
112 message_sender_->Send( | 104 message_sender_->Send( |
113 new P2PMsg_OnIncomingTcpConnection(id_, address)); | 105 new P2PMsg_OnIncomingTcpConnection(id_, address)); |
114 } | 106 } |
115 | 107 |
116 void P2PSocketHostTcpServer::OnAccepted(int result) { | 108 void P2PSocketHostTcpServer::OnAccepted(int result) { |
117 HandleAcceptResult(result); | 109 HandleAcceptResult(result); |
118 if (result == net::OK) | 110 if (result == net::OK) |
119 DoAccept(); | 111 DoAccept(); |
120 } | 112 } |
121 | 113 |
122 void P2PSocketHostTcpServer::Send(const net::IPEndPoint& to, | 114 void P2PSocketHostTcpServer::Send(const net::IPEndPoint& to, |
123 const std::vector<char>& data, | 115 const std::vector<char>& data, |
124 const rtc::PacketOptions& options, | 116 const rtc::PacketOptions& options, |
125 uint64_t packet_id) { | 117 uint64_t packet_id) { |
126 NOTREACHED(); | 118 NOTREACHED(); |
127 OnError(); | 119 OnError(); |
128 } | 120 } |
129 | 121 |
130 P2PSocketHost* P2PSocketHostTcpServer::AcceptIncomingTcpConnection( | 122 std::unique_ptr<P2PSocketHost> |
131 const net::IPEndPoint& remote_address, int id) { | 123 P2PSocketHostTcpServer::AcceptIncomingTcpConnection( |
132 AcceptedSocketsMap::iterator it = accepted_sockets_.find(remote_address); | 124 const net::IPEndPoint& remote_address, |
| 125 int id) { |
| 126 auto it = accepted_sockets_.find(remote_address); |
133 if (it == accepted_sockets_.end()) | 127 if (it == accepted_sockets_.end()) |
134 return NULL; | 128 return nullptr; |
135 | 129 |
136 net::StreamSocket* socket = it->second; | 130 std::unique_ptr<net::StreamSocket> socket = std::move(it->second); |
137 accepted_sockets_.erase(it); | 131 accepted_sockets_.erase(it); |
138 | 132 |
139 std::unique_ptr<P2PSocketHostTcpBase> result; | 133 std::unique_ptr<P2PSocketHostTcpBase> result; |
140 if (client_type_ == P2P_SOCKET_TCP_CLIENT) { | 134 if (client_type_ == P2P_SOCKET_TCP_CLIENT) { |
141 result.reset(new P2PSocketHostTcp(message_sender_, id, client_type_, NULL)); | 135 result.reset( |
| 136 new P2PSocketHostTcp(message_sender_, id, client_type_, nullptr)); |
142 } else { | 137 } else { |
143 result.reset( | 138 result.reset( |
144 new P2PSocketHostStunTcp(message_sender_, id, client_type_, NULL)); | 139 new P2PSocketHostStunTcp(message_sender_, id, client_type_, nullptr)); |
145 } | 140 } |
146 if (!result->InitAccepted(remote_address, socket)) | 141 if (!result->InitAccepted(remote_address, std::move(socket))) |
147 return NULL; | 142 return nullptr; |
148 return result.release(); | 143 return std::move(result); |
149 } | 144 } |
150 | 145 |
151 bool P2PSocketHostTcpServer::SetOption(P2PSocketOption option, | 146 bool P2PSocketHostTcpServer::SetOption(P2PSocketOption option, |
152 int value) { | 147 int value) { |
153 // Currently we don't have use case tcp server sockets are used for p2p. | 148 // Currently we don't have use case tcp server sockets are used for p2p. |
154 return false; | 149 return false; |
155 } | 150 } |
156 | 151 |
157 } // namespace content | 152 } // namespace content |
OLD | NEW |