Skip to content

Commit

Permalink
Fix live activity crash
Browse files Browse the repository at this point in the history
  • Loading branch information
xfangfang committed Jul 21, 2023
1 parent 520fd3c commit 892132e
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 19 deletions.
2 changes: 1 addition & 1 deletion library/libpdr
Submodule libpdr updated 1 files
+7 −6 src/ssdp.cpp
6 changes: 0 additions & 6 deletions wiliwili/include/api/live/danmaku_live.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

#pragma once


#include <borealis.hpp>
#include <borealis/core/singleton.hpp>

Expand All @@ -15,8 +14,6 @@
#include <mutex>
#include <functional>

#define MG_ENABLE_HTTP 1
#define MG_ENABLE_HTTP_WEBSOCKET 1
#include "mongoose.h" // Include Mongoose header file

class LiveDanmaku : public brls::Singleton<LiveDanmaku> {
Expand Down Expand Up @@ -49,7 +46,4 @@ class LiveDanmaku : public brls::Singleton<LiveDanmaku> {
std::mutex mongoose_mutex;
mg_mgr *mgr;
mg_connection *nc;

//人气值
int popularity = 0;
};
19 changes: 11 additions & 8 deletions wiliwili/source/api/danmaku_live.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,6 @@ LiveDanmaku::LiveDanmaku() {
printf("WSAStartup failed with error: %d\n", result);
}
#endif
heartbeat_thread = std::thread([this]() {
while (true) {
std::this_thread::sleep_for(std::chrono::seconds(20));
if (this->is_connected() and this->is_evOK()) {
this->send_heartbeat();
}
}
});
}

LiveDanmaku::~LiveDanmaku() {
Expand Down Expand Up @@ -88,6 +80,17 @@ void LiveDanmaku::connect(int room_id, int uid) {
break;
}
this->mongoose_mutex.unlock();

mg_timer_add(
this->mgr, 20000, MG_TIMER_REPEAT,
[](void *param) {
auto liveDanmaku = static_cast<LiveDanmaku *>(param);
if (liveDanmaku->is_connected() and
liveDanmaku->is_evOK()) {
liveDanmaku->send_heartbeat();
}
},
this);
mg_mgr_poll(this->mgr, wait_time);
}
mg_mgr_free(this->mgr);
Expand Down
19 changes: 15 additions & 4 deletions wiliwili/source/api/util/ws_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,22 @@ std::vector<std::string> parse_packet(const std::vector<uint8_t>& data) {
size_t data_len = data.size();
size_t offset = 0;

uint32_t packet_length;
uint16_t header_length;
uint16_t protocol_version;
uint32_t operation;
while (offset < data_len) {
uint32_t packet_length = ntohl(*reinterpret_cast<const uint32_t*>(data.data() + offset));
uint16_t header_length = ntohs(*reinterpret_cast<const uint16_t*>(data.data() + offset + 4));
uint16_t protocol_version = ntohs(*reinterpret_cast<const uint16_t*>(data.data() + offset + 6));
uint32_t operation = ntohl(*reinterpret_cast<const uint32_t*>(data.data() + offset + 8));
std::memcpy(&packet_length, data.data() + offset, sizeof(uint32_t));
std::memcpy(&header_length, data.data() + offset + 4, sizeof(uint16_t));
std::memcpy(&protocol_version, data.data() + offset + 6,
sizeof(uint16_t));
std::memcpy(&operation, data.data() + offset + 8, sizeof(uint32_t));

packet_length = ntohl(packet_length);
header_length = ntohs(header_length);
protocol_version = ntohs(protocol_version);
operation = ntohl(operation);

offset += header_length;

//| 3 | 服务器 | 数据类型为Int 32 Big Endian | 心跳回应 | Body 内容为房间人气值 |
Expand Down

0 comments on commit 892132e

Please sign in to comment.