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(
24 IPC::Sender* message_sender, int id, P2PSocketType client_type)
25 : P2PSocketHost(message_sender, id),
26 client_type_(client_type),
27 socket_(new net::TCPServerSocket(NULL, net::NetLog::Source())),
29 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 net::IPEndPoint& 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 message_sender_->Send(new P2PMsg_OnSocketCreated(id_, local_address_));
69 void P2PSocketHostTcpServer::OnError() {
72 if (state_ == STATE_UNINITIALIZED || state_ == STATE_OPEN)
73 message_sender_->Send(new P2PMsg_OnError(id_));
78 void P2PSocketHostTcpServer::DoAccept() {
80 int result = socket_->Accept(&accept_socket_, accept_callback_);
81 if (result == net::ERR_IO_PENDING) {
84 HandleAcceptResult(result);
89 void P2PSocketHostTcpServer::HandleAcceptResult(int result) {
91 if (result != net::ERR_IO_PENDING)
96 net::IPEndPoint address;
97 if (accept_socket_->GetPeerAddress(&address) != net::OK) {
98 LOG(ERROR) << "Failed to get address of an accepted socket.";
99 accept_socket_.reset();
102 AcceptedSocketsMap::iterator it = accepted_sockets_.find(address);
103 if (it != accepted_sockets_.end())
106 accepted_sockets_[address] = accept_socket_.release();
107 message_sender_->Send(
108 new P2PMsg_OnIncomingTcpConnection(id_, address));
111 void P2PSocketHostTcpServer::OnAccepted(int result) {
112 HandleAcceptResult(result);
113 if (result == net::OK)
117 void P2PSocketHostTcpServer::Send(const net::IPEndPoint& to,
118 const std::vector<char>& data,
119 net::DiffServCodePoint dscp,
125 P2PSocketHost* P2PSocketHostTcpServer::AcceptIncomingTcpConnection(
126 const net::IPEndPoint& remote_address, int id) {
127 AcceptedSocketsMap::iterator it = accepted_sockets_.find(remote_address);
128 if (it == accepted_sockets_.end())
131 net::StreamSocket* socket = it->second;
132 accepted_sockets_.erase(it);
134 scoped_ptr<P2PSocketHostTcpBase> result;
135 if (client_type_ == P2P_SOCKET_TCP_CLIENT) {
136 result.reset(new P2PSocketHostTcp(message_sender_, id, client_type_, NULL));
138 result.reset(new P2PSocketHostStunTcp(
139 message_sender_, id, client_type_, NULL));
141 if (!result->InitAccepted(remote_address, socket))
143 return result.release();
146 } // namespace content