Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(94)

Side by Side Diff: jingle/glue/pseudotcp_adapter_unittest.cc

Issue 8801005: base::Bind: Convert Socket::Read. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Rebase. Created 9 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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"
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
73 private: 73 private:
74 double volume_; 74 double volume_;
75 double rate_; 75 double rate_;
76 double level_; 76 double level_;
77 base::TimeTicks last_update_; 77 base::TimeTicks last_update_;
78 }; 78 };
79 79
80 class FakeSocket : public net::Socket { 80 class FakeSocket : public net::Socket {
81 public: 81 public:
82 FakeSocket() 82 FakeSocket()
83 : read_callback_(NULL), 83 : old_read_callback_(NULL),
84 rate_limiter_(NULL), 84 rate_limiter_(NULL),
85 latency_ms_(0) { 85 latency_ms_(0) {
86 } 86 }
87 virtual ~FakeSocket() { } 87 virtual ~FakeSocket() { }
88 88
89 void AppendInputPacket(const std::vector<char>& data) { 89 void AppendInputPacket(const std::vector<char>& data) {
90 if (rate_limiter_ && rate_limiter_->DropNextPacket()) 90 if (rate_limiter_ && rate_limiter_->DropNextPacket())
91 return; // Lose the packet. 91 return; // Lose the packet.
92 92
93 if (read_callback_) { 93 if (old_read_callback_ || !read_callback_.is_null()) {
94 int size = std::min(read_buffer_size_, static_cast<int>(data.size())); 94 int size = std::min(read_buffer_size_, static_cast<int>(data.size()));
95 memcpy(read_buffer_->data(), &data[0], data.size()); 95 memcpy(read_buffer_->data(), &data[0], data.size());
96 net::OldCompletionCallback* cb = read_callback_; 96 if (old_read_callback_) {
97 read_callback_ = NULL; 97 net::OldCompletionCallback* cb = old_read_callback_;
98 read_buffer_ = NULL; 98 old_read_callback_ = NULL;
99 cb->Run(size); 99 read_buffer_ = NULL;
100 cb->Run(size);
101 } else {
102 net::CompletionCallback cb = read_callback_;
103 read_callback_.Reset();
104 read_buffer_ = NULL;
105 cb.Run(size);
106 }
100 } else { 107 } else {
101 incoming_packets_.push_back(data); 108 incoming_packets_.push_back(data);
102 } 109 }
103 } 110 }
104 111
105 void Connect(FakeSocket* peer_socket) { 112 void Connect(FakeSocket* peer_socket) {
106 peer_socket_ = peer_socket; 113 peer_socket_ = peer_socket;
107 } 114 }
108 115
109 void set_rate_limiter(RateLimiter* rate_limiter) { 116 void set_rate_limiter(RateLimiter* rate_limiter) {
110 rate_limiter_ = rate_limiter; 117 rate_limiter_ = rate_limiter;
111 }; 118 };
112 119
113 void set_latency(int latency_ms) { latency_ms_ = latency_ms; }; 120 void set_latency(int latency_ms) { latency_ms_ = latency_ms; };
114 121
115 // net::Socket interface. 122 // net::Socket implementation.
116 virtual int Read(net::IOBuffer* buf, int buf_len, 123 virtual int Read(net::IOBuffer* buf, int buf_len,
117 net::OldCompletionCallback* callback) { 124 net::OldCompletionCallback* callback) {
118 CHECK(!read_callback_); 125 CHECK(!old_read_callback_ && read_callback_.is_null());
119 CHECK(buf); 126 CHECK(buf);
120 127
121 if (incoming_packets_.size() > 0) { 128 if (incoming_packets_.size() > 0) {
129 scoped_refptr<net::IOBuffer> buffer(buf);
130 int size = std::min(
131 static_cast<int>(incoming_packets_.front().size()), buf_len);
132 memcpy(buffer->data(), &*incoming_packets_.front().begin(), size);
133 incoming_packets_.pop_front();
134 return size;
135 } else {
136 old_read_callback_ = callback;
137 read_buffer_ = buf;
138 read_buffer_size_ = buf_len;
139 return net::ERR_IO_PENDING;
140 }
141 }
142 virtual int Read(net::IOBuffer* buf, int buf_len,
143 const net::CompletionCallback& callback) {
144 CHECK(!old_read_callback_ && read_callback_.is_null());
145 CHECK(buf);
146
147 if (incoming_packets_.size() > 0) {
122 scoped_refptr<net::IOBuffer> buffer(buf); 148 scoped_refptr<net::IOBuffer> buffer(buf);
123 int size = std::min( 149 int size = std::min(
124 static_cast<int>(incoming_packets_.front().size()), buf_len); 150 static_cast<int>(incoming_packets_.front().size()), buf_len);
125 memcpy(buffer->data(), &*incoming_packets_.front().begin(), size); 151 memcpy(buffer->data(), &*incoming_packets_.front().begin(), size);
126 incoming_packets_.pop_front(); 152 incoming_packets_.pop_front();
127 return size; 153 return size;
128 } else { 154 } else {
129 read_callback_ = callback; 155 read_callback_ = callback;
130 read_buffer_ = buf; 156 read_buffer_ = buf;
131 read_buffer_size_ = buf_len; 157 read_buffer_size_ = buf_len;
(...skipping 21 matching lines...) Expand all
153 return false; 179 return false;
154 } 180 }
155 virtual bool SetSendBufferSize(int32 size) OVERRIDE { 181 virtual bool SetSendBufferSize(int32 size) OVERRIDE {
156 NOTIMPLEMENTED(); 182 NOTIMPLEMENTED();
157 return false; 183 return false;
158 } 184 }
159 185
160 private: 186 private:
161 scoped_refptr<net::IOBuffer> read_buffer_; 187 scoped_refptr<net::IOBuffer> read_buffer_;
162 int read_buffer_size_; 188 int read_buffer_size_;
163 net::OldCompletionCallback* read_callback_; 189 net::OldCompletionCallback* old_read_callback_;
190 net::CompletionCallback read_callback_;
James Hawkins 2011/12/07 00:01:57 #include "net/base/completion_callback.h"
James Hawkins 2011/12/07 00:08:11 Done.
164 191
165 std::deque<std::vector<char> > incoming_packets_; 192 std::deque<std::vector<char> > incoming_packets_;
166 193
167 FakeSocket* peer_socket_; 194 FakeSocket* peer_socket_;
168 RateLimiter* rate_limiter_; 195 RateLimiter* rate_limiter_;
169 int latency_ms_; 196 int latency_ms_;
170 }; 197 };
171 198
172 class TCPChannelTester : public base::RefCountedThreadSafe<TCPChannelTester> { 199 class TCPChannelTester : public base::RefCountedThreadSafe<TCPChannelTester> {
173 public: 200 public:
(...skipping 234 matching lines...) Expand 10 before | Expand all | Expand 10 after
408 host_pseudotcp_->Connect(&host_connect_cb); 435 host_pseudotcp_->Connect(&host_connect_cb);
409 client_pseudotcp_->Connect(&client_connect_cb); 436 client_pseudotcp_->Connect(&client_connect_cb);
410 message_loop_.Run(); 437 message_loop_.Run();
411 438
412 ASSERT_EQ(NULL, host_pseudotcp_.get()); 439 ASSERT_EQ(NULL, host_pseudotcp_.get());
413 } 440 }
414 441
415 } // namespace 442 } // namespace
416 443
417 } // namespace jingle_glue 444 } // namespace jingle_glue
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698