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 // This file defines the interface for peer-to-peer transport. There
      6 // are two types of transport: StreamTransport and DatagramTransport.
      7 // They must both be created using TransportFactory instances and they
      8 // provide the same interface, except that one should be used for
      9 // reliable stream connection and the other one for unreliable
     10 // datagram connection. The Transport interface itself doesn't provide
     11 // methods to send/receive data. Instead it creates an instance of
     12 // net::Socket or net::SocketStream which provides access to the data
     13 // channel. After a new transport is Initialize()'ed the Connect()
     14 // method must be called. Connect() starts asynchronous creation and
     15 // initialization of the connection socket that can be used later to
     16 // send and receive data. The socket is passed to the callback
     17 // specified in the Connect() call. The Transport object must exist
     18 // during the whole lifetime of the connection socket. Later deletion
     19 // of the connection socket causes teardown of the corresponding
     20 // Transport object.
     21 
     22 #ifndef REMOTING_PROTOCOL_TRANSPORT_H_
     23 #define REMOTING_PROTOCOL_TRANSPORT_H_
     24 
     25 #include <string>
     26 
     27 #include "base/basictypes.h"
     28 #include "base/callback_forward.h"
     29 #include "base/memory/scoped_ptr.h"
     30 #include "base/threading/non_thread_safe.h"
     31 #include "net/base/ip_endpoint.h"
     32 
     33 namespace cricket {
     34 class Candidate;
     35 }  // namespace cricket
     36 
     37 namespace net {
     38 class Socket;
     39 class StreamSocket;
     40 }  // namespace net
     41 
     42 namespace remoting {
     43 namespace protocol {
     44 
     45 class ChannelAuthenticator;
     46 struct TransportConfig;
     47 
     48 struct TransportRoute {
     49   enum RouteType {
     50     DIRECT,
     51     STUN,
     52     RELAY,
     53   };
     54 
     55   // Helper method to get string representation of the type.
     56   static std::string GetTypeString(RouteType type);
     57 
     58   TransportRoute();
     59   ~TransportRoute();
     60 
     61   RouteType type;
     62   net::IPEndPoint remote_address;
     63   net::IPEndPoint local_address;
     64 };
     65 
     66 class Transport : public base::NonThreadSafe {
     67  public:
     68   class EventHandler {
     69    public:
     70     EventHandler() {};
     71     virtual ~EventHandler() {};
     72 
     73     // Called when the transport generates a new candidate that needs
     74     // to be passed to the AddRemoteCandidate() method on the remote
     75     // end of the connection.
     76     virtual void OnTransportCandidate(Transport* transport,
     77                                       const cricket::Candidate& candidate) = 0;
     78 
     79     // Called when transport route changes. Can be called even before
     80     // the transport is connected.
     81     virtual void OnTransportRouteChange(Transport* transport,
     82                                         const TransportRoute& route) = 0;
     83 
     84     // Called when the transport inactivity state changes. When
     85     // |ready| is set to false incoming and outgoing data may be
     86     // delayed until connection goes back to the active state, at
     87     // which point that method is called again with |ready| set to
     88     // true. This is useful for UI indication of temporarily broken
     89     // connections.
     90     virtual void OnTransportReady(Transport* transport, bool ready) = 0;
     91 
     92     // Called when when the transport has failed to connect or reconnect.
     93     virtual void OnTransportFailed(Transport* transport) = 0;
     94 
     95     // Called when the transport is about to be deleted.
     96     virtual void OnTransportDeleted(Transport* transport) = 0;
     97   };
     98 
     99   Transport() {}
    100   virtual ~Transport() {}
    101 
    102   // Intialize the transport with the specified parameters.
    103   // |authenticator| is used to secure and authenticate the connection.
    104   virtual void Initialize(const std::string& name,
    105                           Transport::EventHandler* event_handler,
    106                           scoped_ptr<ChannelAuthenticator> authenticator) = 0;
    107 
    108   // Adds |candidate| received from the peer.
    109   virtual void AddRemoteCandidate(const cricket::Candidate& candidate) = 0;
    110 
    111   // Name of the channel. It is used to identify the channel and
    112   // disambiguate candidates it generates from candidates generated by
    113   // parallel connections.
    114   virtual const std::string& name() const = 0;
    115 
    116   // Returns true if the channel is already connected.
    117   virtual bool is_connected() const = 0;
    118 
    119  private:
    120   DISALLOW_COPY_AND_ASSIGN(Transport);
    121 };
    122 
    123 class StreamTransport : public Transport {
    124  public:
    125   typedef base::Callback<void(scoped_ptr<net::StreamSocket>)> ConnectedCallback;
    126 
    127   StreamTransport() { }
    128   virtual ~StreamTransport() { }
    129 
    130   virtual void Connect(const ConnectedCallback& callback) = 0;
    131 
    132  private:
    133   DISALLOW_COPY_AND_ASSIGN(StreamTransport);
    134 };
    135 
    136 class DatagramTransport : public Transport {
    137  public:
    138   typedef base::Callback<void(scoped_ptr<net::Socket>)> ConnectedCallback;
    139 
    140   DatagramTransport() { }
    141   virtual ~DatagramTransport() { }
    142 
    143   virtual void Connect(const ConnectedCallback& callback) = 0;
    144 
    145  private:
    146   DISALLOW_COPY_AND_ASSIGN(DatagramTransport);
    147 };
    148 
    149 class TransportFactory {
    150  public:
    151   TransportFactory() { }
    152   virtual ~TransportFactory() { }
    153 
    154   // Sets configuration for the transports created by this factory.
    155   virtual void SetTransportConfig(const TransportConfig& config) = 0;
    156 
    157   virtual scoped_ptr<StreamTransport> CreateStreamTransport() = 0;
    158   virtual scoped_ptr<DatagramTransport> CreateDatagramTransport() = 0;
    159 
    160  private:
    161   DISALLOW_COPY_AND_ASSIGN(TransportFactory);
    162 };
    163 
    164 }  // namespace protocol
    165 }  // namespace remoting
    166 
    167 #endif  // REMOTING_PROTOCOL_TRANSPORT_H_
    168