From 6b0773c2e925817dc591651ecce3a973ca437810 Mon Sep 17 00:00:00 2001 From: aozhiwei Date: Sat, 27 May 2023 16:44:02 +0800 Subject: [PATCH 01/19] 1 --- a8/awaiter.cc | 31 +++++++++++++++++++++++++++++++ a8/awaiter.h | 37 +++++++++++++++++++++++++++++++++++++ a8/promise.h | 10 ++++++++++ a8/result.h | 17 +++++++++++++++++ 4 files changed, 95 insertions(+) create mode 100644 a8/awaiter.cc create mode 100644 a8/awaiter.h create mode 100644 a8/promise.h create mode 100644 a8/result.h diff --git a/a8/awaiter.cc b/a8/awaiter.cc new file mode 100644 index 0000000..d9db7b8 --- /dev/null +++ b/a8/awaiter.cc @@ -0,0 +1,31 @@ +#include + +#include + +namespace a8 +{ + + void Awaiter::Await(std::shared_ptr notifyer) + { + notifyers_.push_back(notifyer); + DoAwait(); + } + +#if 0 + std::shared_ptr Awaiter::Sleep(int time) + { + return std::make_shared(time); + } +#endif + + void Awaiter::DoDone() + { + done_ = true; + for (auto notifyer : notifyers_) { + if (!notifyer.expired()) { + notifyer.lock()->DoResume(); + } + } + } + +} diff --git a/a8/awaiter.h b/a8/awaiter.h new file mode 100644 index 0000000..b7d09ff --- /dev/null +++ b/a8/awaiter.h @@ -0,0 +1,37 @@ +#pragma once + +#include + +namespace f8 +{ + class Coroutine; +} + +namespace a8 +{ + + class Awaiter : public std::enable_shared_from_this + { + public: + virtual ~Awaiter() {}; + + std::shared_ptr GetResult() { return results_; } + bool Done() const { return done_; } + virtual void DoResume() {}; + + protected: + bool done_ = false; + + std::list> notifyers_; + void Await(std::shared_ptr notifyer); + virtual void DoAwait() = 0; + void DoDone(); + + private: + std::shared_ptr results_; + std::function cb_; + + friend class f8::Coroutine; + }; + +} diff --git a/a8/promise.h b/a8/promise.h new file mode 100644 index 0000000..b602e28 --- /dev/null +++ b/a8/promise.h @@ -0,0 +1,10 @@ +#pragma once + +namespace a8 +{ + + class Promise : public Awaiter + { + }; + +} diff --git a/a8/result.h b/a8/result.h new file mode 100644 index 0000000..950f41a --- /dev/null +++ b/a8/result.h @@ -0,0 +1,17 @@ +#pragma once + +namespace a8 +{ + class Results + { + public: + + Results(std::vector results):results_(std::move(results)) {}; + + template + T Get(size_t index) const { return std::any_cast(results_.at(index));}; + + private: + std::vector results_; + }; +} From c4042dbd5b5f6a208d1e7c3eaca64e9ebefcf010 Mon Sep 17 00:00:00 2001 From: aozhiwei Date: Sat, 27 May 2023 17:14:48 +0800 Subject: [PATCH 02/19] 1 --- a8/a8.h | 1 + a8/awaiter.cc | 12 +++++------- a8/awaiter.h | 1 + 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/a8/a8.h b/a8/a8.h index 9e5d599..373673d 100644 --- a/a8/a8.h +++ b/a8/a8.h @@ -12,6 +12,7 @@ #include #include +#include #include #include #include diff --git a/a8/awaiter.cc b/a8/awaiter.cc index d9db7b8..46a3013 100644 --- a/a8/awaiter.cc +++ b/a8/awaiter.cc @@ -11,13 +11,6 @@ namespace a8 DoAwait(); } -#if 0 - std::shared_ptr Awaiter::Sleep(int time) - { - return std::make_shared(time); - } -#endif - void Awaiter::DoDone() { done_ = true; @@ -28,4 +21,9 @@ namespace a8 } } + void Awaiter::SetResult(std::vector results) + { + results_ = std::make_shared(results); + } + } diff --git a/a8/awaiter.h b/a8/awaiter.h index b7d09ff..47f2176 100644 --- a/a8/awaiter.h +++ b/a8/awaiter.h @@ -26,6 +26,7 @@ namespace a8 void Await(std::shared_ptr notifyer); virtual void DoAwait() = 0; void DoDone(); + void SetResult(std::vector results); private: std::shared_ptr results_; From 548150779dbe40d7a1a4285fb45c7231eb33cc35 Mon Sep 17 00:00:00 2001 From: aozhiwei Date: Sun, 28 May 2023 09:13:38 +0800 Subject: [PATCH 03/19] 1 --- a8/asiotcpclient.cc | 2 +- a8/asiotcpclient.h | 2 +- a8/websocketclient.cc | 25 +++++++++++++++++++++++++ a8/websocketclient.h | 28 ++++++++++++++++++++++++++++ 4 files changed, 55 insertions(+), 2 deletions(-) create mode 100644 a8/websocketclient.cc create mode 100644 a8/websocketclient.h diff --git a/a8/asiotcpclient.cc b/a8/asiotcpclient.cc index ed5e253..db32390 100644 --- a/a8/asiotcpclient.cc +++ b/a8/asiotcpclient.cc @@ -8,7 +8,7 @@ #include #include -#ifdef USE_ASIO +#ifdef USE_BOOST const int MAX_RECV_BUFFERSIZE = 1024 * 64; diff --git a/a8/asiotcpclient.h b/a8/asiotcpclient.h index a5b6b11..ca59cb9 100644 --- a/a8/asiotcpclient.h +++ b/a8/asiotcpclient.h @@ -1,6 +1,6 @@ #pragma once -#ifdef USE_ASIO +#ifdef USE_BOOST #include diff --git a/a8/websocketclient.cc b/a8/websocketclient.cc new file mode 100644 index 0000000..f773e16 --- /dev/null +++ b/a8/websocketclient.cc @@ -0,0 +1,25 @@ +#include + +#include + +#ifdef USE_BOOST + +namespace a8 +{ + + WebSocketClient::WebSocketClient(asio::io_context& io_context, const std::string& remote_ip, int remote_port): AsioTcpClient(io_context, remote_ip, remote_port) + { + decoded_buff_ = (char *)malloc(1024 * 64 + 1); + decoded_bufflen_ = 0; + } + + WebSocketClient::~WebSocketClient() + { + free(decoded_buff_); + decoded_buff_ = nullptr; + decoded_bufflen_ = 0; + } + +} + +#endif diff --git a/a8/websocketclient.h b/a8/websocketclient.h new file mode 100644 index 0000000..2d74705 --- /dev/null +++ b/a8/websocketclient.h @@ -0,0 +1,28 @@ +#pragma once + +#include + +#ifdef USE_BOOST + +#include + +using asio::ip::tcp; + +namespace a8 +{ + + class WebSocketClient : public AsioTcpClient + { + public: + WebSocketClient(asio::io_context& io_context, const std::string& remote_ip, int remote_port); + virtual ~WebSocketClient() override; + + private: + char *decoded_buff_ = nullptr; + int decoded_bufflen_ = 0; + bool handshook_ = false; + }; + +} + +#endif From afdc699fb76bb947645c9bf9e0cec807d02fb871 Mon Sep 17 00:00:00 2001 From: aozhiwei Date: Sun, 28 May 2023 09:38:05 +0800 Subject: [PATCH 04/19] 1 --- a8/websocketclient.cc | 30 +++++++++++++++++++++++++++++- a8/websocketclient.h | 16 ++++++++++++++-- 2 files changed, 43 insertions(+), 3 deletions(-) diff --git a/a8/websocketclient.cc b/a8/websocketclient.cc index f773e16..1605687 100644 --- a/a8/websocketclient.cc +++ b/a8/websocketclient.cc @@ -7,8 +7,9 @@ namespace a8 { - WebSocketClient::WebSocketClient(asio::io_context& io_context, const std::string& remote_ip, int remote_port): AsioTcpClient(io_context, remote_ip, remote_port) + WebSocketClient::WebSocketClient(asio::io_context& io_context, const std::string& remote_ip, int remote_port) { + tcp_client_ = std::make_shared(io_context, remote_ip, remote_port); decoded_buff_ = (char *)malloc(1024 * 64 + 1); decoded_bufflen_ = 0; } @@ -20,6 +21,33 @@ namespace a8 decoded_bufflen_ = 0; } + void WebSocketClient::Open() + { + tcp_client_->Open(); + } + + void WebSocketClient::Close() + { + tcp_client_->Close(); + } + + bool WebSocketClient::IsActive() + { + return tcp_client_->IsActive(); + } + + bool WebSocketClient::Connected() + { + return tcp_client_->Connected(); + } + + void WebSocketClient::SendBuff(const char* buff, unsigned int bufflen) + { + if (!handshook_) { + abort(); + } + } + } #endif diff --git a/a8/websocketclient.h b/a8/websocketclient.h index 2d74705..20be4cc 100644 --- a/a8/websocketclient.h +++ b/a8/websocketclient.h @@ -11,13 +11,25 @@ using asio::ip::tcp; namespace a8 { - class WebSocketClient : public AsioTcpClient + class WebSocketClient { public: WebSocketClient(asio::io_context& io_context, const std::string& remote_ip, int remote_port); - virtual ~WebSocketClient() override; + virtual ~WebSocketClient(); + + std::function on_error; + std::function on_connect; + std::function on_disconnect; + std::function on_socketread; + + void Open(); + void Close(); + bool IsActive(); + bool Connected(); + void SendBuff(const char* buff, unsigned int bufflen); private: + std::shared_ptr tcp_client_; char *decoded_buff_ = nullptr; int decoded_bufflen_ = 0; bool handshook_ = false; From 45922a8e8a3e00ba0994f8f29bf040bb9cc5203d Mon Sep 17 00:00:00 2001 From: aozhiwei Date: Sun, 28 May 2023 10:14:21 +0800 Subject: [PATCH 05/19] 1 --- a8/websocketclient.cc | 41 +++++++++++++++++++++++++++++++++++++++++ a8/websocketsession.cc | 42 +++++++++++++++++++++--------------------- 2 files changed, 62 insertions(+), 21 deletions(-) diff --git a/a8/websocketclient.cc b/a8/websocketclient.cc index 1605687..041dc12 100644 --- a/a8/websocketclient.cc +++ b/a8/websocketclient.cc @@ -4,6 +4,32 @@ #ifdef USE_BOOST +static const unsigned char FIN = 0x80; +static const unsigned char RSV1 = 0x40; +static const unsigned char RSV2 = 0x20; +static const unsigned char RSV3 = 0x10; +static const unsigned char RSV_MASK = RSV1 | RSV2 | RSV3; +static const unsigned char OPCODE_MASK = 0x0F; + +static const unsigned char TEXT_MODE = 0x01; +static const unsigned char BINARY_MODE = 0x02; + +static const unsigned char WEBSOCKET_OPCODE = 0x0F; +static const unsigned char WEBSOCKET_FRAME_CONTINUE = 0x0; +static const unsigned char WEBSOCKET_FRAME_TEXT = 0x1; +static const unsigned char WEBSOCKET_FRAME_BINARY = 0x2; +static const unsigned char WEBSOCKET_FRAME_CLOSE = 0x8; +static const unsigned char WEBSOCKET_FRAME_PING = 0x9; +static const unsigned char WEBSOCKET_FRAME_PONG = 0xA; + +static const unsigned char WEBSOCKET_MASK = 0x80; +static const unsigned char WEBSOCKET_PAYLOAD_LEN = 0x7F; +static const unsigned char WEBSOCKET_PAYLOAD_LEN_UINT16 = 126; +static const unsigned char WEBSOCKET_PAYLOAD_LEN_UINT64 = 127; + +static const char* WEB_SOCKET_KEY = "Sec-WebSocket-Key: "; +static const char* WEB_SOCKET_KEY2 = "Sec-Websocket-Key: "; + namespace a8 { @@ -46,6 +72,21 @@ namespace a8 if (!handshook_) { abort(); } + unsigned char szbuff [1024 * 65]; + szbuff[0] = FIN | BINARY_MODE | 0; + if (bufflen < 126) { + szbuff[1] = bufflen; + memmove(szbuff + 2, buff, bufflen); + tcp_client_->SendBuff((char*)szbuff, bufflen + 2); + } else if (bufflen <= 0xFFFF) { + szbuff[1] = 126; + szbuff[2] = (bufflen >> 8) & 0xFF; + szbuff[3] = bufflen & 0xFF; + memmove(szbuff + 4, buff, bufflen); + tcp_client_->SendBuff((char*)szbuff, bufflen + 4); + } else { + abort(); + } } } diff --git a/a8/websocketsession.cc b/a8/websocketsession.cc index 0bd4eb8..a723b98 100644 --- a/a8/websocketsession.cc +++ b/a8/websocketsession.cc @@ -9,31 +9,31 @@ #include #include -const unsigned char FIN = 0x80; -const unsigned char RSV1 = 0x40; -const unsigned char RSV2 = 0x20; -const unsigned char RSV3 = 0x10; -const unsigned char RSV_MASK = RSV1 | RSV2 | RSV3; -const unsigned char OPCODE_MASK = 0x0F; +static const unsigned char FIN = 0x80; +static const unsigned char RSV1 = 0x40; +static const unsigned char RSV2 = 0x20; +static const unsigned char RSV3 = 0x10; +static const unsigned char RSV_MASK = RSV1 | RSV2 | RSV3; +static const unsigned char OPCODE_MASK = 0x0F; -const unsigned char TEXT_MODE = 0x01; -const unsigned char BINARY_MODE = 0x02; +static const unsigned char TEXT_MODE = 0x01; +static const unsigned char BINARY_MODE = 0x02; -const unsigned char WEBSOCKET_OPCODE = 0x0F; -const unsigned char WEBSOCKET_FRAME_CONTINUE = 0x0; -const unsigned char WEBSOCKET_FRAME_TEXT = 0x1; -const unsigned char WEBSOCKET_FRAME_BINARY = 0x2; -const unsigned char WEBSOCKET_FRAME_CLOSE = 0x8; -const unsigned char WEBSOCKET_FRAME_PING = 0x9; -const unsigned char WEBSOCKET_FRAME_PONG = 0xA; +static const unsigned char WEBSOCKET_OPCODE = 0x0F; +static const unsigned char WEBSOCKET_FRAME_CONTINUE = 0x0; +static const unsigned char WEBSOCKET_FRAME_TEXT = 0x1; +static const unsigned char WEBSOCKET_FRAME_BINARY = 0x2; +static const unsigned char WEBSOCKET_FRAME_CLOSE = 0x8; +static const unsigned char WEBSOCKET_FRAME_PING = 0x9; +static const unsigned char WEBSOCKET_FRAME_PONG = 0xA; -const unsigned char WEBSOCKET_MASK = 0x80; -const unsigned char WEBSOCKET_PAYLOAD_LEN = 0x7F; -const unsigned char WEBSOCKET_PAYLOAD_LEN_UINT16 = 126; -const unsigned char WEBSOCKET_PAYLOAD_LEN_UINT64 = 127; +static const unsigned char WEBSOCKET_MASK = 0x80; +static const unsigned char WEBSOCKET_PAYLOAD_LEN = 0x7F; +static const unsigned char WEBSOCKET_PAYLOAD_LEN_UINT16 = 126; +static const unsigned char WEBSOCKET_PAYLOAD_LEN_UINT64 = 127; -const char* WEB_SOCKET_KEY = "Sec-WebSocket-Key: "; -const char* WEB_SOCKET_KEY2 = "Sec-Websocket-Key: "; +static const char* WEB_SOCKET_KEY = "Sec-WebSocket-Key: "; +static const char* WEB_SOCKET_KEY2 = "Sec-Websocket-Key: "; namespace a8 { From 7f568dcbd9a0134fd98b59e20768086c87cda483 Mon Sep 17 00:00:00 2001 From: aozhiwei Date: Sun, 28 May 2023 10:20:44 +0800 Subject: [PATCH 06/19] 1 --- a8/websocketclient.cc | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/a8/websocketclient.cc b/a8/websocketclient.cc index 041dc12..a3451a2 100644 --- a/a8/websocketclient.cc +++ b/a8/websocketclient.cc @@ -38,6 +38,26 @@ namespace a8 tcp_client_ = std::make_shared(io_context, remote_ip, remote_port); decoded_buff_ = (char *)malloc(1024 * 64 + 1); decoded_bufflen_ = 0; + tcp_client_->on_error = + [] (a8::AsioTcpClient* socket, int err) + { + + }; + tcp_client_->on_connect = + [] (a8::AsioTcpClient* socket) + { + + }; + tcp_client_->on_disconnect = + [] (a8::AsioTcpClient* socket) + { + + }; + tcp_client_->on_socketread = + [] (a8::AsioTcpClient* socket, char* buf, unsigned int buf_len) + { + + }; } WebSocketClient::~WebSocketClient() From 33a65a4cf309412b9adcce44353293f06cd30010 Mon Sep 17 00:00:00 2001 From: aozhiwei Date: Sun, 28 May 2023 14:38:21 +0800 Subject: [PATCH 07/19] 1 --- a8/websocketclient.cc | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/a8/websocketclient.cc b/a8/websocketclient.cc index a3451a2..b70b98b 100644 --- a/a8/websocketclient.cc +++ b/a8/websocketclient.cc @@ -51,12 +51,20 @@ namespace a8 tcp_client_->on_disconnect = [] (a8::AsioTcpClient* socket) { - + std::string data = a8::Format("GET ws://%s:%d/\r\n", + {socket->GetRemoteAddress(), + socket->GetRemotePort()}); + data += "Upgrade: websocket\r\n"; + data += "Connection: Upgrade\r\n"; + data += "Sec-WebSocket-Version: 13\r\n"; + data += "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==*\r\n"; + data += "\r\n"; + socket->SendBuff(data.data(), data.size()); }; tcp_client_->on_socketread = [] (a8::AsioTcpClient* socket, char* buf, unsigned int buf_len) { - + int i = 0; }; } From 0a6ae557857a2923826bf498548a739b9ae8d248 Mon Sep 17 00:00:00 2001 From: aozhiwei Date: Sun, 28 May 2023 14:53:20 +0800 Subject: [PATCH 08/19] 1 --- a8/websocketclient.cc | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/a8/websocketclient.cc b/a8/websocketclient.cc index b70b98b..1d04213 100644 --- a/a8/websocketclient.cc +++ b/a8/websocketclient.cc @@ -44,11 +44,6 @@ namespace a8 }; tcp_client_->on_connect = - [] (a8::AsioTcpClient* socket) - { - - }; - tcp_client_->on_disconnect = [] (a8::AsioTcpClient* socket) { std::string data = a8::Format("GET ws://%s:%d/\r\n", @@ -61,6 +56,10 @@ namespace a8 data += "\r\n"; socket->SendBuff(data.data(), data.size()); }; + tcp_client_->on_disconnect = + [] (a8::AsioTcpClient* socket) + { + }; tcp_client_->on_socketread = [] (a8::AsioTcpClient* socket, char* buf, unsigned int buf_len) { From b6e0b073133d2d07a7e2775851bee1754e3b3632 Mon Sep 17 00:00:00 2001 From: aozhiwei Date: Sun, 28 May 2023 15:12:15 +0800 Subject: [PATCH 09/19] 1 --- a8/websocketclient.cc | 164 +++++++++++++++++++++++++++++++++++++++++- a8/websocketclient.h | 6 ++ 2 files changed, 168 insertions(+), 2 deletions(-) diff --git a/a8/websocketclient.cc b/a8/websocketclient.cc index 1d04213..ee5d689 100644 --- a/a8/websocketclient.cc +++ b/a8/websocketclient.cc @@ -61,9 +61,13 @@ namespace a8 { }; tcp_client_->on_socketread = - [] (a8::AsioTcpClient* socket, char* buf, unsigned int buf_len) + [this] (a8::AsioTcpClient* socket, char* buf, unsigned int buflen) { - int i = 0; + if (!handshook_) { + buf[buflen] = '\0'; + } else { + + } }; } @@ -116,6 +120,162 @@ namespace a8 } } + void WebSocketClient::ProcessHandShake(char* buf, int& offset, unsigned int buflen) + { + char* pend = strstr(buf + offset, "\r\n\r\n"); + if (!pend) { + return; + } + if (strstr(buf + offset, WEB_SOCKET_KEY) || strstr(buf + offset, WEB_SOCKET_KEY2)) { + ProcessWsHandShake(buf, offset, buflen); + } else { + //ProcessHttpHandShake(buf, offset, buflen); + } + } + + void WebSocketClient::ProcessWsHandShake(char* buf, int& offset, unsigned int buflen) + { + char* pend = strstr(buf + offset, "\r\n\r\n"); + if (!pend) { + return; + } + if (strncmp(buf + offset, "GET ", strlen("GET ")) == 0) { + std::string url; + std::string querystr; + std::string location; + + a8::ParserQueryStr(buf + offset + 4, url, querystr); + } + std::string server_key; + { + char* p1 = strstr(buf + offset, WEB_SOCKET_KEY); + if (!p1) { + p1 = strstr(buf + offset, WEB_SOCKET_KEY2); + } + if (p1) { + p1 += strlen(WEB_SOCKET_KEY); + char* p2 = strstr(p1 , "\r\n"); + if (p2) { + server_key.append(p1, p2-p1); + } + } + } + #if 0 + server_key += "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"; + std::string sha1_data = a8::openssl::Sha1Encode(server_key); + std::string hash_data; + a8::openssl::Base64Encode(sha1_data, hash_data); + + std::string response = "HTTP/1.1 101 Web Socket Protocol Handshake\r\n" + "Upgrade: websocket\r\n" + "Connection: Upgrade\r\n" + "Sec-WebSocket-Accept: " + hash_data + "\r\n" + + "\r\n"; + SendBuff(response.data(), response.size()); + handshook_ = true; + #endif + offset += pend - buf - offset + strlen("\r\n\r\n"); + } + + void WebSocketClient::ProcessUserPacket() + { + int offset = 0; + int prev_offset = 0; + do { + prev_offset = offset; + //DecodeUserPacket(decoded_buff_, offset, decoded_bufflen_); + } while (prev_offset < offset && offset < decoded_bufflen_); + + if (offset > 0 && offset < decoded_bufflen_){ + memmove(decoded_buff_, decoded_buff_ + offset, decoded_bufflen_ - offset); + } + decoded_bufflen_ -= offset; + #if 0 + if (decoded_bufflen_ >= max_packet_len_) { + //收到超长包 + Close(); + return; + } + #endif + } + + void WebSocketClient::DecodeFrame(char* buf, int& offset, unsigned int buflen) + { + if (offset + 2 > (int)buflen) { + return; + } + char* real_buf = buf + offset; + unsigned int ava_len = buflen - offset; + unsigned char header = real_buf[0]; + unsigned char mask_payloadlen = real_buf[1]; + + bool is_final_frame = (header & FIN) == FIN; + #if 0 + bool reserved_bits = (header & FIN) == RSV_MASK; + #endif + unsigned char opcode = header & OPCODE_MASK; + #if 0 + bool opcode_is_control = opcode & 0x8; + #endif + + if (opcode == WEBSOCKET_FRAME_CLOSE) { + Close(); + return; + } + if (opcode != BINARY_MODE) { + if (opcode != WEBSOCKET_FRAME_PING) { + Close(); + return; + } + } + if (!is_final_frame) { + Close(); + return; + } + + bool is_masked = (mask_payloadlen & 0x80) == 0x80; + if (!is_masked) { + Close(); + return; + } + + unsigned char payloadlen = mask_payloadlen & 0x7F; + unsigned int framelen = 0; + int mask_offset = 0; + + if (payloadlen < 126) { + framelen = payloadlen; + mask_offset = 2; + } else if (payloadlen == 126 && ava_len >= 4) { + framelen = ntohs( *(u_short*) (real_buf + 2) ); + mask_offset = 4; + } else if (payloadlen == 127 && ava_len >= 8) { + //int32 or int64? + framelen = ntohl( *(u_long*) (real_buf + 2) ); + mask_offset = 8; + } else { + return; + } + unsigned int real_pkg_len = mask_offset + framelen + (is_masked ? 4 : 0); + if (ava_len < real_pkg_len) { + return; + } + + if (is_masked) { + unsigned char *frame_mask = (unsigned char*)(real_buf + mask_offset); + memcpy(&decoded_buff_[decoded_bufflen_], real_buf + mask_offset + 4, framelen); + for (unsigned int i = 0; i < framelen; i++) { + decoded_buff_[decoded_bufflen_ + i] = (decoded_buff_[i] ^ frame_mask[i%4]); + } + } else { + memcpy(&decoded_buff_[decoded_bufflen_], real_buf + mask_offset, framelen); + } + decoded_bufflen_ += framelen; + + ProcessUserPacket(); + offset += real_pkg_len; + } + } #endif diff --git a/a8/websocketclient.h b/a8/websocketclient.h index 20be4cc..6c0c97e 100644 --- a/a8/websocketclient.h +++ b/a8/websocketclient.h @@ -28,6 +28,12 @@ namespace a8 bool Connected(); void SendBuff(const char* buff, unsigned int bufflen); + private: + void ProcessHandShake(char* buf, int& offset, unsigned int buflen); + void ProcessWsHandShake(char* buf, int& offset, unsigned int buflen); + void ProcessUserPacket(); + void DecodeFrame(char* buf, int& offset, unsigned int buflen); + private: std::shared_ptr tcp_client_; char *decoded_buff_ = nullptr; From 126d511bfbd76ee69f32e3eef73ff8d0b5a83bcf Mon Sep 17 00:00:00 2001 From: aozhiwei Date: Sun, 28 May 2023 15:28:46 +0800 Subject: [PATCH 10/19] 1 --- a8/websocketclient.cc | 53 ++++++++++++++++++++++++++++++++++++------- a8/websocketclient.h | 7 +++++- 2 files changed, 51 insertions(+), 9 deletions(-) diff --git a/a8/websocketclient.cc b/a8/websocketclient.cc index ee5d689..febe053 100644 --- a/a8/websocketclient.cc +++ b/a8/websocketclient.cc @@ -63,11 +63,35 @@ namespace a8 tcp_client_->on_socketread = [this] (a8::AsioTcpClient* socket, char* buf, unsigned int buflen) { - if (!handshook_) { - buf[buflen] = '\0'; - } else { + unsigned int already_read_bytes = 0; + do { + if (already_read_bytes < buflen) { + int read_bytes = std::min(buflen - already_read_bytes, + (unsigned int)max_packet_len_ - recv_bufflen_); + if (read_bytes > 0) { + memmove(&recv_buff_[recv_bufflen_], buf + already_read_bytes, read_bytes); + recv_bufflen_ += read_bytes; + already_read_bytes += read_bytes; + } + } - } + int offset = 0; + int prev_offset = 0; + do { + prev_offset = offset; + DecodePacket(recv_buff_, offset, recv_bufflen_); + } while (prev_offset < offset && offset < recv_bufflen_); + + if (offset > 0 && offset < recv_bufflen_){ + memmove(recv_buff_, recv_buff_ + offset, recv_bufflen_ - offset); + } + recv_bufflen_ -= offset; + if (recv_bufflen_ >= max_packet_len_) { + //收到超长包 + Close(); + return; + } + } while (already_read_bytes < buflen); }; } @@ -129,7 +153,7 @@ namespace a8 if (strstr(buf + offset, WEB_SOCKET_KEY) || strstr(buf + offset, WEB_SOCKET_KEY2)) { ProcessWsHandShake(buf, offset, buflen); } else { - //ProcessHttpHandShake(buf, offset, buflen); + abort(); } } @@ -183,20 +207,18 @@ namespace a8 int prev_offset = 0; do { prev_offset = offset; - //DecodeUserPacket(decoded_buff_, offset, decoded_bufflen_); + DecodeUserPacket(decoded_buff_, offset, decoded_bufflen_); } while (prev_offset < offset && offset < decoded_bufflen_); if (offset > 0 && offset < decoded_bufflen_){ memmove(decoded_buff_, decoded_buff_ + offset, decoded_bufflen_ - offset); } decoded_bufflen_ -= offset; - #if 0 if (decoded_bufflen_ >= max_packet_len_) { //收到超长包 Close(); return; } - #endif } void WebSocketClient::DecodeFrame(char* buf, int& offset, unsigned int buflen) @@ -276,6 +298,21 @@ namespace a8 offset += real_pkg_len; } + void WebSocketClient::DecodePacket(char* buf, int& offset, unsigned int buflen) + { + if (!handshook_) { + buf[buflen] = '\0'; + ProcessHandShake(buf, offset, buflen); + } else { + DecodeFrame(buf, offset, buflen); + } + } + + void WebSocketClient::DecodeUserPacket(char* buf, int& offset, unsigned int buflen) + { + + } + } #endif diff --git a/a8/websocketclient.h b/a8/websocketclient.h index 6c0c97e..97c2fca 100644 --- a/a8/websocketclient.h +++ b/a8/websocketclient.h @@ -29,16 +29,21 @@ namespace a8 void SendBuff(const char* buff, unsigned int bufflen); private: + void DecodePacket(char* buf, int& offset, unsigned int buflen); + void DecodeUserPacket(char* buf, int& offset, unsigned int buflen); + void ProcessHandShake(char* buf, int& offset, unsigned int buflen); void ProcessWsHandShake(char* buf, int& offset, unsigned int buflen); void ProcessUserPacket(); void DecodeFrame(char* buf, int& offset, unsigned int buflen); - private: std::shared_ptr tcp_client_; char *decoded_buff_ = nullptr; int decoded_bufflen_ = 0; bool handshook_ = false; + char *recv_buff_ = nullptr; + int recv_bufflen_ = 0; + int max_packet_len_ = 0; }; } From c4ad3b53f2341b0a7629ce3c5f115bfac7c35f07 Mon Sep 17 00:00:00 2001 From: aozhiwei Date: Sun, 28 May 2023 15:36:08 +0800 Subject: [PATCH 11/19] 1 --- a8/websocketclient.cc | 51 ++++++++++--------------------------------- 1 file changed, 12 insertions(+), 39 deletions(-) diff --git a/a8/websocketclient.cc b/a8/websocketclient.cc index febe053..287bb2a 100644 --- a/a8/websocketclient.cc +++ b/a8/websocketclient.cc @@ -30,11 +30,18 @@ static const unsigned char WEBSOCKET_PAYLOAD_LEN_UINT64 = 127; static const char* WEB_SOCKET_KEY = "Sec-WebSocket-Key: "; static const char* WEB_SOCKET_KEY2 = "Sec-Websocket-Key: "; +static const int DEFAULT_MAX_PACKET_LEN = 1024 * 10; +static const int DEFAULT_MAX_RECV_BUFFERSIZE = 1024 * 64; + namespace a8 { WebSocketClient::WebSocketClient(asio::io_context& io_context, const std::string& remote_ip, int remote_port) { + max_packet_len_ = DEFAULT_MAX_PACKET_LEN; + recv_buff_ = (char *)malloc(max_packet_len_ + 1); + recv_bufflen_ = 0; + tcp_client_ = std::make_shared(io_context, remote_ip, remote_port); decoded_buff_ = (char *)malloc(1024 * 64 + 1); decoded_bufflen_ = 0; @@ -97,6 +104,10 @@ namespace a8 WebSocketClient::~WebSocketClient() { + recv_bufflen_ = 0; + free(recv_buff_); + recv_buff_ = nullptr; + free(decoded_buff_); decoded_buff_ = nullptr; decoded_bufflen_ = 0; @@ -150,11 +161,7 @@ namespace a8 if (!pend) { return; } - if (strstr(buf + offset, WEB_SOCKET_KEY) || strstr(buf + offset, WEB_SOCKET_KEY2)) { - ProcessWsHandShake(buf, offset, buflen); - } else { - abort(); - } + ProcessWsHandShake(buf, offset, buflen); } void WebSocketClient::ProcessWsHandShake(char* buf, int& offset, unsigned int buflen) @@ -163,41 +170,7 @@ namespace a8 if (!pend) { return; } - if (strncmp(buf + offset, "GET ", strlen("GET ")) == 0) { - std::string url; - std::string querystr; - std::string location; - - a8::ParserQueryStr(buf + offset + 4, url, querystr); - } - std::string server_key; - { - char* p1 = strstr(buf + offset, WEB_SOCKET_KEY); - if (!p1) { - p1 = strstr(buf + offset, WEB_SOCKET_KEY2); - } - if (p1) { - p1 += strlen(WEB_SOCKET_KEY); - char* p2 = strstr(p1 , "\r\n"); - if (p2) { - server_key.append(p1, p2-p1); - } - } - } - #if 0 - server_key += "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"; - std::string sha1_data = a8::openssl::Sha1Encode(server_key); - std::string hash_data; - a8::openssl::Base64Encode(sha1_data, hash_data); - - std::string response = "HTTP/1.1 101 Web Socket Protocol Handshake\r\n" - "Upgrade: websocket\r\n" - "Connection: Upgrade\r\n" - "Sec-WebSocket-Accept: " + hash_data + "\r\n" + - "\r\n"; - SendBuff(response.data(), response.size()); handshook_ = true; - #endif offset += pend - buf - offset + strlen("\r\n\r\n"); } From 0832282bbc2f04f417a93b968d50d7795b1f7576 Mon Sep 17 00:00:00 2001 From: aozhiwei Date: Sun, 28 May 2023 15:48:54 +0800 Subject: [PATCH 12/19] 1 --- a8/websocketclient.h | 1 + 1 file changed, 1 insertion(+) diff --git a/a8/websocketclient.h b/a8/websocketclient.h index 97c2fca..98eaf13 100644 --- a/a8/websocketclient.h +++ b/a8/websocketclient.h @@ -36,6 +36,7 @@ namespace a8 void ProcessWsHandShake(char* buf, int& offset, unsigned int buflen); void ProcessUserPacket(); void DecodeFrame(char* buf, int& offset, unsigned int buflen); + private: std::shared_ptr tcp_client_; char *decoded_buff_ = nullptr; From 91b5f74b3a08a51674e825a499beba668b5d23be Mon Sep 17 00:00:00 2001 From: azw Date: Fri, 18 Aug 2023 07:37:19 +0000 Subject: [PATCH 13/19] 1 --- a8/asiotcpclient.cc | 9 ++++++--- a8/asiotcpclient.h | 5 ++++- a8/websocketclient.cc | 45 ++++++++++++++++++++++++++++++++++++++----- a8/websocketclient.h | 2 +- 4 files changed, 51 insertions(+), 10 deletions(-) diff --git a/a8/asiotcpclient.cc b/a8/asiotcpclient.cc index db32390..848c39d 100644 --- a/a8/asiotcpclient.cc +++ b/a8/asiotcpclient.cc @@ -15,17 +15,18 @@ const int MAX_RECV_BUFFERSIZE = 1024 * 64; namespace a8 { - AsioTcpClient::AsioTcpClient(asio::io_context& io_context, const std::string& remote_ip, int remote_port) + AsioTcpClient::AsioTcpClient(std::shared_ptr io_context, const std::string& remote_ip, int remote_port) { + io_context_ = io_context; remote_address_ = remote_ip; remote_port_ = remote_port; endpoint_ = std::make_shared ( - asio::ip::make_address(remote_address_), + asio::ip::address::from_string(remote_address_), remote_port_ ); send_buffer_mutex_ = std::make_shared(); - socket_ = std::make_shared(io_context); + socket_ = std::make_shared(*io_context); } AsioTcpClient::~AsioTcpClient() @@ -95,10 +96,12 @@ namespace a8 { actived_ = true; connected_ = false; + a8::XPrintf("remote_ip:%s remote_port:%d\n", {endpoint_->address().to_string(), endpoint_->port()}); socket_->async_connect (*endpoint_, [this] (const asio::error_code& ec) { + a8::XPrintf("async econnect %d\n", {ec.value()}); HandleConnect(ec); }); } diff --git a/a8/asiotcpclient.h b/a8/asiotcpclient.h index ca59cb9..c86ab10 100644 --- a/a8/asiotcpclient.h +++ b/a8/asiotcpclient.h @@ -16,7 +16,9 @@ namespace a8 std::function on_connect; std::function on_disconnect; std::function on_socketread; - AsioTcpClient(asio::io_context& io_context, const std::string& remote_ip, int remote_port); + AsioTcpClient(std::shared_ptr io_context, + const std::string& remote_ip, + int remote_port); virtual ~AsioTcpClient(); const std::string& GetRemoteAddress() { return remote_address_; } int GetRemotePort() { return remote_port_; } @@ -33,6 +35,7 @@ namespace a8 void DoSend(); private: + std::shared_ptr io_context_; std::string remote_address_; int remote_port_ = 0; diff --git a/a8/websocketclient.cc b/a8/websocketclient.cc index 287bb2a..98dc0b0 100644 --- a/a8/websocketclient.cc +++ b/a8/websocketclient.cc @@ -36,7 +36,7 @@ static const int DEFAULT_MAX_RECV_BUFFERSIZE = 1024 * 64; namespace a8 { - WebSocketClient::WebSocketClient(asio::io_context& io_context, const std::string& remote_ip, int remote_port) + WebSocketClient::WebSocketClient(std::shared_ptr io_context, const std::string& remote_ip, int remote_port) { max_packet_len_ = DEFAULT_MAX_PACKET_LEN; recv_buff_ = (char *)malloc(max_packet_len_ + 1); @@ -46,12 +46,14 @@ namespace a8 decoded_buff_ = (char *)malloc(1024 * 64 + 1); decoded_bufflen_ = 0; tcp_client_->on_error = - [] (a8::AsioTcpClient* socket, int err) + [this] (a8::AsioTcpClient* socket, int err) { - + if (on_error) { + on_error(this, err); + } }; tcp_client_->on_connect = - [] (a8::AsioTcpClient* socket) + [this] (a8::AsioTcpClient* socket) { std::string data = a8::Format("GET ws://%s:%d/\r\n", {socket->GetRemoteAddress(), @@ -64,8 +66,11 @@ namespace a8 socket->SendBuff(data.data(), data.size()); }; tcp_client_->on_disconnect = - [] (a8::AsioTcpClient* socket) + [this] (a8::AsioTcpClient* socket) { + if (on_disconnect) { + on_disconnect(this); + } }; tcp_client_->on_socketread = [this] (a8::AsioTcpClient* socket, char* buf, unsigned int buflen) @@ -170,8 +175,38 @@ namespace a8 if (!pend) { return; } + #if 0 + std::string server_key; + { + char* p1 = strstr(buf + offset, WEB_SOCKET_KEY); + if (!p1) { + p1 = strstr(buf + offset, WEB_SOCKET_KEY2); + } + if (p1) { + p1 += strlen(WEB_SOCKET_KEY); + char* p2 = strstr(p1 , "\r\n"); + if (p2) { + server_key.append(p1, p2-p1); + } + } + } + server_key += "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"; + std::string sha1_data = a8::openssl::Sha1Encode(server_key); + std::string hash_data; + a8::openssl::Base64Encode(sha1_data, hash_data); + + std::string response = "HTTP/1.1 101 Web Socket Protocol Handshake\r\n" + "Upgrade: websocket\r\n" + "Connection: Upgrade\r\n" + "Sec-WebSocket-Accept: " + hash_data + "\r\n" + + "\r\n"; + SendBuff(response.data(), response.size()); handshook_ = true; offset += pend - buf - offset + strlen("\r\n\r\n"); + if (on_connect) { + on_connect(this); + } + #endif } void WebSocketClient::ProcessUserPacket() diff --git a/a8/websocketclient.h b/a8/websocketclient.h index 98eaf13..91c4c09 100644 --- a/a8/websocketclient.h +++ b/a8/websocketclient.h @@ -14,7 +14,7 @@ namespace a8 class WebSocketClient { public: - WebSocketClient(asio::io_context& io_context, const std::string& remote_ip, int remote_port); + WebSocketClient(std::shared_ptr io_context, const std::string& remote_ip, int remote_port); virtual ~WebSocketClient(); std::function on_error; From 0d5c381b38618310d7b438a03c8942b14c0e0725 Mon Sep 17 00:00:00 2001 From: azw Date: Fri, 18 Aug 2023 15:40:46 +0000 Subject: [PATCH 14/19] 1 --- a8/asiotcpclient.cc | 2 -- a8/promise.h | 5 +++++ a8/websocketclient.cc | 29 +---------------------------- 3 files changed, 6 insertions(+), 30 deletions(-) diff --git a/a8/asiotcpclient.cc b/a8/asiotcpclient.cc index 848c39d..8610fbd 100644 --- a/a8/asiotcpclient.cc +++ b/a8/asiotcpclient.cc @@ -96,12 +96,10 @@ namespace a8 { actived_ = true; connected_ = false; - a8::XPrintf("remote_ip:%s remote_port:%d\n", {endpoint_->address().to_string(), endpoint_->port()}); socket_->async_connect (*endpoint_, [this] (const asio::error_code& ec) { - a8::XPrintf("async econnect %d\n", {ec.value()}); HandleConnect(ec); }); } diff --git a/a8/promise.h b/a8/promise.h index b602e28..3cc4d3e 100644 --- a/a8/promise.h +++ b/a8/promise.h @@ -7,4 +7,9 @@ namespace a8 { }; + class NormalPromise : public Awaiter + { + std::function<(std::shared_ptr)> cb; + }; + } diff --git a/a8/websocketclient.cc b/a8/websocketclient.cc index 98dc0b0..0dae9cb 100644 --- a/a8/websocketclient.cc +++ b/a8/websocketclient.cc @@ -175,38 +175,11 @@ namespace a8 if (!pend) { return; } - #if 0 - std::string server_key; - { - char* p1 = strstr(buf + offset, WEB_SOCKET_KEY); - if (!p1) { - p1 = strstr(buf + offset, WEB_SOCKET_KEY2); - } - if (p1) { - p1 += strlen(WEB_SOCKET_KEY); - char* p2 = strstr(p1 , "\r\n"); - if (p2) { - server_key.append(p1, p2-p1); - } - } - } - server_key += "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"; - std::string sha1_data = a8::openssl::Sha1Encode(server_key); - std::string hash_data; - a8::openssl::Base64Encode(sha1_data, hash_data); - - std::string response = "HTTP/1.1 101 Web Socket Protocol Handshake\r\n" - "Upgrade: websocket\r\n" - "Connection: Upgrade\r\n" - "Sec-WebSocket-Accept: " + hash_data + "\r\n" + - "\r\n"; - SendBuff(response.data(), response.size()); handshook_ = true; offset += pend - buf - offset + strlen("\r\n\r\n"); if (on_connect) { on_connect(this); } - #endif } void WebSocketClient::ProcessUserPacket() @@ -318,7 +291,7 @@ namespace a8 void WebSocketClient::DecodeUserPacket(char* buf, int& offset, unsigned int buflen) { - + int i = 0; } } From 888275763feaf537f01c190b23402325ebe1f9da Mon Sep 17 00:00:00 2001 From: azw Date: Sat, 19 Aug 2023 00:01:32 +0800 Subject: [PATCH 15/19] 1 --- a8/promise.cc | 12 ++++++++++++ a8/promise.h | 14 ++++++++++++-- 2 files changed, 24 insertions(+), 2 deletions(-) create mode 100644 a8/promise.cc diff --git a/a8/promise.cc b/a8/promise.cc new file mode 100644 index 0000000..4301210 --- /dev/null +++ b/a8/promise.cc @@ -0,0 +1,12 @@ +#include +#include + +namespace a8 +{ + + void CbPromise::DoAwait() + { + cb_(Awaiter::shared_from_this()); + } + +} diff --git a/a8/promise.h b/a8/promise.h index 3cc4d3e..79bd4ce 100644 --- a/a8/promise.h +++ b/a8/promise.h @@ -1,5 +1,7 @@ #pragma once +#include + namespace a8 { @@ -7,9 +9,17 @@ namespace a8 { }; - class NormalPromise : public Awaiter + class CbPromise : public Awaiter { - std::function<(std::shared_ptr)> cb; + public: + CbPromise(std::function)> cb):Awaiter() + { cb_ = cb; } + + protected: + virtual void DoAwait() override; + + private: + std::function)> cb_; }; } From fe229f0bb160d127e9d0264cb3e09132ed2fe528 Mon Sep 17 00:00:00 2001 From: azw Date: Sat, 19 Aug 2023 10:27:03 +0800 Subject: [PATCH 16/19] 1 --- a8/promise.cc | 5 ----- a8/promise.h | 13 ------------- 2 files changed, 18 deletions(-) diff --git a/a8/promise.cc b/a8/promise.cc index 4301210..d2a83e6 100644 --- a/a8/promise.cc +++ b/a8/promise.cc @@ -4,9 +4,4 @@ namespace a8 { - void CbPromise::DoAwait() - { - cb_(Awaiter::shared_from_this()); - } - } diff --git a/a8/promise.h b/a8/promise.h index 79bd4ce..9c6f6b8 100644 --- a/a8/promise.h +++ b/a8/promise.h @@ -9,17 +9,4 @@ namespace a8 { }; - class CbPromise : public Awaiter - { - public: - CbPromise(std::function)> cb):Awaiter() - { cb_ = cb; } - - protected: - virtual void DoAwait() override; - - private: - std::function)> cb_; - }; - } From d3671aa6088be91fb482dbda3caf531ec0120293 Mon Sep 17 00:00:00 2001 From: azw Date: Sat, 19 Aug 2023 14:02:43 +0800 Subject: [PATCH 17/19] 1 --- a8/websocketclient.cc | 26 ++++++++++++++++---------- a8/websocketsession.cc | 5 +++++ 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/a8/websocketclient.cc b/a8/websocketclient.cc index 0dae9cb..3b3ccd3 100644 --- a/a8/websocketclient.cc +++ b/a8/websocketclient.cc @@ -145,19 +145,25 @@ namespace a8 } unsigned char szbuff [1024 * 65]; szbuff[0] = FIN | BINARY_MODE | 0; - if (bufflen < 126) { - szbuff[1] = bufflen; - memmove(szbuff + 2, buff, bufflen); - tcp_client_->SendBuff((char*)szbuff, bufflen + 2); - } else if (bufflen <= 0xFFFF) { - szbuff[1] = 126; - szbuff[2] = (bufflen >> 8) & 0xFF; - szbuff[3] = bufflen & 0xFF; - memmove(szbuff + 4, buff, bufflen); - tcp_client_->SendBuff((char*)szbuff, bufflen + 4); + int mask_offset = 2; + int payloadlen = bufflen; + if (payloadlen < 126) { + szbuff[1] = payloadlen | 0x80; + mask_offset = 2; + } else if (payloadlen <= 0xFFFF) { + szbuff[1] = 126 | 0x80; + szbuff[2] = (payloadlen >> 8) & 0xFF; + szbuff[3] = payloadlen & 0xFF; + mask_offset = 4; } else { abort(); } + *((int*)(szbuff + mask_offset)) = rand(); + for (unsigned i = 0; i < bufflen; ++i) { + szbuff[mask_offset + 4 + i] = + ((unsigned char)buff[i]) ^ szbuff[mask_offset + (i % 4)] ; + } + tcp_client_->SendBuff((char*)szbuff, bufflen + mask_offset + 4); } void WebSocketClient::ProcessHandShake(char* buf, int& offset, unsigned int buflen) diff --git a/a8/websocketsession.cc b/a8/websocketsession.cc index a723b98..ae23e27 100644 --- a/a8/websocketsession.cc +++ b/a8/websocketsession.cc @@ -188,6 +188,11 @@ namespace a8 } } + /* + finbit|opcode|falgs + unsigned char header FIN|BINARY_MODE + unsigned char mask_payloadlen + */ void WebSocketSession::DecodeFrame(char* buf, int& offset, unsigned int buflen) { if (offset + 2 > (int)buflen) { From 66aa3a6fbbe60129bff200d302f6c3531f158087 Mon Sep 17 00:00:00 2001 From: azw Date: Sat, 19 Aug 2023 14:05:14 +0800 Subject: [PATCH 18/19] 1 --- a8/websocketclient.cc | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/a8/websocketclient.cc b/a8/websocketclient.cc index 3b3ccd3..3d08c74 100644 --- a/a8/websocketclient.cc +++ b/a8/websocketclient.cc @@ -243,10 +243,6 @@ namespace a8 } bool is_masked = (mask_payloadlen & 0x80) == 0x80; - if (!is_masked) { - Close(); - return; - } unsigned char payloadlen = mask_payloadlen & 0x7F; unsigned int framelen = 0; @@ -274,7 +270,8 @@ namespace a8 unsigned char *frame_mask = (unsigned char*)(real_buf + mask_offset); memcpy(&decoded_buff_[decoded_bufflen_], real_buf + mask_offset + 4, framelen); for (unsigned int i = 0; i < framelen; i++) { - decoded_buff_[decoded_bufflen_ + i] = (decoded_buff_[i] ^ frame_mask[i%4]); + decoded_buff_[decoded_bufflen_ + i] = + (decoded_buff_[ decoded_bufflen_ + i] ^ frame_mask[i%4]); } } else { memcpy(&decoded_buff_[decoded_bufflen_], real_buf + mask_offset, framelen); From 261a025565fb8ad5977c3376a9bebf58239a6e98 Mon Sep 17 00:00:00 2001 From: azw Date: Sat, 19 Aug 2023 15:43:55 +0800 Subject: [PATCH 19/19] 1 --- a8/websocketclient.cc | 7 +------ a8/websocketclient.h | 3 +-- 2 files changed, 2 insertions(+), 8 deletions(-) diff --git a/a8/websocketclient.cc b/a8/websocketclient.cc index 3d08c74..4819003 100644 --- a/a8/websocketclient.cc +++ b/a8/websocketclient.cc @@ -194,7 +194,7 @@ namespace a8 int prev_offset = 0; do { prev_offset = offset; - DecodeUserPacket(decoded_buff_, offset, decoded_bufflen_); + on_decode_userpacket(decoded_buff_, offset, decoded_bufflen_); } while (prev_offset < offset && offset < decoded_bufflen_); if (offset > 0 && offset < decoded_bufflen_){ @@ -292,11 +292,6 @@ namespace a8 } } - void WebSocketClient::DecodeUserPacket(char* buf, int& offset, unsigned int buflen) - { - int i = 0; - } - } #endif diff --git a/a8/websocketclient.h b/a8/websocketclient.h index 91c4c09..6f7d007 100644 --- a/a8/websocketclient.h +++ b/a8/websocketclient.h @@ -20,7 +20,7 @@ namespace a8 std::function on_error; std::function on_connect; std::function on_disconnect; - std::function on_socketread; + std::function on_decode_userpacket; void Open(); void Close(); @@ -30,7 +30,6 @@ namespace a8 private: void DecodePacket(char* buf, int& offset, unsigned int buflen); - void DecodeUserPacket(char* buf, int& offset, unsigned int buflen); void ProcessHandShake(char* buf, int& offset, unsigned int buflen); void ProcessWsHandShake(char* buf, int& offset, unsigned int buflen);