From e28b2c3ba7237ddb61265083f722e5a15f178ec8 Mon Sep 17 00:00:00 2001 From: Apex Liu Date: Tue, 17 May 2022 03:59:54 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A7=A3=E5=86=B3win=E4=B8=8B=E5=9F=9F?= =?UTF-8?q?=E5=90=8D=E8=AE=BF=E9=97=AE=E6=97=B6=E5=8A=A9=E6=89=8B=E6=97=A0?= =?UTF-8?q?=E6=B3=95=E8=BF=9E=E6=8E=A5websocket=E7=9A=84=E9=97=AE=E9=A2=98?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- client/tp_assist_win/msocketx.cpp | 362 ------------------ client/tp_assist_win/msocketx.h | 117 ------ client/tp_assist_win/tp_assist.vs2020.sln | 25 ++ client/tp_assist_win/tp_assist.vs2020.vcxproj | 192 ++++++++++ .../tp_assist.vs2020.vcxproj.filters | 174 +++++++++ client/tp_assist_win/ts_ws_client.cpp | 45 ++- client/tp_assist_win/ts_ws_client.h | 2 +- 7 files changed, 422 insertions(+), 495 deletions(-) delete mode 100644 client/tp_assist_win/msocketx.cpp delete mode 100644 client/tp_assist_win/msocketx.h create mode 100644 client/tp_assist_win/tp_assist.vs2020.sln create mode 100644 client/tp_assist_win/tp_assist.vs2020.vcxproj create mode 100644 client/tp_assist_win/tp_assist.vs2020.vcxproj.filters diff --git a/client/tp_assist_win/msocketx.cpp b/client/tp_assist_win/msocketx.cpp deleted file mode 100644 index 0d285f1..0000000 --- a/client/tp_assist_win/msocketx.cpp +++ /dev/null @@ -1,362 +0,0 @@ -#include "stdafx.h" -#include "msocketx.h" - -#ifdef _WIN32 -#pragma warning(disable:4244) -#endif - -msocketx::msocketx() -{ - m_sock = INVALID_SOCKET; -} - -msocketx::~msocketx() -{ - close(); -} -int msocketx::getsockname(unsigned int& uiip, unsigned short& usport) -{ - sockaddr_in addr; - socklen_t ilen = sizeof(addr); - - int nret = ::getsockname(m_sock, (sockaddr*)&addr, &ilen); - if (nret != SOCKET_ERROR) - { - uiip = addr.sin_addr.s_addr; - usport = ntohs(addr.sin_port); - } - - return nret; -} - -int msocketx::shutdown(int ihow) -{ - return ::shutdown(m_sock, ihow); -} - -int msocketx::closesocket() -{ -#ifdef _WIN32 - return ::closesocket(m_sock); -#else - return ::close(m_sock); -#endif -} - -void msocketx::close() -{ - if (isvalid()) - { - shutdown(0); - closesocket(); - m_sock = INVALID_SOCKET; - } -} - -bool msocketx::startup() -{ -#ifdef _WIN32 - WSADATA wsaData; - if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) - return false; -#endif - return true; -} - -bool msocketx::clearup() -{ -#ifdef _WIN32 - return WSACleanup() == 0; -#endif - return true; -} - -int msocketx::getlasterror() -{ -#ifdef _WIN32 - return WSAGetLastError(); -#else - return errno; -#endif -} - -void msocketx::attach(SOCKET sock) -{ - if (sock == INVALID_SOCKET) - return; - m_sock = sock; -} - -bool msocketx::isvalid() -{ - return m_sock != INVALID_SOCKET; -} - -bool msocketx::setsockbuff(unsigned int uirecvlen /* = 4 * 1024 * 1024 */, - unsigned int uisendlen /* = 4 * 1024 * 1024 */) -{ - return (setsockopt(m_sock, SOL_SOCKET, SO_SNDBUF, (char*)&uisendlen, sizeof(uisendlen)) == 0) && - (setsockopt(m_sock, SOL_SOCKET, SO_RCVBUF, (char*)&uirecvlen, sizeof(uirecvlen)) == 0); -} - -bool msocketx::setsocknagle(bool benable /* = true */) -{ - socklen_t iflag = (benable ? 0 : 1); - return setsockopt(m_sock, IPPROTO_TCP, TCP_NODELAY, (char*)&iflag, sizeof(iflag)) == 0; -} - -bool msocketx::setsocktime(unsigned int uimillisecond /* = 500 */) -{ -#ifdef _WIN32 - return (setsockopt(m_sock, SOL_SOCKET, SO_SNDTIMEO, (char*)&uimillisecond, sizeof(uimillisecond)) == 0) && - (setsockopt(m_sock, SOL_SOCKET, SO_RCVTIMEO, (char*)&uimillisecond, sizeof(uimillisecond)) == 0); -#else - struct timeval timeout; - timeout.tv_sec = uimillisecond / 1000; - timeout.tv_usec = (uimillisecond % 1000) * 1000; - return (setsockopt(m_sock, SOL_SOCKET, SO_SNDTIMEO, (char*)&timeout, sizeof(timeout)) == 0) && - (setsockopt(m_sock, SOL_SOCKET, SO_RCVTIMEO, (char*)&timeout, sizeof(timeout)) == 0); -#endif -} - -bool msocketx::setsock() -{ - return setsockbuff() && setsocktime(); -} - -bool msocketx::setblock(bool bblock /* = false */) -{ -#ifdef _WIN32 - u_long b = bblock ? 0 : 1; - return ioctlsocket(m_sock, FIONBIO, &b) == 0; -#else - int flags = fcntl(m_sock, F_GETFL, 0); - if (bblock) - flags |= O_NONBLOCK; - else - flags &= (~O_NONBLOCK); - return fcntl(m_sock, F_SETFL, flags) != -1; -#endif -} - -bool msocketx::create(int itype, int iprotocol) -{ - if (isvalid()) - return true; - m_sock = socket(AF_INET, itype, iprotocol); - - return isvalid(); -} - -bool msocketx::bind(unsigned short usport, const char* pstrip /* = NULL */) -{ - sockaddr_in addr = { 0 }; - unsigned long nResult = 0; - - addr.sin_family = AF_INET; - - if (pstrip == NULL) - addr.sin_addr.s_addr = htonl(INADDR_ANY); - else - { - nResult = inet_addr(pstrip); - if (nResult == INADDR_NONE) - return false; - - addr.sin_addr.s_addr = nResult; - } - addr.sin_port = htons(usport); - - return ::bind(m_sock, (sockaddr*)&addr, sizeof(addr)) == 0; -} - -int msocketx::sendto(const void* pbuf, unsigned int ilen, unsigned int uiip, unsigned short usport, int iflags) -{ - if (pbuf == NULL) - return -1; - - sockaddr_in addr = { 0 }; - - addr.sin_family = AF_INET; - addr.sin_addr.s_addr = uiip; - addr.sin_port = htons(usport); - - return ::sendto(m_sock, (char*)pbuf, ilen, iflags, (sockaddr*)&addr, sizeof(addr)); -} - -int msocketx::sendto(const void* pbuf, unsigned int ilen, const char* pszip, unsigned short usport, int iflags) -{ - if (pbuf == NULL) - return -1; - return sendto(pbuf, ilen, inet_addr(pszip), usport, iflags); -} - -int msocketx::recvfrom(void* pbuf, unsigned int ilen, unsigned int& uiip, unsigned short& usport, int iflags) -{ - if (pbuf == NULL) - return -1; - - sockaddr_in srcaddr = { 0 }; - socklen_t iaddrlen = sizeof(srcaddr); - - int nret = ::recvfrom(m_sock, (char*)pbuf, ilen, iflags, (sockaddr*)&srcaddr, &iaddrlen); - if (nret != SOCKET_ERROR) - { - usport = htons(srcaddr.sin_port); - uiip = srcaddr.sin_addr.s_addr; - } - return nret; -} - -int msocketx::send(const void* pbuf, unsigned int ilen, int iflags) -{ - if (pbuf == NULL) - return -1; - - return ::send(m_sock, (char*)pbuf, ilen, iflags); -} - -int msocketx::recv(void* pbuf, unsigned int ilen, int iflags) -{ - if (pbuf == NULL) - return -1; - - return ::recv(m_sock, (char*)pbuf, ilen, iflags); -} - -bool msocketx::listen(int ibacklog) -{ - return ::listen(m_sock, ibacklog) == 0; -} - -bool msocketx::accept(SOCKET& sock, sockaddr* peeraddr, socklen_t* addrlen) -{ - sock = ::accept(m_sock, peeraddr, addrlen); - return sock != INVALID_SOCKET; -} - -bool msocketx::accept(msocketx& sock, sockaddr* peeraddr, socklen_t* addrlen) -{ - SOCKET socktmp = ::accept(m_sock, peeraddr, addrlen); - sock.m_sock = socktmp; - return socktmp != INVALID_SOCKET; -} - -int msocketx::connect(const char* pszip, unsigned short usport, bool bblock) -{ - if (pszip == NULL) - return -1; - - if (!isvalid()) - { - if (!create(SOCK_STREAM, 0)) - return -1; - } - setblock(bblock); - - sockaddr_in addr = { 0 }; - - addr.sin_port = htons(usport); - addr.sin_addr.s_addr = inet_addr(pszip); - addr.sin_family = AF_INET; - - return ::connect(m_sock, (sockaddr*)&addr, sizeof(addr)); -} - -int msocketx::wait(unsigned int uimilli, int iflagx) -{ - timeval timeout; - timeout.tv_sec = uimilli / 1000; - timeout.tv_usec = (uimilli % 1000) * 1000; - - fd_set *prfds = NULL, *pwfds = NULL, *pefds = NULL; - int iret = 0; - - if ((iflagx & CAN_CONNECTX) == CAN_CONNECTX) - { - pwfds = new(std::nothrow) fd_set; - if (pwfds == NULL) - return -1; - - pefds = new(std::nothrow) fd_set; - if (pefds == NULL) - { - if (pwfds) - delete pwfds; - return -1; - } - - FD_ZERO(pwfds); - FD_ZERO(pefds); - - FD_SET(m_sock, pwfds); - FD_SET(m_sock, pefds); - - iret = ::select(m_sock + 1, NULL, pwfds, pefds, &timeout); - if (iret > 0) - { - if (FD_ISSET(m_sock, pwfds) || FD_ISSET(m_sock, pefds)) - { - int iopt = 0; - socklen_t ioptlen = sizeof(iopt); - if (getsockopt(m_sock, SOL_SOCKET, SO_ERROR, (char*)&iopt, &ioptlen) == 0) - { - if (iopt == 0) - iret = CAN_CONNECTX; - else - iret = SOCKET_ERROR; - } - else - iret = SOCKET_ERROR; - } - } - if (pwfds) - delete pwfds; - if (pefds) - delete pefds; - return iret; - } - - if ((iflagx & CAN_READX) == CAN_READX || (iflagx & CAN_ACCEPTX) == CAN_ACCEPTX) - { - prfds = new(std::nothrow) fd_set; - if (prfds == NULL) - return -1; - - FD_ZERO(prfds); - FD_SET(m_sock, prfds); - } - if ((iflagx & CAN_WRITEX) == CAN_WRITEX) - { - pwfds = new(std::nothrow) fd_set; - if (pwfds == NULL) - { - if (prfds) - delete prfds; - return -1; - } - - FD_ZERO(pwfds); - FD_SET(m_sock, pwfds); - } - - iret = ::select(m_sock + 1, prfds, pwfds, NULL, &timeout); - if (iret > 0) - { - int itmp = 0; - if (prfds) - if (FD_ISSET(m_sock, prfds)) - itmp |= CAN_READX; - if (pwfds) - if (FD_ISSET(m_sock, pwfds)) - itmp |= CAN_WRITEX; - iret = itmp; - } - - if (pwfds) - delete pwfds; - if (prfds) - delete prfds; - - return iret; -} diff --git a/client/tp_assist_win/msocketx.h b/client/tp_assist_win/msocketx.h deleted file mode 100644 index 66102fe..0000000 --- a/client/tp_assist_win/msocketx.h +++ /dev/null @@ -1,117 +0,0 @@ -#ifndef MSOCKETX_H_ -#define MSOCKETX_H_ - -#ifdef _WIN32 -#pragma comment(lib,"ws2_32.lib") -#include "winsock2.h" -typedef int socklen_t; -#else -#include -#include -#include -#include //for ntohs -#include //for timeval -#include -#include -#include -typedef int SOCKET; -#define SOCKET_ERROR (-1) -#define INVALID_SOCKET (-1) -#endif - -#define CAN_READX 1 -#define CAN_WRITEX 2 -#define CAN_CONNECTX 4 -#define CAN_ACCEPTX 8 - -class msocketx -{ -public: - msocketx(); - virtual ~msocketx(); -public: - // startup ×îÏȵ÷ÓÃ,linux¿ÉÒÔ²»Óõ÷Óà - static bool startup(); - - // clearup ³ÌÐò½áÊøÊ±µ÷ÓÃ,linux¿ÉÒÔ²»Óõ÷Óà - static bool clearup(); - - // getlasterror »ñÈ¡ÉÏÒ»´Î´íÎó´úÂë - static int getlasterror(); - -public: - // attch ¹ÒÔØÒ»¸ösocket - void attach(SOCKET sock); - - // isvalid ÊÇ·ñÓÐЧ - inline bool isvalid(); - - // close »áÏÈÅжÏisvalid,È»ºóshutdown,È»ºóclosesocket,×îºó¸³ÖµINVALIDSOCKET - void close(); - - // setsock »áµ÷ÓÃsetsockbuff,setsocktime. - bool setsock(); - - // create ½¨Á¢socket - bool create(int itype,int iprotocol); - - // bind °ó¶¨Ö¸¶¨ipºÍ¶Ë¿Ú,Èç¹ûpstripΪ¿ÕÔò°ó¶¨ËùÓÐip - bool bind(unsigned short usport,const char* pstrip = NULL); - - // sendto ·¢ËÍÊÕ¾Ý - int sendto(const void* pbuf, unsigned int ilen, unsigned int uiip, unsigned short usport, int iflags); - - // sendto ·¢ËÍÊÕ¾Ý - int sendto(const void* pbuf, unsigned int ilen, const char* pszip, unsigned short usport, int iflags); - - // recvfrom ½ÓÊÕÊý¾Ý - int recvfrom(void* pbuf, unsigned int ilen, unsigned int& uiip, unsigned short& usport, int iflags); - - // send ·¢ËÍÊý¾Ý - int send(const void* pbuf, unsigned int ilen,int iflags); - - // recv ½ÓÊÕÊý¾Ý - int recv(void* pbuf,unsigned int ilen,int iflags); - - // listen ¼àÌý - bool listen(int ibacklog); - - // accept ½ÓÊÕ - bool accept(SOCKET& sock,sockaddr* peeraddr,socklen_t* addrlen); - - bool accept(msocketx& sock,sockaddr* peeraddr,socklen_t* addrlen); - - // connect Á¬½Ó,usportΪÖ÷»ú×Ö½ÚÐò. - int connect(const char* pszip,unsigned short usport,bool bblock); - - // wait µÈ´ýiflagxʼþ,ʼþÈçÉÏÃæµÄCAN_READX...³ö´í·µ»Ø-1,0±íʾ³¬Ê±, - // Õý³£·µ»Ø¿É²Ù×÷ÀàÐÍ,ÓÃ&È¡³öÅÐ¶Ï - int wait(unsigned int uimilli,int iflagx); -public: - // setblock ÉèÖÃÊÇ·ñ×èÈû - bool setblock(bool bblock = false); - - // setsockbuff ÉèÖÃsocketϵͳ»º³åÇø´óС - bool setsockbuff(unsigned int uirecvlen = 4 * 1024 * 1024, - unsigned int uisendlen = 4 * 1024 * 1024); - - // setsocktime ÉèÖÃsend\recv²Ù×÷³¬Ê±Ê±¼ä. - bool setsocktime(unsigned int uimillisecond = 500); - - // setsocknagle ÊÇ·ñÆôÓÃnagleËã·¨. - bool setsocknagle(bool benable = true); - - // shutdown 0 read,1 write,2 both - int shutdown(int ihow); - - // closesocket ×î»ù±¾µÄ¹Øµôsocket - int closesocket(); - - // getsockname »ñÈ¡¸Ãsocketµ±Ç°°ó¶¨µØÖ·,ulip ÎªÍøÂçÐò,usportΪÖ÷»úÐò - int getsockname(unsigned int& uiip,unsigned short& usport); - -private: - SOCKET m_sock; -}; - -#endif //MSOCKETX_H_ diff --git a/client/tp_assist_win/tp_assist.vs2020.sln b/client/tp_assist_win/tp_assist.vs2020.sln new file mode 100644 index 0000000..89fcf0b --- /dev/null +++ b/client/tp_assist_win/tp_assist.vs2020.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.2.32505.173 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "tp_assist", "tp_assist.vs2020.vcxproj", "{63B7A8F2-9722-487C-A92A-3DB5D8CA1473}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|x86 = Debug|x86 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {63B7A8F2-9722-487C-A92A-3DB5D8CA1473}.Debug|x86.ActiveCfg = Debug|Win32 + {63B7A8F2-9722-487C-A92A-3DB5D8CA1473}.Debug|x86.Build.0 = Debug|Win32 + {63B7A8F2-9722-487C-A92A-3DB5D8CA1473}.Release|x86.ActiveCfg = Release|Win32 + {63B7A8F2-9722-487C-A92A-3DB5D8CA1473}.Release|x86.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {397ABE48-C68C-4ECB-BABF-978C775AC9CB} + EndGlobalSection +EndGlobal diff --git a/client/tp_assist_win/tp_assist.vs2020.vcxproj b/client/tp_assist_win/tp_assist.vs2020.vcxproj new file mode 100644 index 0000000..0350bbf --- /dev/null +++ b/client/tp_assist_win/tp_assist.vs2020.vcxproj @@ -0,0 +1,192 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + + {63B7A8F2-9722-487C-A92A-3DB5D8CA1473} + Win32Proj + tp_assist + tp_assist + 10.0 + + + + Application + true + v143 + Unicode + + + Application + false + v143 + true + Unicode + false + + + + + + + + + + + + + true + ..\..\out\client\$(PlatformTarget)\$(Configuration)\ + ..\..\out\_tmp_\$(ProjectName)\$(PlatformTarget)\$(Configuration)\ + C:\apps\vld\include;$(IncludePath) + C:\apps\vld\lib\Win32;$(LibraryPath) + + + false + ..\..\out\client\$(PlatformTarget)\$(Configuration)\ + ..\..\out\_tmp_\$(ProjectName)\$(PlatformTarget)\$(Configuration)\ + + + + Use + Level3 + Disabled + WIN32;_DEBUG;_WINDOWS;_WINSOCK_DEPRECATED_NO_WARNINGS;MG_ENABLE_DEBUG;MG_ENABLE_SSL;MG_ENABLE_THREADS;MG_DISABLE_HTTP_DIGEST_AUTH;MG_DISABLE_MQTT;MG_DISABLE_SSI;MG_DISABLE_FILESYSTEM;MG_ENABLE_SYNC_RESOLVER;MG_ENABLE_GETADDRINFO;%(PreprocessorDefinitions) + true + ..\..\common\teleport;..\..\common\libex\include;..\..\external\jsoncpp\include;..\..\external\openssl\include + MultiThreadedDebug + + + Windows + true + %(AdditionalDependencies) + ..\..\external\openssl\lib;%(AdditionalLibraryDirectories) + + + + + Level3 + Use + MaxSpeed + true + true + WIN32;MG_ENABLE_SSL;NDEBUG;_WINDOWS;_WINSOCK_DEPRECATED_NO_WARNINGS;MG_ENABLE_THREADS;MG_DISABLE_HTTP_DIGEST_AUTH;MG_DISABLE_MQTT;MG_DISABLE_SSI;MG_DISABLE_FILESYSTEM;%(PreprocessorDefinitions) + true + ..\..\common\teleport;..\..\common\libex\include;..\..\external\jsoncpp\include;..\..\external\openssl\include + MultiThreaded + + + Windows + true + true + true + %(AdditionalDependencies) + ..\..\external\openssl\lib;%(AdditionalLibraryDirectories) + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + NotUsing + NotUsing + + + NotUsing + NotUsing + + + NotUsing + NotUsing + + + NotUsing + NotUsing + + + NotUsing + NotUsing + + + NotUsing + NotUsing + + + NotUsing + NotUsing + + + NotUsing + NotUsing + + + NotUsing + NotUsing + + + NotUsing + NotUsing + + + + Create + Create + + + + + + NotUsing + NotUsing + + + Use + Use + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/client/tp_assist_win/tp_assist.vs2020.vcxproj.filters b/client/tp_assist_win/tp_assist.vs2020.vcxproj.filters new file mode 100644 index 0000000..8a2d763 --- /dev/null +++ b/client/tp_assist_win/tp_assist.vs2020.vcxproj.filters @@ -0,0 +1,174 @@ + + + + + main app + + + main app + + + main app + + + main app + + + main app + + + jsoncpp + + + jsoncpp + + + jsoncpp + + + mongoose + + + libex\src + + + libex\src + + + libex\src + + + libex\src + + + libex\src + + + libex\src + + + main app + + + main app + + + + + resource + + + main app + + + main app + + + main app + + + main app + + + main app + + + main app + + + mongoose + + + libex\header + + + libex\header + + + libex\header + + + libex\header + + + libex\header + + + libex\header + + + libex\header + + + main app + + + main app + + + libex\header + + + libex\header + + + libex\header + + + teleport + + + main app + + + main app + + + + + resource + + + resource + + + resource + + + + + {52b425b1-8aa9-4e08-acbd-c88387350530} + + + {adabe93d-3938-4b11-9352-5b67a1efd7e3} + + + {35a345a0-6147-4c87-97c9-3b0b2a57e348} + + + {0942cec3-67df-4d19-bbc1-e962145e496f} + + + {a88e05d3-51f4-463f-84cc-c3bc86f07aac} + + + {e3e7a811-5905-4ad5-86a7-9721af5d015a} + + + {d7d49fa4-5192-42c5-bc70-5584d9d646c6} + + + {1291a5cf-cb08-4ad6-8a86-8a0486297c63} + + + + + resource + + + + + jsoncpp + + + \ No newline at end of file diff --git a/client/tp_assist_win/ts_ws_client.cpp b/client/tp_assist_win/ts_ws_client.cpp index 73ff77f..11f60ef 100644 --- a/client/tp_assist_win/ts_ws_client.cpp +++ b/client/tp_assist_win/ts_ws_client.cpp @@ -156,21 +156,21 @@ void TsWsClient::url_scheme_handler(const std::string& url) std::string::size_type pos_protocol = url.find("://"); if (pos_protocol == std::string::npos) { - EXLOGE("[ws] invalid url: %s\n", url.c_str()); + EXLOGE("[url-schema] invalid url: %s\n", url.c_str()); return; } std::string::size_type pos_method = url.find('?'); if (pos_method == std::string::npos) { - EXLOGE("[ws] invalid url: %s\n", url.c_str()); + EXLOGE("[url-schema] invalid url: %s\n", url.c_str()); return; } protocol.assign(url, 0, pos_protocol); if (protocol != "teleport") { - EXLOGE("[ws] invalid protocol: %s\n", protocol.c_str()); + EXLOGE("[url-schema] invalid protocol: %s\n", protocol.c_str()); return; } @@ -180,14 +180,14 @@ void TsWsClient::url_scheme_handler(const std::string& url) method.erase(method.length() - 1, 1); if (method != "register") { - EXLOGE("[ws] unknown method: %s\n", method.c_str()); + EXLOGE("[url-schema] unknown method: %s\n", method.c_str()); return; } param.assign(url, pos_method + 7); // ?param= if (param.empty()) { - EXLOGE("[ws] invalid protocol: %s\n", protocol.c_str()); + EXLOGE("[url-schema] invalid protocol: %s\n", protocol.c_str()); return; } @@ -199,12 +199,12 @@ void TsWsClient::url_scheme_handler(const std::string& url) memset(&sztmp[0], 0, len); if (-1 == ts_url_decode(param.c_str(), (int)param.length(), &sztmp[0], (int)len, 0)) { - EXLOGE("[ws] url-decode param failed: %s\n", param.c_str()); + EXLOGE("[url-schema] url-decode param failed: %s\n", param.c_str()); return; } param = &sztmp[0]; - EXLOGV("[rpc] method=%s, json_param=%s\n", method.c_str(), param.c_str()); + EXLOGV("[url-schema] method=%s, json_param=%s\n", method.c_str(), param.c_str()); Json::CharReaderBuilder jcrb; std::unique_ptr const jreader(jcrb.newCharReader()); @@ -214,12 +214,12 @@ void TsWsClient::url_scheme_handler(const std::string& url) ex_astr err; if (!jreader->parse(str_json_begin, str_json_begin + param.length(), &js_root, &err)) { - EXLOGE("[ws] param not in json format: %s\n", param.c_str()); + EXLOGE("[url-schema] param not in json format: %s\n", param.c_str()); return; } if (!js_root.isObject()) { - EXLOGE("[ws] invalid param, need json object: %s\n", param.c_str()); + EXLOGE("[url-schema] invalid param, need json object: %s\n", param.c_str()); return; } @@ -235,7 +235,7 @@ void TsWsClient::_process_register(const std::string& param, Json::Value& js_roo // check param if (!js_root["ws_url"].isString() || !js_root["assist_id"].isNumeric() || !js_root["session_id"].isString()) { - EXLOGE("[ws] invalid param: %s\n", param.c_str()); + EXLOGE("[url-schema] invalid param: %s\n", param.c_str()); return; } @@ -247,15 +247,15 @@ void TsWsClient::_process_register(const std::string& param, Json::Value& js_roo protocol.assign(ws_url, 0, 3); if (protocol == "ws:") { - g_ws_client._register(ws_url, assist_id, session_id); + g_ws_client._register(false, ws_url, assist_id, session_id); } else if (protocol == "wss") { - g_wss_client._register(ws_url, assist_id, session_id); + g_wss_client._register(true, ws_url, assist_id, session_id); } else { - EXLOGE("[ws] invalid ws_url: %s\n", ws_url.c_str()); + EXLOGE("[url-schema] invalid ws_url: %s\n", ws_url.c_str()); return; } } @@ -286,7 +286,7 @@ void TsWsClient::_thread_loop(void) EXLOGV("[ws] main loop end.\n"); } -void TsWsClient::_register(const std::string& ws_url, uint32_t assist_id, const std::string& session_id) +void TsWsClient::_register(bool is_ssl, const std::string& ws_url, uint32_t assist_id, const std::string& session_id) { if (m_assist_id == 0) m_assist_id = assist_id; @@ -307,7 +307,22 @@ void TsWsClient::_register(const std::string& ws_url, uint32_t assist_id, const std::string url = ws_url; url += msg; - m_nc = mg_connect_ws(&m_mg_mgr, _mg_event_handler, url.c_str(), NULL, NULL); + EXLOGD("mg_connect_ws: %s\n", url.c_str()); + + if (is_ssl) + { + struct mg_connect_opts opts; + memset(&opts, 0, sizeof(opts)); + opts.ssl_ca_cert = "*"; + opts.ssl_server_name = "*"; + m_nc = mg_connect_ws_opt(&m_mg_mgr, _mg_event_handler, opts, url.c_str(), "wss", NULL); + } + else + { + m_nc = mg_connect_ws(&m_mg_mgr, _mg_event_handler, url.c_str(), NULL, NULL); + } + + // m_nc = mg_connect_ws(&m_mg_mgr, _mg_event_handler, url.c_str(), NULL, NULL); if (!m_nc) { EXLOGE("[ws] TsWsClient::init failed: %s\n", url.c_str()); diff --git a/client/tp_assist_win/ts_ws_client.h b/client/tp_assist_win/ts_ws_client.h index 2f5f95a..e420662 100644 --- a/client/tp_assist_win/ts_ws_client.h +++ b/client/tp_assist_win/ts_ws_client.h @@ -44,7 +44,7 @@ protected: // bool _on_init(); private: - void _register(const std::string& ws_url, uint32_t assist_id, const std::string& session_id); + void _register(bool is_ssl, const std::string& ws_url, uint32_t assist_id, const std::string& session_id); void _on_message(const std::string& message, std::string& buf);