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 <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" |
11 #include "base/compiler_specific.h" | 11 #include "base/compiler_specific.h" |
12 #include "jingle/glue/thread_wrapper.h" | 12 #include "jingle/glue/thread_wrapper.h" |
| 13 #include "net/base/completion_callback.h" |
13 #include "net/base/io_buffer.h" | 14 #include "net/base/io_buffer.h" |
14 #include "net/base/net_errors.h" | 15 #include "net/base/net_errors.h" |
15 #include "net/base/test_completion_callback.h" | 16 #include "net/base/test_completion_callback.h" |
16 #include "net/udp/udp_socket.h" | 17 #include "net/udp/udp_socket.h" |
17 #include "testing/gmock/include/gmock/gmock.h" | 18 #include "testing/gmock/include/gmock/gmock.h" |
18 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
19 | 20 |
20 | 21 |
21 namespace jingle_glue { | 22 namespace jingle_glue { |
22 namespace { | 23 namespace { |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
73 private: | 74 private: |
74 double volume_; | 75 double volume_; |
75 double rate_; | 76 double rate_; |
76 double level_; | 77 double level_; |
77 base::TimeTicks last_update_; | 78 base::TimeTicks last_update_; |
78 }; | 79 }; |
79 | 80 |
80 class FakeSocket : public net::Socket { | 81 class FakeSocket : public net::Socket { |
81 public: | 82 public: |
82 FakeSocket() | 83 FakeSocket() |
83 : read_callback_(NULL), | 84 : old_read_callback_(NULL), |
84 rate_limiter_(NULL), | 85 rate_limiter_(NULL), |
85 latency_ms_(0) { | 86 latency_ms_(0) { |
86 } | 87 } |
87 virtual ~FakeSocket() { } | 88 virtual ~FakeSocket() { } |
88 | 89 |
89 void AppendInputPacket(const std::vector<char>& data) { | 90 void AppendInputPacket(const std::vector<char>& data) { |
90 if (rate_limiter_ && rate_limiter_->DropNextPacket()) | 91 if (rate_limiter_ && rate_limiter_->DropNextPacket()) |
91 return; // Lose the packet. | 92 return; // Lose the packet. |
92 | 93 |
93 if (read_callback_) { | 94 if (old_read_callback_ || !read_callback_.is_null()) { |
94 int size = std::min(read_buffer_size_, static_cast<int>(data.size())); | 95 int size = std::min(read_buffer_size_, static_cast<int>(data.size())); |
95 memcpy(read_buffer_->data(), &data[0], data.size()); | 96 memcpy(read_buffer_->data(), &data[0], data.size()); |
96 net::OldCompletionCallback* cb = read_callback_; | 97 if (old_read_callback_) { |
97 read_callback_ = NULL; | 98 net::OldCompletionCallback* cb = old_read_callback_; |
98 read_buffer_ = NULL; | 99 old_read_callback_ = NULL; |
99 cb->Run(size); | 100 read_buffer_ = NULL; |
| 101 cb->Run(size); |
| 102 } else { |
| 103 net::CompletionCallback cb = read_callback_; |
| 104 read_callback_.Reset(); |
| 105 read_buffer_ = NULL; |
| 106 cb.Run(size); |
| 107 } |
100 } else { | 108 } else { |
101 incoming_packets_.push_back(data); | 109 incoming_packets_.push_back(data); |
102 } | 110 } |
103 } | 111 } |
104 | 112 |
105 void Connect(FakeSocket* peer_socket) { | 113 void Connect(FakeSocket* peer_socket) { |
106 peer_socket_ = peer_socket; | 114 peer_socket_ = peer_socket; |
107 } | 115 } |
108 | 116 |
109 void set_rate_limiter(RateLimiter* rate_limiter) { | 117 void set_rate_limiter(RateLimiter* rate_limiter) { |
110 rate_limiter_ = rate_limiter; | 118 rate_limiter_ = rate_limiter; |
111 }; | 119 }; |
112 | 120 |
113 void set_latency(int latency_ms) { latency_ms_ = latency_ms; }; | 121 void set_latency(int latency_ms) { latency_ms_ = latency_ms; }; |
114 | 122 |
115 // net::Socket interface. | 123 // net::Socket implementation. |
116 virtual int Read(net::IOBuffer* buf, int buf_len, | 124 virtual int Read(net::IOBuffer* buf, int buf_len, |
117 net::OldCompletionCallback* callback) { | 125 net::OldCompletionCallback* callback) { |
118 CHECK(!read_callback_); | 126 CHECK(!old_read_callback_ && read_callback_.is_null()); |
119 CHECK(buf); | 127 CHECK(buf); |
120 | 128 |
121 if (incoming_packets_.size() > 0) { | 129 if (incoming_packets_.size() > 0) { |
| 130 scoped_refptr<net::IOBuffer> buffer(buf); |
| 131 int size = std::min( |
| 132 static_cast<int>(incoming_packets_.front().size()), buf_len); |
| 133 memcpy(buffer->data(), &*incoming_packets_.front().begin(), size); |
| 134 incoming_packets_.pop_front(); |
| 135 return size; |
| 136 } else { |
| 137 old_read_callback_ = callback; |
| 138 read_buffer_ = buf; |
| 139 read_buffer_size_ = buf_len; |
| 140 return net::ERR_IO_PENDING; |
| 141 } |
| 142 } |
| 143 virtual int Read(net::IOBuffer* buf, int buf_len, |
| 144 const net::CompletionCallback& callback) { |
| 145 CHECK(!old_read_callback_ && read_callback_.is_null()); |
| 146 CHECK(buf); |
| 147 |
| 148 if (incoming_packets_.size() > 0) { |
122 scoped_refptr<net::IOBuffer> buffer(buf); | 149 scoped_refptr<net::IOBuffer> buffer(buf); |
123 int size = std::min( | 150 int size = std::min( |
124 static_cast<int>(incoming_packets_.front().size()), buf_len); | 151 static_cast<int>(incoming_packets_.front().size()), buf_len); |
125 memcpy(buffer->data(), &*incoming_packets_.front().begin(), size); | 152 memcpy(buffer->data(), &*incoming_packets_.front().begin(), size); |
126 incoming_packets_.pop_front(); | 153 incoming_packets_.pop_front(); |
127 return size; | 154 return size; |
128 } else { | 155 } else { |
129 read_callback_ = callback; | 156 read_callback_ = callback; |
130 read_buffer_ = buf; | 157 read_buffer_ = buf; |
131 read_buffer_size_ = buf_len; | 158 read_buffer_size_ = buf_len; |
(...skipping 21 matching lines...) Expand all Loading... |
153 return false; | 180 return false; |
154 } | 181 } |
155 virtual bool SetSendBufferSize(int32 size) OVERRIDE { | 182 virtual bool SetSendBufferSize(int32 size) OVERRIDE { |
156 NOTIMPLEMENTED(); | 183 NOTIMPLEMENTED(); |
157 return false; | 184 return false; |
158 } | 185 } |
159 | 186 |
160 private: | 187 private: |
161 scoped_refptr<net::IOBuffer> read_buffer_; | 188 scoped_refptr<net::IOBuffer> read_buffer_; |
162 int read_buffer_size_; | 189 int read_buffer_size_; |
163 net::OldCompletionCallback* read_callback_; | 190 net::OldCompletionCallback* old_read_callback_; |
| 191 net::CompletionCallback read_callback_; |
164 | 192 |
165 std::deque<std::vector<char> > incoming_packets_; | 193 std::deque<std::vector<char> > incoming_packets_; |
166 | 194 |
167 FakeSocket* peer_socket_; | 195 FakeSocket* peer_socket_; |
168 RateLimiter* rate_limiter_; | 196 RateLimiter* rate_limiter_; |
169 int latency_ms_; | 197 int latency_ms_; |
170 }; | 198 }; |
171 | 199 |
172 class TCPChannelTester : public base::RefCountedThreadSafe<TCPChannelTester> { | 200 class TCPChannelTester : public base::RefCountedThreadSafe<TCPChannelTester> { |
173 public: | 201 public: |
(...skipping 234 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
408 host_pseudotcp_->Connect(&host_connect_cb); | 436 host_pseudotcp_->Connect(&host_connect_cb); |
409 client_pseudotcp_->Connect(&client_connect_cb); | 437 client_pseudotcp_->Connect(&client_connect_cb); |
410 message_loop_.Run(); | 438 message_loop_.Run(); |
411 | 439 |
412 ASSERT_EQ(NULL, host_pseudotcp_.get()); | 440 ASSERT_EQ(NULL, host_pseudotcp_.get()); |
413 } | 441 } |
414 | 442 |
415 } // namespace | 443 } // namespace |
416 | 444 |
417 } // namespace jingle_glue | 445 } // namespace jingle_glue |
OLD | NEW |