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 #ifndef REMOTING_PROTOCOL_CHANNEL_DISPATCHER_BASE_H_
6 #define REMOTING_PROTOCOL_CHANNEL_DISPATCHER_BASE_H_
10 #include "base/basictypes.h"
11 #include "base/callback.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "remoting/base/buffered_socket_writer.h"
14 #include "remoting/protocol/errors.h"
15 #include "remoting/protocol/message_reader.h"
25 class StreamChannelFactory
;
28 // Base class for channel message dispatchers. It's responsible for
29 // creating the named channel. Derived dispatchers then dispatch
30 // incoming messages on this channel as well as send outgoing
32 class ChannelDispatcherBase
{
37 virtual ~EventHandler() {}
39 virtual void OnChannelInitialized(
40 ChannelDispatcherBase
* channel_dispatcher
) = 0;
41 virtual void OnChannelError(ChannelDispatcherBase
* channel_dispatcher
,
45 // The callback is called when initialization is finished. The
46 // parameter is set to true on success.
47 typedef base::Callback
<void(bool)> InitializedCallback
;
49 virtual ~ChannelDispatcherBase();
51 // Creates and connects the channel in the specified
52 // |session|. Caller retains ownership of the Session.
53 void Init(Session
* session
,
54 const ChannelConfig
& config
,
55 EventHandler
* event_handler
);
57 const std::string
& channel_name() { return channel_name_
; }
59 // Returns true if the channel is currently connected.
60 bool is_connected() { return channel_
!= nullptr; }
63 explicit ChannelDispatcherBase(const char* channel_name
);
65 BufferedSocketWriter
* writer() { return &writer_
; }
66 MessageReader
* reader() { return &reader_
; }
69 void OnChannelReady(scoped_ptr
<net::StreamSocket
> socket
);
70 void OnReadWriteFailed(int error
);
72 std::string channel_name_
;
73 StreamChannelFactory
* channel_factory_
;
74 EventHandler
* event_handler_
;
75 scoped_ptr
<net::StreamSocket
> channel_
;
77 BufferedSocketWriter writer_
;
78 MessageReader reader_
;
80 DISALLOW_COPY_AND_ASSIGN(ChannelDispatcherBase
);
83 } // namespace protocol
84 } // namespace remoting
86 #endif // REMOTING_PROTOCOL_CHANNEL_DISPATCHER_BASE_H_