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"
8 #include "remoting/protocol/p2p_stream_socket.h"
9 #include "remoting/protocol/session.h"
10 #include "remoting/protocol/session_config.h"
11 #include "remoting/protocol/stream_channel_factory.h"
16 ChannelDispatcherBase::ChannelDispatcherBase(const char* channel_name
)
17 : channel_name_(channel_name
),
18 channel_factory_(nullptr),
19 event_handler_(nullptr) {
22 ChannelDispatcherBase::~ChannelDispatcherBase() {
24 channel_factory_
->CancelChannelCreation(channel_name_
);
27 void ChannelDispatcherBase::Init(Session
* session
,
28 const ChannelConfig
& config
,
29 EventHandler
* event_handler
) {
31 switch (config
.transport
) {
32 case ChannelConfig::TRANSPORT_MUX_STREAM
:
33 channel_factory_
= session
->GetMultiplexedChannelFactory();
36 case ChannelConfig::TRANSPORT_QUIC_STREAM
:
37 channel_factory_
= session
->GetQuicChannelFactory();
40 case ChannelConfig::TRANSPORT_STREAM
:
41 channel_factory_
= session
->GetTransportChannelFactory();
45 LOG(FATAL
) << "Unknown transport type: " << config
.transport
;
48 event_handler_
= event_handler
;
50 channel_factory_
->CreateChannel(channel_name_
, base::Bind(
51 &ChannelDispatcherBase::OnChannelReady
, base::Unretained(this)));
54 void ChannelDispatcherBase::OnChannelReady(
55 scoped_ptr
<P2PStreamSocket
> socket
) {
57 event_handler_
->OnChannelError(this, CHANNEL_CONNECTION_ERROR
);
61 channel_factory_
= nullptr;
62 channel_
= socket
.Pass();
64 base::Bind(&P2PStreamSocket::Write
, base::Unretained(channel_
.get())),
65 base::Bind(&ChannelDispatcherBase::OnReadWriteFailed
,
66 base::Unretained(this)));
67 reader_
.StartReading(channel_
.get(),
68 base::Bind(&ChannelDispatcherBase::OnReadWriteFailed
,
69 base::Unretained(this)));
71 event_handler_
->OnChannelInitialized(this);
74 void ChannelDispatcherBase::OnReadWriteFailed(int error
) {
75 event_handler_
->OnChannelError(this, CHANNEL_CONNECTION_ERROR
);
78 } // namespace protocol
79 } // namespace remoting