| 6 | #include <sys/time.h> |
| 7 | #endif |
| 8 |
| 9 | #include "rtc_base/async_tcp_socket.h" |
| 10 | #include "p2p/base/basic_packet_socket_factory.h" |
| 11 | #include "rtc_base/logging.h" |
| 12 | #include "rtc_base/thread.h" |
| 13 | #include "p2p/base/packet_transport_internal.h" |
| 14 |
| 15 | #include "FieldTrialsConfig.h" |
| 16 |
| 17 | /* get system time */ |
| 18 | static inline void itimeofday(long *sec, long *usec) { |
| 19 | #ifdef _MSC_VER |
| 20 | SYSTEMTIME system_time; |
| 21 | FILETIME file_time; |
| 22 | uint64_t time; |
| 23 |
| 24 | GetSystemTime( &system_time ); |
| 25 | SystemTimeToFileTime( &system_time, &file_time ); |
| 26 | time = ((uint64_t)file_time.dwLowDateTime ) ; |
| 27 | time += ((uint64_t)file_time.dwHighDateTime) << 32; |
| 28 |
| 29 | // Windows FILETIME epoch (1601-01-01) to Unix epoch (1970-01-01) in 100-nanosecond intervals |
| 30 | constexpr auto EPOCH = 11644473600000000ULL; |
| 31 |
| 32 | if (sec) *sec = (long) ((time - EPOCH) / 10000000L); |
| 33 | if (usec) *usec = (long) (system_time.wMilliseconds * 1000); |
| 34 | #else |
| 35 | struct timeval time; |
| 36 | gettimeofday(&time, NULL); |
| 37 | if (sec) *sec = time.tv_sec; |
| 38 | if (usec) *usec = time.tv_usec; |
| 39 | #endif |
| 40 | } |
| 41 |
| 42 | /* get clock in millisecond 64 */ |
| 43 | static inline IINT64 iclock64(void) { |
| 44 | long s, u; |
| 45 | IINT64 value; |
| 46 | itimeofday(&s, &u); |
| 47 | value = ((IINT64)s) * 1000 + (u / 1000); |
| 48 | return value; |
| 49 | } |
| 50 |
| 51 | static inline IUINT32 iclock() { |
| 52 | return (IUINT32)(iclock64() & 0xfffffffful); |
| 53 | } |
| 54 |
| 55 | namespace tgcalls { |
| 56 |
| 57 | SignalingKcpConnection::SignalingKcpConnection(std::shared_ptr<Threads> threads, std::function<void(const std::vector<uint8_t> &)> onIncomingData, std::function<void(const std::vector<uint8_t> &)> emitData) : |
| 58 | _threads(threads), |
| 59 | _emitData(emitData), |
| 60 | _onIncomingData(onIncomingData) { |
| 61 | _receiveBuffer.resize(512 * 1024); |
| 62 |
| 63 | _kcp = ikcp_create(0, this); |
| 64 | _kcp->output = &SignalingKcpConnection::udpOutput; |
| 65 |
| 66 | ikcp_wndsize(_kcp, 128, 128); |
| 67 | ikcp_nodelay(_kcp, 0, 10, 0, 0); |
| 68 |
| 69 | //_onIncomingData |
| 70 | } |
| 71 |
| 72 | int SignalingKcpConnection::udpOutput(const char *buf, int len, ikcpcb *kcp, void *user) { |
| 73 | SignalingKcpConnection *connection = (SignalingKcpConnection *)user; |
| 74 | if (connection) { |
| 75 | connection->_emitData(std::vector<uint8_t>(buf, buf + len)); |
| 76 | } |
| 77 | return 0; |
| 78 | } |
| 79 |
| 80 | SignalingKcpConnection::~SignalingKcpConnection() { |
| 81 |
| 82 | } |
| 83 |
| 84 | void SignalingKcpConnection::start() { |
| 85 | scheduleInternalUpdate(0); |
| 86 | } |
| 87 |
| 88 | void SignalingKcpConnection::scheduleInternalUpdate(int timeoutMs) { |
| 89 | const auto weak = std::weak_ptr<SignalingKcpConnection>(shared_from_this()); |
| 90 | _threads->getMediaThread()->PostDelayedTask([weak]() { |
| 91 | auto strong = weak.lock(); |
| 92 | if (!strong) { |
| 93 | return; |
| 94 | } |
| 95 |
| 96 | strong->performInternalUpdate(); |
| 97 |
| 98 | strong->scheduleInternalUpdate(10); |
| 99 | }, webrtc::TimeDelta::Millis(timeoutMs)); |
| 100 | } |
| 101 |
| 102 | void SignalingKcpConnection::performInternalUpdate() { |
| 103 | ikcp_update(_kcp, iclock()); |
| 104 |
| 105 | while (true) { |
| 106 | int result = ikcp_recv(_kcp, (char *)_receiveBuffer.data(), (int)_receiveBuffer.size()); |
| 107 | if (result >= 0) { |
| 108 | std::vector<uint8_t> packet; |
| 109 | packet.resize(result); |
| 110 | std::copy(_receiveBuffer.begin(), _receiveBuffer.begin() + result, packet.begin()); |
| 111 | _onIncomingData(packet); |
| 112 | } else { |
| 113 | break; |
| 114 | } |
| 115 | } |
| 116 | } |
| 117 |
| 118 | void SignalingKcpConnection::receiveExternal(const std::vector<uint8_t> &data) { |
| 119 | ikcp_input(_kcp, (const char *)data.data(), (long)data.size()); |
| 120 | } |
| 121 |
| 122 | void SignalingKcpConnection::send(const std::vector<uint8_t> &data) { |
| 123 | ikcp_send(_kcp, (const char *)data.data(), (int)data.size()); |
| 124 | } |
| 125 |
| 126 | } |
| 127 |