| 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" | 9 #include "base/stl_util.h" |
| 10 #include "content/browser/renderer_host/p2p/socket_host_tcp.h" | 10 #include "content/browser/renderer_host/p2p/socket_host_tcp.h" |
| 11 #include "content/common/p2p_messages.h" | 11 #include "content/common/p2p_messages.h" |
| 12 #include "net/base/address_list.h" | 12 #include "net/base/address_list.h" |
| 13 #include "net/base/net_errors.h" | 13 #include "net/base/net_errors.h" |
| 14 #include "net/base/net_util.h" | 14 #include "net/base/net_util.h" |
| 15 #include "net/socket/stream_socket.h" | 15 #include "net/socket/stream_socket.h" |
| 16 | 16 |
| 17 namespace { | 17 namespace { |
| 18 const int kListenBacklog = 5; | 18 const int kListenBacklog = 5; |
| 19 } // namespace | 19 } // namespace |
| 20 | 20 |
| 21 namespace content { | 21 namespace content { |
| 22 | 22 |
| 23 P2PSocketHostTcpServer::P2PSocketHostTcpServer( | 23 P2PSocketHostTcpServer::P2PSocketHostTcpServer( |
| 24 IPC::Sender* message_sender, | 24 IPC::Sender* message_sender, int id) |
| 25 int routing_id, int id) | 25 : P2PSocketHost(message_sender, id), |
| 26 : P2PSocketHost(message_sender, routing_id, id), | |
| 27 socket_(new net::TCPServerSocket(NULL, net::NetLog::Source())), | 26 socket_(new net::TCPServerSocket(NULL, net::NetLog::Source())), |
| 28 ALLOW_THIS_IN_INITIALIZER_LIST(accept_callback_( | 27 ALLOW_THIS_IN_INITIALIZER_LIST(accept_callback_( |
| 29 base::Bind(&P2PSocketHostTcpServer::OnAccepted, | 28 base::Bind(&P2PSocketHostTcpServer::OnAccepted, |
| 30 base::Unretained(this)))) { | 29 base::Unretained(this)))) { |
| 31 } | 30 } |
| 32 | 31 |
| 33 P2PSocketHostTcpServer::~P2PSocketHostTcpServer() { | 32 P2PSocketHostTcpServer::~P2PSocketHostTcpServer() { |
| 34 STLDeleteContainerPairSecondPointers(accepted_sockets_.begin(), | 33 STLDeleteContainerPairSecondPointers(accepted_sockets_.begin(), |
| 35 accepted_sockets_.end()); | 34 accepted_sockets_.end()); |
| 36 | 35 |
| (...skipping 17 matching lines...) Expand all Loading... |
| 54 result = socket_->GetLocalAddress(&local_address_); | 53 result = socket_->GetLocalAddress(&local_address_); |
| 55 if (result < 0) { | 54 if (result < 0) { |
| 56 LOG(ERROR) << "P2PSocketHostTcpServer::Init(): can't to get local address: " | 55 LOG(ERROR) << "P2PSocketHostTcpServer::Init(): can't to get local address: " |
| 57 << result; | 56 << result; |
| 58 OnError(); | 57 OnError(); |
| 59 return false; | 58 return false; |
| 60 } | 59 } |
| 61 VLOG(1) << "Local address: " << local_address_.ToString(); | 60 VLOG(1) << "Local address: " << local_address_.ToString(); |
| 62 | 61 |
| 63 state_ = STATE_OPEN; | 62 state_ = STATE_OPEN; |
| 64 message_sender_->Send(new P2PMsg_OnSocketCreated(routing_id_, id_, | 63 message_sender_->Send(new P2PMsg_OnSocketCreated(id_, local_address_)); |
| 65 local_address_)); | |
| 66 DoAccept(); | 64 DoAccept(); |
| 67 return true; | 65 return true; |
| 68 } | 66 } |
| 69 | 67 |
| 70 void P2PSocketHostTcpServer::OnError() { | 68 void P2PSocketHostTcpServer::OnError() { |
| 71 socket_.reset(); | 69 socket_.reset(); |
| 72 | 70 |
| 73 if (state_ == STATE_UNINITIALIZED || state_ == STATE_OPEN) | 71 if (state_ == STATE_UNINITIALIZED || state_ == STATE_OPEN) |
| 74 message_sender_->Send(new P2PMsg_OnError(routing_id_, id_)); | 72 message_sender_->Send(new P2PMsg_OnError(id_)); |
| 75 | 73 |
| 76 state_ = STATE_ERROR; | 74 state_ = STATE_ERROR; |
| 77 } | 75 } |
| 78 | 76 |
| 79 void P2PSocketHostTcpServer::DoAccept() { | 77 void P2PSocketHostTcpServer::DoAccept() { |
| 80 while (true) { | 78 while (true) { |
| 81 int result = socket_->Accept(&accept_socket_, accept_callback_); | 79 int result = socket_->Accept(&accept_socket_, accept_callback_); |
| 82 if (result == net::ERR_IO_PENDING) { | 80 if (result == net::ERR_IO_PENDING) { |
| 83 break; | 81 break; |
| 84 } else { | 82 } else { |
| (...skipping 14 matching lines...) Expand all Loading... |
| 99 LOG(ERROR) << "Failed to get address of an accepted socket."; | 97 LOG(ERROR) << "Failed to get address of an accepted socket."; |
| 100 accept_socket_.reset(); | 98 accept_socket_.reset(); |
| 101 return; | 99 return; |
| 102 } | 100 } |
| 103 AcceptedSocketsMap::iterator it = accepted_sockets_.find(address); | 101 AcceptedSocketsMap::iterator it = accepted_sockets_.find(address); |
| 104 if (it != accepted_sockets_.end()) | 102 if (it != accepted_sockets_.end()) |
| 105 delete it->second; | 103 delete it->second; |
| 106 | 104 |
| 107 accepted_sockets_[address] = accept_socket_.release(); | 105 accepted_sockets_[address] = accept_socket_.release(); |
| 108 message_sender_->Send( | 106 message_sender_->Send( |
| 109 new P2PMsg_OnIncomingTcpConnection(routing_id_, id_, address)); | 107 new P2PMsg_OnIncomingTcpConnection(id_, address)); |
| 110 } | 108 } |
| 111 | 109 |
| 112 void P2PSocketHostTcpServer::OnAccepted(int result) { | 110 void P2PSocketHostTcpServer::OnAccepted(int result) { |
| 113 HandleAcceptResult(result); | 111 HandleAcceptResult(result); |
| 114 if (result == net::OK) | 112 if (result == net::OK) |
| 115 DoAccept(); | 113 DoAccept(); |
| 116 } | 114 } |
| 117 | 115 |
| 118 void P2PSocketHostTcpServer::Send(const net::IPEndPoint& to, | 116 void P2PSocketHostTcpServer::Send(const net::IPEndPoint& to, |
| 119 const std::vector<char>& data) { | 117 const std::vector<char>& data) { |
| 120 NOTREACHED(); | 118 NOTREACHED(); |
| 121 OnError(); | 119 OnError(); |
| 122 } | 120 } |
| 123 | 121 |
| 124 P2PSocketHost* P2PSocketHostTcpServer::AcceptIncomingTcpConnection( | 122 P2PSocketHost* P2PSocketHostTcpServer::AcceptIncomingTcpConnection( |
| 125 const net::IPEndPoint& remote_address, int id) { | 123 const net::IPEndPoint& remote_address, int id) { |
| 126 AcceptedSocketsMap::iterator it = accepted_sockets_.find(remote_address); | 124 AcceptedSocketsMap::iterator it = accepted_sockets_.find(remote_address); |
| 127 if (it == accepted_sockets_.end()) | 125 if (it == accepted_sockets_.end()) |
| 128 return NULL; | 126 return NULL; |
| 129 | 127 |
| 130 net::StreamSocket* socket = it->second; | 128 net::StreamSocket* socket = it->second; |
| 131 accepted_sockets_.erase(it); | 129 accepted_sockets_.erase(it); |
| 132 scoped_ptr<P2PSocketHostTcp> result( | 130 scoped_ptr<P2PSocketHostTcp> result( |
| 133 new P2PSocketHostTcp(message_sender_, routing_id_, id)); | 131 new P2PSocketHostTcp(message_sender_, id)); |
| 134 if (!result->InitAccepted(remote_address, socket)) | 132 if (!result->InitAccepted(remote_address, socket)) |
| 135 return NULL; | 133 return NULL; |
| 136 | 134 |
| 137 return result.release(); | 135 return result.release(); |
| 138 } | 136 } |
| 139 | 137 |
| 140 } // namespace content | 138 } // namespace content |
| OLD | NEW |