- add sources.
[platform/framework/web/crosswalk.git] / src / content / browser / renderer_host / p2p / socket_host_tcp_server.cc
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.
4
5 #include "content/browser/renderer_host/p2p/socket_host_tcp_server.h"
6
7 #include "base/bind.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"
16
17 namespace {
18 const int kListenBacklog = 5;
19 }  // namespace
20
21 namespace content {
22
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())),
28       accept_callback_(
29           base::Bind(&P2PSocketHostTcpServer::OnAccepted,
30                      base::Unretained(this))) {
31 }
32
33 P2PSocketHostTcpServer::~P2PSocketHostTcpServer() {
34   STLDeleteContainerPairSecondPointers(accepted_sockets_.begin(),
35                                        accepted_sockets_.end());
36
37   if (state_ == STATE_OPEN) {
38     DCHECK(socket_.get());
39     socket_.reset();
40   }
41 }
42
43 bool P2PSocketHostTcpServer::Init(const net::IPEndPoint& local_address,
44                                   const net::IPEndPoint& remote_address) {
45   DCHECK_EQ(state_, STATE_UNINITIALIZED);
46
47   int result = socket_->Listen(local_address, kListenBacklog);
48   if (result < 0) {
49     LOG(ERROR) << "Listen() failed: " << result;
50     OnError();
51     return false;
52   }
53
54   result = socket_->GetLocalAddress(&local_address_);
55   if (result < 0) {
56     LOG(ERROR) << "P2PSocketHostTcpServer::Init(): can't to get local address: "
57                << result;
58     OnError();
59     return false;
60   }
61   VLOG(1) << "Local address: " << local_address_.ToString();
62
63   state_ = STATE_OPEN;
64   message_sender_->Send(new P2PMsg_OnSocketCreated(id_, local_address_));
65   DoAccept();
66   return true;
67 }
68
69 void P2PSocketHostTcpServer::OnError() {
70   socket_.reset();
71
72   if (state_ == STATE_UNINITIALIZED || state_ == STATE_OPEN)
73     message_sender_->Send(new P2PMsg_OnError(id_));
74
75   state_ = STATE_ERROR;
76 }
77
78 void P2PSocketHostTcpServer::DoAccept() {
79   while (true) {
80     int result = socket_->Accept(&accept_socket_, accept_callback_);
81     if (result == net::ERR_IO_PENDING) {
82       break;
83     } else {
84       HandleAcceptResult(result);
85     }
86   }
87 }
88
89 void P2PSocketHostTcpServer::HandleAcceptResult(int result) {
90   if (result < 0) {
91     if (result != net::ERR_IO_PENDING)
92       OnError();
93     return;
94   }
95
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();
100     return;
101   }
102   AcceptedSocketsMap::iterator it = accepted_sockets_.find(address);
103   if (it != accepted_sockets_.end())
104     delete it->second;
105
106   accepted_sockets_[address] = accept_socket_.release();
107   message_sender_->Send(
108       new P2PMsg_OnIncomingTcpConnection(id_, address));
109 }
110
111 void P2PSocketHostTcpServer::OnAccepted(int result) {
112   HandleAcceptResult(result);
113   if (result == net::OK)
114     DoAccept();
115 }
116
117 void P2PSocketHostTcpServer::Send(const net::IPEndPoint& to,
118                                   const std::vector<char>& data,
119                                   net::DiffServCodePoint dscp,
120                                   uint64 packet_id) {
121   NOTREACHED();
122   OnError();
123 }
124
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())
129     return NULL;
130
131   net::StreamSocket* socket = it->second;
132   accepted_sockets_.erase(it);
133
134   scoped_ptr<P2PSocketHostTcpBase> result;
135   if (client_type_ == P2P_SOCKET_TCP_CLIENT) {
136     result.reset(new P2PSocketHostTcp(message_sender_, id, client_type_, NULL));
137   } else {
138     result.reset(new P2PSocketHostStunTcp(
139                     message_sender_, id, client_type_, NULL));
140   }
141   if (!result->InitAccepted(remote_address, socket))
142     return NULL;
143   return result.release();
144 }
145
146 }  // namespace content