2020-08-14 18:58:22 +02:00
|
|
|
#ifndef TGCALLS_NETWORK_MANAGER_H
|
|
|
|
#define TGCALLS_NETWORK_MANAGER_H
|
|
|
|
|
|
|
|
#include "rtc_base/thread.h"
|
|
|
|
|
|
|
|
#include "EncryptedConnection.h"
|
|
|
|
#include "Instance.h"
|
|
|
|
#include "Message.h"
|
2020-10-01 03:59:32 +02:00
|
|
|
#include "Stats.h"
|
2020-08-14 18:58:22 +02:00
|
|
|
|
|
|
|
#include "rtc_base/copy_on_write_buffer.h"
|
|
|
|
#include "api/candidate.h"
|
2021-06-25 02:43:10 +02:00
|
|
|
#include "rtc_base/network_monitor_factory.h"
|
2020-08-14 18:58:22 +02:00
|
|
|
|
|
|
|
#include <functional>
|
|
|
|
#include <memory>
|
|
|
|
|
|
|
|
namespace rtc {
|
|
|
|
class BasicPacketSocketFactory;
|
|
|
|
class BasicNetworkManager;
|
|
|
|
class PacketTransportInternal;
|
2020-08-22 01:59:49 +02:00
|
|
|
struct NetworkRoute;
|
2020-08-14 18:58:22 +02:00
|
|
|
} // namespace rtc
|
|
|
|
|
|
|
|
namespace cricket {
|
|
|
|
class BasicPortAllocator;
|
|
|
|
class P2PTransportChannel;
|
|
|
|
class IceTransportInternal;
|
2023-02-18 22:24:25 +01:00
|
|
|
class RelayPortFactoryInterface;
|
2020-08-14 18:58:22 +02:00
|
|
|
} // namespace cricket
|
|
|
|
|
|
|
|
namespace webrtc {
|
|
|
|
class BasicAsyncResolverFactory;
|
2020-10-01 03:59:32 +02:00
|
|
|
class TurnCustomizer;
|
2020-08-14 18:58:22 +02:00
|
|
|
} // namespace webrtc
|
|
|
|
|
|
|
|
namespace tgcalls {
|
|
|
|
|
|
|
|
struct Message;
|
|
|
|
|
2020-08-15 23:06:36 +02:00
|
|
|
class NetworkManager : public sigslot::has_slots<>, public std::enable_shared_from_this<NetworkManager> {
|
2020-08-14 18:58:22 +02:00
|
|
|
public:
|
|
|
|
struct State {
|
|
|
|
bool isReadyToSendData = false;
|
2020-08-15 23:06:36 +02:00
|
|
|
bool isFailed = false;
|
2020-08-14 18:58:22 +02:00
|
|
|
};
|
2020-08-15 23:06:36 +02:00
|
|
|
|
|
|
|
struct InterfaceTrafficStats {
|
|
|
|
int64_t incoming = 0;
|
|
|
|
int64_t outgoing = 0;
|
|
|
|
};
|
2020-08-14 18:58:22 +02:00
|
|
|
|
|
|
|
NetworkManager(
|
|
|
|
rtc::Thread *thread,
|
|
|
|
EncryptionKey encryptionKey,
|
|
|
|
bool enableP2P,
|
2020-10-01 03:59:32 +02:00
|
|
|
bool enableTCP,
|
|
|
|
bool enableStunMarking,
|
2020-08-14 18:58:22 +02:00
|
|
|
std::vector<RtcServer> const &rtcServers,
|
2020-12-23 08:48:30 +01:00
|
|
|
std::unique_ptr<Proxy> proxy,
|
2020-08-14 18:58:22 +02:00
|
|
|
std::function<void(const State &)> stateUpdated,
|
|
|
|
std::function<void(DecryptedMessage &&)> transportMessageReceived,
|
|
|
|
std::function<void(Message &&)> sendSignalingMessage,
|
|
|
|
std::function<void(int delayMs, int cause)> sendTransportServiceAsync);
|
|
|
|
~NetworkManager();
|
|
|
|
|
2020-08-15 23:06:36 +02:00
|
|
|
void start();
|
2020-08-14 18:58:22 +02:00
|
|
|
void receiveSignalingMessage(DecryptedMessage &&message);
|
|
|
|
uint32_t sendMessage(const Message &message);
|
|
|
|
void sendTransportService(int cause);
|
2020-08-15 23:06:36 +02:00
|
|
|
void setIsLocalNetworkLowCost(bool isLocalNetworkLowCost);
|
|
|
|
TrafficStats getNetworkStats();
|
2020-10-01 03:59:32 +02:00
|
|
|
void fillCallStats(CallStats &callStats);
|
|
|
|
void logCurrentNetworkState();
|
2020-08-14 18:58:22 +02:00
|
|
|
|
|
|
|
private:
|
2020-08-15 23:06:36 +02:00
|
|
|
void checkConnectionTimeout();
|
2020-08-14 18:58:22 +02:00
|
|
|
void candidateGathered(cricket::IceTransportInternal *transport, const cricket::Candidate &candidate);
|
|
|
|
void candidateGatheringState(cricket::IceTransportInternal *transport);
|
|
|
|
void transportStateChanged(cricket::IceTransportInternal *transport);
|
|
|
|
void transportReadyToSend(cricket::IceTransportInternal *transport);
|
|
|
|
void transportPacketReceived(rtc::PacketTransportInternal *transport, const char *bytes, size_t size, const int64_t ×tamp, int unused);
|
|
|
|
void transportRouteChanged(absl::optional<rtc::NetworkRoute> route);
|
2020-08-15 23:06:36 +02:00
|
|
|
void addTrafficStats(int64_t byteCount, bool isIncoming);
|
2020-08-14 18:58:22 +02:00
|
|
|
|
|
|
|
rtc::Thread *_thread = nullptr;
|
2020-08-15 23:06:36 +02:00
|
|
|
bool _enableP2P = false;
|
2020-10-01 03:59:32 +02:00
|
|
|
bool _enableTCP = false;
|
|
|
|
bool _enableStunMarking = false;
|
2020-08-15 23:06:36 +02:00
|
|
|
std::vector<RtcServer> _rtcServers;
|
2020-12-23 08:48:30 +01:00
|
|
|
std::unique_ptr<Proxy> _proxy;
|
2020-08-14 18:58:22 +02:00
|
|
|
EncryptedConnection _transport;
|
|
|
|
bool _isOutgoing = false;
|
|
|
|
std::function<void(const NetworkManager::State &)> _stateUpdated;
|
|
|
|
std::function<void(DecryptedMessage &&)> _transportMessageReceived;
|
|
|
|
std::function<void(Message &&)> _sendSignalingMessage;
|
|
|
|
|
2021-06-25 02:43:10 +02:00
|
|
|
std::unique_ptr<rtc::NetworkMonitorFactory> _networkMonitorFactory;
|
2020-08-14 18:58:22 +02:00
|
|
|
std::unique_ptr<rtc::BasicPacketSocketFactory> _socketFactory;
|
|
|
|
std::unique_ptr<rtc::BasicNetworkManager> _networkManager;
|
2020-10-01 03:59:32 +02:00
|
|
|
std::unique_ptr<webrtc::TurnCustomizer> _turnCustomizer;
|
2023-02-18 22:24:25 +01:00
|
|
|
std::unique_ptr<cricket::RelayPortFactoryInterface> _relayPortFactory;
|
2020-08-14 18:58:22 +02:00
|
|
|
std::unique_ptr<cricket::BasicPortAllocator> _portAllocator;
|
|
|
|
std::unique_ptr<webrtc::BasicAsyncResolverFactory> _asyncResolverFactory;
|
|
|
|
std::unique_ptr<cricket::P2PTransportChannel> _transportChannel;
|
|
|
|
|
|
|
|
PeerIceParameters _localIceParameters;
|
|
|
|
absl::optional<PeerIceParameters> _remoteIceParameters;
|
2020-08-15 23:06:36 +02:00
|
|
|
|
|
|
|
bool _isLocalNetworkLowCost = false;
|
|
|
|
int64_t _lastNetworkActivityMs = 0;
|
|
|
|
InterfaceTrafficStats _trafficStatsWifi;
|
|
|
|
InterfaceTrafficStats _trafficStatsCellular;
|
2020-10-01 03:59:32 +02:00
|
|
|
|
|
|
|
absl::optional<CallStatsConnectionEndpointType> _currentEndpointType;
|
|
|
|
std::vector<CallStatsNetworkRecord> _networkRecords;
|
2020-08-14 18:58:22 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace tgcalls
|
|
|
|
|
|
|
|
#endif
|