| 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 "jingle/glue/pseudotcp_adapter.h" | 5 #include "jingle/glue/pseudotcp_adapter.h" |
| 6 | 6 |
| 7 #include "base/compiler_specific.h" | 7 #include "base/compiler_specific.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/time.h" | 9 #include "base/time.h" |
| 10 #include "net/base/address_list.h" | 10 #include "net/base/address_list.h" |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 46 virtual void OnTcpClosed(cricket::PseudoTcp* tcp, uint32 error) OVERRIDE; | 46 virtual void OnTcpClosed(cricket::PseudoTcp* tcp, uint32 error) OVERRIDE; |
| 47 // This is triggered by NotifyClock, NotifyPacket, Recv and Send. | 47 // This is triggered by NotifyClock, NotifyPacket, Recv and Send. |
| 48 virtual WriteResult TcpWritePacket(cricket::PseudoTcp* tcp, | 48 virtual WriteResult TcpWritePacket(cricket::PseudoTcp* tcp, |
| 49 const char* buffer, size_t len) OVERRIDE; | 49 const char* buffer, size_t len) OVERRIDE; |
| 50 | 50 |
| 51 void SetAckDelay(int delay_ms); | 51 void SetAckDelay(int delay_ms); |
| 52 void SetNoDelay(bool no_delay); | 52 void SetNoDelay(bool no_delay); |
| 53 void SetReceiveBufferSize(int32 size); | 53 void SetReceiveBufferSize(int32 size); |
| 54 void SetSendBufferSize(int32 size); | 54 void SetSendBufferSize(int32 size); |
| 55 | 55 |
| 56 void DeleteSocket(); |
| 57 |
| 56 private: | 58 private: |
| 57 // These are invoked by the underlying Socket, and may trigger callbacks. | 59 // These are invoked by the underlying Socket, and may trigger callbacks. |
| 58 // They hold a reference to |this| while running, to protect from deletion. | 60 // They hold a reference to |this| while running, to protect from deletion. |
| 59 void OnRead(int result); | 61 void OnRead(int result); |
| 60 void OnWritten(int result); | 62 void OnWritten(int result); |
| 61 | 63 |
| 62 // These may trigger callbacks, so the holder must hold a reference on | 64 // These may trigger callbacks, so the holder must hold a reference on |
| 63 // the stack while calling them. | 65 // the stack while calling them. |
| 64 void DoReadFromSocket(); | 66 void DoReadFromSocket(); |
| 65 void HandleReadResults(int result); | 67 void HandleReadResults(int result); |
| (...skipping 209 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 275 } | 277 } |
| 276 | 278 |
| 277 void PseudoTcpAdapter::Core::SetReceiveBufferSize(int32 size) { | 279 void PseudoTcpAdapter::Core::SetReceiveBufferSize(int32 size) { |
| 278 pseudo_tcp_.SetOption(cricket::PseudoTcp::OPT_RCVBUF, size); | 280 pseudo_tcp_.SetOption(cricket::PseudoTcp::OPT_RCVBUF, size); |
| 279 } | 281 } |
| 280 | 282 |
| 281 void PseudoTcpAdapter::Core::SetSendBufferSize(int32 size) { | 283 void PseudoTcpAdapter::Core::SetSendBufferSize(int32 size) { |
| 282 pseudo_tcp_.SetOption(cricket::PseudoTcp::OPT_SNDBUF, size); | 284 pseudo_tcp_.SetOption(cricket::PseudoTcp::OPT_SNDBUF, size); |
| 283 } | 285 } |
| 284 | 286 |
| 287 void PseudoTcpAdapter::Core::DeleteSocket() { |
| 288 socket_.reset(); |
| 289 } |
| 290 |
| 285 cricket::IPseudoTcpNotify::WriteResult PseudoTcpAdapter::Core::TcpWritePacket( | 291 cricket::IPseudoTcpNotify::WriteResult PseudoTcpAdapter::Core::TcpWritePacket( |
| 286 PseudoTcp* tcp, | 292 PseudoTcp* tcp, |
| 287 const char* buffer, | 293 const char* buffer, |
| 288 size_t len) { | 294 size_t len) { |
| 289 DCHECK_EQ(tcp, &pseudo_tcp_); | 295 DCHECK_EQ(tcp, &pseudo_tcp_); |
| 290 | 296 |
| 291 // If we already have a write pending, we behave like a congested network, | 297 // If we already have a write pending, we behave like a congested network, |
| 292 // returning success for the write, but dropping the packet. PseudoTcp will | 298 // returning success for the write, but dropping the packet. PseudoTcp will |
| 293 // back-off and retransmit, adjusting for the perceived congestion. | 299 // back-off and retransmit, adjusting for the perceived congestion. |
| 294 if (socket_write_pending_) | 300 if (socket_write_pending_) |
| 295 return IPseudoTcpNotify::WR_SUCCESS; | 301 return IPseudoTcpNotify::WR_SUCCESS; |
| 296 | 302 |
| 297 scoped_refptr<net::IOBuffer> write_buffer = new net::IOBuffer(len); | 303 scoped_refptr<net::IOBuffer> write_buffer = new net::IOBuffer(len); |
| 298 memcpy(write_buffer->data(), buffer, len); | 304 memcpy(write_buffer->data(), buffer, len); |
| 299 | 305 |
| 300 // Our underlying socket is datagram-oriented, which means it should either | 306 // Our underlying socket is datagram-oriented, which means it should either |
| 301 // send exactly as many bytes as we requested, or fail. | 307 // send exactly as many bytes as we requested, or fail. |
| 302 int result = socket_->Write(write_buffer, len, | 308 int result; |
| 303 base::Bind(&PseudoTcpAdapter::Core::OnWritten, | 309 if (socket_.get()) { |
| 304 base::Unretained(this))); | 310 result = socket_->Write(write_buffer, len, |
| 311 base::Bind(&PseudoTcpAdapter::Core::OnWritten, |
| 312 base::Unretained(this))); |
| 313 } else { |
| 314 result = net::ERR_CONNECTION_CLOSED; |
| 315 } |
| 305 if (result == net::ERR_IO_PENDING) { | 316 if (result == net::ERR_IO_PENDING) { |
| 306 socket_write_pending_ = true; | 317 socket_write_pending_ = true; |
| 307 return IPseudoTcpNotify::WR_SUCCESS; | 318 return IPseudoTcpNotify::WR_SUCCESS; |
| 308 } if (result == net::ERR_MSG_TOO_BIG) { | 319 } if (result == net::ERR_MSG_TOO_BIG) { |
| 309 return IPseudoTcpNotify::WR_TOO_LARGE; | 320 return IPseudoTcpNotify::WR_TOO_LARGE; |
| 310 } else if (result < 0) { | 321 } else if (result < 0) { |
| 311 return IPseudoTcpNotify::WR_FAIL; | 322 return IPseudoTcpNotify::WR_FAIL; |
| 312 } else { | 323 } else { |
| 313 return IPseudoTcpNotify::WR_SUCCESS; | 324 return IPseudoTcpNotify::WR_SUCCESS; |
| 314 } | 325 } |
| 315 } | 326 } |
| 316 | 327 |
| 317 void PseudoTcpAdapter::Core::DoReadFromSocket() { | 328 void PseudoTcpAdapter::Core::DoReadFromSocket() { |
| 318 if (!socket_read_buffer_) | 329 if (!socket_read_buffer_) |
| 319 socket_read_buffer_ = new net::IOBuffer(kReadBufferSize); | 330 socket_read_buffer_ = new net::IOBuffer(kReadBufferSize); |
| 320 | 331 |
| 321 while (true) { | 332 int result = 1; |
| 322 int result = socket_->Read(socket_read_buffer_, kReadBufferSize, | 333 while (socket_.get() && result > 0) { |
| 323 base::Bind(&PseudoTcpAdapter::Core::OnRead, | 334 result = socket_->Read(socket_read_buffer_, kReadBufferSize, |
| 324 base::Unretained(this))); | 335 base::Bind(&PseudoTcpAdapter::Core::OnRead, |
| 325 if (result == net::ERR_IO_PENDING) | 336 base::Unretained(this))); |
| 326 break; | 337 if (result != net::ERR_IO_PENDING) |
| 327 | 338 HandleReadResults(result); |
| 328 HandleReadResults(result); | |
| 329 } | 339 } |
| 330 } | 340 } |
| 331 | 341 |
| 332 void PseudoTcpAdapter::Core::HandleReadResults(int result) { | 342 void PseudoTcpAdapter::Core::HandleReadResults(int result) { |
| 333 if (result <= 0) { | 343 if (result <= 0) { |
| 334 LOG(ERROR) << "Read returned " << result; | 344 LOG(ERROR) << "Read returned " << result; |
| 335 return; | 345 return; |
| 336 } | 346 } |
| 337 | 347 |
| 338 // TODO(wez): Disconnect on failure of NotifyPacket? | 348 // TODO(wez): Disconnect on failure of NotifyPacket? |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 378 } | 388 } |
| 379 | 389 |
| 380 // Public interface implemention. | 390 // Public interface implemention. |
| 381 | 391 |
| 382 PseudoTcpAdapter::PseudoTcpAdapter(net::Socket* socket) | 392 PseudoTcpAdapter::PseudoTcpAdapter(net::Socket* socket) |
| 383 : core_(new Core(socket)) { | 393 : core_(new Core(socket)) { |
| 384 } | 394 } |
| 385 | 395 |
| 386 PseudoTcpAdapter::~PseudoTcpAdapter() { | 396 PseudoTcpAdapter::~PseudoTcpAdapter() { |
| 387 Disconnect(); | 397 Disconnect(); |
| 398 |
| 399 // Make sure that the underlying socket is destroyed before PseudoTcp. |
| 400 core_->DeleteSocket(); |
| 388 } | 401 } |
| 389 | 402 |
| 390 int PseudoTcpAdapter::Read(net::IOBuffer* buffer, int buffer_size, | 403 int PseudoTcpAdapter::Read(net::IOBuffer* buffer, int buffer_size, |
| 391 const net::CompletionCallback& callback) { | 404 const net::CompletionCallback& callback) { |
| 392 DCHECK(CalledOnValidThread()); | 405 DCHECK(CalledOnValidThread()); |
| 393 return core_->Read(buffer, buffer_size, callback); | 406 return core_->Read(buffer, buffer_size, callback); |
| 394 } | 407 } |
| 395 | 408 |
| 396 int PseudoTcpAdapter::Write(net::IOBuffer* buffer, int buffer_size, | 409 int PseudoTcpAdapter::Write(net::IOBuffer* buffer, int buffer_size, |
| 397 const net::CompletionCallback& callback) { | 410 const net::CompletionCallback& callback) { |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 494 DCHECK(CalledOnValidThread()); | 507 DCHECK(CalledOnValidThread()); |
| 495 core_->SetAckDelay(delay_ms); | 508 core_->SetAckDelay(delay_ms); |
| 496 } | 509 } |
| 497 | 510 |
| 498 void PseudoTcpAdapter::SetNoDelay(bool no_delay) { | 511 void PseudoTcpAdapter::SetNoDelay(bool no_delay) { |
| 499 DCHECK(CalledOnValidThread()); | 512 DCHECK(CalledOnValidThread()); |
| 500 core_->SetNoDelay(no_delay); | 513 core_->SetNoDelay(no_delay); |
| 501 } | 514 } |
| 502 | 515 |
| 503 } // namespace jingle_glue | 516 } // namespace jingle_glue |
| OLD | NEW |