Home | History | Annotate | Download | only in protocol
      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.
      4 
      5 #ifndef REMOTING_PROTOCOL_CONNECTION_TO_CLIENT_H_
      6 #define REMOTING_PROTOCOL_CONNECTION_TO_CLIENT_H_
      7 
      8 #include <deque>
      9 #include <string>
     10 #include <vector>
     11 
     12 #include "base/memory/scoped_ptr.h"
     13 #include "base/synchronization/lock.h"
     14 #include "base/threading/non_thread_safe.h"
     15 #include "remoting/protocol/audio_writer.h"
     16 #include "remoting/protocol/session.h"
     17 #include "remoting/protocol/video_writer.h"
     18 
     19 namespace net {
     20 class IPEndPoint;
     21 }  // namespace net
     22 
     23 namespace remoting {
     24 namespace protocol {
     25 
     26 class ClientStub;
     27 class ClipboardStub;
     28 class HostStub;
     29 class InputStub;
     30 class HostControlDispatcher;
     31 class HostEventDispatcher;
     32 
     33 // This class represents a remote viewer connection to the chromoting
     34 // host. It sets up all protocol channels and connects them to the
     35 // stubs.
     36 class ConnectionToClient : public base::NonThreadSafe,
     37                            public Session::EventHandler {
     38  public:
     39   class EventHandler {
     40    public:
     41     // Called when the network connection is authenticated.
     42     virtual void OnConnectionAuthenticated(ConnectionToClient* connection) = 0;
     43 
     44     // Called when the network connection is authenticated and all
     45     // channels are connected.
     46     virtual void OnConnectionChannelsConnected(
     47         ConnectionToClient* connection) = 0;
     48 
     49     // Called when the network connection is closed or failed.
     50     virtual void OnConnectionClosed(ConnectionToClient* connection,
     51                                     ErrorCode error) = 0;
     52 
     53     // Called when sequence number is updated.
     54     virtual void OnSequenceNumberUpdated(ConnectionToClient* connection,
     55                                          int64 sequence_number) = 0;
     56 
     57     // Called on notification of a route change event, which happens when a
     58     // channel is connected.
     59     virtual void OnRouteChange(ConnectionToClient* connection,
     60                                const std::string& channel_name,
     61                                const TransportRoute& route) = 0;
     62 
     63    protected:
     64     virtual ~EventHandler() {}
     65   };
     66 
     67   // Constructs a ConnectionToClient object for the |session|. Takes
     68   // ownership of |session|.
     69   explicit ConnectionToClient(Session* session);
     70   virtual ~ConnectionToClient();
     71 
     72   // Set |event_handler| for connection events. Must be called once when this
     73   // object is created.
     74   void SetEventHandler(EventHandler* event_handler);
     75 
     76   // Returns the connection in use.
     77   virtual Session* session();
     78 
     79   // Disconnect the client connection.
     80   virtual void Disconnect();
     81 
     82   // Update the sequence number when received from the client. EventHandler
     83   // will be called.
     84   virtual void UpdateSequenceNumber(int64 sequence_number);
     85 
     86   // Get the stubs used by the host to transmit messages to the client.
     87   // The stubs must not be accessed before OnConnectionAuthenticated(), or
     88   // after OnConnectionClosed().
     89   // Note that the audio stub will be NULL if audio is not enabled.
     90   virtual VideoStub* video_stub();
     91   virtual AudioStub* audio_stub();
     92   virtual ClientStub* client_stub();
     93 
     94   // Set/get the stubs which will handle messages we receive from the client.
     95   // All stubs MUST be set before the session's channels become connected.
     96   virtual void set_clipboard_stub(ClipboardStub* clipboard_stub);
     97   virtual ClipboardStub* clipboard_stub();
     98   virtual void set_host_stub(HostStub* host_stub);
     99   virtual HostStub* host_stub();
    100   virtual void set_input_stub(InputStub* input_stub);
    101   virtual InputStub* input_stub();
    102 
    103   // Session::EventHandler interface.
    104   virtual void OnSessionStateChange(Session::State state) OVERRIDE;
    105   virtual void OnSessionRouteChange(const std::string& channel_name,
    106                                     const TransportRoute& route) OVERRIDE;
    107 
    108  private:
    109   // Callback for channel initialization.
    110   void OnChannelInitialized(bool successful);
    111 
    112   void NotifyIfChannelsReady();
    113 
    114   void Close(ErrorCode error);
    115 
    116   // Stops writing in the channels.
    117   void CloseChannels();
    118 
    119   // Event handler for handling events sent from this object.
    120   EventHandler* handler_;
    121 
    122   // Stubs that are called for incoming messages.
    123   ClipboardStub* clipboard_stub_;
    124   HostStub* host_stub_;
    125   InputStub* input_stub_;
    126 
    127   // The libjingle channel used to send and receive data from the remote client.
    128   scoped_ptr<Session> session_;
    129 
    130   scoped_ptr<HostControlDispatcher> control_dispatcher_;
    131   scoped_ptr<HostEventDispatcher> event_dispatcher_;
    132   scoped_ptr<VideoWriter> video_writer_;
    133   scoped_ptr<AudioWriter> audio_writer_;
    134 
    135   DISALLOW_COPY_AND_ASSIGN(ConnectionToClient);
    136 };
    137 
    138 }  // namespace protocol
    139 }  // namespace remoting
    140 
    141 #endif  // REMOTING_PROTOCOL_CONNECTION_TO_CLIENT_H_
    142