1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
|
// Copyright 2015 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef REMOTING_PROTOCOL_ICE_TRANSPORT_CHANNEL_H_
#define REMOTING_PROTOCOL_ICE_TRANSPORT_CHANNEL_H_
#include <string>
#include "base/functional/callback_forward.h"
#include "base/memory/raw_ptr.h"
#include "base/threading/thread_checker.h"
#include "base/timer/timer.h"
#include "remoting/protocol/network_settings.h"
#include "remoting/protocol/transport.h"
#include "third_party/webrtc/p2p/base/ice_transport_internal.h"
#include "third_party/webrtc/p2p/base/p2p_transport_channel.h"
#include "third_party/webrtc/p2p/base/packet_transport_internal.h"
#include "third_party/webrtc/rtc_base/third_party/sigslot/sigslot.h"
namespace remoting::protocol {
class P2PDatagramSocket;
class TransportContext;
class IceTransportChannel : public sigslot::has_slots<> {
public:
class Delegate {
public:
Delegate() {}
virtual ~Delegate() {}
// Called to pass ICE credentials to the session. Used only for STANDARD
// version of ICE, see SetIceVersion().
virtual void OnChannelIceCredentials(IceTransportChannel* transport,
const std::string& ufrag,
const std::string& password) = 0;
// Called when the transport generates a new candidate that needs
// to be passed to the AddRemoteCandidate() method on the remote
// end of the connection.
virtual void OnChannelCandidate(IceTransportChannel* transport,
const webrtc::Candidate& candidate) = 0;
// Called when transport route changes. Can be called even before
// the transport is connected.
virtual void OnChannelRouteChange(IceTransportChannel* transport,
const TransportRoute& route) = 0;
// Called when when the channel has failed to connect or reconnect.
virtual void OnChannelFailed(IceTransportChannel* transport) = 0;
// Called when the channel is about to be deleted.
virtual void OnChannelDeleted(IceTransportChannel* transport) = 0;
};
typedef base::OnceCallback<void(std::unique_ptr<P2PDatagramSocket>)>
ConnectedCallback;
IceTransportChannel(scoped_refptr<TransportContext> transport_context,
const NetworkSettings& network_settings);
IceTransportChannel(const IceTransportChannel&) = delete;
IceTransportChannel& operator=(const IceTransportChannel&) = delete;
~IceTransportChannel() override;
// Connects the channel and calls the |callback| after that.
void Connect(const std::string& name,
Delegate* delegate,
ConnectedCallback callback);
// Sets remote ICE credentials.
void SetRemoteCredentials(const std::string& ufrag,
const std::string& password);
// Adds |candidate| received from the peer.
void AddRemoteCandidate(const webrtc::Candidate& candidate);
// Name of the channel. Used to identify the channel and disambiguate
// candidates it generates from candidates generated by parallel connections.
const std::string& name() const;
// Returns true if the channel is already connected.
bool is_connected() const;
private:
void OnPortAllocatorCreated(
std::unique_ptr<webrtc::PortAllocator> port_allocator);
void NotifyConnected();
// Signal handlers for webrtc::IceTransportInternal.
void OnCandidateGathered(webrtc::IceTransportInternal* ice_transport,
const webrtc::Candidate& candidate);
void OnRouteChange(webrtc::IceTransportInternal* ice_transport,
const webrtc::Candidate& candidate);
void OnWritableState(webrtc::PacketTransportInternal* transport);
// Callback for TransportChannelSocketAdapter to notify when the socket is
// destroyed.
void OnChannelDestroyed();
void NotifyRouteChanged();
// Tries to connect by restarting ICE. Called by |reconnect_timer_|.
void TryReconnect();
scoped_refptr<TransportContext> transport_context_;
std::string name_;
raw_ptr<Delegate> delegate_ = nullptr;
ConnectedCallback callback_;
std::string ice_username_fragment_;
std::unique_ptr<webrtc::PortAllocator> port_allocator_;
std::string remote_ice_username_fragment_;
std::string remote_ice_password_;
std::list<webrtc::Candidate> pending_candidates_;
std::unique_ptr<webrtc::P2PTransportChannel> channel_;
int connect_attempts_left_;
base::RepeatingTimer reconnect_timer_;
NetworkSettings network_settings_;
THREAD_CHECKER(thread_checker_);
base::WeakPtrFactory<IceTransportChannel> weak_factory_{this};
};
} // namespace remoting::protocol
#endif // REMOTING_PROTOCOL_ICE_TRANSPORT_CHANNEL_H_
|