fix some bugs on Windows
parent
c08c8fe686
commit
84e2e092f0
|
@ -54,6 +54,7 @@ public:
|
||||||
std::unique_lock<std::mutex> lk(m_);
|
std::unique_lock<std::mutex> lk(m_);
|
||||||
|
|
||||||
if (closed_.load(std::memory_order_acquire)) {
|
if (closed_.load(std::memory_order_acquire)) {
|
||||||
|
LOG_ERROR("Event dispatcher closed");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -64,10 +65,12 @@ public:
|
||||||
});
|
});
|
||||||
|
|
||||||
if (closed_.load(std::memory_order_acquire)) {
|
if (closed_.load(std::memory_order_acquire)) {
|
||||||
|
LOG_ERROR("Event dispatcher closed");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!result) {
|
if (!result) {
|
||||||
|
LOG_ERROR("Event dispatcher timeout");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -83,12 +86,14 @@ public:
|
||||||
if (!message_copy.empty()) {
|
if (!message_copy.empty()) {
|
||||||
if (!sink->write(message_copy.data(), message_copy.size())) {
|
if (!sink->write(message_copy.data(), message_copy.size())) {
|
||||||
close();
|
close();
|
||||||
|
LOG_ERROR("Event dispatcher write failed");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
} catch (...) {
|
} catch (...) {
|
||||||
close();
|
close();
|
||||||
|
LOG_ERROR("Event dispatcher exception");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -338,12 +338,9 @@ bool client::parse_sse_data(const char* data, size_t length) {
|
||||||
std::string event_type = "message";
|
std::string event_type = "message";
|
||||||
auto event_pos = sse_data.find("event: ");
|
auto event_pos = sse_data.find("event: ");
|
||||||
if (event_pos != std::string::npos) {
|
if (event_pos != std::string::npos) {
|
||||||
auto event_end = sse_data.find("\n", event_pos);
|
auto event_end = sse_data.find("\r\n", event_pos);
|
||||||
if (event_end != std::string::npos) {
|
if (event_end != std::string::npos) {
|
||||||
event_type = sse_data.substr(event_pos + 7, event_end - (event_pos + 7));
|
event_type = sse_data.substr(event_pos + 7, event_end - (event_pos + 7));
|
||||||
if (!event_type.empty() && event_type.back() == '\r') {
|
|
||||||
event_type.pop_back();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -352,7 +349,7 @@ bool client::parse_sse_data(const char* data, size_t length) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
auto newline_pos = sse_data.find("\n", data_pos);
|
auto newline_pos = sse_data.find("\r\n", data_pos);
|
||||||
if (newline_pos == std::string::npos) {
|
if (newline_pos == std::string::npos) {
|
||||||
newline_pos = sse_data.length();
|
newline_pos = sse_data.length();
|
||||||
}
|
}
|
||||||
|
|
|
@ -406,7 +406,7 @@ void server::handle_sse(const httplib::Request& req, httplib::Response& res) {
|
||||||
// 发送初始会话URI
|
// 发送初始会话URI
|
||||||
std::this_thread::sleep_for(std::chrono::milliseconds(500));
|
std::this_thread::sleep_for(std::chrono::milliseconds(500));
|
||||||
std::stringstream ss;
|
std::stringstream ss;
|
||||||
ss << "event: endpoint\ndata: " << session_uri << "\n\n";
|
ss << "event: endpoint\r\ndata: " << session_uri << "\r\n\r\n";
|
||||||
session_dispatcher->send_event(ss.str());
|
session_dispatcher->send_event(ss.str());
|
||||||
|
|
||||||
// 更新活动时间(发送消息后)
|
// 更新活动时间(发送消息后)
|
||||||
|
@ -415,14 +415,14 @@ void server::handle_sse(const httplib::Request& req, httplib::Response& res) {
|
||||||
// 定期发送心跳,检测连接状态
|
// 定期发送心跳,检测连接状态
|
||||||
int heartbeat_count = 0;
|
int heartbeat_count = 0;
|
||||||
while (running_ && !session_dispatcher->is_closed()) {
|
while (running_ && !session_dispatcher->is_closed()) {
|
||||||
std::this_thread::sleep_for(std::chrono::seconds(10));
|
std::this_thread::sleep_for(std::chrono::seconds(5) + std::chrono::milliseconds(rand() % 500)); // NOTE: DO NOT set it the same as the timeout of wait_event
|
||||||
|
|
||||||
if (session_dispatcher->is_closed() || !running_) {
|
if (session_dispatcher->is_closed() || !running_) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::stringstream heartbeat;
|
std::stringstream heartbeat;
|
||||||
heartbeat << "event: heartbeat\ndata: " << heartbeat_count++ << "\n\n";
|
heartbeat << "event: heartbeat\r\ndata: " << heartbeat_count++ << "\r\n\r\n";
|
||||||
|
|
||||||
try {
|
try {
|
||||||
bool sent = session_dispatcher->send_event(heartbeat.str());
|
bool sent = session_dispatcher->send_event(heartbeat.str());
|
||||||
|
@ -630,7 +630,7 @@ void server::handle_jsonrpc(const httplib::Request& req, httplib::Response& res)
|
||||||
|
|
||||||
// 通过SSE发送响应
|
// 通过SSE发送响应
|
||||||
std::stringstream ss;
|
std::stringstream ss;
|
||||||
ss << "event: message\ndata: " << response_json.dump() << "\n\n";
|
ss << "event: message\r\ndata: " << response_json.dump() << "\r\n\r\n";
|
||||||
bool result = dispatcher->send_event(ss.str());
|
bool result = dispatcher->send_event(ss.str());
|
||||||
|
|
||||||
if (!result) {
|
if (!result) {
|
||||||
|
@ -830,7 +830,7 @@ void server::send_request(const std::string& session_id, const std::string& meth
|
||||||
|
|
||||||
// 发送请求
|
// 发送请求
|
||||||
std::stringstream ss;
|
std::stringstream ss;
|
||||||
ss << "event: message\ndata: " << req.to_json().dump() << "\n\n";
|
ss << "event: message\r\ndata: " << req.to_json().dump() << "\r\n\r\n";
|
||||||
bool result = dispatcher->send_event(ss.str());
|
bool result = dispatcher->send_event(ss.str());
|
||||||
|
|
||||||
if (!result) {
|
if (!result) {
|
||||||
|
|
Loading…
Reference in New Issue