123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483 |
- #ifndef P2P_BASE_CONNECTION_H_
- #define P2P_BASE_CONNECTION_H_
- #include <memory>
- #include <string>
- #include <vector>
- #include "absl/types/optional.h"
- #include "api/candidate.h"
- #include "api/transport/stun.h"
- #include "logging/rtc_event_log/ice_logger.h"
- #include "p2p/base/candidate_pair_interface.h"
- #include "p2p/base/connection_info.h"
- #include "p2p/base/p2p_transport_channel_ice_field_trials.h"
- #include "p2p/base/stun_request.h"
- #include "p2p/base/transport_description.h"
- #include "rtc_base/async_packet_socket.h"
- #include "rtc_base/message_handler.h"
- #include "rtc_base/network.h"
- #include "rtc_base/numerics/event_based_exponential_moving_average.h"
- #include "rtc_base/rate_tracker.h"
- namespace cricket {
- constexpr int kGoogPingVersion = 1;
- class Port;
- class Connection;
- struct CandidatePair final : public CandidatePairInterface {
- ~CandidatePair() override = default;
- const Candidate& local_candidate() const override { return local; }
- const Candidate& remote_candidate() const override { return remote; }
- Candidate local;
- Candidate remote;
- };
- class ConnectionRequest : public StunRequest {
- public:
- explicit ConnectionRequest(Connection* connection);
- void Prepare(StunMessage* request) override;
- void OnResponse(StunMessage* response) override;
- void OnErrorResponse(StunMessage* response) override;
- void OnTimeout() override;
- void OnSent() override;
- int resend_delay() override;
- private:
- Connection* const connection_;
- };
- class Connection : public CandidatePairInterface,
- public rtc::MessageHandlerAutoCleanup,
- public sigslot::has_slots<> {
- public:
- struct SentPing {
- SentPing(const std::string id, int64_t sent_time, uint32_t nomination)
- : id(id), sent_time(sent_time), nomination(nomination) {}
- std::string id;
- int64_t sent_time;
- uint32_t nomination;
- };
- ~Connection() override;
-
- uint32_t id() const { return id_; }
-
-
- const Candidate& local_candidate() const override;
-
- const Candidate& remote_candidate() const override;
-
- virtual const rtc::Network* network() const;
-
- virtual int generation() const;
-
- virtual uint64_t priority() const;
- enum WriteState {
- STATE_WRITABLE = 0,
- STATE_WRITE_UNRELIABLE = 1,
- STATE_WRITE_INIT = 2,
- STATE_WRITE_TIMEOUT = 3,
- };
- WriteState write_state() const { return write_state_; }
- bool writable() const { return write_state_ == STATE_WRITABLE; }
- bool receiving() const { return receiving_; }
-
-
- bool connected() const { return connected_; }
- bool weak() const { return !(writable() && receiving() && connected()); }
- bool active() const { return write_state_ != STATE_WRITE_TIMEOUT; }
-
- bool dead(int64_t now) const;
-
- int rtt() const { return rtt_; }
- int unwritable_timeout() const;
- void set_unwritable_timeout(const absl::optional<int>& value_ms) {
- unwritable_timeout_ = value_ms;
- }
- int unwritable_min_checks() const;
- void set_unwritable_min_checks(const absl::optional<int>& value) {
- unwritable_min_checks_ = value;
- }
- int inactive_timeout() const;
- void set_inactive_timeout(const absl::optional<int>& value) {
- inactive_timeout_ = value;
- }
-
-
- ConnectionInfo stats();
- sigslot::signal1<Connection*> SignalStateChange;
-
-
- sigslot::signal1<Connection*> SignalDestroyed;
-
-
-
- virtual int Send(const void* data,
- size_t size,
- const rtc::PacketOptions& options) = 0;
-
- virtual int GetError() = 0;
- sigslot::signal4<Connection*, const char*, size_t, int64_t> SignalReadPacket;
- sigslot::signal1<Connection*> SignalReadyToSend;
-
- void OnReadPacket(const char* data, size_t size, int64_t packet_time_us);
-
- void OnReadyToSend();
-
-
-
-
- bool pruned() const { return pruned_; }
- void Prune();
- bool use_candidate_attr() const { return use_candidate_attr_; }
- void set_use_candidate_attr(bool enable);
- void set_nomination(uint32_t value) { nomination_ = value; }
- uint32_t remote_nomination() const { return remote_nomination_; }
-
-
-
-
-
-
-
- bool nominated() const { return acked_nomination_ || remote_nomination_; }
- void set_remote_ice_mode(IceMode mode) { remote_ice_mode_ = mode; }
- int receiving_timeout() const;
- void set_receiving_timeout(absl::optional<int> receiving_timeout_ms) {
- receiving_timeout_ = receiving_timeout_ms;
- }
-
- void Destroy();
-
- void FailAndDestroy();
-
-
- void FailAndPrune();
-
-
- void UpdateState(int64_t now);
-
- int64_t last_ping_sent() const { return last_ping_sent_; }
- void Ping(int64_t now);
- void ReceivedPingResponse(
- int rtt,
- const std::string& request_id,
- const absl::optional<uint32_t>& nomination = absl::nullopt);
- int64_t last_ping_response_received() const {
- return last_ping_response_received_;
- }
- const absl::optional<std::string>& last_ping_id_received() const {
- return last_ping_id_received_;
- }
-
- int rtt_samples() const { return rtt_samples_; }
-
-
- int64_t last_ping_received() const { return last_ping_received_; }
- void ReceivedPing(
- const absl::optional<std::string>& request_id = absl::nullopt);
-
- void HandleStunBindingOrGoogPingRequest(IceMessage* msg);
-
-
-
- void HandlePiggybackCheckAcknowledgementIfAny(StunMessage* msg);
- int64_t last_data_received() const { return last_data_received_; }
-
- std::string ToDebugId() const;
- std::string ToString() const;
- std::string ToSensitiveString() const;
-
- const webrtc::IceCandidatePairDescription& ToLogDescription();
- void set_ice_event_log(webrtc::IceEventLog* ice_event_log) {
- ice_event_log_ = ice_event_log;
- }
-
- void PrintPingsSinceLastResponse(std::string* pings, size_t max);
- bool reported() const { return reported_; }
- void set_reported(bool reported) { reported_ = reported; }
-
-
-
- bool selected() const { return selected_; }
- void set_selected(bool selected) { selected_ = selected; }
-
-
- sigslot::signal1<Connection*> SignalNominated;
-
- void HandleRoleConflictFromPeer();
- IceCandidatePairState state() const { return state_; }
- int num_pings_sent() const { return num_pings_sent_; }
- IceMode remote_ice_mode() const { return remote_ice_mode_; }
- uint32_t ComputeNetworkCost() const;
-
-
-
- void MaybeSetRemoteIceParametersAndGeneration(const IceParameters& params,
- int generation);
-
-
-
- void MaybeUpdatePeerReflexiveCandidate(const Candidate& new_candidate);
-
-
- int64_t last_received() const;
-
- int64_t receiving_unchanged_since() const {
- return receiving_unchanged_since_;
- }
- bool stable(int64_t now) const;
-
- bool TooManyOutstandingPings(const absl::optional<int>& val) const;
- void SetIceFieldTrials(const IceFieldTrials* field_trials);
- const rtc::EventBasedExponentialMovingAverage& GetRttEstimate() const {
- return rtt_estimate_;
- }
-
-
-
-
-
-
-
-
-
-
-
-
- void ForgetLearnedState();
- void SendStunBindingResponse(const StunMessage* request);
- void SendGoogPingResponse(const StunMessage* request);
- void SendResponseMessage(const StunMessage& response);
-
- Port* PortForTest() { return port_; }
- const Port* PortForTest() const { return port_; }
-
- uint32_t acked_nomination() const { return acked_nomination_; }
-
- void set_remote_nomination(uint32_t remote_nomination) {
- remote_nomination_ = remote_nomination;
- }
- protected:
- enum { MSG_DELETE = 0, MSG_FIRST_AVAILABLE };
-
- Connection(Port* port, size_t index, const Candidate& candidate);
-
- void OnSendStunPacket(const void* data, size_t size, StunRequest* req);
-
- virtual void OnConnectionRequestResponse(ConnectionRequest* req,
- StunMessage* response);
- void OnConnectionRequestErrorResponse(ConnectionRequest* req,
- StunMessage* response);
- void OnConnectionRequestTimeout(ConnectionRequest* req);
- void OnConnectionRequestSent(ConnectionRequest* req);
- bool rtt_converged() const;
-
-
- bool missing_responses(int64_t now) const;
-
- void set_write_state(WriteState value);
- void UpdateReceiving(int64_t now);
- void set_state(IceCandidatePairState state);
- void set_connected(bool value);
- uint32_t nomination() const { return nomination_; }
- void OnMessage(rtc::Message* pmsg) override;
-
- Port* port() { return port_; }
- const Port* port() const { return port_; }
- uint32_t id_;
- Port* port_;
- size_t local_candidate_index_;
- Candidate remote_candidate_;
- ConnectionInfo stats_;
- rtc::RateTracker recv_rate_tracker_;
- rtc::RateTracker send_rate_tracker_;
- private:
-
-
- void MaybeUpdateLocalCandidate(ConnectionRequest* request,
- StunMessage* response);
- void LogCandidatePairConfig(webrtc::IceCandidatePairConfigType type);
- void LogCandidatePairEvent(webrtc::IceCandidatePairEventType type,
- uint32_t transaction_id);
-
-
- bool ShouldSendGoogPing(const StunMessage* message);
- WriteState write_state_;
- bool receiving_;
- bool connected_;
- bool pruned_;
- bool selected_ = false;
-
-
-
-
- bool use_candidate_attr_;
-
-
-
-
-
- uint32_t nomination_ = 0;
-
- uint32_t acked_nomination_ = 0;
-
-
-
- uint32_t remote_nomination_ = 0;
- IceMode remote_ice_mode_;
- StunRequestManager requests_;
- int rtt_;
- int rtt_samples_ = 0;
-
- uint64_t total_round_trip_time_ms_ = 0;
-
- absl::optional<uint32_t> current_round_trip_time_ms_;
- int64_t last_ping_sent_;
- int64_t last_ping_received_;
-
- int64_t last_data_received_;
- int64_t last_ping_response_received_;
- int64_t receiving_unchanged_since_ = 0;
- std::vector<SentPing> pings_since_last_response_;
-
-
- absl::optional<std::string> last_ping_id_received_;
- absl::optional<int> unwritable_timeout_;
- absl::optional<int> unwritable_min_checks_;
- absl::optional<int> inactive_timeout_;
- bool reported_;
- IceCandidatePairState state_;
-
- absl::optional<int> receiving_timeout_;
- int64_t time_created_ms_;
- int num_pings_sent_ = 0;
- absl::optional<webrtc::IceCandidatePairDescription> log_description_;
- webrtc::IceEventLog* ice_event_log_ = nullptr;
-
-
-
-
- absl::optional<bool> remote_support_goog_ping_;
- std::unique_ptr<StunMessage> cached_stun_binding_;
- const IceFieldTrials* field_trials_;
- rtc::EventBasedExponentialMovingAverage rtt_estimate_;
- friend class Port;
- friend class ConnectionRequest;
- friend class P2PTransportChannel;
- };
- class ProxyConnection : public Connection {
- public:
- ProxyConnection(Port* port, size_t index, const Candidate& remote_candidate);
- int Send(const void* data,
- size_t size,
- const rtc::PacketOptions& options) override;
- int GetError() override;
- private:
- int error_ = 0;
- };
- }
- #endif
|