From 004f3d1f49c54bc62743a838161ac157ffc37e41 Mon Sep 17 00:00:00 2001 From: George Hazan Date: Thu, 13 Jun 2024 16:39:12 +0300 Subject: websocket internal code went to MWebSocket --- include/m_netlib.h | 68 ++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 53 insertions(+), 15 deletions(-) (limited to 'include') diff --git a/include/m_netlib.h b/include/m_netlib.h index f4630248cb..5157c118e5 100644 --- a/include/m_netlib.h +++ b/include/m_netlib.h @@ -755,27 +755,65 @@ EXTERN_C MIR_APP_DLL(void*) Netlib_GetTlsUnique(HNETLIBCONN nlc, int &cbLen, int ///////////////////////////////////////////////////////////////////////////////////////// // WebSocket support -struct WSHeader +class MIR_APP_EXPORT MWebSocket : public MNonCopyable { - WSHeader() - { - memset(this, 0, sizeof(*this)); - } + mir_cs m_cs; + bool m_bTerminated = false; + +protected: + HNETLIBUSER m_nlu = 0; + HNETLIBCONN m_hConn = 0; + +public: + MWebSocket(); + ~MWebSocket(); + + // packet processor + virtual void process(const uint8_t *buf, size_t cbLen) = 0; + + // connects to a WebSocket server + MHttpResponse* connect(HANDLE nlu, const char *szHost, const MHttpHeaders *pHeaders = nullptr); + + // runs a socket reading cycle + void run(); + + // terminates a reading cycle + void terminate(); + + // sends a packet to WebSocket + void sendText(const char *pData); + void sendBinary(const void *pData, size_t strLen); +}; + +class MIR_APP_EXPORT MJsonWebSocket : public MWebSocket +{ + void process(const uint8_t *buf, size_t cbLen) override; + +public: + MJsonWebSocket() {} + + virtual void process(const class JSONNode &json) = 0; +}; + +template class WebSocket : public MWebSocket +{ + T *p; + +public: + WebSocket(T *_1) : p(_1) {} - bool bIsFinal, bIsMasked; - int opCode, firstByte; - size_t payloadSize, headerSize; + void process(const uint8_t *buf, size_t cbLen) override; }; -// connects to a WebSocket server -EXTERN_C MIR_APP_DLL(MHttpResponse*) WebSocket_Connect(HNETLIBUSER, const char *szHost, const MHttpHeaders *pHeaders = nullptr); +template class JsonWebSocket : public MJsonWebSocket +{ + T *p; -// validates that the provided buffer contains full WebSocket datagram -EXTERN_C MIR_APP_DLL(bool) WebSocket_InitHeader(WSHeader &hdr, const void *pData, size_t bufSize); +public: + JsonWebSocket(T *_1) : p(_1) {} -// sends a packet to WebSocket -EXTERN_C MIR_APP_DLL(void) WebSocket_SendText(HNETLIBCONN nlc, const char *pData); -EXTERN_C MIR_APP_DLL(void) WebSocket_SendBinary(HNETLIBCONN nlc, const void *pData, size_t strLen); + void process(const JSONNode &node) override; +}; ///////////////////////////////////////////////////////////////////////////////////////// // Netlib hooks (0.8+) -- cgit v1.2.3