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 // 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
22 #ifndef REMOTING_PROTOCOL_TRANSPORT_H_
23 #define REMOTING_PROTOCOL_TRANSPORT_H_
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"
35 } // namespace cricket
45 class ChannelAuthenticator
;
47 enum class TransportRole
{
52 struct TransportRoute
{
59 // Helper method to get string representation of the type.
60 static std::string
GetTypeString(RouteType type
);
66 net::IPEndPoint remote_address
;
67 net::IPEndPoint local_address
;
70 class Transport
: public base::NonThreadSafe
{
75 virtual ~EventHandler() {};
77 // Called to pass ICE credentials to the session. Used only for STANDARD
78 // version of ICE, see SetIceVersion().
79 virtual void OnTransportIceCredentials(Transport
* transport
,
80 const std::string
& ufrag
,
81 const std::string
& password
) = 0;
83 // Called when the transport generates a new candidate that needs
84 // to be passed to the AddRemoteCandidate() method on the remote
85 // end of the connection.
86 virtual void OnTransportCandidate(Transport
* transport
,
87 const cricket::Candidate
& candidate
) = 0;
89 // Called when transport route changes. Can be called even before
90 // the transport is connected.
91 virtual void OnTransportRouteChange(Transport
* transport
,
92 const TransportRoute
& route
) = 0;
94 // Called when when the transport has failed to connect or reconnect.
95 virtual void OnTransportFailed(Transport
* transport
) = 0;
97 // Called when the transport is about to be deleted.
98 virtual void OnTransportDeleted(Transport
* transport
) = 0;
101 typedef base::Callback
<void(scoped_ptr
<net::Socket
>)> ConnectedCallback
;
104 virtual ~Transport() {}
106 // Connects the transport and calls the |callback| after that.
107 virtual void Connect(const std::string
& name
,
108 Transport::EventHandler
* event_handler
,
109 const ConnectedCallback
& callback
) = 0;
111 // Sets remote ICE credentials.
112 virtual void SetRemoteCredentials(const std::string
& ufrag
,
113 const std::string
& password
) = 0;
115 // Adds |candidate| received from the peer.
116 virtual void AddRemoteCandidate(const cricket::Candidate
& candidate
) = 0;
118 // Name of the channel. It is used to identify the channel and
119 // disambiguate candidates it generates from candidates generated by
120 // parallel connections.
121 virtual const std::string
& name() const = 0;
123 // Returns true if the channel is already connected.
124 virtual bool is_connected() const = 0;
126 // Sets ICE version for the transport.
128 // TODO(sergeyu): Remove this when support for legacy ICE is removed.
130 virtual void SetUseStandardIce(bool use_standard_ice
) {}
133 DISALLOW_COPY_AND_ASSIGN(Transport
);
136 class TransportFactory
{
138 TransportFactory() { }
139 virtual ~TransportFactory() { }
141 // Called to notify transport factory that a new transport might be created
142 // soon, e.g. when a new session is being created. Implementation may use it
143 // to start asynchronous preparation, e.g. fetch a new relay token if
144 // necessary while the session is being authenticated.
145 virtual void PrepareTokens() = 0;
147 virtual scoped_ptr
<Transport
> CreateTransport() = 0;
150 DISALLOW_COPY_AND_ASSIGN(TransportFactory
);
153 } // namespace protocol
154 } // namespace remoting
156 #endif // REMOTING_PROTOCOL_TRANSPORT_H_