| 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 347 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 358 cb.Run(result); | 358 cb.Run(result); |
| 359 } | 359 } |
| 360 } | 360 } |
| 361 | 361 |
| 362 ChannelMultiplexer::ChannelMultiplexer(ChannelFactory* factory, | 362 ChannelMultiplexer::ChannelMultiplexer(ChannelFactory* factory, |
| 363 const std::string& base_channel_name) | 363 const std::string& base_channel_name) |
| 364 : base_channel_factory_(factory), | 364 : base_channel_factory_(factory), |
| 365 base_channel_name_(base_channel_name), | 365 base_channel_name_(base_channel_name), |
| 366 next_channel_id_(0), | 366 next_channel_id_(0), |
| 367 destroyed_flag_(NULL) { | 367 destroyed_flag_(NULL) { |
| 368 factory->CreateStreamChannel( | |
| 369 base_channel_name, | |
| 370 base::Bind(&ChannelMultiplexer::OnBaseChannelReady, | |
| 371 base::Unretained(this))); | |
| 372 } | 368 } |
| 373 | 369 |
| 374 ChannelMultiplexer::~ChannelMultiplexer() { | 370 ChannelMultiplexer::~ChannelMultiplexer() { |
| 375 DCHECK(pending_channels_.empty()); | 371 DCHECK(pending_channels_.empty()); |
| 376 STLDeleteValues(&channels_); | 372 STLDeleteValues(&channels_); |
| 377 | 373 |
| 378 // Cancel creation of the base channel if it hasn't finished. | 374 // Cancel creation of the base channel if it hasn't finished. |
| 379 if (base_channel_factory_) | 375 if (base_channel_factory_) |
| 380 base_channel_factory_->CancelChannelCreation(base_channel_name_); | 376 base_channel_factory_->CancelChannelCreation(base_channel_name_); |
| 381 | 377 |
| 382 if (destroyed_flag_) | 378 if (destroyed_flag_) |
| 383 *destroyed_flag_ = true; | 379 *destroyed_flag_ = true; |
| 384 } | 380 } |
| 385 | 381 |
| 386 void ChannelMultiplexer::CreateStreamChannel( | 382 void ChannelMultiplexer::CreateStreamChannel( |
| 387 const std::string& name, | 383 const std::string& name, |
| 388 const StreamChannelCallback& callback) { | 384 const StreamChannelCallback& callback) { |
| 389 if (base_channel_.get()) { | 385 if (base_channel_.get()) { |
| 390 // Already have |base_channel_|. Create new multiplexed channel | 386 // Already have |base_channel_|. Create new multiplexed channel |
| 391 // synchronously. | 387 // synchronously. |
| 392 callback.Run(GetOrCreateChannel(name)->CreateSocket()); | 388 callback.Run(GetOrCreateChannel(name)->CreateSocket()); |
| 393 } else if (!base_channel_.get() && !base_channel_factory_) { | 389 } else if (!base_channel_.get() && !base_channel_factory_) { |
| 394 // Fail synchronously if we failed to create |base_channel_|. | 390 // Fail synchronously if we failed to create |base_channel_|. |
| 395 callback.Run(scoped_ptr<net::StreamSocket>()); | 391 callback.Run(scoped_ptr<net::StreamSocket>()); |
| 396 } else { | 392 } else { |
| 397 // Still waiting for the |base_channel_|. | 393 // Still waiting for the |base_channel_|. |
| 398 pending_channels_.push_back(PendingChannel(name, callback)); | 394 pending_channels_.push_back(PendingChannel(name, callback)); |
| 395 |
| 396 // If this is the first multiplexed channel then create the base channel. |
| 397 if (pending_channels_.size() == 1U) { |
| 398 base_channel_factory_->CreateStreamChannel( |
| 399 base_channel_name_, |
| 400 base::Bind(&ChannelMultiplexer::OnBaseChannelReady, |
| 401 base::Unretained(this))); |
| 402 } |
| 399 } | 403 } |
| 400 } | 404 } |
| 401 | 405 |
| 402 void ChannelMultiplexer::CreateDatagramChannel( | 406 void ChannelMultiplexer::CreateDatagramChannel( |
| 403 const std::string& name, | 407 const std::string& name, |
| 404 const DatagramChannelCallback& callback) { | 408 const DatagramChannelCallback& callback) { |
| 405 NOTIMPLEMENTED(); | 409 NOTIMPLEMENTED(); |
| 406 callback.Run(scoped_ptr<net::Socket>()); | 410 callback.Run(scoped_ptr<net::Socket>()); |
| 407 } | 411 } |
| 408 | 412 |
| (...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 504 channel->OnIncomingPacket(packet.Pass(), done_task); | 508 channel->OnIncomingPacket(packet.Pass(), done_task); |
| 505 } | 509 } |
| 506 | 510 |
| 507 bool ChannelMultiplexer::DoWrite(scoped_ptr<MultiplexPacket> packet, | 511 bool ChannelMultiplexer::DoWrite(scoped_ptr<MultiplexPacket> packet, |
| 508 const base::Closure& done_task) { | 512 const base::Closure& done_task) { |
| 509 return writer_.Write(SerializeAndFrameMessage(*packet), done_task); | 513 return writer_.Write(SerializeAndFrameMessage(*packet), done_task); |
| 510 } | 514 } |
| 511 | 515 |
| 512 } // namespace protocol | 516 } // namespace protocol |
| 513 } // namespace remoting | 517 } // namespace remoting |
| OLD | NEW |