| 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 "ipc/ipc_sender.h" | 9 #include "ipc/ipc_sender.h" |
| 10 #include "net/base/io_buffer.h" | 10 #include "net/base/io_buffer.h" |
| 11 #include "net/base/net_errors.h" | 11 #include "net/base/net_errors.h" |
| 12 #include "net/base/net_util.h" | 12 #include "net/base/net_util.h" |
| 13 #include "net/socket/tcp_client_socket.h" | 13 #include "net/socket/tcp_client_socket.h" |
| 14 | 14 |
| 15 namespace { | 15 namespace { |
| 16 const int kReadBufferSize = 4096; | 16 const int kReadBufferSize = 4096; |
| 17 const int kPacketHeaderSize = sizeof(uint16); | 17 const int kPacketHeaderSize = sizeof(uint16); |
| 18 } // namespace | 18 } // namespace |
| 19 | 19 |
| 20 namespace content { | 20 namespace content { |
| 21 | 21 |
| 22 P2PSocketHostTcp::P2PSocketHostTcp(IPC::Sender* message_sender, int id) | 22 P2PSocketHostTcp::P2PSocketHostTcp(IPC::Sender* message_sender, int id) |
| 23 : P2PSocketHost(message_sender, id), | 23 : P2PSocketHost(message_sender, id), |
| 24 write_pending_(false), |
| 24 connected_(false) { | 25 connected_(false) { |
| 25 } | 26 } |
| 26 | 27 |
| 27 P2PSocketHostTcp::~P2PSocketHostTcp() { | 28 P2PSocketHostTcp::~P2PSocketHostTcp() { |
| 28 if (state_ == STATE_OPEN) { | 29 if (state_ == STATE_OPEN) { |
| 29 DCHECK(socket_.get()); | 30 DCHECK(socket_.get()); |
| 30 socket_.reset(); | 31 socket_.reset(); |
| 31 } | 32 } |
| 32 } | 33 } |
| 33 | 34 |
| (...skipping 185 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 219 if (write_buffer_) { | 220 if (write_buffer_) { |
| 220 write_queue_.push(buffer); | 221 write_queue_.push(buffer); |
| 221 return; | 222 return; |
| 222 } | 223 } |
| 223 | 224 |
| 224 write_buffer_ = buffer; | 225 write_buffer_ = buffer; |
| 225 DoWrite(); | 226 DoWrite(); |
| 226 } | 227 } |
| 227 | 228 |
| 228 void P2PSocketHostTcp::DoWrite() { | 229 void P2PSocketHostTcp::DoWrite() { |
| 229 while (write_buffer_ && state_ == STATE_OPEN) { | 230 while (write_buffer_ && state_ == STATE_OPEN && !write_pending_) { |
| 230 int result = socket_->Write(write_buffer_, write_buffer_->BytesRemaining(), | 231 int result = socket_->Write(write_buffer_, write_buffer_->BytesRemaining(), |
| 231 base::Bind(&P2PSocketHostTcp::OnWritten, | 232 base::Bind(&P2PSocketHostTcp::OnWritten, |
| 232 base::Unretained(this))); | 233 base::Unretained(this))); |
| 233 HandleWriteResult(result); | 234 HandleWriteResult(result); |
| 234 } | 235 } |
| 235 } | 236 } |
| 236 | 237 |
| 237 void P2PSocketHostTcp::OnWritten(int result) { | 238 void P2PSocketHostTcp::OnWritten(int result) { |
| 239 DCHECK(write_pending_); |
| 238 DCHECK_NE(result, net::ERR_IO_PENDING); | 240 DCHECK_NE(result, net::ERR_IO_PENDING); |
| 241 |
| 242 write_pending_ = false; |
| 239 HandleWriteResult(result); | 243 HandleWriteResult(result); |
| 240 DoWrite(); | 244 DoWrite(); |
| 241 } | 245 } |
| 242 | 246 |
| 243 void P2PSocketHostTcp::HandleWriteResult(int result) { | 247 void P2PSocketHostTcp::HandleWriteResult(int result) { |
| 244 DCHECK(write_buffer_); | 248 DCHECK(write_buffer_); |
| 245 if (result >= 0) { | 249 if (result >= 0) { |
| 246 write_buffer_->DidConsume(result); | 250 write_buffer_->DidConsume(result); |
| 247 if (write_buffer_->BytesRemaining() == 0) { | 251 if (write_buffer_->BytesRemaining() == 0) { |
| 248 message_sender_->Send(new P2PMsg_OnSendComplete(id_)); | 252 message_sender_->Send(new P2PMsg_OnSendComplete(id_)); |
| 249 if (write_queue_.empty()) { | 253 if (write_queue_.empty()) { |
| 250 write_buffer_ = NULL; | 254 write_buffer_ = NULL; |
| 251 } else { | 255 } else { |
| 252 write_buffer_ = write_queue_.front(); | 256 write_buffer_ = write_queue_.front(); |
| 253 write_queue_.pop(); | 257 write_queue_.pop(); |
| 254 } | 258 } |
| 255 } | 259 } |
| 256 } else if (result != net::ERR_IO_PENDING) { | 260 } else if (result == net::ERR_IO_PENDING) { |
| 261 write_pending_ = true; |
| 262 } else { |
| 257 LOG(ERROR) << "Error when sending data in TCP socket: " << result; | 263 LOG(ERROR) << "Error when sending data in TCP socket: " << result; |
| 258 OnError(); | 264 OnError(); |
| 259 } | 265 } |
| 260 } | 266 } |
| 261 | 267 |
| 262 P2PSocketHost* P2PSocketHostTcp::AcceptIncomingTcpConnection( | 268 P2PSocketHost* P2PSocketHostTcp::AcceptIncomingTcpConnection( |
| 263 const net::IPEndPoint& remote_address, int id) { | 269 const net::IPEndPoint& remote_address, int id) { |
| 264 NOTREACHED(); | 270 NOTREACHED(); |
| 265 OnError(); | 271 OnError(); |
| 266 return NULL; | 272 return NULL; |
| 267 } | 273 } |
| 268 | 274 |
| 269 } // namespace content | 275 } // namespace content |
| OLD | NEW |