Update to 2.0.0
This commit is contained in:
File diff suppressed because one or more lines are too long
@@ -8,4 +8,4 @@ files:
|
||||
license: MIT
|
||||
repository: https://github.com/78/esp-ml307
|
||||
url: https://github.com/78/esp-ml307
|
||||
version: 3.3.0
|
||||
version: 3.3.3
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
#include <string>
|
||||
#include <map>
|
||||
#include <thread>
|
||||
#include <mutex>
|
||||
#include <freertos/FreeRTOS.h>
|
||||
#include <freertos/event_groups.h>
|
||||
|
||||
@@ -40,8 +41,10 @@ private:
|
||||
std::string receive_buffer_;
|
||||
bool handshake_completed_ = false;
|
||||
bool connected_ = false;
|
||||
|
||||
// Mutex for sending data and replying pong
|
||||
std::mutex send_mutex_;
|
||||
|
||||
// FreeRTOS 事件组用于同步握手
|
||||
EventGroupHandle_t handshake_event_group_;
|
||||
static const EventBits_t HANDSHAKE_SUCCESS_BIT = BIT0;
|
||||
static const EventBits_t HANDSHAKE_FAILED_BIT = BIT1;
|
||||
|
||||
@@ -233,7 +233,7 @@ void HttpClient::Close() {
|
||||
void HttpClient::OnTcpData(const std::string& data) {
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
|
||||
// 检查 body_chunks_ 大小,如果超过 8KB 且 heap 小于 32KB 则阻塞
|
||||
// 检查 body_chunks_ 大小,如果超过 8KB
|
||||
{
|
||||
std::unique_lock<std::mutex> read_lock(read_mutex_);
|
||||
write_cv_.wait(read_lock, [this, size=data.size()] {
|
||||
@@ -241,8 +241,7 @@ void HttpClient::OnTcpData(const std::string& data) {
|
||||
for (const auto& chunk : body_chunks_) {
|
||||
total_size += chunk.data.size();
|
||||
}
|
||||
size_t free_heap = esp_get_free_heap_size();
|
||||
return total_size < MAX_BODY_CHUNKS_SIZE || !connected_ || free_heap >= 32768;
|
||||
return total_size < MAX_BODY_CHUNKS_SIZE || !connected_;
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -246,6 +246,7 @@ bool WebSocket::Send(const void* data, size_t len, bool binary, bool fin) {
|
||||
continuation_ = !fin;
|
||||
|
||||
// 发送帧
|
||||
std::lock_guard<std::mutex> lock(send_mutex_);
|
||||
return tcp_->Send(frame) >= 0;
|
||||
}
|
||||
|
||||
@@ -381,11 +382,11 @@ void WebSocket::OnTcpData(const std::string& data) {
|
||||
}
|
||||
break;
|
||||
case 0x9: // Ping
|
||||
// 发送 Pong
|
||||
SendControlFrame(0xA, payload.data(), payload_length);
|
||||
std::thread([this, payload, payload_length]() {
|
||||
SendControlFrame(0xA, payload.data(), payload_length);
|
||||
}).detach();
|
||||
break;
|
||||
case 0xA: // Pong
|
||||
// 可以在这里处理 Pong
|
||||
break;
|
||||
default:
|
||||
ESP_LOGE(TAG, "Unknown opcode: %d", opcode);
|
||||
@@ -432,5 +433,6 @@ bool WebSocket::SendControlFrame(uint8_t opcode, const void* data, size_t len) {
|
||||
}
|
||||
|
||||
// 发送帧
|
||||
std::lock_guard<std::mutex> lock(send_mutex_);
|
||||
return tcp_->Send(frame) >= 0;
|
||||
}
|
||||
Reference in New Issue
Block a user