| 6 | #include "EncryptedConnection.h" |
| 7 | #include "Instance.h" |
| 8 | #include "Message.h" |
| 9 | #include "Stats.h" |
| 10 |
| 11 | #include "rtc_base/copy_on_write_buffer.h" |
| 12 | #include "api/candidate.h" |
| 13 | #include "rtc_base/network_monitor_factory.h" |
| 14 |
| 15 | #include <functional> |
| 16 | #include <memory> |
| 17 |
| 18 | namespace rtc { |
| 19 | class BasicPacketSocketFactory; |
| 20 | class BasicNetworkManager; |
| 21 | class PacketTransportInternal; |
| 22 | struct NetworkRoute; |
| 23 | } // namespace rtc |
| 24 |
| 25 | namespace cricket { |
| 26 | class BasicPortAllocator; |
| 27 | class P2PTransportChannel; |
| 28 | class IceTransportInternal; |
| 29 | class RelayPortFactoryInterface; |
| 30 | } // namespace cricket |
| 31 |
| 32 | namespace webrtc { |
| 33 | class BasicAsyncResolverFactory; |
| 34 | class TurnCustomizer; |
| 35 | } // namespace webrtc |
| 36 |
| 37 | namespace tgcalls { |
| 38 |
| 39 | struct Message; |
| 40 |
| 41 | class NetworkManager : public sigslot::has_slots<>, public std::enable_shared_from_this<NetworkManager> { |
| 42 | public: |
| 43 | struct State { |
| 44 | bool isReadyToSendData = false; |
| 45 | bool isFailed = false; |
| 46 | }; |
| 47 |
| 48 | struct InterfaceTrafficStats { |
| 49 | int64_t incoming = 0; |
| 50 | int64_t outgoing = 0; |
| 51 | }; |
| 52 |
| 53 | NetworkManager( |
| 54 | rtc::Thread *thread, |
| 55 | EncryptionKey encryptionKey, |
| 56 | bool enableP2P, |
| 57 | bool enableTCP, |
| 58 | bool enableStunMarking, |
| 59 | std::vector<RtcServer> const &rtcServers, |
| 60 | std::unique_ptr<Proxy> proxy, |
| 61 | std::function<void(const State &)> stateUpdated, |
| 62 | std::function<void(DecryptedMessage &&)> transportMessageReceived, |
| 63 | std::function<void(Message &&)> sendSignalingMessage, |
| 64 | std::function<void(int delayMs, int cause)> sendTransportServiceAsync); |
| 65 | ~NetworkManager(); |
| 66 |
| 67 | void start(); |
| 68 | void receiveSignalingMessage(DecryptedMessage &&message); |
| 69 | uint32_t sendMessage(const Message &message); |
| 70 | void sendTransportService(int cause); |
| 71 | void setIsLocalNetworkLowCost(bool isLocalNetworkLowCost); |
| 72 | TrafficStats getNetworkStats(); |
| 73 | void fillCallStats(CallStats &callStats); |
| 74 | void logCurrentNetworkState(); |
| 75 |
| 76 | private: |
| 77 | void checkConnectionTimeout(); |
| 78 | void candidateGathered(cricket::IceTransportInternal *transport, const cricket::Candidate &candidate); |
| 79 | void candidateGatheringState(cricket::IceTransportInternal *transport); |
| 80 | void transportStateChanged(cricket::IceTransportInternal *transport); |
| 81 | void transportReadyToSend(cricket::IceTransportInternal *transport); |
| 82 | void transportPacketReceived(rtc::PacketTransportInternal *transport, const char *bytes, size_t size, const int64_t ×tamp, int unused); |
| 83 | void transportRouteChanged(absl::optional<rtc::NetworkRoute> route); |
| 84 | void addTrafficStats(int64_t byteCount, bool isIncoming); |
| 85 |
| 86 | rtc::Thread *_thread = nullptr; |
| 87 | bool _enableP2P = false; |
| 88 | bool _enableTCP = false; |
| 89 | bool _enableStunMarking = false; |
| 90 | std::vector<RtcServer> _rtcServers; |
| 91 | std::unique_ptr<Proxy> _proxy; |
| 92 | EncryptedConnection _transport; |
| 93 | bool _isOutgoing = false; |
| 94 | std::function<void(const NetworkManager::State &)> _stateUpdated; |
| 95 | std::function<void(DecryptedMessage &&)> _transportMessageReceived; |
| 96 | std::function<void(Message &&)> _sendSignalingMessage; |
| 97 |
| 98 | std::unique_ptr<rtc::NetworkMonitorFactory> _networkMonitorFactory; |
| 99 | std::unique_ptr<rtc::BasicPacketSocketFactory> _socketFactory; |
| 100 | std::unique_ptr<rtc::BasicNetworkManager> _networkManager; |
| 101 | std::unique_ptr<webrtc::TurnCustomizer> _turnCustomizer; |
| 102 | std::unique_ptr<cricket::RelayPortFactoryInterface> _relayPortFactory; |
| 103 | std::unique_ptr<cricket::BasicPortAllocator> _portAllocator; |
| 104 | std::unique_ptr<webrtc::BasicAsyncResolverFactory> _asyncResolverFactory; |
| 105 | std::unique_ptr<cricket::P2PTransportChannel> _transportChannel; |
| 106 |
| 107 | PeerIceParameters _localIceParameters; |
| 108 | absl::optional<PeerIceParameters> _remoteIceParameters; |
| 109 |
| 110 | bool _isLocalNetworkLowCost = false; |
| 111 | int64_t _lastNetworkActivityMs = 0; |
| 112 | InterfaceTrafficStats _trafficStatsWifi; |
| 113 | InterfaceTrafficStats _trafficStatsCellular; |
| 114 |
| 115 | absl::optional<CallStatsConnectionEndpointType> _currentEndpointType; |
| 116 | std::vector<CallStatsNetworkRecord> _networkRecords; |
| 117 | }; |
| 118 |
| 119 | } // namespace tgcalls |
| 120 |
| 121 | #endif |
| 122 |