| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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" |
| 8 #include "base/bind_helpers.h" |
| 7 #include "base/stl_util.h" | 9 #include "base/stl_util.h" |
| 8 #include "content/browser/renderer_host/p2p/socket_host_tcp.h" | 10 #include "content/browser/renderer_host/p2p/socket_host_tcp.h" |
| 9 #include "content/common/p2p_messages.h" | 11 #include "content/common/p2p_messages.h" |
| 10 #include "net/base/address_list.h" | 12 #include "net/base/address_list.h" |
| 11 #include "net/base/net_errors.h" | 13 #include "net/base/net_errors.h" |
| 12 #include "net/base/net_util.h" | 14 #include "net/base/net_util.h" |
| 13 #include "net/base/sys_addrinfo.h" | 15 #include "net/base/sys_addrinfo.h" |
| 14 #include "net/socket/stream_socket.h" | 16 #include "net/socket/stream_socket.h" |
| 15 | 17 |
| 16 namespace { | 18 namespace { |
| 17 const int kListenBacklog = 5; | 19 const int kListenBacklog = 5; |
| 18 } // namespace | 20 } // namespace |
| 19 | 21 |
| 20 namespace content { | 22 namespace content { |
| 21 | 23 |
| 22 P2PSocketHostTcpServer::P2PSocketHostTcpServer( | 24 P2PSocketHostTcpServer::P2PSocketHostTcpServer( |
| 23 IPC::Message::Sender* message_sender, | 25 IPC::Message::Sender* message_sender, |
| 24 int routing_id, int id) | 26 int routing_id, int id) |
| 25 : P2PSocketHost(message_sender, routing_id, id), | 27 : P2PSocketHost(message_sender, routing_id, id), |
| 26 socket_(new net::TCPServerSocket(NULL, net::NetLog::Source())), | 28 socket_(new net::TCPServerSocket(NULL, net::NetLog::Source())), |
| 27 ALLOW_THIS_IN_INITIALIZER_LIST( | 29 ALLOW_THIS_IN_INITIALIZER_LIST(accept_callback_( |
| 28 accept_callback_(this, &P2PSocketHostTcpServer::OnAccepted)) { | 30 base::Bind(&P2PSocketHostTcpServer::OnAccepted, |
| 31 base::Unretained(this)))) { |
| 29 } | 32 } |
| 30 | 33 |
| 31 P2PSocketHostTcpServer::~P2PSocketHostTcpServer() { | 34 P2PSocketHostTcpServer::~P2PSocketHostTcpServer() { |
| 32 STLDeleteContainerPairSecondPointers(accepted_sockets_.begin(), | 35 STLDeleteContainerPairSecondPointers(accepted_sockets_.begin(), |
| 33 accepted_sockets_.end()); | 36 accepted_sockets_.end()); |
| 34 | 37 |
| 35 if (state_ == STATE_OPEN) { | 38 if (state_ == STATE_OPEN) { |
| 36 DCHECK(socket_.get()); | 39 DCHECK(socket_.get()); |
| 37 socket_.reset(); | 40 socket_.reset(); |
| 38 } | 41 } |
| (...skipping 30 matching lines...) Expand all Loading... |
| 69 socket_.reset(); | 72 socket_.reset(); |
| 70 | 73 |
| 71 if (state_ == STATE_UNINITIALIZED || state_ == STATE_OPEN) | 74 if (state_ == STATE_UNINITIALIZED || state_ == STATE_OPEN) |
| 72 message_sender_->Send(new P2PMsg_OnError(routing_id_, id_)); | 75 message_sender_->Send(new P2PMsg_OnError(routing_id_, id_)); |
| 73 | 76 |
| 74 state_ = STATE_ERROR; | 77 state_ = STATE_ERROR; |
| 75 } | 78 } |
| 76 | 79 |
| 77 void P2PSocketHostTcpServer::DoAccept() { | 80 void P2PSocketHostTcpServer::DoAccept() { |
| 78 while (true) { | 81 while (true) { |
| 79 int result = socket_->Accept(&accept_socket_, &accept_callback_); | 82 int result = socket_->Accept(&accept_socket_, accept_callback_); |
| 80 if (result == net::ERR_IO_PENDING) { | 83 if (result == net::ERR_IO_PENDING) { |
| 81 break; | 84 break; |
| 82 } else { | 85 } else { |
| 83 HandleAcceptResult(result); | 86 HandleAcceptResult(result); |
| 84 } | 87 } |
| 85 } | 88 } |
| 86 } | 89 } |
| 87 | 90 |
| 88 void P2PSocketHostTcpServer::HandleAcceptResult(int result) { | 91 void P2PSocketHostTcpServer::HandleAcceptResult(int result) { |
| 89 if (result < 0) { | 92 if (result < 0) { |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 132 accepted_sockets_.erase(it); | 135 accepted_sockets_.erase(it); |
| 133 scoped_ptr<P2PSocketHostTcp> result( | 136 scoped_ptr<P2PSocketHostTcp> result( |
| 134 new P2PSocketHostTcp(message_sender_, routing_id_, id)); | 137 new P2PSocketHostTcp(message_sender_, routing_id_, id)); |
| 135 if (!result->InitAccepted(remote_address, socket)) | 138 if (!result->InitAccepted(remote_address, socket)) |
| 136 return NULL; | 139 return NULL; |
| 137 | 140 |
| 138 return result.release(); | 141 return result.release(); |
| 139 } | 142 } |
| 140 | 143 |
| 141 } // namespace content | 144 } // namespace content |
| OLD | NEW |