Re-land: C++ readability review
[chromium-blink-merge.git] / remoting / protocol / channel_dispatcher_base.cc
blobee90ee56dcbc7bc02756ebf80e919e90889d6cbd
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
3 // found in the LICENSE file.
5 #include "remoting/protocol/channel_dispatcher_base.h"
7 #include "base/bind.h"
8 #include "net/socket/stream_socket.h"
9 #include "remoting/protocol/session.h"
10 #include "remoting/protocol/session_config.h"
11 #include "remoting/protocol/stream_channel_factory.h"
13 namespace remoting {
14 namespace protocol {
16 ChannelDispatcherBase::ChannelDispatcherBase(const char* channel_name)
17 : channel_name_(channel_name),
18 channel_factory_(nullptr),
19 event_handler_(nullptr) {
22 ChannelDispatcherBase::~ChannelDispatcherBase() {
23 writer()->Close();
24 if (channel_factory_)
25 channel_factory_->CancelChannelCreation(channel_name_);
28 void ChannelDispatcherBase::Init(Session* session,
29 const ChannelConfig& config,
30 EventHandler* event_handler) {
31 DCHECK(session);
32 switch (config.transport) {
33 case ChannelConfig::TRANSPORT_MUX_STREAM:
34 channel_factory_ = session->GetMultiplexedChannelFactory();
35 break;
37 case ChannelConfig::TRANSPORT_STREAM:
38 channel_factory_ = session->GetTransportChannelFactory();
39 break;
41 default:
42 LOG(FATAL) << "Unknown transport type: " << config.transport;
45 event_handler_ = event_handler;
47 channel_factory_->CreateChannel(channel_name_, base::Bind(
48 &ChannelDispatcherBase::OnChannelReady, base::Unretained(this)));
51 void ChannelDispatcherBase::OnChannelReady(
52 scoped_ptr<net::StreamSocket> socket) {
53 if (!socket.get()) {
54 event_handler_->OnChannelError(this, CHANNEL_CONNECTION_ERROR);
55 return;
58 channel_factory_ = nullptr;
59 channel_ = socket.Pass();
60 writer_.Init(channel_.get(), base::Bind(&ChannelDispatcherBase::OnWriteFailed,
61 base::Unretained(this)));
62 reader_.StartReading(channel_.get());
64 event_handler_->OnChannelInitialized(this);
67 void ChannelDispatcherBase::OnWriteFailed(int error) {
68 event_handler_->OnChannelError(this, CHANNEL_CONNECTION_ERROR);
71 } // namespace protocol
72 } // namespace remoting