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 <stddef.h> | 7 #include <stddef.h> |
8 #include <string.h> | 8 #include <string.h> |
9 | 9 |
10 #include <utility> | 10 #include <utility> |
(...skipping 287 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
298 DCHECK_GT(result, 0); | 298 DCHECK_GT(result, 0); |
299 base::ResetAndReturn(&read_callback_).Run(result); | 299 base::ResetAndReturn(&read_callback_).Run(result); |
300 } | 300 } |
301 } | 301 } |
302 | 302 |
303 ChannelMultiplexer::ChannelMultiplexer(StreamChannelFactory* factory, | 303 ChannelMultiplexer::ChannelMultiplexer(StreamChannelFactory* factory, |
304 const std::string& base_channel_name) | 304 const std::string& base_channel_name) |
305 : base_channel_factory_(factory), | 305 : base_channel_factory_(factory), |
306 base_channel_name_(base_channel_name), | 306 base_channel_name_(base_channel_name), |
307 next_channel_id_(0), | 307 next_channel_id_(0), |
308 parser_(base::Bind(&ChannelMultiplexer::OnIncomingPacket, | 308 weak_factory_(this) {} |
309 base::Unretained(this)), | |
310 &reader_), | |
311 weak_factory_(this) { | |
312 } | |
313 | 309 |
314 ChannelMultiplexer::~ChannelMultiplexer() { | 310 ChannelMultiplexer::~ChannelMultiplexer() { |
315 DCHECK(pending_channels_.empty()); | 311 DCHECK(pending_channels_.empty()); |
316 STLDeleteValues(&channels_); | 312 STLDeleteValues(&channels_); |
317 | 313 |
318 // Cancel creation of the base channel if it hasn't finished. | 314 // Cancel creation of the base channel if it hasn't finished. |
319 if (base_channel_factory_) | 315 if (base_channel_factory_) |
320 base_channel_factory_->CancelChannelCreation(base_channel_name_); | 316 base_channel_factory_->CancelChannelCreation(base_channel_name_); |
321 } | 317 } |
322 | 318 |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
354 } | 350 } |
355 | 351 |
356 void ChannelMultiplexer::OnBaseChannelReady( | 352 void ChannelMultiplexer::OnBaseChannelReady( |
357 scoped_ptr<P2PStreamSocket> socket) { | 353 scoped_ptr<P2PStreamSocket> socket) { |
358 base_channel_factory_ = nullptr; | 354 base_channel_factory_ = nullptr; |
359 base_channel_ = std::move(socket); | 355 base_channel_ = std::move(socket); |
360 | 356 |
361 if (base_channel_.get()) { | 357 if (base_channel_.get()) { |
362 // Initialize reader and writer. | 358 // Initialize reader and writer. |
363 reader_.StartReading(base_channel_.get(), | 359 reader_.StartReading(base_channel_.get(), |
| 360 base::Bind(&ChannelMultiplexer::OnIncomingPacket, |
| 361 base::Unretained(this)), |
364 base::Bind(&ChannelMultiplexer::OnBaseChannelError, | 362 base::Bind(&ChannelMultiplexer::OnBaseChannelError, |
365 base::Unretained(this))); | 363 base::Unretained(this))); |
366 writer_.Start(base::Bind(&P2PStreamSocket::Write, | 364 writer_.Start(base::Bind(&P2PStreamSocket::Write, |
367 base::Unretained(base_channel_.get())), | 365 base::Unretained(base_channel_.get())), |
368 base::Bind(&ChannelMultiplexer::OnBaseChannelError, | 366 base::Bind(&ChannelMultiplexer::OnBaseChannelError, |
369 base::Unretained(this))); | 367 base::Unretained(this))); |
370 } | 368 } |
371 | 369 |
372 DoCreatePendingChannels(); | 370 DoCreatePendingChannels(); |
373 } | 371 } |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
417 } | 415 } |
418 } | 416 } |
419 | 417 |
420 void ChannelMultiplexer::NotifyBaseChannelError(const std::string& name, | 418 void ChannelMultiplexer::NotifyBaseChannelError(const std::string& name, |
421 int error) { | 419 int error) { |
422 std::map<std::string, MuxChannel*>::iterator it = channels_.find(name); | 420 std::map<std::string, MuxChannel*>::iterator it = channels_.find(name); |
423 if (it != channels_.end()) | 421 if (it != channels_.end()) |
424 it->second->OnBaseChannelError(error); | 422 it->second->OnBaseChannelError(error); |
425 } | 423 } |
426 | 424 |
427 void ChannelMultiplexer::OnIncomingPacket(scoped_ptr<MultiplexPacket> packet) { | 425 void ChannelMultiplexer::OnIncomingPacket(scoped_ptr<CompoundBuffer> buffer) { |
| 426 scoped_ptr<MultiplexPacket> packet = |
| 427 ParseMessage<MultiplexPacket>(buffer.get()); |
| 428 if (!packet) |
| 429 return; |
| 430 |
428 DCHECK(packet->has_channel_id()); | 431 DCHECK(packet->has_channel_id()); |
429 if (!packet->has_channel_id()) { | 432 if (!packet->has_channel_id()) { |
430 LOG(ERROR) << "Received packet without channel_id."; | 433 LOG(ERROR) << "Received packet without channel_id."; |
431 return; | 434 return; |
432 } | 435 } |
433 | 436 |
434 int receive_id = packet->channel_id(); | 437 int receive_id = packet->channel_id(); |
435 MuxChannel* channel = nullptr; | 438 MuxChannel* channel = nullptr; |
436 std::map<int, MuxChannel*>::iterator it = | 439 std::map<int, MuxChannel*>::iterator it = |
437 channels_by_receive_id_.find(receive_id); | 440 channels_by_receive_id_.find(receive_id); |
(...skipping 14 matching lines...) Expand all Loading... |
452 channel->OnIncomingPacket(std::move(packet)); | 455 channel->OnIncomingPacket(std::move(packet)); |
453 } | 456 } |
454 | 457 |
455 void ChannelMultiplexer::DoWrite(scoped_ptr<MultiplexPacket> packet, | 458 void ChannelMultiplexer::DoWrite(scoped_ptr<MultiplexPacket> packet, |
456 const base::Closure& done_task) { | 459 const base::Closure& done_task) { |
457 writer_.Write(SerializeAndFrameMessage(*packet), done_task); | 460 writer_.Write(SerializeAndFrameMessage(*packet), done_task); |
458 } | 461 } |
459 | 462 |
460 } // namespace protocol | 463 } // namespace protocol |
461 } // namespace remoting | 464 } // namespace remoting |
OLD | NEW |