ONC: Remove augmentation of unmanaged network properties.
[chromium-blink-merge.git] / remoting / protocol / transport.h
blobeb20e12a3c382c1a7218d566402595baaf24be10
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.
22 #ifndef REMOTING_PROTOCOL_TRANSPORT_H_
23 #define REMOTING_PROTOCOL_TRANSPORT_H_
25 #include <string>
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"
33 namespace cricket {
34 class Candidate;
35 } // namespace cricket
37 namespace net {
38 class Socket;
39 class StreamSocket;
40 } // namespace net
42 namespace remoting {
43 namespace protocol {
45 class ChannelAuthenticator;
47 struct TransportRoute {
48 enum RouteType {
49 DIRECT,
50 STUN,
51 RELAY,
54 // Helper method to get string representation of the type.
55 static std::string GetTypeString(RouteType type);
57 TransportRoute();
58 ~TransportRoute();
60 RouteType type;
61 net::IPEndPoint remote_address;
62 net::IPEndPoint local_address;
65 class Transport : public base::NonThreadSafe {
66 public:
67 class EventHandler {
68 public:
69 EventHandler() {};
70 virtual ~EventHandler() {};
72 // Called when the transport generates a new candidate that needs
73 // to be passed to the AddRemoteCandidate() method on the remote
74 // end of the connection.
75 virtual void OnTransportCandidate(Transport* transport,
76 const cricket::Candidate& candidate) = 0;
78 // Called when transport route changes. Can be called even before
79 // the transport is connected.
80 virtual void OnTransportRouteChange(Transport* transport,
81 const TransportRoute& route) = 0;
83 // Called when when the transport has failed to connect or reconnect.
84 virtual void OnTransportFailed(Transport* transport) = 0;
86 // Called when the transport is about to be deleted.
87 virtual void OnTransportDeleted(Transport* transport) = 0;
90 Transport() {}
91 virtual ~Transport() {}
93 // Intialize the transport with the specified parameters.
94 // |authenticator| is used to secure and authenticate the connection.
95 virtual void Initialize(const std::string& name,
96 Transport::EventHandler* event_handler,
97 scoped_ptr<ChannelAuthenticator> authenticator) = 0;
99 // Adds |candidate| received from the peer.
100 virtual void AddRemoteCandidate(const cricket::Candidate& candidate) = 0;
102 // Name of the channel. It is used to identify the channel and
103 // disambiguate candidates it generates from candidates generated by
104 // parallel connections.
105 virtual const std::string& name() const = 0;
107 // Returns true if the channel is already connected.
108 virtual bool is_connected() const = 0;
110 private:
111 DISALLOW_COPY_AND_ASSIGN(Transport);
114 class StreamTransport : public Transport {
115 public:
116 typedef base::Callback<void(scoped_ptr<net::StreamSocket>)> ConnectedCallback;
118 StreamTransport() { }
119 virtual ~StreamTransport() { }
121 virtual void Connect(const ConnectedCallback& callback) = 0;
123 private:
124 DISALLOW_COPY_AND_ASSIGN(StreamTransport);
127 class DatagramTransport : public Transport {
128 public:
129 typedef base::Callback<void(scoped_ptr<net::Socket>)> ConnectedCallback;
131 DatagramTransport() { }
132 virtual ~DatagramTransport() { }
134 virtual void Connect(const ConnectedCallback& callback) = 0;
136 private:
137 DISALLOW_COPY_AND_ASSIGN(DatagramTransport);
140 class TransportFactory {
141 public:
142 TransportFactory() { }
143 virtual ~TransportFactory() { }
145 // Called to notify transport factory that a new transport might be created
146 // soon, e.g. when a new session is being created. Implementation may use it
147 // to start asynchronous preparation, e.g. fetch a new relay token if
148 // necessary while the session is being authenticated.
149 virtual void PrepareTokens() = 0;
151 virtual scoped_ptr<StreamTransport> CreateStreamTransport() = 0;
152 virtual scoped_ptr<DatagramTransport> CreateDatagramTransport() = 0;
154 private:
155 DISALLOW_COPY_AND_ASSIGN(TransportFactory);
158 } // namespace protocol
159 } // namespace remoting
161 #endif // REMOTING_PROTOCOL_TRANSPORT_H_