From 51142aefdf4c39fa47fde37600d4d7d2aa2322fc Mon Sep 17 00:00:00 2001 From: Cyan Date: Fri, 1 May 2020 14:01:40 +0800 Subject: [PATCH 1/8] =?UTF-8?q?=E4=BF=AE=E6=94=B9third-party=E4=B8=BAmirai?= =?UTF-8?q?-third-party;?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CMakeLists.txt | 4 +- .../ThreadPool.h | 0 mirai-third-party/easywsclient.cpp | 568 ++++++++++++++++++ mirai-third-party/easywsclient.hpp | 73 +++ {third-party => mirai-third-party}/httplib.h | 0 .../nlohmann/json.hpp | 0 6 files changed, 643 insertions(+), 2 deletions(-) rename {third-party => mirai-third-party}/ThreadPool.h (100%) create mode 100644 mirai-third-party/easywsclient.cpp create mode 100644 mirai-third-party/easywsclient.hpp rename {third-party => mirai-third-party}/httplib.h (100%) rename {third-party => mirai-third-party}/nlohmann/json.hpp (100%) diff --git a/CMakeLists.txt b/CMakeLists.txt index c5a8352..6649ad7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -27,7 +27,7 @@ target_include_directories(${PROJECT_NAME} PUBLIC $ ) target_include_directories(${PROJECT_NAME} PUBLIC - $ + $ $ ) @@ -63,7 +63,7 @@ if(MIRAI_CPP_INSTALL) ) install( - DIRECTORY third-party + DIRECTORY mirai-third-party DESTINATION ${CMAKE_INSTALL_PREFIX} ) diff --git a/third-party/ThreadPool.h b/mirai-third-party/ThreadPool.h similarity index 100% rename from third-party/ThreadPool.h rename to mirai-third-party/ThreadPool.h diff --git a/mirai-third-party/easywsclient.cpp b/mirai-third-party/easywsclient.cpp new file mode 100644 index 0000000..693f47e --- /dev/null +++ b/mirai-third-party/easywsclient.cpp @@ -0,0 +1,568 @@ + +#ifdef _WIN32 + #if defined(_MSC_VER) && !defined(_CRT_SECURE_NO_WARNINGS) + #define _CRT_SECURE_NO_WARNINGS // _CRT_SECURE_NO_WARNINGS for sscanf errors in MSVC2013 Express + #endif + #ifndef WIN32_LEAN_AND_MEAN + #define WIN32_LEAN_AND_MEAN + #endif + #include + #include + #include + #pragma comment( lib, "ws2_32" ) + #include + #include + #include + #include + #include + #ifndef _SSIZE_T_DEFINED + typedef int ssize_t; + #define _SSIZE_T_DEFINED + #endif + #ifndef _SOCKET_T_DEFINED + typedef SOCKET socket_t; + #define _SOCKET_T_DEFINED + #endif + #ifndef snprintf + #define snprintf _snprintf_s + #endif + #if _MSC_VER >=1600 + // vs2010 or later + #include + #else + typedef __int8 int8_t; + typedef unsigned __int8 uint8_t; + typedef __int32 int32_t; + typedef unsigned __int32 uint32_t; + typedef __int64 int64_t; + typedef unsigned __int64 uint64_t; + #endif + #define socketerrno WSAGetLastError() + #define SOCKET_EAGAIN_EINPROGRESS WSAEINPROGRESS + #define SOCKET_EWOULDBLOCK WSAEWOULDBLOCK +#else + #include + #include + #include + #include + #include + #include + #include + #include + #include + #include + #include + #ifndef _SOCKET_T_DEFINED + typedef int socket_t; + #define _SOCKET_T_DEFINED + #endif + #ifndef INVALID_SOCKET + #define INVALID_SOCKET (-1) + #endif + #ifndef SOCKET_ERROR + #define SOCKET_ERROR (-1) + #endif + #define closesocket(s) ::close(s) + #include + #define socketerrno errno + #define SOCKET_EAGAIN_EINPROGRESS EAGAIN + #define SOCKET_EWOULDBLOCK EWOULDBLOCK +#endif + +#include +#include +#include + +#include "easywsclient.hpp" + +using easywsclient::Callback_Imp; +using easywsclient::BytesCallback_Imp; + +namespace { // private module-only namespace + +socket_t hostname_connect(const std::string& hostname, int port) { + struct addrinfo hints; + struct addrinfo *result; + struct addrinfo *p; + int ret; + socket_t sockfd = INVALID_SOCKET; + char sport[16]; + memset(&hints, 0, sizeof(hints)); + hints.ai_family = AF_UNSPEC; + hints.ai_socktype = SOCK_STREAM; + snprintf(sport, 16, "%d", port); + if ((ret = getaddrinfo(hostname.c_str(), sport, &hints, &result)) != 0) + { + fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(ret)); + return 1; + } + for(p = result; p != NULL; p = p->ai_next) + { + sockfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol); + if (sockfd == INVALID_SOCKET) { continue; } + if (connect(sockfd, p->ai_addr, p->ai_addrlen) != SOCKET_ERROR) { + break; + } + closesocket(sockfd); + sockfd = INVALID_SOCKET; + } + freeaddrinfo(result); + return sockfd; +} + + +class _DummyWebSocket : public easywsclient::WebSocket +{ + public: + void poll(int timeout) { } + void send(const std::string& message) { } + void sendBinary(const std::string& message) { } + void sendBinary(const std::vector& message) { } + void sendPing() { } + void close() { } + readyStateValues getReadyState() const { return CLOSED; } + void _dispatch(Callback_Imp & callable) { } + void _dispatchBinary(BytesCallback_Imp& callable) { } +}; + + +class _RealWebSocket : public easywsclient::WebSocket +{ + public: + // http://tools.ietf.org/html/rfc6455#section-5.2 Base Framing Protocol + // + // 0 1 2 3 + // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 + // +-+-+-+-+-------+-+-------------+-------------------------------+ + // |F|R|R|R| opcode|M| Payload len | Extended payload length | + // |I|S|S|S| (4) |A| (7) | (16/64) | + // |N|V|V|V| |S| | (if payload len==126/127) | + // | |1|2|3| |K| | | + // +-+-+-+-+-------+-+-------------+ - - - - - - - - - - - - - - - + + // | Extended payload length continued, if payload len == 127 | + // + - - - - - - - - - - - - - - - +-------------------------------+ + // | |Masking-key, if MASK set to 1 | + // +-------------------------------+-------------------------------+ + // | Masking-key (continued) | Payload Data | + // +-------------------------------- - - - - - - - - - - - - - - - + + // : Payload Data continued ... : + // + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + // | Payload Data continued ... | + // +---------------------------------------------------------------+ + struct wsheader_type { + unsigned header_size; + bool fin; + bool mask; + enum opcode_type { + CONTINUATION = 0x0, + TEXT_FRAME = 0x1, + BINARY_FRAME = 0x2, + CLOSE = 8, + PING = 9, + PONG = 0xa, + } opcode; + int N0; + uint64_t N; + uint8_t masking_key[4]; + }; + + std::vector rxbuf; + std::vector txbuf; + std::vector receivedData; + + socket_t sockfd; + readyStateValues readyState; + bool useMask; + bool isRxBad; + + _RealWebSocket(socket_t sockfd, bool useMask) + : sockfd(sockfd) + , readyState(OPEN) + , useMask(useMask) + , isRxBad(false) { + } + + readyStateValues getReadyState() const { + return readyState; + } + + void poll(int timeout) { // timeout in milliseconds + if (readyState == CLOSED) { + if (timeout > 0) { + timeval tv = { timeout/1000, (timeout%1000) * 1000 }; + select(0, NULL, NULL, NULL, &tv); + } + return; + } + if (timeout != 0) { + fd_set rfds; + fd_set wfds; + timeval tv = { timeout/1000, (timeout%1000) * 1000 }; + FD_ZERO(&rfds); + FD_ZERO(&wfds); + FD_SET(sockfd, &rfds); + if (txbuf.size()) { FD_SET(sockfd, &wfds); } + select(sockfd + 1, &rfds, &wfds, 0, timeout > 0 ? &tv : 0); + } + while (true) { + // FD_ISSET(0, &rfds) will be true + int N = rxbuf.size(); + ssize_t ret; + rxbuf.resize(N + 1500); + ret = recv(sockfd, (char*)&rxbuf[0] + N, 1500, 0); + if (false) { } + else if (ret < 0 && (socketerrno == SOCKET_EWOULDBLOCK || socketerrno == SOCKET_EAGAIN_EINPROGRESS)) { + rxbuf.resize(N); + break; + } + else if (ret <= 0) { + rxbuf.resize(N); + closesocket(sockfd); + readyState = CLOSED; + throw std::runtime_error(ret < 0 ? "Connection error!" : "Connection closed!"); + // fputs(ret < 0 ? "Connection error!\n" : "Connection closed!\n", stderr); + break; + } + else { + rxbuf.resize(N + ret); + } + } + while (txbuf.size()) { + int ret = ::send(sockfd, (char*)&txbuf[0], txbuf.size(), 0); + if (false) { } // ?? + else if (ret < 0 && (socketerrno == SOCKET_EWOULDBLOCK || socketerrno == SOCKET_EAGAIN_EINPROGRESS)) { + break; + } + else if (ret <= 0) { + closesocket(sockfd); + readyState = CLOSED; + throw std::runtime_error(ret < 0 ? "Connection error!" : "Connection closed!"); + // fputs(ret < 0 ? "Connection error!\n" : "Connection closed!\n", stderr); + break; + } + else { + txbuf.erase(txbuf.begin(), txbuf.begin() + ret); + } + } + if (!txbuf.size() && readyState == CLOSING) { + closesocket(sockfd); + readyState = CLOSED; + } + } + + // Callable must have signature: void(const std::string & message). + // Should work with C functions, C++ functors, and C++11 std::function and + // lambda: + //template + //void dispatch(Callable callable) + virtual void _dispatch(Callback_Imp & callable) { + struct CallbackAdapter : public BytesCallback_Imp + // Adapt void(const std::string&) to void(const std::string&) + { + Callback_Imp& callable; + CallbackAdapter(Callback_Imp& callable) : callable(callable) { } + void operator()(const std::vector& message) { + std::string stringMessage(message.begin(), message.end()); + callable(stringMessage); + } + }; + CallbackAdapter bytesCallback(callable); + _dispatchBinary(bytesCallback); + } + + virtual void _dispatchBinary(BytesCallback_Imp & callable) { + // TODO: consider acquiring a lock on rxbuf... + if (isRxBad) { + return; + } + while (true) { + wsheader_type ws; + if (rxbuf.size() < 2) { return; /* Need at least 2 */ } + const uint8_t * data = (uint8_t *) &rxbuf[0]; // peek, but don't consume + ws.fin = (data[0] & 0x80) == 0x80; + ws.opcode = (wsheader_type::opcode_type) (data[0] & 0x0f); + ws.mask = (data[1] & 0x80) == 0x80; + ws.N0 = (data[1] & 0x7f); + ws.header_size = 2 + (ws.N0 == 126? 2 : 0) + (ws.N0 == 127? 8 : 0) + (ws.mask? 4 : 0); + if (rxbuf.size() < ws.header_size) { return; /* Need: ws.header_size - rxbuf.size() */ } + int i = 0; + if (ws.N0 < 126) { + ws.N = ws.N0; + i = 2; + } + else if (ws.N0 == 126) { + ws.N = 0; + ws.N |= ((uint64_t) data[2]) << 8; + ws.N |= ((uint64_t) data[3]) << 0; + i = 4; + } + else if (ws.N0 == 127) { + ws.N = 0; + ws.N |= ((uint64_t) data[2]) << 56; + ws.N |= ((uint64_t) data[3]) << 48; + ws.N |= ((uint64_t) data[4]) << 40; + ws.N |= ((uint64_t) data[5]) << 32; + ws.N |= ((uint64_t) data[6]) << 24; + ws.N |= ((uint64_t) data[7]) << 16; + ws.N |= ((uint64_t) data[8]) << 8; + ws.N |= ((uint64_t) data[9]) << 0; + i = 10; + if (ws.N & 0x8000000000000000ull) { + // https://tools.ietf.org/html/rfc6455 writes the "the most + // significant bit MUST be 0." + // + // We can't drop the frame, because (1) we don't we don't + // know how much data to skip over to find the next header, + // and (2) this would be an impractically long length, even + // if it were valid. So just close() and return immediately + // for now. + isRxBad = true; + // fprintf(stderr, "ERROR: Frame has invalid frame length. Closing.\n"); + close(); + throw std::runtime_error("ERROR: Frame has invalid frame length. Closing."); + return; + } + } + if (ws.mask) { + ws.masking_key[0] = ((uint8_t) data[i+0]) << 0; + ws.masking_key[1] = ((uint8_t) data[i+1]) << 0; + ws.masking_key[2] = ((uint8_t) data[i+2]) << 0; + ws.masking_key[3] = ((uint8_t) data[i+3]) << 0; + } + else { + ws.masking_key[0] = 0; + ws.masking_key[1] = 0; + ws.masking_key[2] = 0; + ws.masking_key[3] = 0; + } + + // Note: The checks above should hopefully ensure this addition + // cannot overflow: + if (rxbuf.size() < ws.header_size+ws.N) { return; /* Need: ws.header_size+ws.N - rxbuf.size() */ } + + // We got a whole message, now do something with it: + if (false) { } + else if ( + ws.opcode == wsheader_type::TEXT_FRAME + || ws.opcode == wsheader_type::BINARY_FRAME + || ws.opcode == wsheader_type::CONTINUATION + ) { + if (ws.mask) { for (size_t i = 0; i != ws.N; ++i) { rxbuf[i+ws.header_size] ^= ws.masking_key[i&0x3]; } } + receivedData.insert(receivedData.end(), rxbuf.begin()+ws.header_size, rxbuf.begin()+ws.header_size+(size_t)ws.N);// just feed + if (ws.fin) { + callable((const std::vector) receivedData); + receivedData.erase(receivedData.begin(), receivedData.end()); + std::vector ().swap(receivedData);// free memory + } + } + else if (ws.opcode == wsheader_type::PING) { + if (ws.mask) { for (size_t i = 0; i != ws.N; ++i) { rxbuf[i+ws.header_size] ^= ws.masking_key[i&0x3]; } } + std::string data(rxbuf.begin()+ws.header_size, rxbuf.begin()+ws.header_size+(size_t)ws.N); + sendData(wsheader_type::PONG, data.size(), data.begin(), data.end()); + } + else if (ws.opcode == wsheader_type::PONG) { } + else if (ws.opcode == wsheader_type::CLOSE) { close(); } + else + { + close(); + rxbuf.erase(rxbuf.begin(), rxbuf.begin() + ws.header_size + (size_t)ws.N); + throw std::runtime_error("ERROR: Got unexpected WebSocket message."); + // fprintf(stderr, "ERROR: Got unexpected WebSocket message.\n"); close(); + } + + rxbuf.erase(rxbuf.begin(), rxbuf.begin() + ws.header_size+(size_t)ws.N); + } + } + + void sendPing() { + std::string empty; + sendData(wsheader_type::PING, empty.size(), empty.begin(), empty.end()); + } + + void send(const std::string& message) { + sendData(wsheader_type::TEXT_FRAME, message.size(), message.begin(), message.end()); + } + + void sendBinary(const std::string& message) { + sendData(wsheader_type::BINARY_FRAME, message.size(), message.begin(), message.end()); + } + + void sendBinary(const std::vector& message) { + sendData(wsheader_type::BINARY_FRAME, message.size(), message.begin(), message.end()); + } + + template + void sendData(wsheader_type::opcode_type type, uint64_t message_size, Iterator message_begin, Iterator message_end) { + // TODO: + // Masking key should (must) be derived from a high quality random + // number generator, to mitigate attacks on non-WebSocket friendly + // middleware: + const uint8_t masking_key[4] = { 0x12, 0x34, 0x56, 0x78 }; + // TODO: consider acquiring a lock on txbuf... + if (readyState == CLOSING || readyState == CLOSED) { return; } + std::vector header; + header.assign(2 + (message_size >= 126 ? 2 : 0) + (message_size >= 65536 ? 6 : 0) + (useMask ? 4 : 0), 0); + header[0] = 0x80 | type; + if (false) { } + else if (message_size < 126) { + header[1] = (message_size & 0xff) | (useMask ? 0x80 : 0); + if (useMask) { + header[2] = masking_key[0]; + header[3] = masking_key[1]; + header[4] = masking_key[2]; + header[5] = masking_key[3]; + } + } + else if (message_size < 65536) { + header[1] = 126 | (useMask ? 0x80 : 0); + header[2] = (message_size >> 8) & 0xff; + header[3] = (message_size >> 0) & 0xff; + if (useMask) { + header[4] = masking_key[0]; + header[5] = masking_key[1]; + header[6] = masking_key[2]; + header[7] = masking_key[3]; + } + } + else { // TODO: run coverage testing here + header[1] = 127 | (useMask ? 0x80 : 0); + header[2] = (message_size >> 56) & 0xff; + header[3] = (message_size >> 48) & 0xff; + header[4] = (message_size >> 40) & 0xff; + header[5] = (message_size >> 32) & 0xff; + header[6] = (message_size >> 24) & 0xff; + header[7] = (message_size >> 16) & 0xff; + header[8] = (message_size >> 8) & 0xff; + header[9] = (message_size >> 0) & 0xff; + if (useMask) { + header[10] = masking_key[0]; + header[11] = masking_key[1]; + header[12] = masking_key[2]; + header[13] = masking_key[3]; + } + } + // N.B. - txbuf will keep growing until it can be transmitted over the socket: + txbuf.insert(txbuf.end(), header.begin(), header.end()); + txbuf.insert(txbuf.end(), message_begin, message_end); + if (useMask) { + size_t message_offset = txbuf.size() - message_size; + for (size_t i = 0; i != message_size; ++i) { + txbuf[message_offset + i] ^= masking_key[i&0x3]; + } + } + } + + void close() { + if(readyState == CLOSING || readyState == CLOSED) { return; } + readyState = CLOSING; + uint8_t closeFrame[6] = {0x88, 0x80, 0x00, 0x00, 0x00, 0x00}; // last 4 bytes are a masking key + std::vector header(closeFrame, closeFrame+6); + txbuf.insert(txbuf.end(), header.begin(), header.end()); + } + +}; + + +easywsclient::WebSocket::pointer from_url(const std::string& url, bool useMask, const std::string& origin) { + char host[512]; + int port; + char path[512]; + if (url.size() >= 512) { + throw std::runtime_error("ERROR: url size limit exceeded: " + url); + // fprintf(stderr, "ERROR: url size limit exceeded: %s\n", url.c_str()); + return NULL; + } + if (origin.size() >= 200) { + throw std::runtime_error("ERROR: origin size limit exceeded: " + origin); + // fprintf(stderr, "ERROR: origin size limit exceeded: %s\n", origin.c_str()); + return NULL; + } + if (false) { } + else if (sscanf(url.c_str(), "ws://%[^:/]:%d/%s", host, &port, path) == 3) { + } + else if (sscanf(url.c_str(), "ws://%[^:/]/%s", host, path) == 2) { + port = 80; + } + else if (sscanf(url.c_str(), "ws://%[^:/]:%d", host, &port) == 2) { + path[0] = '\0'; + } + else if (sscanf(url.c_str(), "ws://%[^:/]", host) == 1) { + port = 80; + path[0] = '\0'; + } + else { + throw std::runtime_error("ERROR: Could not parse WebSocket url: " + url); + // fprintf(stderr, "ERROR: Could not parse WebSocket url: %s\n", url.c_str()); + return NULL; + } + //fprintf(stderr, "easywsclient: connecting: host=%s port=%d path=/%s\n", host, port, path); + socket_t sockfd = hostname_connect(host, port); + if (sockfd == INVALID_SOCKET) { + throw std::runtime_error("Unable to connect to " + std::string(host) + ":" + std::to_string(port)); + // fprintf(stderr, "Unable to connect to %s:%d\n", host, port); + return NULL; + } + { + // XXX: this should be done non-blocking, + char line[1024]; + int status; + int i; + snprintf(line, 1024, "GET /%s HTTP/1.1\r\n", path); ::send(sockfd, line, strlen(line), 0); + if (port == 80) { + snprintf(line, 1024, "Host: %s\r\n", host); ::send(sockfd, line, strlen(line), 0); + } + else { + snprintf(line, 1024, "Host: %s:%d\r\n", host, port); ::send(sockfd, line, strlen(line), 0); + } + snprintf(line, 1024, "Upgrade: websocket\r\n"); ::send(sockfd, line, strlen(line), 0); + snprintf(line, 1024, "Connection: Upgrade\r\n"); ::send(sockfd, line, strlen(line), 0); + if (!origin.empty()) { + snprintf(line, 1024, "Origin: %s\r\n", origin.c_str()); ::send(sockfd, line, strlen(line), 0); + } + snprintf(line, 1024, "Sec-WebSocket-Key: x3JJHMbDL1EzLkh9GBhXDw==\r\n"); ::send(sockfd, line, strlen(line), 0); + snprintf(line, 1024, "Sec-WebSocket-Version: 13\r\n"); ::send(sockfd, line, strlen(line), 0); + snprintf(line, 1024, "\r\n"); ::send(sockfd, line, strlen(line), 0); + for (i = 0; i < 2 || (i < 1023 && line[i-2] != '\r' && line[i-1] != '\n'); ++i) { if (recv(sockfd, line+i, 1, 0) == 0) { return NULL; } } + line[i] = 0; + if (i == 1023) { fprintf(stderr, "ERROR: Got invalid status line connecting to: %s\n", url.c_str()); return NULL; } + if (sscanf(line, "HTTP/1.1 %d", &status) != 1 || status != 101) { fprintf(stderr, "ERROR: Got bad status connecting to %s: %s", url.c_str(), line); return NULL; } + // TODO: verify response headers, + while (true) { + for (i = 0; i < 2 || (i < 1023 && line[i-2] != '\r' && line[i-1] != '\n'); ++i) { if (recv(sockfd, line+i, 1, 0) == 0) { return NULL; } } + if (line[0] == '\r' && line[1] == '\n') { break; } + } + } + int flag = 1; + setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY, (char*) &flag, sizeof(flag)); // Disable Nagle's algorithm +#ifdef _WIN32 + u_long on = 1; + ioctlsocket(sockfd, FIONBIO, &on); +#else + fcntl(sockfd, F_SETFL, O_NONBLOCK); +#endif + //fprintf(stderr, "Connected to: %s\n", url.c_str()); + return easywsclient::WebSocket::pointer(new _RealWebSocket(sockfd, useMask)); +} + +} // end of module-only namespace + + + +namespace easywsclient { + +WebSocket::pointer WebSocket::create_dummy() { + static pointer dummy = pointer(new _DummyWebSocket); + return dummy; +} + + +WebSocket::pointer WebSocket::from_url(const std::string& url, const std::string& origin) { + return ::from_url(url, true, origin); +} + +WebSocket::pointer WebSocket::from_url_no_mask(const std::string& url, const std::string& origin) { + return ::from_url(url, false, origin); +} + + +} // namespace easywsclient diff --git a/mirai-third-party/easywsclient.hpp b/mirai-third-party/easywsclient.hpp new file mode 100644 index 0000000..2ffd9bc --- /dev/null +++ b/mirai-third-party/easywsclient.hpp @@ -0,0 +1,73 @@ +#ifndef EASYWSCLIENT_HPP_20120819_MIOFVASDTNUASZDQPLFD +#define EASYWSCLIENT_HPP_20120819_MIOFVASDTNUASZDQPLFD + +// This code comes from: +// https://github.com/dhbaird/easywsclient +// +// To get the latest version: +// wget https://raw.github.com/dhbaird/easywsclient/master/easywsclient.hpp +// wget https://raw.github.com/dhbaird/easywsclient/master/easywsclient.cpp + +#include +#include + +namespace easywsclient +{ + +struct Callback_Imp { virtual void operator()(const std::string& message) = 0; }; +struct BytesCallback_Imp { virtual void operator()(const std::vector& message) = 0; }; + +class WebSocket { + public: + typedef WebSocket * pointer; + typedef enum readyStateValues { CLOSING, CLOSED, CONNECTING, OPEN } readyStateValues; + + // Factories: + static pointer create_dummy(); + static pointer from_url(const std::string& url, const std::string& origin = std::string()); + static pointer from_url_no_mask(const std::string& url, const std::string& origin = std::string()); + + // Interfaces: + virtual ~WebSocket() { } + virtual void poll(int timeout = 0) = 0; // timeout in milliseconds + virtual void send(const std::string& message) = 0; + virtual void sendBinary(const std::string& message) = 0; + virtual void sendBinary(const std::vector& message) = 0; + virtual void sendPing() = 0; + virtual void close() = 0; + virtual readyStateValues getReadyState() const = 0; + + template + void dispatch(Callable callable) + // For callbacks that accept a string argument. + { // N.B. this is compatible with both C++11 lambdas, functors and C function pointers + struct _Callback : public Callback_Imp { + Callable& callable; + _Callback(Callable& callable) : callable(callable) { } + void operator()(const std::string& message) { callable(message); } + }; + _Callback callback(callable); + _dispatch(callback); + } + + template + void dispatchBinary(Callable callable) + // For callbacks that accept a std::vector argument. + { // N.B. this is compatible with both C++11 lambdas, functors and C function pointers + struct _Callback : public BytesCallback_Imp { + Callable& callable; + _Callback(Callable& callable) : callable(callable) { } + void operator()(const std::vector& message) { callable(message); } + }; + _Callback callback(callable); + _dispatchBinary(callback); + } + + protected: + virtual void _dispatch(Callback_Imp& callable) = 0; + virtual void _dispatchBinary(BytesCallback_Imp& callable) = 0; +}; + +} // namespace easywsclient + +#endif /* EASYWSCLIENT_HPP_20120819_MIOFVASDTNUASZDQPLFD */ diff --git a/third-party/httplib.h b/mirai-third-party/httplib.h similarity index 100% rename from third-party/httplib.h rename to mirai-third-party/httplib.h diff --git a/third-party/nlohmann/json.hpp b/mirai-third-party/nlohmann/json.hpp similarity index 100% rename from third-party/nlohmann/json.hpp rename to mirai-third-party/nlohmann/json.hpp From 2fb7645001b04b32d7b5881859dab8913e74b2d2 Mon Sep 17 00:00:00 2001 From: Cyan Date: Fri, 1 May 2020 14:52:20 +0800 Subject: [PATCH 2/8] =?UTF-8?q?=E6=8F=90=E5=8F=96=E5=88=86=E7=A6=BB?= =?UTF-8?q?=E5=A4=84=E7=90=86=E4=BA=8B=E4=BB=B6=E5=87=BD=E6=95=B0;?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- include/mirai_bot.hpp | 3 ++- src/mirai_bot.cpp | 38 ++++++++++++++++++++------------------ 2 files changed, 22 insertions(+), 19 deletions(-) diff --git a/include/mirai_bot.hpp b/include/mirai_bot.hpp index 6164aa6..0e0f9a4 100644 --- a/include/mirai_bot.hpp +++ b/include/mirai_bot.hpp @@ -117,7 +117,8 @@ namespace Cyan private: bool SessionVerify(); bool SessionRelease(); - unsigned int FetchMessagesAndEvents(unsigned int count = 10); + unsigned int FetchEvents(unsigned int count = 10); + void ProcessEvents(const nlohmann::json& ele); template inline WeakEvent MakeWeakEvent(const json& json_) { diff --git a/src/mirai_bot.cpp b/src/mirai_bot.cpp index fae9b97..e4b74a3 100644 --- a/src/mirai_bot.cpp +++ b/src/mirai_bot.cpp @@ -571,7 +571,7 @@ namespace Cyan unsigned count = 0; try { - count = FetchMessagesAndEvents(count_per_loop); + count = FetchEvents(count_per_loop); } catch (const std::exception& ex) { @@ -656,7 +656,7 @@ namespace Cyan } - unsigned int MiraiBot::FetchMessagesAndEvents(unsigned int count) + unsigned int MiraiBot::FetchEvents(unsigned int count) { int received_count = 0; stringstream api_url; @@ -689,24 +689,9 @@ namespace Cyan } for (const auto& ele : reJson["data"]) { - string event_name = ele["type"].get(); - MiraiEvent mirai_event = MiraiEventStr(event_name); - // 寻找能处理事件的 Processor - auto pit = processors_.find(mirai_event); - if (pit != processors_.end()) - { - auto exector = pit->second; - WeakEvent pevent = CreateEvent(mirai_event, ele); - pool_.enqueue([=]() - { - exector(pevent); - }); - - } + ProcessEvents(ele); received_count++; } - - } else throw std::runtime_error("网络错误"); @@ -714,6 +699,23 @@ namespace Cyan } + void MiraiBot::ProcessEvents(const nlohmann::json& ele) + { + string event_name = ele["type"].get(); + MiraiEvent mirai_event = MiraiEventStr(event_name); + // 寻找能处理事件的 Processor + auto pit = processors_.find(mirai_event); + if (pit != processors_.end()) + { + auto exector = pit->second; + WeakEvent pevent = CreateEvent(mirai_event, ele); + pool_.enqueue([=]() + { + exector(pevent); + }); + } + } + WeakEvent MiraiBot::CreateEvent(MiraiEvent mirai_event, const json& json_) { From fbcaec7362595bfb74bd7e84df0988504bf20abf Mon Sep 17 00:00:00 2001 From: Cyan Date: Fri, 1 May 2020 15:25:49 +0800 Subject: [PATCH 3/8] =?UTF-8?q?=E4=BF=AE=E6=94=B9wsclient=E5=BA=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- mirai-third-party/easywsclient.cpp | 616 ++++++++++++++++++----------- mirai-third-party/easywsclient.hpp | 69 ++-- 2 files changed, 424 insertions(+), 261 deletions(-) diff --git a/mirai-third-party/easywsclient.cpp b/mirai-third-party/easywsclient.cpp index 693f47e..21a8316 100644 --- a/mirai-third-party/easywsclient.cpp +++ b/mirai-third-party/easywsclient.cpp @@ -1,72 +1,72 @@ #ifdef _WIN32 - #if defined(_MSC_VER) && !defined(_CRT_SECURE_NO_WARNINGS) - #define _CRT_SECURE_NO_WARNINGS // _CRT_SECURE_NO_WARNINGS for sscanf errors in MSVC2013 Express - #endif - #ifndef WIN32_LEAN_AND_MEAN - #define WIN32_LEAN_AND_MEAN - #endif - #include - #include - #include - #pragma comment( lib, "ws2_32" ) - #include - #include - #include - #include - #include - #ifndef _SSIZE_T_DEFINED - typedef int ssize_t; - #define _SSIZE_T_DEFINED - #endif - #ifndef _SOCKET_T_DEFINED - typedef SOCKET socket_t; - #define _SOCKET_T_DEFINED - #endif - #ifndef snprintf - #define snprintf _snprintf_s - #endif - #if _MSC_VER >=1600 - // vs2010 or later - #include - #else - typedef __int8 int8_t; - typedef unsigned __int8 uint8_t; - typedef __int32 int32_t; - typedef unsigned __int32 uint32_t; - typedef __int64 int64_t; - typedef unsigned __int64 uint64_t; - #endif - #define socketerrno WSAGetLastError() - #define SOCKET_EAGAIN_EINPROGRESS WSAEINPROGRESS - #define SOCKET_EWOULDBLOCK WSAEWOULDBLOCK +#if defined(_MSC_VER) && !defined(_CRT_SECURE_NO_WARNINGS) +#define _CRT_SECURE_NO_WARNINGS // _CRT_SECURE_NO_WARNINGS for sscanf errors in MSVC2013 Express +#endif +#ifndef WIN32_LEAN_AND_MEAN +#define WIN32_LEAN_AND_MEAN +#endif +#include +#include +#include +#pragma comment(lib, "ws2_32") +#include +#include +#include +#include +#include +#ifndef _SSIZE_T_DEFINED +typedef int ssize_t; +#define _SSIZE_T_DEFINED +#endif +#ifndef _SOCKET_T_DEFINED +typedef SOCKET socket_t; +#define _SOCKET_T_DEFINED +#endif +#ifndef snprintf +#define snprintf _snprintf_s +#endif +#if _MSC_VER >= 1600 +// vs2010 or later +#include +#else +typedef __int8 int8_t; +typedef unsigned __int8 uint8_t; +typedef __int32 int32_t; +typedef unsigned __int32 uint32_t; +typedef __int64 int64_t; +typedef unsigned __int64 uint64_t; +#endif +#define socketerrno WSAGetLastError() +#define SOCKET_EAGAIN_EINPROGRESS WSAEINPROGRESS +#define SOCKET_EWOULDBLOCK WSAEWOULDBLOCK #else - #include - #include - #include - #include - #include - #include - #include - #include - #include - #include - #include - #ifndef _SOCKET_T_DEFINED - typedef int socket_t; - #define _SOCKET_T_DEFINED - #endif - #ifndef INVALID_SOCKET - #define INVALID_SOCKET (-1) - #endif - #ifndef SOCKET_ERROR - #define SOCKET_ERROR (-1) - #endif - #define closesocket(s) ::close(s) - #include - #define socketerrno errno - #define SOCKET_EAGAIN_EINPROGRESS EAGAIN - #define SOCKET_EWOULDBLOCK EWOULDBLOCK +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#ifndef _SOCKET_T_DEFINED +typedef int socket_t; +#define _SOCKET_T_DEFINED +#endif +#ifndef INVALID_SOCKET +#define INVALID_SOCKET (-1) +#endif +#ifndef SOCKET_ERROR +#define SOCKET_ERROR (-1) +#endif +#define closesocket(s) ::close(s) +#include +#define socketerrno errno +#define SOCKET_EAGAIN_EINPROGRESS EAGAIN +#define SOCKET_EWOULDBLOCK EWOULDBLOCK #endif #include @@ -75,12 +75,14 @@ #include "easywsclient.hpp" -using easywsclient::Callback_Imp; using easywsclient::BytesCallback_Imp; +using easywsclient::Callback_Imp; -namespace { // private module-only namespace +namespace +{ // private module-only namespace -socket_t hostname_connect(const std::string& hostname, int port) { +socket_t hostname_connect(const std::string &hostname, int port) +{ struct addrinfo hints; struct addrinfo *result; struct addrinfo *p; @@ -93,14 +95,18 @@ socket_t hostname_connect(const std::string& hostname, int port) { snprintf(sport, 16, "%d", port); if ((ret = getaddrinfo(hostname.c_str(), sport, &hints, &result)) != 0) { - fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(ret)); - return 1; + fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(ret)); + return 1; } - for(p = result; p != NULL; p = p->ai_next) + for (p = result; p != NULL; p = p->ai_next) { sockfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol); - if (sockfd == INVALID_SOCKET) { continue; } - if (connect(sockfd, p->ai_addr, p->ai_addrlen) != SOCKET_ERROR) { + if (sockfd == INVALID_SOCKET) + { + continue; + } + if (connect(sockfd, p->ai_addr, p->ai_addrlen) != SOCKET_ERROR) + { break; } closesocket(sockfd); @@ -110,25 +116,23 @@ socket_t hostname_connect(const std::string& hostname, int port) { return sockfd; } - class _DummyWebSocket : public easywsclient::WebSocket { - public: - void poll(int timeout) { } - void send(const std::string& message) { } - void sendBinary(const std::string& message) { } - void sendBinary(const std::vector& message) { } - void sendPing() { } - void close() { } +public: + void poll(int timeout) {} + void send(const std::string &message) {} + void sendBinary(const std::string &message) {} + void sendBinary(const std::vector &message) {} + void sendPing() {} + void close() {} readyStateValues getReadyState() const { return CLOSED; } - void _dispatch(Callback_Imp & callable) { } - void _dispatchBinary(BytesCallback_Imp& callable) { } + void _dispatch(Callback_Imp &callable) {} + void _dispatchBinary(BytesCallback_Imp &callable) {} }; - class _RealWebSocket : public easywsclient::WebSocket { - public: +public: // http://tools.ietf.org/html/rfc6455#section-5.2 Base Framing Protocol // // 0 1 2 3 @@ -149,11 +153,13 @@ class _RealWebSocket : public easywsclient::WebSocket // + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + // | Payload Data continued ... | // +---------------------------------------------------------------+ - struct wsheader_type { + struct wsheader_type + { unsigned header_size; bool fin; bool mask; - enum opcode_type { + enum opcode_type + { CONTINUATION = 0x0, TEXT_FRAME = 0x1, BINARY_FRAME = 0x2, @@ -176,46 +182,74 @@ class _RealWebSocket : public easywsclient::WebSocket bool isRxBad; _RealWebSocket(socket_t sockfd, bool useMask) - : sockfd(sockfd) - , readyState(OPEN) - , useMask(useMask) - , isRxBad(false) { + : sockfd(sockfd), readyState(OPEN), useMask(useMask), isRxBad(false) + { +#ifdef _WIN32 + INT rc; + WSADATA wsaData; + + rc = WSAStartup(MAKEWORD(2, 2), &wsaData); + if (rc) + { + throw std::runtime_error("WSAStartup Failed."); + } +#endif + } + + ~_RealWebSocket() + { +#ifdef _WIN32 + WSACleanup(); +#endif } - readyStateValues getReadyState() const { - return readyState; + readyStateValues getReadyState() const + { + return readyState; } - void poll(int timeout) { // timeout in milliseconds - if (readyState == CLOSED) { - if (timeout > 0) { - timeval tv = { timeout/1000, (timeout%1000) * 1000 }; + void poll(int timeout) + { // timeout in milliseconds + if (readyState == CLOSED) + { + if (timeout > 0) + { + timeval tv = {timeout / 1000, (timeout % 1000) * 1000}; select(0, NULL, NULL, NULL, &tv); } return; } - if (timeout != 0) { + if (timeout != 0) + { fd_set rfds; fd_set wfds; - timeval tv = { timeout/1000, (timeout%1000) * 1000 }; + timeval tv = {timeout / 1000, (timeout % 1000) * 1000}; FD_ZERO(&rfds); FD_ZERO(&wfds); FD_SET(sockfd, &rfds); - if (txbuf.size()) { FD_SET(sockfd, &wfds); } + if (txbuf.size()) + { + FD_SET(sockfd, &wfds); + } select(sockfd + 1, &rfds, &wfds, 0, timeout > 0 ? &tv : 0); } - while (true) { + while (true) + { // FD_ISSET(0, &rfds) will be true int N = rxbuf.size(); ssize_t ret; rxbuf.resize(N + 1500); - ret = recv(sockfd, (char*)&rxbuf[0] + N, 1500, 0); - if (false) { } - else if (ret < 0 && (socketerrno == SOCKET_EWOULDBLOCK || socketerrno == SOCKET_EAGAIN_EINPROGRESS)) { + ret = recv(sockfd, (char *)&rxbuf[0] + N, 1500, 0); + if (false) + { + } + else if (ret < 0 && (socketerrno == SOCKET_EWOULDBLOCK || socketerrno == SOCKET_EAGAIN_EINPROGRESS)) + { rxbuf.resize(N); break; } - else if (ret <= 0) { + else if (ret <= 0) + { rxbuf.resize(N); closesocket(sockfd); readyState = CLOSED; @@ -223,28 +257,36 @@ class _RealWebSocket : public easywsclient::WebSocket // fputs(ret < 0 ? "Connection error!\n" : "Connection closed!\n", stderr); break; } - else { + else + { rxbuf.resize(N + ret); } } - while (txbuf.size()) { - int ret = ::send(sockfd, (char*)&txbuf[0], txbuf.size(), 0); - if (false) { } // ?? - else if (ret < 0 && (socketerrno == SOCKET_EWOULDBLOCK || socketerrno == SOCKET_EAGAIN_EINPROGRESS)) { + while (txbuf.size()) + { + int ret = ::send(sockfd, (char *)&txbuf[0], txbuf.size(), 0); + if (false) + { + } // ?? + else if (ret < 0 && (socketerrno == SOCKET_EWOULDBLOCK || socketerrno == SOCKET_EAGAIN_EINPROGRESS)) + { break; } - else if (ret <= 0) { + else if (ret <= 0) + { closesocket(sockfd); readyState = CLOSED; throw std::runtime_error(ret < 0 ? "Connection error!" : "Connection closed!"); // fputs(ret < 0 ? "Connection error!\n" : "Connection closed!\n", stderr); break; } - else { + else + { txbuf.erase(txbuf.begin(), txbuf.begin() + ret); } } - if (!txbuf.size() && readyState == CLOSING) { + if (!txbuf.size() && readyState == CLOSING) + { closesocket(sockfd); readyState = CLOSED; } @@ -255,13 +297,15 @@ class _RealWebSocket : public easywsclient::WebSocket // lambda: //template //void dispatch(Callable callable) - virtual void _dispatch(Callback_Imp & callable) { + virtual void _dispatch(Callback_Imp &callable) + { struct CallbackAdapter : public BytesCallback_Imp - // Adapt void(const std::string&) to void(const std::string&) + // Adapt void(const std::string&) to void(const std::string&) { - Callback_Imp& callable; - CallbackAdapter(Callback_Imp& callable) : callable(callable) { } - void operator()(const std::vector& message) { + Callback_Imp &callable; + CallbackAdapter(Callback_Imp &callable) : callable(callable) {} + void operator()(const std::vector &message) + { std::string stringMessage(message.begin(), message.end()); callable(stringMessage); } @@ -270,44 +314,57 @@ class _RealWebSocket : public easywsclient::WebSocket _dispatchBinary(bytesCallback); } - virtual void _dispatchBinary(BytesCallback_Imp & callable) { + virtual void _dispatchBinary(BytesCallback_Imp &callable) + { // TODO: consider acquiring a lock on rxbuf... - if (isRxBad) { + if (isRxBad) + { return; } - while (true) { + while (true) + { wsheader_type ws; - if (rxbuf.size() < 2) { return; /* Need at least 2 */ } - const uint8_t * data = (uint8_t *) &rxbuf[0]; // peek, but don't consume + if (rxbuf.size() < 2) + { + return; /* Need at least 2 */ + } + const uint8_t *data = (uint8_t *)&rxbuf[0]; // peek, but don't consume ws.fin = (data[0] & 0x80) == 0x80; - ws.opcode = (wsheader_type::opcode_type) (data[0] & 0x0f); + ws.opcode = (wsheader_type::opcode_type)(data[0] & 0x0f); ws.mask = (data[1] & 0x80) == 0x80; ws.N0 = (data[1] & 0x7f); - ws.header_size = 2 + (ws.N0 == 126? 2 : 0) + (ws.N0 == 127? 8 : 0) + (ws.mask? 4 : 0); - if (rxbuf.size() < ws.header_size) { return; /* Need: ws.header_size - rxbuf.size() */ } + ws.header_size = 2 + (ws.N0 == 126 ? 2 : 0) + (ws.N0 == 127 ? 8 : 0) + (ws.mask ? 4 : 0); + if (rxbuf.size() < ws.header_size) + { + return; /* Need: ws.header_size - rxbuf.size() */ + } int i = 0; - if (ws.N0 < 126) { + if (ws.N0 < 126) + { ws.N = ws.N0; i = 2; } - else if (ws.N0 == 126) { + else if (ws.N0 == 126) + { ws.N = 0; - ws.N |= ((uint64_t) data[2]) << 8; - ws.N |= ((uint64_t) data[3]) << 0; + ws.N |= ((uint64_t)data[2]) << 8; + ws.N |= ((uint64_t)data[3]) << 0; i = 4; } - else if (ws.N0 == 127) { + else if (ws.N0 == 127) + { ws.N = 0; - ws.N |= ((uint64_t) data[2]) << 56; - ws.N |= ((uint64_t) data[3]) << 48; - ws.N |= ((uint64_t) data[4]) << 40; - ws.N |= ((uint64_t) data[5]) << 32; - ws.N |= ((uint64_t) data[6]) << 24; - ws.N |= ((uint64_t) data[7]) << 16; - ws.N |= ((uint64_t) data[8]) << 8; - ws.N |= ((uint64_t) data[9]) << 0; + ws.N |= ((uint64_t)data[2]) << 56; + ws.N |= ((uint64_t)data[3]) << 48; + ws.N |= ((uint64_t)data[4]) << 40; + ws.N |= ((uint64_t)data[5]) << 32; + ws.N |= ((uint64_t)data[6]) << 24; + ws.N |= ((uint64_t)data[7]) << 16; + ws.N |= ((uint64_t)data[8]) << 8; + ws.N |= ((uint64_t)data[9]) << 0; i = 10; - if (ws.N & 0x8000000000000000ull) { + if (ws.N & 0x8000000000000000ull) + { // https://tools.ietf.org/html/rfc6455 writes the "the most // significant bit MUST be 0." // @@ -323,13 +380,15 @@ class _RealWebSocket : public easywsclient::WebSocket return; } } - if (ws.mask) { - ws.masking_key[0] = ((uint8_t) data[i+0]) << 0; - ws.masking_key[1] = ((uint8_t) data[i+1]) << 0; - ws.masking_key[2] = ((uint8_t) data[i+2]) << 0; - ws.masking_key[3] = ((uint8_t) data[i+3]) << 0; + if (ws.mask) + { + ws.masking_key[0] = ((uint8_t)data[i + 0]) << 0; + ws.masking_key[1] = ((uint8_t)data[i + 1]) << 0; + ws.masking_key[2] = ((uint8_t)data[i + 2]) << 0; + ws.masking_key[3] = ((uint8_t)data[i + 3]) << 0; } - else { + else + { ws.masking_key[0] = 0; ws.masking_key[1] = 0; ws.masking_key[2] = 0; @@ -338,93 +397,130 @@ class _RealWebSocket : public easywsclient::WebSocket // Note: The checks above should hopefully ensure this addition // cannot overflow: - if (rxbuf.size() < ws.header_size+ws.N) { return; /* Need: ws.header_size+ws.N - rxbuf.size() */ } + if (rxbuf.size() < ws.header_size + ws.N) + { + return; /* Need: ws.header_size+ws.N - rxbuf.size() */ + } // We got a whole message, now do something with it: - if (false) { } + if (false) + { + } else if ( - ws.opcode == wsheader_type::TEXT_FRAME - || ws.opcode == wsheader_type::BINARY_FRAME - || ws.opcode == wsheader_type::CONTINUATION - ) { - if (ws.mask) { for (size_t i = 0; i != ws.N; ++i) { rxbuf[i+ws.header_size] ^= ws.masking_key[i&0x3]; } } - receivedData.insert(receivedData.end(), rxbuf.begin()+ws.header_size, rxbuf.begin()+ws.header_size+(size_t)ws.N);// just feed - if (ws.fin) { - callable((const std::vector) receivedData); + ws.opcode == wsheader_type::TEXT_FRAME || ws.opcode == wsheader_type::BINARY_FRAME || ws.opcode == wsheader_type::CONTINUATION) + { + if (ws.mask) + { + for (size_t i = 0; i != ws.N; ++i) + { + rxbuf[i + ws.header_size] ^= ws.masking_key[i & 0x3]; + } + } + receivedData.insert(receivedData.end(), rxbuf.begin() + ws.header_size, rxbuf.begin() + ws.header_size + (size_t)ws.N); // just feed + if (ws.fin) + { + callable((const std::vector)receivedData); receivedData.erase(receivedData.begin(), receivedData.end()); - std::vector ().swap(receivedData);// free memory + std::vector().swap(receivedData); // free memory } } - else if (ws.opcode == wsheader_type::PING) { - if (ws.mask) { for (size_t i = 0; i != ws.N; ++i) { rxbuf[i+ws.header_size] ^= ws.masking_key[i&0x3]; } } - std::string data(rxbuf.begin()+ws.header_size, rxbuf.begin()+ws.header_size+(size_t)ws.N); + else if (ws.opcode == wsheader_type::PING) + { + if (ws.mask) + { + for (size_t i = 0; i != ws.N; ++i) + { + rxbuf[i + ws.header_size] ^= ws.masking_key[i & 0x3]; + } + } + std::string data(rxbuf.begin() + ws.header_size, rxbuf.begin() + ws.header_size + (size_t)ws.N); sendData(wsheader_type::PONG, data.size(), data.begin(), data.end()); } - else if (ws.opcode == wsheader_type::PONG) { } - else if (ws.opcode == wsheader_type::CLOSE) { close(); } - else - { + else if (ws.opcode == wsheader_type::PONG) + { + } + else if (ws.opcode == wsheader_type::CLOSE) + { + close(); + } + else + { close(); rxbuf.erase(rxbuf.begin(), rxbuf.begin() + ws.header_size + (size_t)ws.N); throw std::runtime_error("ERROR: Got unexpected WebSocket message."); - // fprintf(stderr, "ERROR: Got unexpected WebSocket message.\n"); close(); + // fprintf(stderr, "ERROR: Got unexpected WebSocket message.\n"); close(); } - rxbuf.erase(rxbuf.begin(), rxbuf.begin() + ws.header_size+(size_t)ws.N); + rxbuf.erase(rxbuf.begin(), rxbuf.begin() + ws.header_size + (size_t)ws.N); } } - void sendPing() { + void sendPing() + { std::string empty; sendData(wsheader_type::PING, empty.size(), empty.begin(), empty.end()); } - void send(const std::string& message) { + void send(const std::string &message) + { sendData(wsheader_type::TEXT_FRAME, message.size(), message.begin(), message.end()); } - void sendBinary(const std::string& message) { + void sendBinary(const std::string &message) + { sendData(wsheader_type::BINARY_FRAME, message.size(), message.begin(), message.end()); } - void sendBinary(const std::vector& message) { + void sendBinary(const std::vector &message) + { sendData(wsheader_type::BINARY_FRAME, message.size(), message.begin(), message.end()); } - template - void sendData(wsheader_type::opcode_type type, uint64_t message_size, Iterator message_begin, Iterator message_end) { + template + void sendData(wsheader_type::opcode_type type, uint64_t message_size, Iterator message_begin, Iterator message_end) + { // TODO: // Masking key should (must) be derived from a high quality random // number generator, to mitigate attacks on non-WebSocket friendly // middleware: - const uint8_t masking_key[4] = { 0x12, 0x34, 0x56, 0x78 }; + const uint8_t masking_key[4] = {0x12, 0x34, 0x56, 0x78}; // TODO: consider acquiring a lock on txbuf... - if (readyState == CLOSING || readyState == CLOSED) { return; } + if (readyState == CLOSING || readyState == CLOSED) + { + return; + } std::vector header; header.assign(2 + (message_size >= 126 ? 2 : 0) + (message_size >= 65536 ? 6 : 0) + (useMask ? 4 : 0), 0); header[0] = 0x80 | type; - if (false) { } - else if (message_size < 126) { + if (false) + { + } + else if (message_size < 126) + { header[1] = (message_size & 0xff) | (useMask ? 0x80 : 0); - if (useMask) { + if (useMask) + { header[2] = masking_key[0]; header[3] = masking_key[1]; header[4] = masking_key[2]; header[5] = masking_key[3]; } } - else if (message_size < 65536) { + else if (message_size < 65536) + { header[1] = 126 | (useMask ? 0x80 : 0); header[2] = (message_size >> 8) & 0xff; header[3] = (message_size >> 0) & 0xff; - if (useMask) { + if (useMask) + { header[4] = masking_key[0]; header[5] = masking_key[1]; header[6] = masking_key[2]; header[7] = masking_key[3]; } } - else { // TODO: run coverage testing here + else + { // TODO: run coverage testing here header[1] = 127 | (useMask ? 0x80 : 0); header[2] = (message_size >> 56) & 0xff; header[3] = (message_size >> 48) & 0xff; @@ -432,9 +528,10 @@ class _RealWebSocket : public easywsclient::WebSocket header[5] = (message_size >> 32) & 0xff; header[6] = (message_size >> 24) & 0xff; header[7] = (message_size >> 16) & 0xff; - header[8] = (message_size >> 8) & 0xff; - header[9] = (message_size >> 0) & 0xff; - if (useMask) { + header[8] = (message_size >> 8) & 0xff; + header[9] = (message_size >> 0) & 0xff; + if (useMask) + { header[10] = masking_key[0]; header[11] = masking_key[1]; header[12] = masking_key[2]; @@ -444,60 +541,75 @@ class _RealWebSocket : public easywsclient::WebSocket // N.B. - txbuf will keep growing until it can be transmitted over the socket: txbuf.insert(txbuf.end(), header.begin(), header.end()); txbuf.insert(txbuf.end(), message_begin, message_end); - if (useMask) { + if (useMask) + { size_t message_offset = txbuf.size() - message_size; - for (size_t i = 0; i != message_size; ++i) { - txbuf[message_offset + i] ^= masking_key[i&0x3]; + for (size_t i = 0; i != message_size; ++i) + { + txbuf[message_offset + i] ^= masking_key[i & 0x3]; } } } - void close() { - if(readyState == CLOSING || readyState == CLOSED) { return; } + void close() + { + if (readyState == CLOSING || readyState == CLOSED) + { + return; + } readyState = CLOSING; uint8_t closeFrame[6] = {0x88, 0x80, 0x00, 0x00, 0x00, 0x00}; // last 4 bytes are a masking key - std::vector header(closeFrame, closeFrame+6); + std::vector header(closeFrame, closeFrame + 6); txbuf.insert(txbuf.end(), header.begin(), header.end()); } - }; - -easywsclient::WebSocket::pointer from_url(const std::string& url, bool useMask, const std::string& origin) { +easywsclient::WebSocket::pointer from_url(const std::string &url, bool useMask, const std::string &origin) +{ char host[512]; int port; char path[512]; - if (url.size() >= 512) { + if (url.size() >= 512) + { throw std::runtime_error("ERROR: url size limit exceeded: " + url); - // fprintf(stderr, "ERROR: url size limit exceeded: %s\n", url.c_str()); - return NULL; + // fprintf(stderr, "ERROR: url size limit exceeded: %s\n", url.c_str()); + return NULL; } - if (origin.size() >= 200) { + if (origin.size() >= 200) + { throw std::runtime_error("ERROR: origin size limit exceeded: " + origin); - // fprintf(stderr, "ERROR: origin size limit exceeded: %s\n", origin.c_str()); - return NULL; + // fprintf(stderr, "ERROR: origin size limit exceeded: %s\n", origin.c_str()); + return NULL; + } + if (false) + { } - if (false) { } - else if (sscanf(url.c_str(), "ws://%[^:/]:%d/%s", host, &port, path) == 3) { + else if (sscanf(url.c_str(), "ws://%[^:/]:%d/%s", host, &port, path) == 3) + { } - else if (sscanf(url.c_str(), "ws://%[^:/]/%s", host, path) == 2) { + else if (sscanf(url.c_str(), "ws://%[^:/]/%s", host, path) == 2) + { port = 80; } - else if (sscanf(url.c_str(), "ws://%[^:/]:%d", host, &port) == 2) { + else if (sscanf(url.c_str(), "ws://%[^:/]:%d", host, &port) == 2) + { path[0] = '\0'; } - else if (sscanf(url.c_str(), "ws://%[^:/]", host) == 1) { + else if (sscanf(url.c_str(), "ws://%[^:/]", host) == 1) + { port = 80; path[0] = '\0'; } - else { + else + { throw std::runtime_error("ERROR: Could not parse WebSocket url: " + url); // fprintf(stderr, "ERROR: Could not parse WebSocket url: %s\n", url.c_str()); return NULL; } //fprintf(stderr, "easywsclient: connecting: host=%s port=%d path=/%s\n", host, port, path); socket_t sockfd = hostname_connect(host, port); - if (sockfd == INVALID_SOCKET) { + if (sockfd == INVALID_SOCKET) + { throw std::runtime_error("Unable to connect to " + std::string(host) + ":" + std::to_string(port)); // fprintf(stderr, "Unable to connect to %s:%d\n", host, port); return NULL; @@ -507,33 +619,69 @@ easywsclient::WebSocket::pointer from_url(const std::string& url, bool useMask, char line[1024]; int status; int i; - snprintf(line, 1024, "GET /%s HTTP/1.1\r\n", path); ::send(sockfd, line, strlen(line), 0); - if (port == 80) { - snprintf(line, 1024, "Host: %s\r\n", host); ::send(sockfd, line, strlen(line), 0); + snprintf(line, 1024, "GET /%s HTTP/1.1\r\n", path); + ::send(sockfd, line, strlen(line), 0); + if (port == 80) + { + snprintf(line, 1024, "Host: %s\r\n", host); + ::send(sockfd, line, strlen(line), 0); } - else { - snprintf(line, 1024, "Host: %s:%d\r\n", host, port); ::send(sockfd, line, strlen(line), 0); + else + { + snprintf(line, 1024, "Host: %s:%d\r\n", host, port); + ::send(sockfd, line, strlen(line), 0); } - snprintf(line, 1024, "Upgrade: websocket\r\n"); ::send(sockfd, line, strlen(line), 0); - snprintf(line, 1024, "Connection: Upgrade\r\n"); ::send(sockfd, line, strlen(line), 0); - if (!origin.empty()) { - snprintf(line, 1024, "Origin: %s\r\n", origin.c_str()); ::send(sockfd, line, strlen(line), 0); + snprintf(line, 1024, "Upgrade: websocket\r\n"); + ::send(sockfd, line, strlen(line), 0); + snprintf(line, 1024, "Connection: Upgrade\r\n"); + ::send(sockfd, line, strlen(line), 0); + if (!origin.empty()) + { + snprintf(line, 1024, "Origin: %s\r\n", origin.c_str()); + ::send(sockfd, line, strlen(line), 0); + } + snprintf(line, 1024, "Sec-WebSocket-Key: x3JJHMbDL1EzLkh9GBhXDw==\r\n"); + ::send(sockfd, line, strlen(line), 0); + snprintf(line, 1024, "Sec-WebSocket-Version: 13\r\n"); + ::send(sockfd, line, strlen(line), 0); + snprintf(line, 1024, "\r\n"); + ::send(sockfd, line, strlen(line), 0); + for (i = 0; i < 2 || (i < 1023 && line[i - 2] != '\r' && line[i - 1] != '\n'); ++i) + { + if (recv(sockfd, line + i, 1, 0) == 0) + { + return NULL; + } } - snprintf(line, 1024, "Sec-WebSocket-Key: x3JJHMbDL1EzLkh9GBhXDw==\r\n"); ::send(sockfd, line, strlen(line), 0); - snprintf(line, 1024, "Sec-WebSocket-Version: 13\r\n"); ::send(sockfd, line, strlen(line), 0); - snprintf(line, 1024, "\r\n"); ::send(sockfd, line, strlen(line), 0); - for (i = 0; i < 2 || (i < 1023 && line[i-2] != '\r' && line[i-1] != '\n'); ++i) { if (recv(sockfd, line+i, 1, 0) == 0) { return NULL; } } line[i] = 0; - if (i == 1023) { fprintf(stderr, "ERROR: Got invalid status line connecting to: %s\n", url.c_str()); return NULL; } - if (sscanf(line, "HTTP/1.1 %d", &status) != 1 || status != 101) { fprintf(stderr, "ERROR: Got bad status connecting to %s: %s", url.c_str(), line); return NULL; } + if (i == 1023) + { + fprintf(stderr, "ERROR: Got invalid status line connecting to: %s\n", url.c_str()); + return NULL; + } + if (sscanf(line, "HTTP/1.1 %d", &status) != 1 || status != 101) + { + fprintf(stderr, "ERROR: Got bad status connecting to %s: %s", url.c_str(), line); + return NULL; + } // TODO: verify response headers, - while (true) { - for (i = 0; i < 2 || (i < 1023 && line[i-2] != '\r' && line[i-1] != '\n'); ++i) { if (recv(sockfd, line+i, 1, 0) == 0) { return NULL; } } - if (line[0] == '\r' && line[1] == '\n') { break; } + while (true) + { + for (i = 0; i < 2 || (i < 1023 && line[i - 2] != '\r' && line[i - 1] != '\n'); ++i) + { + if (recv(sockfd, line + i, 1, 0) == 0) + { + return NULL; + } + } + if (line[0] == '\r' && line[1] == '\n') + { + break; + } } } int flag = 1; - setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY, (char*) &flag, sizeof(flag)); // Disable Nagle's algorithm + setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY, (char *)&flag, sizeof(flag)); // Disable Nagle's algorithm #ifdef _WIN32 u_long on = 1; ioctlsocket(sockfd, FIONBIO, &on); @@ -544,25 +692,25 @@ easywsclient::WebSocket::pointer from_url(const std::string& url, bool useMask, return easywsclient::WebSocket::pointer(new _RealWebSocket(sockfd, useMask)); } -} // end of module-only namespace - - +} // namespace -namespace easywsclient { +namespace easywsclient +{ -WebSocket::pointer WebSocket::create_dummy() { +WebSocket::pointer WebSocket::create_dummy() +{ static pointer dummy = pointer(new _DummyWebSocket); return dummy; } - -WebSocket::pointer WebSocket::from_url(const std::string& url, const std::string& origin) { +WebSocket::pointer WebSocket::from_url(const std::string &url, const std::string &origin) +{ return ::from_url(url, true, origin); } -WebSocket::pointer WebSocket::from_url_no_mask(const std::string& url, const std::string& origin) { +WebSocket::pointer WebSocket::from_url_no_mask(const std::string &url, const std::string &origin) +{ return ::from_url(url, false, origin); } - } // namespace easywsclient diff --git a/mirai-third-party/easywsclient.hpp b/mirai-third-party/easywsclient.hpp index 2ffd9bc..c7b8b94 100644 --- a/mirai-third-party/easywsclient.hpp +++ b/mirai-third-party/easywsclient.hpp @@ -14,58 +14,73 @@ namespace easywsclient { -struct Callback_Imp { virtual void operator()(const std::string& message) = 0; }; -struct BytesCallback_Imp { virtual void operator()(const std::vector& message) = 0; }; +struct Callback_Imp +{ + virtual void operator()(const std::string &message) = 0; +}; +struct BytesCallback_Imp +{ + virtual void operator()(const std::vector &message) = 0; +}; -class WebSocket { - public: - typedef WebSocket * pointer; - typedef enum readyStateValues { CLOSING, CLOSED, CONNECTING, OPEN } readyStateValues; +class WebSocket +{ +public: + typedef WebSocket *pointer; + typedef enum readyStateValues + { + CLOSING, + CLOSED, + CONNECTING, + OPEN + } readyStateValues; // Factories: static pointer create_dummy(); - static pointer from_url(const std::string& url, const std::string& origin = std::string()); - static pointer from_url_no_mask(const std::string& url, const std::string& origin = std::string()); + static pointer from_url(const std::string &url, const std::string &origin = std::string()); + static pointer from_url_no_mask(const std::string &url, const std::string &origin = std::string()); // Interfaces: - virtual ~WebSocket() { } + virtual ~WebSocket() {} virtual void poll(int timeout = 0) = 0; // timeout in milliseconds - virtual void send(const std::string& message) = 0; - virtual void sendBinary(const std::string& message) = 0; - virtual void sendBinary(const std::vector& message) = 0; + virtual void send(const std::string &message) = 0; + virtual void sendBinary(const std::string &message) = 0; + virtual void sendBinary(const std::vector &message) = 0; virtual void sendPing() = 0; virtual void close() = 0; virtual readyStateValues getReadyState() const = 0; - template + template void dispatch(Callable callable) - // For callbacks that accept a string argument. + // For callbacks that accept a string argument. { // N.B. this is compatible with both C++11 lambdas, functors and C function pointers - struct _Callback : public Callback_Imp { - Callable& callable; - _Callback(Callable& callable) : callable(callable) { } - void operator()(const std::string& message) { callable(message); } + struct _Callback : public Callback_Imp + { + Callable &callable; + _Callback(Callable &callable) : callable(callable) {} + void operator()(const std::string &message) { callable(message); } }; _Callback callback(callable); _dispatch(callback); } - template + template void dispatchBinary(Callable callable) - // For callbacks that accept a std::vector argument. + // For callbacks that accept a std::vector argument. { // N.B. this is compatible with both C++11 lambdas, functors and C function pointers - struct _Callback : public BytesCallback_Imp { - Callable& callable; - _Callback(Callable& callable) : callable(callable) { } - void operator()(const std::vector& message) { callable(message); } + struct _Callback : public BytesCallback_Imp + { + Callable &callable; + _Callback(Callable &callable) : callable(callable) {} + void operator()(const std::vector &message) { callable(message); } }; _Callback callback(callable); _dispatchBinary(callback); } - protected: - virtual void _dispatch(Callback_Imp& callable) = 0; - virtual void _dispatchBinary(BytesCallback_Imp& callable) = 0; +protected: + virtual void _dispatch(Callback_Imp &callable) = 0; + virtual void _dispatchBinary(BytesCallback_Imp &callable) = 0; }; } // namespace easywsclient From 444c6e82f7718387b3e7865c911acaf791fdd077 Mon Sep 17 00:00:00 2001 From: Cyan Date: Fri, 1 May 2020 15:26:10 +0800 Subject: [PATCH 4/8] =?UTF-8?q?ws=E8=8E=B7=E5=8F=96=E4=BA=8B=E4=BB=B6?= =?UTF-8?q?=E5=87=BD=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- include/mirai_bot.hpp | 19 ++++++++++++++++--- src/mirai_bot.cpp | 31 +++++++++++++++++++++++++++++-- 2 files changed, 45 insertions(+), 5 deletions(-) diff --git a/include/mirai_bot.hpp b/include/mirai_bot.hpp index 0e0f9a4..4b3df18 100644 --- a/include/mirai_bot.hpp +++ b/include/mirai_bot.hpp @@ -47,8 +47,18 @@ namespace Cyan class EXPORTED MiraiBot { public: - MiraiBot() :qq_(0), pool_(4), http_client_("localhost", 8080) {} - MiraiBot(const string& host, int port) : qq_(0), pool_(4), http_client_(host, port) {} + MiraiBot() : + qq_(0), + pool_(4), + http_client_("localhost", 8080), + host_("localhost"), + port_(8080) {} + MiraiBot(const string& host, int port) : + qq_(0), + pool_(4), + http_client_(host, port), + host_(host), + port_(port) {} ~MiraiBot() { Release(); @@ -117,7 +127,8 @@ namespace Cyan private: bool SessionVerify(); bool SessionRelease(); - unsigned int FetchEvents(unsigned int count = 10); + unsigned int FetchEvents_HTTP(unsigned int count = 10); + void FetchEvents_WS(); void ProcessEvents(const nlohmann::json& ele); template inline WeakEvent MakeWeakEvent(const json& json_) @@ -157,6 +168,8 @@ namespace Cyan string authKey_; QQ_t qq_; string sessionKey_; + string host_; + int port_; httplib::Client http_client_; unordered_map > processors_; ThreadPool pool_; diff --git a/src/mirai_bot.cpp b/src/mirai_bot.cpp index e4b74a3..8c1f488 100644 --- a/src/mirai_bot.cpp +++ b/src/mirai_bot.cpp @@ -1,4 +1,7 @@ #include "mirai_bot.hpp" +#include "easywsclient.hpp" +#define _SSIZE_T_DEFINED +#include "easywsclient.cpp" namespace Cyan { @@ -571,7 +574,8 @@ namespace Cyan unsigned count = 0; try { - count = FetchEvents(count_per_loop); + FetchEvents_WS(); + count = FetchEvents_HTTP(count_per_loop); } catch (const std::exception& ex) { @@ -656,7 +660,7 @@ namespace Cyan } - unsigned int MiraiBot::FetchEvents(unsigned int count) + unsigned int MiraiBot::FetchEvents_HTTP(unsigned int count) { int received_count = 0; stringstream api_url; @@ -699,6 +703,29 @@ namespace Cyan } + void MiraiBot::FetchEvents_WS() + { + using namespace easywsclient; + stringstream url; + url << "ws://" << host_ << ":" << port_ << "/all?sessionKey=" << sessionKey_; + std::shared_ptr ws(WebSocket::from_url(url.str())); + if (!ws) + { + throw std::runtime_error("无法建立 WebSocket 连接!"); + } + while (ws->getReadyState() != WebSocket::CLOSED) + { + ws->poll(); + string eventJsonStr; + ws->dispatch([&](const std::string& message) + { + eventJsonStr = message; + }); + json j = json::parse(eventJsonStr); + ProcessEvents(j); + } + } + void MiraiBot::ProcessEvents(const nlohmann::json& ele) { string event_name = ele["type"].get(); From 8331d6fb30690e3c9b0b6714a2620980ccede366 Mon Sep 17 00:00:00 2001 From: Cyan Date: Fri, 1 May 2020 16:00:26 +0800 Subject: [PATCH 5/8] =?UTF-8?q?=E9=85=8D=E7=BD=AESession=E5=BC=80=E5=90=AF?= =?UTF-8?q?WebSocket;?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- include/mirai_bot.hpp | 39 +++++++++++++++++++++++-------- src/mirai_bot.cpp | 53 +++++++++++++++++++++++++++++++++++++------ 2 files changed, 76 insertions(+), 16 deletions(-) diff --git a/include/mirai_bot.hpp b/include/mirai_bot.hpp index 4b3df18..c982409 100644 --- a/include/mirai_bot.hpp +++ b/include/mirai_bot.hpp @@ -48,17 +48,21 @@ namespace Cyan { public: MiraiBot() : - qq_(0), - pool_(4), + qq_(0), + pool_(4), http_client_("localhost", 8080), host_("localhost"), - port_(8080) {} - MiraiBot(const string& host, int port) : - qq_(0), - pool_(4), - http_client_(host, port), + port_(8080), + cacheSize_(4096), + ws_enabled_(false) {} + MiraiBot(const string& host, int port) : + qq_(0), + pool_(4), + http_client_(host, port), host_(host), - port_(port) {} + port_(port), + cacheSize_(4096), + ws_enabled_(false) {} ~MiraiBot() { Release(); @@ -122,11 +126,26 @@ namespace Cyan std::this_thread::sleep_for(std::chrono::milliseconds(ms)); } + MiraiBot& UseWebSocket() + { + this->ws_enabled_ = true; + SessionConfigure(cacheSize_, ws_enabled_); + return *this; + } + + MiraiBot& UseHTTP() + { + this->ws_enabled_ = false; + SessionConfigure(cacheSize_, ws_enabled_); + return *this; + } + void EventLoop(function errLogger = nullptr); private: bool SessionVerify(); bool SessionRelease(); + bool SessionConfigure(int cacheSize, bool enableWebsocket); unsigned int FetchEvents_HTTP(unsigned int count = 10); void FetchEvents_WS(); void ProcessEvents(const nlohmann::json& ele); @@ -151,7 +170,7 @@ namespace Cyan } } - + // 因为 httplib 使用 string 来保存文件内容,这里适配一下 inline string ReadFile(const string& filename) { @@ -170,6 +189,8 @@ namespace Cyan string sessionKey_; string host_; int port_; + int cacheSize_; + bool ws_enabled_; httplib::Client http_client_; unordered_map > processors_; ThreadPool pool_; diff --git a/src/mirai_bot.cpp b/src/mirai_bot.cpp index 8c1f488..75cef9a 100644 --- a/src/mirai_bot.cpp +++ b/src/mirai_bot.cpp @@ -569,13 +569,16 @@ namespace Cyan { const unsigned count_per_loop = 20; const unsigned time_interval = 100; + SessionConfigure(cacheSize_, ws_enabled_); while (true) { unsigned count = 0; try { - FetchEvents_WS(); - count = FetchEvents_HTTP(count_per_loop); + if (ws_enabled_) + FetchEvents_WS(); + else + count = FetchEvents_HTTP(count_per_loop); } catch (const std::exception& ex) { @@ -659,6 +662,38 @@ namespace Cyan return false; } + bool MiraiBot::SessionConfigure(int cacheSize, bool enableWebsocket) + { + json data = + { + { "sessionKey", sessionKey_ }, + { "cacheSize", cacheSize }, + { "enableWebsocket", enableWebsocket } + }; + + auto res = http_client_.Post("/config", data.dump(), "application/json;charset=UTF-8"); + if (res) + { + if (res->status != 200) + throw std::runtime_error("[mirai-api-http error]: " + res->body); + json reJson; + reJson = reJson.parse(res->body); + int code = reJson["code"].get(); + if (code == 0) + return true; + else + { + string msg = reJson["msg"].get(); + throw runtime_error(msg); + } + + } + else + throw std::runtime_error("网络错误"); + return false; + + } + unsigned int MiraiBot::FetchEvents_HTTP(unsigned int count) { @@ -713,16 +748,20 @@ namespace Cyan { throw std::runtime_error("无法建立 WebSocket 连接!"); } - while (ws->getReadyState() != WebSocket::CLOSED) + string eventJsonStr; + while (ws->getReadyState() != WebSocket::CLOSED && this->ws_enabled_) { + ws->poll(); - string eventJsonStr; ws->dispatch([&](const std::string& message) { - eventJsonStr = message; + eventJsonStr = std::move(message); }); - json j = json::parse(eventJsonStr); - ProcessEvents(j); + if (!eventJsonStr.empty()) + { + json j = json::parse(eventJsonStr); + ProcessEvents(j); + } } } From 08f5c616a38f51a814f6033d5534916098d052b7 Mon Sep 17 00:00:00 2001 From: Cyan Date: Fri, 1 May 2020 16:00:38 +0800 Subject: [PATCH 6/8] =?UTF-8?q?=E4=BF=AE=E6=94=B9=20Example;?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- examples/MessageType.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/MessageType.cpp b/examples/MessageType.cpp index 76c6c34..15a8d3e 100644 --- a/examples/MessageType.cpp +++ b/examples/MessageType.cpp @@ -60,7 +60,7 @@ int main() }); - bot.EventLoop(); + bot.UseWebSocket().EventLoop(); return 0; } \ No newline at end of file From 1a352595752636d662f082c79fcca19e0f021cbe Mon Sep 17 00:00:00 2001 From: Cyan Date: Fri, 1 May 2020 17:13:26 +0800 Subject: [PATCH 7/8] =?UTF-8?q?=E9=99=8D=E4=BD=8E=20CPU=20=E4=BD=BF?= =?UTF-8?q?=E7=94=A8;?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/mirai_bot.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/mirai_bot.cpp b/src/mirai_bot.cpp index 75cef9a..ebabafa 100644 --- a/src/mirai_bot.cpp +++ b/src/mirai_bot.cpp @@ -752,15 +752,17 @@ namespace Cyan while (ws->getReadyState() != WebSocket::CLOSED && this->ws_enabled_) { - ws->poll(); + ws->poll(20); ws->dispatch([&](const std::string& message) { - eventJsonStr = std::move(message); + eventJsonStr = message; }); + // 这部分不能在lambda表示中,否则异常无法被EventLoop捕捉 if (!eventJsonStr.empty()) { json j = json::parse(eventJsonStr); ProcessEvents(j); + eventJsonStr.resize(0); } } } From 13183cfcb65721c2084f4cd468aca28e49886318 Mon Sep 17 00:00:00 2001 From: Cyan Date: Fri, 1 May 2020 17:17:16 +0800 Subject: [PATCH 8/8] examples; --- examples/FetchEventsViahHTTP.cpp | 57 ++++++++++++++++++++++++++++++++ examples/MessageType.cpp | 2 +- include/mirai_bot.hpp | 4 +-- 3 files changed, 60 insertions(+), 3 deletions(-) create mode 100644 examples/FetchEventsViahHTTP.cpp diff --git a/examples/FetchEventsViahHTTP.cpp b/examples/FetchEventsViahHTTP.cpp new file mode 100644 index 0000000..8b913a8 --- /dev/null +++ b/examples/FetchEventsViahHTTP.cpp @@ -0,0 +1,57 @@ +#include +// 使用静态库必须要在引入 mirai.h 前定义这个宏 +#define MIRAICPP_STATICLIB +#include + +int main() +{ + using namespace std; + using namespace Cyan; + system("chcp 65001"); + MiraiBot bot("127.0.0.1", 539); + while (true) + { + try + { + bot.Auth("INITKEY7A3O1a9v", 1589588851_qq); + break; + } + catch (const std::exception& ex) + { + cout << ex.what() << endl; + } + MiraiBot::SleepSeconds(1); + } + cout << "成功登录 bot。" << endl; + + bot.OnEventReceived( + [&](GroupMessage gm) + { + gm.QuoteReply(gm.MessageChain); + }); + + bot.OnEventReceived( + [&](FriendMessage fm) + { + fm.Reply("你好呀, " + fm.MessageChain); + }); + + bot.OnEventReceived( + [&](TempMessage tm) + { + tm.Reply(tm.MessageChain); + }); + + // 默认使用 WebSocket 拉取事件、消息 + // 如果要使用 HTTP 可以在 EventLoop 前执行 UseHTTP + // 记录轮询事件时的错误 + bot.UseHTTP().EventLoop([](const char* errMsg) + { + cout << "轮询事件时出错: " << errMsg << endl; + }); + + // 默认参数是在 cerr 输出错误 + // bot.EventLoop(); + + return 0; +} \ No newline at end of file diff --git a/examples/MessageType.cpp b/examples/MessageType.cpp index 15a8d3e..76c6c34 100644 --- a/examples/MessageType.cpp +++ b/examples/MessageType.cpp @@ -60,7 +60,7 @@ int main() }); - bot.UseWebSocket().EventLoop(); + bot.EventLoop(); return 0; } \ No newline at end of file diff --git a/include/mirai_bot.hpp b/include/mirai_bot.hpp index c982409..615a513 100644 --- a/include/mirai_bot.hpp +++ b/include/mirai_bot.hpp @@ -54,7 +54,7 @@ namespace Cyan host_("localhost"), port_(8080), cacheSize_(4096), - ws_enabled_(false) {} + ws_enabled_(true) {} MiraiBot(const string& host, int port) : qq_(0), pool_(4), @@ -62,7 +62,7 @@ namespace Cyan host_(host), port_(port), cacheSize_(4096), - ws_enabled_(false) {} + ws_enabled_(true) {} ~MiraiBot() { Release();