| 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.h" | 5 #include "content/browser/renderer_host/p2p/socket_host_tcp.h" |
| 6 | 6 |
| 7 #include "base/sys_byteorder.h" | 7 #include "base/sys_byteorder.h" |
| 8 #include "content/common/p2p_messages.h" | 8 #include "content/common/p2p_messages.h" |
| 9 #include "net/base/io_buffer.h" | 9 #include "net/base/io_buffer.h" |
| 10 #include "net/base/net_errors.h" | 10 #include "net/base/net_errors.h" |
| 11 #include "net/base/net_util.h" | 11 #include "net/base/net_util.h" |
| 12 #include "net/socket/tcp_client_socket.h" | 12 #include "net/socket/tcp_client_socket.h" |
| 13 | 13 |
| 14 namespace { | 14 namespace { |
| 15 const int kReadBufferSize = 4096; | 15 const int kReadBufferSize = 4096; |
| 16 const int kPacketHeaderSize = sizeof(uint16); | 16 const int kPacketHeaderSize = sizeof(uint16); |
| 17 } // namespace | 17 } // namespace |
| 18 | 18 |
| 19 namespace content { | 19 namespace content { |
| 20 | 20 |
| 21 P2PSocketHostTcp::P2PSocketHostTcp(IPC::Sender* message_sender, | 21 P2PSocketHostTcp::P2PSocketHostTcp(IPC::Sender* message_sender, int id) |
| 22 int routing_id, int id) | 22 : P2PSocketHost(message_sender, id), |
| 23 : P2PSocketHost(message_sender, routing_id, id), | |
| 24 connected_(false) { | 23 connected_(false) { |
| 25 } | 24 } |
| 26 | 25 |
| 27 P2PSocketHostTcp::~P2PSocketHostTcp() { | 26 P2PSocketHostTcp::~P2PSocketHostTcp() { |
| 28 if (state_ == STATE_OPEN) { | 27 if (state_ == STATE_OPEN) { |
| 29 DCHECK(socket_.get()); | 28 DCHECK(socket_.get()); |
| 30 socket_.reset(); | 29 socket_.reset(); |
| 31 } | 30 } |
| 32 } | 31 } |
| 33 | 32 |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 65 } | 64 } |
| 66 | 65 |
| 67 return state_ != STATE_ERROR; | 66 return state_ != STATE_ERROR; |
| 68 } | 67 } |
| 69 | 68 |
| 70 void P2PSocketHostTcp::OnError() { | 69 void P2PSocketHostTcp::OnError() { |
| 71 socket_.reset(); | 70 socket_.reset(); |
| 72 | 71 |
| 73 if (state_ == STATE_UNINITIALIZED || state_ == STATE_CONNECTING || | 72 if (state_ == STATE_UNINITIALIZED || state_ == STATE_CONNECTING || |
| 74 state_ == STATE_OPEN) { | 73 state_ == STATE_OPEN) { |
| 75 message_sender_->Send(new P2PMsg_OnError(routing_id_, id_)); | 74 message_sender_->Send(new P2PMsg_OnError(id_)); |
| 76 } | 75 } |
| 77 | 76 |
| 78 state_ = STATE_ERROR; | 77 state_ = STATE_ERROR; |
| 79 } | 78 } |
| 80 | 79 |
| 81 void P2PSocketHostTcp::OnConnected(int result) { | 80 void P2PSocketHostTcp::OnConnected(int result) { |
| 82 DCHECK_EQ(state_, STATE_CONNECTING); | 81 DCHECK_EQ(state_, STATE_CONNECTING); |
| 83 DCHECK_NE(result, net::ERR_IO_PENDING); | 82 DCHECK_NE(result, net::ERR_IO_PENDING); |
| 84 | 83 |
| 85 if (result != net::OK) { | 84 if (result != net::OK) { |
| 86 OnError(); | 85 OnError(); |
| 87 return; | 86 return; |
| 88 } | 87 } |
| 89 | 88 |
| 90 if (!socket_->SetSendBufferSize(kMaxSendBufferSize)) { | 89 if (!socket_->SetSendBufferSize(kMaxSendBufferSize)) { |
| 91 LOG(WARNING) << "Failed to set send buffer size for TCP socket."; | 90 LOG(WARNING) << "Failed to set send buffer size for TCP socket."; |
| 92 } | 91 } |
| 93 | 92 |
| 94 net::IPEndPoint address; | 93 net::IPEndPoint address; |
| 95 result = socket_->GetLocalAddress(&address); | 94 result = socket_->GetLocalAddress(&address); |
| 96 if (result < 0) { | 95 if (result < 0) { |
| 97 LOG(ERROR) << "P2PSocket::Init(): unable to get local address: " | 96 LOG(ERROR) << "P2PSocket::Init(): unable to get local address: " |
| 98 << result; | 97 << result; |
| 99 OnError(); | 98 OnError(); |
| 100 return; | 99 return; |
| 101 } | 100 } |
| 102 | 101 |
| 103 VLOG(1) << "Local address: " << address.ToString(); | 102 VLOG(1) << "Local address: " << address.ToString(); |
| 104 state_ = STATE_OPEN; | 103 state_ = STATE_OPEN; |
| 105 message_sender_->Send(new P2PMsg_OnSocketCreated(routing_id_, id_, address)); | 104 message_sender_->Send(new P2PMsg_OnSocketCreated(id_, address)); |
| 106 DoRead(); | 105 DoRead(); |
| 107 } | 106 } |
| 108 | 107 |
| 109 void P2PSocketHostTcp::DoRead() { | 108 void P2PSocketHostTcp::DoRead() { |
| 110 int result; | 109 int result; |
| 111 do { | 110 do { |
| 112 if (!read_buffer_) { | 111 if (!read_buffer_) { |
| 113 read_buffer_ = new net::GrowableIOBuffer(); | 112 read_buffer_ = new net::GrowableIOBuffer(); |
| 114 read_buffer_->SetCapacity(kReadBufferSize); | 113 read_buffer_->SetCapacity(kReadBufferSize); |
| 115 } else if (read_buffer_->RemainingCapacity() < kReadBufferSize) { | 114 } else if (read_buffer_->RemainingCapacity() < kReadBufferSize) { |
| (...skipping 27 matching lines...) Expand all Loading... |
| 143 } else if (!stun || type == STUN_DATA_INDICATION) { | 142 } else if (!stun || type == STUN_DATA_INDICATION) { |
| 144 LOG(ERROR) << "Received unexpected data packet from " | 143 LOG(ERROR) << "Received unexpected data packet from " |
| 145 << remote_address_.ToString() | 144 << remote_address_.ToString() |
| 146 << " before STUN binding is finished. " | 145 << " before STUN binding is finished. " |
| 147 << "Terminating connection."; | 146 << "Terminating connection."; |
| 148 OnError(); | 147 OnError(); |
| 149 return; | 148 return; |
| 150 } | 149 } |
| 151 } | 150 } |
| 152 | 151 |
| 153 message_sender_->Send(new P2PMsg_OnDataReceived(routing_id_, id_, | 152 message_sender_->Send(new P2PMsg_OnDataReceived(id_, remote_address_, data)); |
| 154 remote_address_, data)); | |
| 155 } | 153 } |
| 156 | 154 |
| 157 void P2PSocketHostTcp::DidCompleteRead(int result) { | 155 void P2PSocketHostTcp::DidCompleteRead(int result) { |
| 158 DCHECK_EQ(state_, STATE_OPEN); | 156 DCHECK_EQ(state_, STATE_OPEN); |
| 159 | 157 |
| 160 if (result == net::ERR_IO_PENDING) { | 158 if (result == net::ERR_IO_PENDING) { |
| 161 return; | 159 return; |
| 162 } else if (result < 0){ | 160 } else if (result < 0){ |
| 163 LOG(ERROR) << "Error when reading from TCP socket: " << result; | 161 LOG(ERROR) << "Error when reading from TCP socket: " << result; |
| 164 OnError(); | 162 OnError(); |
| (...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 268 } | 266 } |
| 269 | 267 |
| 270 P2PSocketHost* P2PSocketHostTcp::AcceptIncomingTcpConnection( | 268 P2PSocketHost* P2PSocketHostTcp::AcceptIncomingTcpConnection( |
| 271 const net::IPEndPoint& remote_address, int id) { | 269 const net::IPEndPoint& remote_address, int id) { |
| 272 NOTREACHED(); | 270 NOTREACHED(); |
| 273 OnError(); | 271 OnError(); |
| 274 return NULL; | 272 return NULL; |
| 275 } | 273 } |
| 276 | 274 |
| 277 } // namespace content | 275 } // namespace content |
| OLD | NEW |