sergeyu | a774906 | 2015-10-28 21:57:45 | [diff] [blame] | 1 | // Copyright 2015 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_ICE_TRANSPORT_CHANNEL_H_ |
| 6 | #define REMOTING_PROTOCOL_ICE_TRANSPORT_CHANNEL_H_ |
| 7 | |
| 8 | #include <string> |
| 9 | |
| 10 | #include "base/callback_forward.h" |
| 11 | #include "base/macros.h" |
| 12 | #include "base/threading/thread_checker.h" |
| 13 | #include "base/timer/timer.h" |
| 14 | #include "remoting/protocol/network_settings.h" |
| 15 | #include "remoting/protocol/transport.h" |
zhihuang | 1aa292f3 | 2017-01-03 19:37:51 | [diff] [blame] | 16 | // TODO(zhihuang):Replace #include by forward declaration once proper |
| 17 | // inheritance is defined for cricket::IceTransportInternal and |
| 18 | // cricket::P2PTransportChannel. |
Steve Anton | 0c3c2a8 | 2019-01-15 01:51:54 | [diff] [blame] | 19 | #include "third_party/webrtc/p2p/base/ice_transport_internal.h" |
sprang | d72236c | 2016-10-21 08:36:04 | [diff] [blame] | 20 | // TODO(johan): Replace #include by forward declaration once proper inheritance |
| 21 | // is defined for rtc::PacketTransportInterface and cricket::TransportChannel. |
Steve Anton | 0c3c2a8 | 2019-01-15 01:51:54 | [diff] [blame] | 22 | #include "third_party/webrtc/p2p/base/packet_transport_interface.h" |
Artem Titov | 68b05be | 2018-07-31 08:06:14 | [diff] [blame] | 23 | #include "third_party/webrtc/rtc_base/third_party/sigslot/sigslot.h" |
sprang | d72236c | 2016-10-21 08:36:04 | [diff] [blame] | 24 | |
sergeyu | a774906 | 2015-10-28 21:57:45 | [diff] [blame] | 25 | namespace cricket { |
| 26 | class Candidate; |
| 27 | class P2PTransportChannel; |
| 28 | class PortAllocator; |
sergeyu | a774906 | 2015-10-28 21:57:45 | [diff] [blame] | 29 | } // namespace cricket |
| 30 | |
| 31 | namespace remoting { |
| 32 | namespace protocol { |
| 33 | |
jbriance | e9b2ece | 2016-12-01 07:29:52 | [diff] [blame] | 34 | class P2PDatagramSocket; |
sergeyu | 86030f38 | 2015-12-16 22:45:00 | [diff] [blame] | 35 | class TransportContext; |
| 36 | |
sergeyu | a774906 | 2015-10-28 21:57:45 | [diff] [blame] | 37 | class IceTransportChannel : public sigslot::has_slots<> { |
| 38 | public: |
| 39 | class Delegate { |
| 40 | public: |
Nico Weber | 7452f1c | 2019-02-11 15:06:17 | [diff] [blame] | 41 | Delegate() {} |
| 42 | virtual ~Delegate() {} |
sergeyu | a774906 | 2015-10-28 21:57:45 | [diff] [blame] | 43 | |
| 44 | // Called to pass ICE credentials to the session. Used only for STANDARD |
| 45 | // version of ICE, see SetIceVersion(). |
sergeyu | e576760 | 2015-12-28 19:49:03 | [diff] [blame] | 46 | virtual void OnChannelIceCredentials(IceTransportChannel* transport, |
sergeyu | a774906 | 2015-10-28 21:57:45 | [diff] [blame] | 47 | const std::string& ufrag, |
| 48 | const std::string& password) = 0; |
| 49 | |
| 50 | // Called when the transport generates a new candidate that needs |
| 51 | // to be passed to the AddRemoteCandidate() method on the remote |
| 52 | // end of the connection. |
sergeyu | e576760 | 2015-12-28 19:49:03 | [diff] [blame] | 53 | virtual void OnChannelCandidate(IceTransportChannel* transport, |
sergeyu | a774906 | 2015-10-28 21:57:45 | [diff] [blame] | 54 | const cricket::Candidate& candidate) = 0; |
| 55 | |
| 56 | // Called when transport route changes. Can be called even before |
| 57 | // the transport is connected. |
sergeyu | e576760 | 2015-12-28 19:49:03 | [diff] [blame] | 58 | virtual void OnChannelRouteChange(IceTransportChannel* transport, |
sergeyu | a774906 | 2015-10-28 21:57:45 | [diff] [blame] | 59 | const TransportRoute& route) = 0; |
| 60 | |
sergeyu | e576760 | 2015-12-28 19:49:03 | [diff] [blame] | 61 | // Called when when the channel has failed to connect or reconnect. |
| 62 | virtual void OnChannelFailed(IceTransportChannel* transport) = 0; |
sergeyu | a774906 | 2015-10-28 21:57:45 | [diff] [blame] | 63 | |
sergeyu | e576760 | 2015-12-28 19:49:03 | [diff] [blame] | 64 | // Called when the channel is about to be deleted. |
| 65 | virtual void OnChannelDeleted(IceTransportChannel* transport) = 0; |
sergeyu | a774906 | 2015-10-28 21:57:45 | [diff] [blame] | 66 | }; |
| 67 | |
dcheng | 0765c49 | 2016-04-06 22:41:53 | [diff] [blame] | 68 | typedef base::Callback<void(std::unique_ptr<P2PDatagramSocket>)> |
| 69 | ConnectedCallback; |
sergeyu | a774906 | 2015-10-28 21:57:45 | [diff] [blame] | 70 | |
sergeyu | 86030f38 | 2015-12-16 22:45:00 | [diff] [blame] | 71 | explicit IceTransportChannel( |
| 72 | scoped_refptr<TransportContext> transport_context); |
sergeyu | a774906 | 2015-10-28 21:57:45 | [diff] [blame] | 73 | ~IceTransportChannel() override; |
| 74 | |
sergeyu | a774906 | 2015-10-28 21:57:45 | [diff] [blame] | 75 | // Connects the channel and calls the |callback| after that. |
| 76 | void Connect(const std::string& name, |
| 77 | Delegate* delegate, |
| 78 | const ConnectedCallback& callback); |
| 79 | |
| 80 | // Sets remote ICE credentials. |
| 81 | void SetRemoteCredentials(const std::string& ufrag, |
| 82 | const std::string& password); |
| 83 | |
| 84 | // Adds |candidate| received from the peer. |
| 85 | void AddRemoteCandidate(const cricket::Candidate& candidate); |
| 86 | |
| 87 | // Name of the channel. Used to identify the channel and disambiguate |
| 88 | // candidates it generates from candidates generated by parallel connections. |
| 89 | const std::string& name() const; |
| 90 | |
| 91 | // Returns true if the channel is already connected. |
| 92 | bool is_connected() const; |
| 93 | |
| 94 | private: |
sergeyu | 86030f38 | 2015-12-16 22:45:00 | [diff] [blame] | 95 | void OnPortAllocatorCreated( |
dcheng | 0765c49 | 2016-04-06 22:41:53 | [diff] [blame] | 96 | std::unique_ptr<cricket::PortAllocator> port_allocator); |
sergeyu | 86030f38 | 2015-12-16 22:45:00 | [diff] [blame] | 97 | |
sergeyu | a774906 | 2015-10-28 21:57:45 | [diff] [blame] | 98 | void NotifyConnected(); |
| 99 | |
zhihuang | 1aa292f3 | 2017-01-03 19:37:51 | [diff] [blame] | 100 | // Signal handlers for cricket::IceTransportInternal. |
zhihuang | f6a93f2 | 2017-01-16 02:11:12 | [diff] [blame] | 101 | void OnCandidateGathered(cricket::IceTransportInternal* ice_transport, |
sergeyu | a774906 | 2015-10-28 21:57:45 | [diff] [blame] | 102 | const cricket::Candidate& candidate); |
zhihuang | 1aa292f3 | 2017-01-03 19:37:51 | [diff] [blame] | 103 | void OnRouteChange(cricket::IceTransportInternal* ice_transport, |
sergeyu | a774906 | 2015-10-28 21:57:45 | [diff] [blame] | 104 | const cricket::Candidate& candidate); |
sprang | d72236c | 2016-10-21 08:36:04 | [diff] [blame] | 105 | void OnWritableState(rtc::PacketTransportInterface* transport); |
sergeyu | a774906 | 2015-10-28 21:57:45 | [diff] [blame] | 106 | |
| 107 | // Callback for TransportChannelSocketAdapter to notify when the socket is |
| 108 | // destroyed. |
| 109 | void OnChannelDestroyed(); |
| 110 | |
| 111 | void NotifyRouteChanged(); |
| 112 | |
| 113 | // Tries to connect by restarting ICE. Called by |reconnect_timer_|. |
| 114 | void TryReconnect(); |
| 115 | |
sergeyu | 86030f38 | 2015-12-16 22:45:00 | [diff] [blame] | 116 | scoped_refptr<TransportContext> transport_context_; |
sergeyu | a774906 | 2015-10-28 21:57:45 | [diff] [blame] | 117 | |
| 118 | std::string name_; |
sergeyu | 86030f38 | 2015-12-16 22:45:00 | [diff] [blame] | 119 | Delegate* delegate_ = nullptr; |
sergeyu | a774906 | 2015-10-28 21:57:45 | [diff] [blame] | 120 | ConnectedCallback callback_; |
| 121 | std::string ice_username_fragment_; |
| 122 | |
dcheng | 0765c49 | 2016-04-06 22:41:53 | [diff] [blame] | 123 | std::unique_ptr<cricket::PortAllocator> port_allocator_; |
sergeyu | a774906 | 2015-10-28 21:57:45 | [diff] [blame] | 124 | |
| 125 | std::string remote_ice_username_fragment_; |
| 126 | std::string remote_ice_password_; |
| 127 | std::list<cricket::Candidate> pending_candidates_; |
dcheng | 0765c49 | 2016-04-06 22:41:53 | [diff] [blame] | 128 | std::unique_ptr<cricket::P2PTransportChannel> channel_; |
sergeyu | a774906 | 2015-10-28 21:57:45 | [diff] [blame] | 129 | int connect_attempts_left_; |
| 130 | base::RepeatingTimer reconnect_timer_; |
| 131 | |
| 132 | base::ThreadChecker thread_checker_; |
| 133 | |
Jeremy Roman | 7c5cfabd | 2019-08-12 15:45:27 | [diff] [blame] | 134 | base::WeakPtrFactory<IceTransportChannel> weak_factory_{this}; |
sergeyu | a774906 | 2015-10-28 21:57:45 | [diff] [blame] | 135 | |
| 136 | DISALLOW_COPY_AND_ASSIGN(IceTransportChannel); |
| 137 | }; |
| 138 | |
| 139 | } // namespace protocol |
| 140 | } // namespace remoting |
| 141 | |
| 142 | #endif // REMOTING_PROTOCOL_ICE_TRANSPORT_CHANNEL_H_ |