![]() System : Linux absol.cf 5.4.0-198-generic #218-Ubuntu SMP Fri Sep 27 20:18:53 UTC 2024 x86_64 User : www-data ( 33) PHP Version : 7.4.33 Disable Function : pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare, Directory : /usr/local/lib/node_modules/mediasoup/worker/include/handles/ |
Upload File : |
#ifndef MS_TCP_CONNECTION_HPP #define MS_TCP_CONNECTION_HPP #include "common.hpp" #include <uv.h> #include <string> class TcpConnection { protected: using onSendCallback = const std::function<void(bool sent)>; public: class Listener { public: virtual ~Listener() = default; public: virtual void OnTcpConnectionClosed(TcpConnection* connection) = 0; }; public: /* Struct for the data field of uv_req_t when writing into the connection. */ struct UvWriteData { explicit UvWriteData(size_t storeSize) { this->store = new uint8_t[storeSize]; } // Disable copy constructor because of the dynamically allocated data (store). UvWriteData(const UvWriteData&) = delete; ~UvWriteData() { delete[] this->store; delete this->cb; } uv_write_t req; uint8_t* store{ nullptr }; TcpConnection::onSendCallback* cb{ nullptr }; }; public: explicit TcpConnection(size_t bufferSize); TcpConnection& operator=(const TcpConnection&) = delete; TcpConnection(const TcpConnection&) = delete; virtual ~TcpConnection(); public: void Close(); virtual void Dump() const; void Setup( Listener* listener, struct sockaddr_storage* localAddr, const std::string& localIp, uint16_t localPort); bool IsClosed() const { return this->closed; } uv_tcp_t* GetUvHandle() const { return this->uvHandle; } void Start(); void Write(const uint8_t* data, size_t len, TcpConnection::onSendCallback* cb); void Write( const uint8_t* data1, size_t len1, const uint8_t* data2, size_t len2, TcpConnection::onSendCallback* cb); void ErrorReceiving(); const struct sockaddr* GetLocalAddress() const { return reinterpret_cast<const struct sockaddr*>(this->localAddr); } int GetLocalFamily() const { return reinterpret_cast<const struct sockaddr*>(this->localAddr)->sa_family; } const std::string& GetLocalIp() const { return this->localIp; } uint16_t GetLocalPort() const { return this->localPort; } const struct sockaddr* GetPeerAddress() const { return reinterpret_cast<const struct sockaddr*>(&this->peerAddr); } const std::string& GetPeerIp() const { return this->peerIp; } uint16_t GetPeerPort() const { return this->peerPort; } size_t GetRecvBytes() const { return this->recvBytes; } size_t GetSentBytes() const { return this->sentBytes; } private: bool SetPeerAddress(); /* Callbacks fired by UV events. */ public: void OnUvReadAlloc(size_t suggestedSize, uv_buf_t* buf); void OnUvRead(ssize_t nread, const uv_buf_t* buf); void OnUvWrite(int status, onSendCallback* cb); /* Pure virtual methods that must be implemented by the subclass. */ protected: virtual void UserOnTcpConnectionRead() = 0; protected: // Passed by argument. size_t bufferSize{ 0u }; // Allocated by this. uint8_t* buffer{ nullptr }; // Others. size_t bufferDataLen{ 0u }; std::string localIp; uint16_t localPort{ 0u }; struct sockaddr_storage peerAddr; std::string peerIp; uint16_t peerPort{ 0u }; private: // Passed by argument. Listener* listener{ nullptr }; // Allocated by this. uv_tcp_t* uvHandle{ nullptr }; // Others. struct sockaddr_storage* localAddr{ nullptr }; bool closed{ false }; size_t recvBytes{ 0u }; size_t sentBytes{ 0u }; bool isClosedByPeer{ false }; bool hasError{ false }; }; #endif