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