diff options
author | George Hazan <george.hazan@gmail.com> | 2024-06-13 16:39:12 +0300 |
---|---|---|
committer | George Hazan <george.hazan@gmail.com> | 2024-06-13 16:39:12 +0300 |
commit | 004f3d1f49c54bc62743a838161ac157ffc37e41 (patch) | |
tree | 722b78c8e8f32c16cf9959068d86f7bf775e41cf /include | |
parent | 778558984c08b787d0e73d692086b6935e56a156 (diff) |
websocket internal code went to MWebSocket
Diffstat (limited to 'include')
-rw-r--r-- | include/m_netlib.h | 68 |
1 files changed, 53 insertions, 15 deletions
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 T> 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 T> 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+)
|