| 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 "remoting/protocol/channel_multiplexer.h" | 5 #include "remoting/protocol/channel_multiplexer.h" |
| 6 | 6 |
| 7 #include <string.h> | 7 #include <string.h> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/callback.h" | 10 #include "base/callback.h" |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 77 void set_receive_id(int id) { receive_id_ = id; } | 77 void set_receive_id(int id) { receive_id_ = id; } |
| 78 | 78 |
| 79 // Called by ChannelMultiplexer. | 79 // Called by ChannelMultiplexer. |
| 80 scoped_ptr<P2PStreamSocket> CreateSocket(); | 80 scoped_ptr<P2PStreamSocket> CreateSocket(); |
| 81 void OnIncomingPacket(scoped_ptr<MultiplexPacket> packet, | 81 void OnIncomingPacket(scoped_ptr<MultiplexPacket> packet, |
| 82 const base::Closure& done_task); | 82 const base::Closure& done_task); |
| 83 void OnBaseChannelError(int error); | 83 void OnBaseChannelError(int error); |
| 84 | 84 |
| 85 // Called by MuxSocket. | 85 // Called by MuxSocket. |
| 86 void OnSocketDestroyed(); | 86 void OnSocketDestroyed(); |
| 87 bool DoWrite(scoped_ptr<MultiplexPacket> packet, | 87 void DoWrite(scoped_ptr<MultiplexPacket> packet, |
| 88 const base::Closure& done_task); | 88 const base::Closure& done_task); |
| 89 int DoRead(const scoped_refptr<net::IOBuffer>& buffer, int buffer_len); | 89 int DoRead(const scoped_refptr<net::IOBuffer>& buffer, int buffer_len); |
| 90 | 90 |
| 91 private: | 91 private: |
| 92 ChannelMultiplexer* multiplexer_; | 92 ChannelMultiplexer* multiplexer_; |
| 93 std::string name_; | 93 std::string name_; |
| 94 int send_id_; | 94 int send_id_; |
| 95 bool id_sent_; | 95 bool id_sent_; |
| 96 int receive_id_; | 96 int receive_id_; |
| 97 MuxSocket* socket_; | 97 MuxSocket* socket_; |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 175 void ChannelMultiplexer::MuxChannel::OnBaseChannelError(int error) { | 175 void ChannelMultiplexer::MuxChannel::OnBaseChannelError(int error) { |
| 176 if (socket_) | 176 if (socket_) |
| 177 socket_->OnBaseChannelError(error); | 177 socket_->OnBaseChannelError(error); |
| 178 } | 178 } |
| 179 | 179 |
| 180 void ChannelMultiplexer::MuxChannel::OnSocketDestroyed() { | 180 void ChannelMultiplexer::MuxChannel::OnSocketDestroyed() { |
| 181 DCHECK(socket_); | 181 DCHECK(socket_); |
| 182 socket_ = nullptr; | 182 socket_ = nullptr; |
| 183 } | 183 } |
| 184 | 184 |
| 185 bool ChannelMultiplexer::MuxChannel::DoWrite( | 185 void ChannelMultiplexer::MuxChannel::DoWrite( |
| 186 scoped_ptr<MultiplexPacket> packet, | 186 scoped_ptr<MultiplexPacket> packet, |
| 187 const base::Closure& done_task) { | 187 const base::Closure& done_task) { |
| 188 packet->set_channel_id(send_id_); | 188 packet->set_channel_id(send_id_); |
| 189 if (!id_sent_) { | 189 if (!id_sent_) { |
| 190 packet->set_channel_name(name_); | 190 packet->set_channel_name(name_); |
| 191 id_sent_ = true; | 191 id_sent_ = true; |
| 192 } | 192 } |
| 193 return multiplexer_->DoWrite(packet.Pass(), done_task); | 193 multiplexer_->DoWrite(packet.Pass(), done_task); |
| 194 } | 194 } |
| 195 | 195 |
| 196 int ChannelMultiplexer::MuxChannel::DoRead( | 196 int ChannelMultiplexer::MuxChannel::DoRead( |
| 197 const scoped_refptr<net::IOBuffer>& buffer, | 197 const scoped_refptr<net::IOBuffer>& buffer, |
| 198 int buffer_len) { | 198 int buffer_len) { |
| 199 int pos = 0; | 199 int pos = 0; |
| 200 while (buffer_len > 0 && !pending_packets_.empty()) { | 200 while (buffer_len > 0 && !pending_packets_.empty()) { |
| 201 DCHECK(!pending_packets_.front()->is_empty()); | 201 DCHECK(!pending_packets_.front()->is_empty()); |
| 202 int result = pending_packets_.front()->Read( | 202 int result = pending_packets_.front()->Read( |
| 203 buffer->data() + pos, buffer_len); | 203 buffer->data() + pos, buffer_len); |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 249 DCHECK(write_callback_.is_null()); | 249 DCHECK(write_callback_.is_null()); |
| 250 | 250 |
| 251 if (base_channel_error_ != net::OK) | 251 if (base_channel_error_ != net::OK) |
| 252 return base_channel_error_; | 252 return base_channel_error_; |
| 253 | 253 |
| 254 scoped_ptr<MultiplexPacket> packet(new MultiplexPacket()); | 254 scoped_ptr<MultiplexPacket> packet(new MultiplexPacket()); |
| 255 size_t size = std::min(kMaxPacketSize, buffer_len); | 255 size_t size = std::min(kMaxPacketSize, buffer_len); |
| 256 packet->mutable_data()->assign(buffer->data(), size); | 256 packet->mutable_data()->assign(buffer->data(), size); |
| 257 | 257 |
| 258 write_pending_ = true; | 258 write_pending_ = true; |
| 259 bool result = channel_->DoWrite(packet.Pass(), base::Bind( | 259 channel_->DoWrite(packet.Pass(), base::Bind( |
| 260 &ChannelMultiplexer::MuxSocket::OnWriteComplete, AsWeakPtr())); | 260 &ChannelMultiplexer::MuxSocket::OnWriteComplete, AsWeakPtr())); |
| 261 | 261 |
| 262 if (!result) { | |
| 263 // Cannot complete the write, e.g. if the connection has been terminated. | |
| 264 return net::ERR_FAILED; | |
| 265 } | |
| 266 | |
| 267 // OnWriteComplete() might be called above synchronously. | 262 // OnWriteComplete() might be called above synchronously. |
| 268 if (write_pending_) { | 263 if (write_pending_) { |
| 269 DCHECK(write_callback_.is_null()); | 264 DCHECK(write_callback_.is_null()); |
| 270 write_callback_ = callback; | 265 write_callback_ = callback; |
| 271 write_result_ = size; | 266 write_result_ = size; |
| 272 return net::ERR_IO_PENDING; | 267 return net::ERR_IO_PENDING; |
| 273 } | 268 } |
| 274 | 269 |
| 275 return size; | 270 return size; |
| 276 } | 271 } |
| (...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 458 return; | 453 return; |
| 459 } | 454 } |
| 460 channel = GetOrCreateChannel(packet->channel_name()); | 455 channel = GetOrCreateChannel(packet->channel_name()); |
| 461 channel->set_receive_id(receive_id); | 456 channel->set_receive_id(receive_id); |
| 462 channels_by_receive_id_[receive_id] = channel; | 457 channels_by_receive_id_[receive_id] = channel; |
| 463 } | 458 } |
| 464 | 459 |
| 465 channel->OnIncomingPacket(packet.Pass(), done_task); | 460 channel->OnIncomingPacket(packet.Pass(), done_task); |
| 466 } | 461 } |
| 467 | 462 |
| 468 bool ChannelMultiplexer::DoWrite(scoped_ptr<MultiplexPacket> packet, | 463 void ChannelMultiplexer::DoWrite(scoped_ptr<MultiplexPacket> packet, |
| 469 const base::Closure& done_task) { | 464 const base::Closure& done_task) { |
| 470 return writer_.Write(SerializeAndFrameMessage(*packet), done_task); | 465 writer_.Write(SerializeAndFrameMessage(*packet), done_task); |
| 471 } | 466 } |
| 472 | 467 |
| 473 } // namespace protocol | 468 } // namespace protocol |
| 474 } // namespace remoting | 469 } // namespace remoting |
| OLD | NEW |