| 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 "jingle/glue/pseudotcp_adapter.h" | 5 #include "jingle/glue/pseudotcp_adapter.h" |
| 6 | 6 |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 44 // |rate| is in drops per second. | 44 // |rate| is in drops per second. |
| 45 LeakyBucket(double volume, double rate) | 45 LeakyBucket(double volume, double rate) |
| 46 : volume_(volume), | 46 : volume_(volume), |
| 47 rate_(rate), | 47 rate_(rate), |
| 48 level_(0.0), | 48 level_(0.0), |
| 49 last_update_(base::TimeTicks::HighResNow()) { | 49 last_update_(base::TimeTicks::HighResNow()) { |
| 50 } | 50 } |
| 51 | 51 |
| 52 virtual ~LeakyBucket() { } | 52 virtual ~LeakyBucket() { } |
| 53 | 53 |
| 54 virtual bool DropNextPacket() OVERRIDE { | 54 virtual bool DropNextPacket() override { |
| 55 base::TimeTicks now = base::TimeTicks::HighResNow(); | 55 base::TimeTicks now = base::TimeTicks::HighResNow(); |
| 56 double interval = (now - last_update_).InSecondsF(); | 56 double interval = (now - last_update_).InSecondsF(); |
| 57 last_update_ = now; | 57 last_update_ = now; |
| 58 level_ = level_ + 1.0 - interval * rate_; | 58 level_ = level_ + 1.0 - interval * rate_; |
| 59 if (level_ > volume_) { | 59 if (level_ > volume_) { |
| 60 level_ = volume_; | 60 level_ = volume_; |
| 61 return true; | 61 return true; |
| 62 } else if (level_ < 0.0) { | 62 } else if (level_ < 0.0) { |
| 63 level_ = 0.0; | 63 level_ = 0.0; |
| 64 } | 64 } |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 101 } | 101 } |
| 102 | 102 |
| 103 void set_rate_limiter(RateLimiter* rate_limiter) { | 103 void set_rate_limiter(RateLimiter* rate_limiter) { |
| 104 rate_limiter_ = rate_limiter; | 104 rate_limiter_ = rate_limiter; |
| 105 }; | 105 }; |
| 106 | 106 |
| 107 void set_latency(int latency_ms) { latency_ms_ = latency_ms; }; | 107 void set_latency(int latency_ms) { latency_ms_ = latency_ms; }; |
| 108 | 108 |
| 109 // net::Socket interface. | 109 // net::Socket interface. |
| 110 virtual int Read(net::IOBuffer* buf, int buf_len, | 110 virtual int Read(net::IOBuffer* buf, int buf_len, |
| 111 const net::CompletionCallback& callback) OVERRIDE { | 111 const net::CompletionCallback& callback) override { |
| 112 CHECK(read_callback_.is_null()); | 112 CHECK(read_callback_.is_null()); |
| 113 CHECK(buf); | 113 CHECK(buf); |
| 114 | 114 |
| 115 if (incoming_packets_.size() > 0) { | 115 if (incoming_packets_.size() > 0) { |
| 116 scoped_refptr<net::IOBuffer> buffer(buf); | 116 scoped_refptr<net::IOBuffer> buffer(buf); |
| 117 int size = std::min( | 117 int size = std::min( |
| 118 static_cast<int>(incoming_packets_.front().size()), buf_len); | 118 static_cast<int>(incoming_packets_.front().size()), buf_len); |
| 119 memcpy(buffer->data(), &*incoming_packets_.front().begin(), size); | 119 memcpy(buffer->data(), &*incoming_packets_.front().begin(), size); |
| 120 incoming_packets_.pop_front(); | 120 incoming_packets_.pop_front(); |
| 121 return size; | 121 return size; |
| 122 } else { | 122 } else { |
| 123 read_callback_ = callback; | 123 read_callback_ = callback; |
| 124 read_buffer_ = buf; | 124 read_buffer_ = buf; |
| 125 read_buffer_size_ = buf_len; | 125 read_buffer_size_ = buf_len; |
| 126 return net::ERR_IO_PENDING; | 126 return net::ERR_IO_PENDING; |
| 127 } | 127 } |
| 128 } | 128 } |
| 129 | 129 |
| 130 virtual int Write(net::IOBuffer* buf, int buf_len, | 130 virtual int Write(net::IOBuffer* buf, int buf_len, |
| 131 const net::CompletionCallback& callback) OVERRIDE { | 131 const net::CompletionCallback& callback) override { |
| 132 DCHECK(buf); | 132 DCHECK(buf); |
| 133 if (peer_socket_) { | 133 if (peer_socket_) { |
| 134 base::MessageLoop::current()->PostDelayedTask( | 134 base::MessageLoop::current()->PostDelayedTask( |
| 135 FROM_HERE, | 135 FROM_HERE, |
| 136 base::Bind(&FakeSocket::AppendInputPacket, | 136 base::Bind(&FakeSocket::AppendInputPacket, |
| 137 base::Unretained(peer_socket_), | 137 base::Unretained(peer_socket_), |
| 138 std::vector<char>(buf->data(), buf->data() + buf_len)), | 138 std::vector<char>(buf->data(), buf->data() + buf_len)), |
| 139 base::TimeDelta::FromMilliseconds(latency_ms_)); | 139 base::TimeDelta::FromMilliseconds(latency_ms_)); |
| 140 } | 140 } |
| 141 | 141 |
| 142 return buf_len; | 142 return buf_len; |
| 143 } | 143 } |
| 144 | 144 |
| 145 virtual int SetReceiveBufferSize(int32 size) OVERRIDE { | 145 virtual int SetReceiveBufferSize(int32 size) override { |
| 146 NOTIMPLEMENTED(); | 146 NOTIMPLEMENTED(); |
| 147 return net::ERR_NOT_IMPLEMENTED; | 147 return net::ERR_NOT_IMPLEMENTED; |
| 148 } | 148 } |
| 149 virtual int SetSendBufferSize(int32 size) OVERRIDE { | 149 virtual int SetSendBufferSize(int32 size) override { |
| 150 NOTIMPLEMENTED(); | 150 NOTIMPLEMENTED(); |
| 151 return net::ERR_NOT_IMPLEMENTED; | 151 return net::ERR_NOT_IMPLEMENTED; |
| 152 } | 152 } |
| 153 | 153 |
| 154 private: | 154 private: |
| 155 scoped_refptr<net::IOBuffer> read_buffer_; | 155 scoped_refptr<net::IOBuffer> read_buffer_; |
| 156 int read_buffer_size_; | 156 int read_buffer_size_; |
| 157 net::CompletionCallback read_callback_; | 157 net::CompletionCallback read_callback_; |
| 158 | 158 |
| 159 std::deque<std::vector<char> > incoming_packets_; | 159 std::deque<std::vector<char> > incoming_packets_; |
| (...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 291 | 291 |
| 292 scoped_refptr<net::DrainableIOBuffer> output_buffer_; | 292 scoped_refptr<net::DrainableIOBuffer> output_buffer_; |
| 293 scoped_refptr<net::GrowableIOBuffer> input_buffer_; | 293 scoped_refptr<net::GrowableIOBuffer> input_buffer_; |
| 294 | 294 |
| 295 int write_errors_; | 295 int write_errors_; |
| 296 int read_errors_; | 296 int read_errors_; |
| 297 }; | 297 }; |
| 298 | 298 |
| 299 class PseudoTcpAdapterTest : public testing::Test { | 299 class PseudoTcpAdapterTest : public testing::Test { |
| 300 protected: | 300 protected: |
| 301 virtual void SetUp() OVERRIDE { | 301 virtual void SetUp() override { |
| 302 JingleThreadWrapper::EnsureForCurrentMessageLoop(); | 302 JingleThreadWrapper::EnsureForCurrentMessageLoop(); |
| 303 | 303 |
| 304 host_socket_ = new FakeSocket(); | 304 host_socket_ = new FakeSocket(); |
| 305 client_socket_ = new FakeSocket(); | 305 client_socket_ = new FakeSocket(); |
| 306 | 306 |
| 307 host_socket_->Connect(client_socket_); | 307 host_socket_->Connect(client_socket_); |
| 308 client_socket_->Connect(host_socket_); | 308 client_socket_->Connect(host_socket_); |
| 309 | 309 |
| 310 host_pseudotcp_.reset(new PseudoTcpAdapter(host_socket_)); | 310 host_pseudotcp_.reset(new PseudoTcpAdapter(host_socket_)); |
| 311 client_pseudotcp_.reset(new PseudoTcpAdapter(client_socket_)); | 311 client_pseudotcp_.reset(new PseudoTcpAdapter(client_socket_)); |
| (...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 433 client_pseudotcp_.get()); | 433 client_pseudotcp_.get()); |
| 434 | 434 |
| 435 tester->Start(); | 435 tester->Start(); |
| 436 message_loop_.Run(); | 436 message_loop_.Run(); |
| 437 tester->CheckResults(); | 437 tester->CheckResults(); |
| 438 } | 438 } |
| 439 | 439 |
| 440 } // namespace | 440 } // namespace |
| 441 | 441 |
| 442 } // namespace jingle_glue | 442 } // namespace jingle_glue |
| OLD | NEW |