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
3 // found in the LICENSE file.
5 #include "content/browser/renderer_host/p2p/socket_host_tcp_server.h"
8 #include "base/bind_helpers.h"
9 #include "base/stl_util.h"
10 #include "content/browser/renderer_host/p2p/socket_host_tcp.h"
11 #include "content/common/p2p_messages.h"
12 #include "net/base/address_list.h"
13 #include "net/base/net_errors.h"
14 #include "net/base/net_util.h"
15 #include "net/socket/stream_socket.h"
18 const int kListenBacklog
= 5;
23 P2PSocketHostTcpServer::P2PSocketHostTcpServer(IPC::Sender
* message_sender
,
25 P2PSocketType client_type
)
26 : P2PSocketHost(message_sender
, socket_id
, P2PSocketHost::TCP
),
27 client_type_(client_type
),
28 socket_(new net::TCPServerSocket(NULL
, net::NetLog::Source())),
29 accept_callback_(base::Bind(&P2PSocketHostTcpServer::OnAccepted
,
30 base::Unretained(this))) {
33 P2PSocketHostTcpServer::~P2PSocketHostTcpServer() {
34 STLDeleteContainerPairSecondPointers(accepted_sockets_
.begin(),
35 accepted_sockets_
.end());
37 if (state_
== STATE_OPEN
) {
38 DCHECK(socket_
.get());
43 bool P2PSocketHostTcpServer::Init(const net::IPEndPoint
& local_address
,
44 const P2PHostAndIPEndPoint
& remote_address
) {
45 DCHECK_EQ(state_
, STATE_UNINITIALIZED
);
47 int result
= socket_
->Listen(local_address
, kListenBacklog
);
49 LOG(ERROR
) << "Listen() failed: " << result
;
54 result
= socket_
->GetLocalAddress(&local_address_
);
56 LOG(ERROR
) << "P2PSocketHostTcpServer::Init(): can't to get local address: "
61 VLOG(1) << "Local address: " << local_address_
.ToString();
64 // NOTE: Remote address can be empty as socket is just listening
66 message_sender_
->Send(new P2PMsg_OnSocketCreated(
67 id_
, local_address_
, remote_address
.ip_address
));
72 void P2PSocketHostTcpServer::OnError() {
75 if (state_
== STATE_UNINITIALIZED
|| state_
== STATE_OPEN
)
76 message_sender_
->Send(new P2PMsg_OnError(id_
));
81 void P2PSocketHostTcpServer::DoAccept() {
83 int result
= socket_
->Accept(&accept_socket_
, accept_callback_
);
84 if (result
== net::ERR_IO_PENDING
) {
87 HandleAcceptResult(result
);
92 void P2PSocketHostTcpServer::HandleAcceptResult(int result
) {
94 if (result
!= net::ERR_IO_PENDING
)
99 net::IPEndPoint address
;
100 if (accept_socket_
->GetPeerAddress(&address
) != net::OK
) {
101 LOG(ERROR
) << "Failed to get address of an accepted socket.";
102 accept_socket_
.reset();
105 AcceptedSocketsMap::iterator it
= accepted_sockets_
.find(address
);
106 if (it
!= accepted_sockets_
.end())
109 accepted_sockets_
[address
] = accept_socket_
.release();
110 message_sender_
->Send(
111 new P2PMsg_OnIncomingTcpConnection(id_
, address
));
114 void P2PSocketHostTcpServer::OnAccepted(int result
) {
115 HandleAcceptResult(result
);
116 if (result
== net::OK
)
120 void P2PSocketHostTcpServer::Send(const net::IPEndPoint
& to
,
121 const std::vector
<char>& data
,
122 const rtc::PacketOptions
& options
,
128 P2PSocketHost
* P2PSocketHostTcpServer::AcceptIncomingTcpConnection(
129 const net::IPEndPoint
& remote_address
, int id
) {
130 AcceptedSocketsMap::iterator it
= accepted_sockets_
.find(remote_address
);
131 if (it
== accepted_sockets_
.end())
134 net::StreamSocket
* socket
= it
->second
;
135 accepted_sockets_
.erase(it
);
137 scoped_ptr
<P2PSocketHostTcpBase
> result
;
138 if (client_type_
== P2P_SOCKET_TCP_CLIENT
) {
139 result
.reset(new P2PSocketHostTcp(message_sender_
, id
, client_type_
, NULL
));
142 new P2PSocketHostStunTcp(message_sender_
, id
, client_type_
, NULL
));
144 if (!result
->InitAccepted(remote_address
, socket
))
146 return result
.release();
149 bool P2PSocketHostTcpServer::SetOption(P2PSocketOption option
,
151 // Currently we don't have use case tcp server sockets are used for p2p.
155 } // namespace content