| 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 173 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 184 | 184 |
| 185 ChannelMultiplexer::MuxChannel::MuxChannel( | 185 ChannelMultiplexer::MuxChannel::MuxChannel( |
| 186 ChannelMultiplexer* multiplexer, | 186 ChannelMultiplexer* multiplexer, |
| 187 const std::string& name, | 187 const std::string& name, |
| 188 int send_id) | 188 int send_id) |
| 189 : multiplexer_(multiplexer), | 189 : multiplexer_(multiplexer), |
| 190 name_(name), | 190 name_(name), |
| 191 send_id_(send_id), | 191 send_id_(send_id), |
| 192 id_sent_(false), | 192 id_sent_(false), |
| 193 receive_id_(kChannelIdUnknown), | 193 receive_id_(kChannelIdUnknown), |
| 194 socket_(NULL) { | 194 socket_(nullptr) { |
| 195 } | 195 } |
| 196 | 196 |
| 197 ChannelMultiplexer::MuxChannel::~MuxChannel() { | 197 ChannelMultiplexer::MuxChannel::~MuxChannel() { |
| 198 // Socket must be destroyed before the channel. | 198 // Socket must be destroyed before the channel. |
| 199 DCHECK(!socket_); | 199 DCHECK(!socket_); |
| 200 STLDeleteElements(&pending_packets_); | 200 STLDeleteElements(&pending_packets_); |
| 201 } | 201 } |
| 202 | 202 |
| 203 scoped_ptr<net::StreamSocket> ChannelMultiplexer::MuxChannel::CreateSocket() { | 203 scoped_ptr<net::StreamSocket> ChannelMultiplexer::MuxChannel::CreateSocket() { |
| 204 DCHECK(!socket_); // Can't create more than one socket per channel. | 204 DCHECK(!socket_); // Can't create more than one socket per channel. |
| (...skipping 15 matching lines...) Expand all Loading... |
| 220 } | 220 } |
| 221 } | 221 } |
| 222 | 222 |
| 223 void ChannelMultiplexer::MuxChannel::OnWriteFailed() { | 223 void ChannelMultiplexer::MuxChannel::OnWriteFailed() { |
| 224 if (socket_) | 224 if (socket_) |
| 225 socket_->OnWriteFailed(); | 225 socket_->OnWriteFailed(); |
| 226 } | 226 } |
| 227 | 227 |
| 228 void ChannelMultiplexer::MuxChannel::OnSocketDestroyed() { | 228 void ChannelMultiplexer::MuxChannel::OnSocketDestroyed() { |
| 229 DCHECK(socket_); | 229 DCHECK(socket_); |
| 230 socket_ = NULL; | 230 socket_ = nullptr; |
| 231 } | 231 } |
| 232 | 232 |
| 233 bool ChannelMultiplexer::MuxChannel::DoWrite( | 233 bool ChannelMultiplexer::MuxChannel::DoWrite( |
| 234 scoped_ptr<MultiplexPacket> packet, | 234 scoped_ptr<MultiplexPacket> packet, |
| 235 const base::Closure& done_task) { | 235 const base::Closure& done_task) { |
| 236 packet->set_channel_id(send_id_); | 236 packet->set_channel_id(send_id_); |
| 237 if (!id_sent_) { | 237 if (!id_sent_) { |
| 238 packet->set_channel_name(name_); | 238 packet->set_channel_name(name_); |
| 239 id_sent_ = true; | 239 id_sent_ = true; |
| 240 } | 240 } |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 328 if (!write_callback_.is_null()) { | 328 if (!write_callback_.is_null()) { |
| 329 net::CompletionCallback cb; | 329 net::CompletionCallback cb; |
| 330 std::swap(cb, write_callback_); | 330 std::swap(cb, write_callback_); |
| 331 cb.Run(net::ERR_FAILED); | 331 cb.Run(net::ERR_FAILED); |
| 332 } | 332 } |
| 333 } | 333 } |
| 334 | 334 |
| 335 void ChannelMultiplexer::MuxSocket::OnPacketReceived() { | 335 void ChannelMultiplexer::MuxSocket::OnPacketReceived() { |
| 336 if (!read_callback_.is_null()) { | 336 if (!read_callback_.is_null()) { |
| 337 int result = channel_->DoRead(read_buffer_.get(), read_buffer_size_); | 337 int result = channel_->DoRead(read_buffer_.get(), read_buffer_size_); |
| 338 read_buffer_ = NULL; | 338 read_buffer_ = nullptr; |
| 339 DCHECK_GT(result, 0); | 339 DCHECK_GT(result, 0); |
| 340 net::CompletionCallback cb; | 340 net::CompletionCallback cb; |
| 341 std::swap(cb, read_callback_); | 341 std::swap(cb, read_callback_); |
| 342 cb.Run(result); | 342 cb.Run(result); |
| 343 } | 343 } |
| 344 } | 344 } |
| 345 | 345 |
| 346 ChannelMultiplexer::ChannelMultiplexer(StreamChannelFactory* factory, | 346 ChannelMultiplexer::ChannelMultiplexer(StreamChannelFactory* factory, |
| 347 const std::string& base_channel_name) | 347 const std::string& base_channel_name) |
| 348 : base_channel_factory_(factory), | 348 : base_channel_factory_(factory), |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 388 it != pending_channels_.end(); ++it) { | 388 it != pending_channels_.end(); ++it) { |
| 389 if (it->name == name) { | 389 if (it->name == name) { |
| 390 pending_channels_.erase(it); | 390 pending_channels_.erase(it); |
| 391 return; | 391 return; |
| 392 } | 392 } |
| 393 } | 393 } |
| 394 } | 394 } |
| 395 | 395 |
| 396 void ChannelMultiplexer::OnBaseChannelReady( | 396 void ChannelMultiplexer::OnBaseChannelReady( |
| 397 scoped_ptr<net::StreamSocket> socket) { | 397 scoped_ptr<net::StreamSocket> socket) { |
| 398 base_channel_factory_ = NULL; | 398 base_channel_factory_ = nullptr; |
| 399 base_channel_ = socket.Pass(); | 399 base_channel_ = socket.Pass(); |
| 400 | 400 |
| 401 if (base_channel_.get()) { | 401 if (base_channel_.get()) { |
| 402 // Initialize reader and writer. | 402 // Initialize reader and writer. |
| 403 reader_.Init(base_channel_.get(), | 403 reader_.Init(base_channel_.get(), |
| 404 base::Bind(&ChannelMultiplexer::OnIncomingPacket, | 404 base::Bind(&ChannelMultiplexer::OnIncomingPacket, |
| 405 base::Unretained(this))); | 405 base::Unretained(this))); |
| 406 writer_.Init(base_channel_.get(), | 406 writer_.Init(base_channel_.get(), |
| 407 base::Bind(&ChannelMultiplexer::OnWriteFailed, | 407 base::Bind(&ChannelMultiplexer::OnWriteFailed, |
| 408 base::Unretained(this))); | 408 base::Unretained(this))); |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 465 void ChannelMultiplexer::OnIncomingPacket(scoped_ptr<MultiplexPacket> packet, | 465 void ChannelMultiplexer::OnIncomingPacket(scoped_ptr<MultiplexPacket> packet, |
| 466 const base::Closure& done_task) { | 466 const base::Closure& done_task) { |
| 467 DCHECK(packet->has_channel_id()); | 467 DCHECK(packet->has_channel_id()); |
| 468 if (!packet->has_channel_id()) { | 468 if (!packet->has_channel_id()) { |
| 469 LOG(ERROR) << "Received packet without channel_id."; | 469 LOG(ERROR) << "Received packet without channel_id."; |
| 470 done_task.Run(); | 470 done_task.Run(); |
| 471 return; | 471 return; |
| 472 } | 472 } |
| 473 | 473 |
| 474 int receive_id = packet->channel_id(); | 474 int receive_id = packet->channel_id(); |
| 475 MuxChannel* channel = NULL; | 475 MuxChannel* channel = nullptr; |
| 476 std::map<int, MuxChannel*>::iterator it = | 476 std::map<int, MuxChannel*>::iterator it = |
| 477 channels_by_receive_id_.find(receive_id); | 477 channels_by_receive_id_.find(receive_id); |
| 478 if (it != channels_by_receive_id_.end()) { | 478 if (it != channels_by_receive_id_.end()) { |
| 479 channel = it->second; | 479 channel = it->second; |
| 480 } else { | 480 } else { |
| 481 // This is a new |channel_id| we haven't seen before. Look it up by name. | 481 // This is a new |channel_id| we haven't seen before. Look it up by name. |
| 482 if (!packet->has_channel_name()) { | 482 if (!packet->has_channel_name()) { |
| 483 LOG(ERROR) << "Received packet with unknown channel_id and " | 483 LOG(ERROR) << "Received packet with unknown channel_id and " |
| 484 "without channel_name."; | 484 "without channel_name."; |
| 485 done_task.Run(); | 485 done_task.Run(); |
| 486 return; | 486 return; |
| 487 } | 487 } |
| 488 channel = GetOrCreateChannel(packet->channel_name()); | 488 channel = GetOrCreateChannel(packet->channel_name()); |
| 489 channel->set_receive_id(receive_id); | 489 channel->set_receive_id(receive_id); |
| 490 channels_by_receive_id_[receive_id] = channel; | 490 channels_by_receive_id_[receive_id] = channel; |
| 491 } | 491 } |
| 492 | 492 |
| 493 channel->OnIncomingPacket(packet.Pass(), done_task); | 493 channel->OnIncomingPacket(packet.Pass(), done_task); |
| 494 } | 494 } |
| 495 | 495 |
| 496 bool ChannelMultiplexer::DoWrite(scoped_ptr<MultiplexPacket> packet, | 496 bool ChannelMultiplexer::DoWrite(scoped_ptr<MultiplexPacket> packet, |
| 497 const base::Closure& done_task) { | 497 const base::Closure& done_task) { |
| 498 return writer_.Write(SerializeAndFrameMessage(*packet), done_task); | 498 return writer_.Write(SerializeAndFrameMessage(*packet), done_task); |
| 499 } | 499 } |
| 500 | 500 |
| 501 } // namespace protocol | 501 } // namespace protocol |
| 502 } // namespace remoting | 502 } // namespace remoting |
| OLD | NEW |