| 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 #ifndef REMOTING_PROTOCOL_CHANNEL_DISPATCHER_BASE_H_ | 5 #ifndef REMOTING_PROTOCOL_CHANNEL_DISPATCHER_BASE_H_ |
| 6 #define REMOTING_PROTOCOL_CHANNEL_DISPATCHER_BASE_H_ | 6 #define REMOTING_PROTOCOL_CHANNEL_DISPATCHER_BASE_H_ |
| 7 | 7 |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
| 11 #include "base/callback.h" | 11 #include "base/callback.h" |
| 12 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
| 13 #include "remoting/protocol/buffered_socket_writer.h" |
| 14 #include "remoting/protocol/errors.h" |
| 15 #include "remoting/protocol/message_reader.h" |
| 13 | 16 |
| 14 namespace net { | 17 namespace net { |
| 15 class StreamSocket; | 18 class StreamSocket; |
| 16 } // namespace net | 19 } // namespace net |
| 17 | 20 |
| 18 namespace remoting { | 21 namespace remoting { |
| 19 namespace protocol { | 22 namespace protocol { |
| 20 | 23 |
| 21 struct ChannelConfig; | 24 struct ChannelConfig; |
| 22 class StreamChannelFactory; | 25 class StreamChannelFactory; |
| 23 class Session; | 26 class Session; |
| 24 | 27 |
| 25 // Base class for channel message dispatchers. It's responsible for | 28 // Base class for channel message dispatchers. It's responsible for |
| 26 // creating the named channel. Derived dispatchers then dispatch | 29 // creating the named channel. Derived dispatchers then dispatch |
| 27 // incoming messages on this channel as well as send outgoing | 30 // incoming messages on this channel as well as send outgoing |
| 28 // messages. | 31 // messages. |
| 29 class ChannelDispatcherBase { | 32 class ChannelDispatcherBase { |
| 30 public: | 33 public: |
| 34 class EventHandler { |
| 35 public: |
| 36 EventHandler() {} |
| 37 virtual ~EventHandler() {} |
| 38 |
| 39 virtual void OnChannelInitialized( |
| 40 ChannelDispatcherBase* channel_dispatcher) = 0; |
| 41 virtual void OnChannelError(ChannelDispatcherBase* channel_dispatcher, |
| 42 ErrorCode error) = 0; |
| 43 }; |
| 44 |
| 31 // The callback is called when initialization is finished. The | 45 // The callback is called when initialization is finished. The |
| 32 // parameter is set to true on success. | 46 // parameter is set to true on success. |
| 33 typedef base::Callback<void(bool)> InitializedCallback; | 47 typedef base::Callback<void(bool)> InitializedCallback; |
| 34 | 48 |
| 35 virtual ~ChannelDispatcherBase(); | 49 virtual ~ChannelDispatcherBase(); |
| 36 | 50 |
| 37 // Creates and connects the channel in the specified | 51 // Creates and connects the channel in the specified |
| 38 // |session|. Caller retains ownership of the Session. | 52 // |session|. Caller retains ownership of the Session. |
| 39 void Init(Session* session, | 53 void Init(Session* session, |
| 40 const ChannelConfig& config, | 54 const ChannelConfig& config, |
| 41 const InitializedCallback& callback); | 55 EventHandler* event_handler); |
| 56 |
| 57 const std::string& channel_name() { return channel_name_; } |
| 42 | 58 |
| 43 // Returns true if the channel is currently connected. | 59 // Returns true if the channel is currently connected. |
| 44 bool is_connected() { return channel() != nullptr; } | 60 bool is_connected() { return channel_ != nullptr; } |
| 45 | 61 |
| 46 protected: | 62 protected: |
| 47 explicit ChannelDispatcherBase(const char* channel_name); | 63 explicit ChannelDispatcherBase(const char* channel_name); |
| 48 | 64 |
| 49 net::StreamSocket* channel() { return channel_.get(); } | 65 BufferedSocketWriter* writer() { return &writer_; } |
| 50 | 66 MessageReader* reader() { return &reader_; } |
| 51 // Called when channel is initialized. Must be overriden in the | |
| 52 // child classes. Should not delete the dispatcher. | |
| 53 virtual void OnInitialized() = 0; | |
| 54 | 67 |
| 55 private: | 68 private: |
| 56 void OnChannelReady(scoped_ptr<net::StreamSocket> socket); | 69 void OnChannelReady(scoped_ptr<net::StreamSocket> socket); |
| 70 void OnWriteFailed(int error); |
| 57 | 71 |
| 58 std::string channel_name_; | 72 std::string channel_name_; |
| 59 StreamChannelFactory* channel_factory_; | 73 StreamChannelFactory* channel_factory_; |
| 60 InitializedCallback initialized_callback_; | 74 EventHandler* event_handler_; |
| 61 scoped_ptr<net::StreamSocket> channel_; | 75 scoped_ptr<net::StreamSocket> channel_; |
| 62 | 76 |
| 77 BufferedSocketWriter writer_; |
| 78 MessageReader reader_; |
| 79 |
| 63 DISALLOW_COPY_AND_ASSIGN(ChannelDispatcherBase); | 80 DISALLOW_COPY_AND_ASSIGN(ChannelDispatcherBase); |
| 64 }; | 81 }; |
| 65 | 82 |
| 66 } // namespace protocol | 83 } // namespace protocol |
| 67 } // namespace remoting | 84 } // namespace remoting |
| 68 | 85 |
| 69 #endif // REMOTING_PROTOCOL_CHANNEL_DISPATCHER_BASE_H_ | 86 #endif // REMOTING_PROTOCOL_CHANNEL_DISPATCHER_BASE_H_ |
| OLD | NEW |