[fix] use serialize and deserialize function to process RemoteAction::HostInfo

This commit is contained in:
dijunkun
2025-05-15 17:52:40 +08:00
parent a6beb48b1f
commit 0a1014dded
4 changed files with 106 additions and 17 deletions

View File

@@ -216,7 +216,7 @@ int ScreenCapturerWgc::Stop() {
int ScreenCapturerWgc::SwitchTo(int monitor_index) {
if (monitor_index_ == monitor_index) {
LOG_INFO("Already on monitor {}:{}", monitor_index_,
LOG_INFO("Already on monitor {}:{}", monitor_index_ + 1,
display_info_list_[monitor_index_].name);
return 0;
}

View File

@@ -21,6 +21,77 @@
#define STREAM_FRASH (SDL_USEREVENT + 1)
std::vector<char> Render::SerializeRemoteAction(const RemoteAction& action) {
std::vector<char> buffer;
buffer.push_back(static_cast<char>(action.type));
if (action.type == ControlType::host_infomation) {
size_t name_len = action.i.host_name_size;
buffer.insert(buffer.end(), reinterpret_cast<char*>(&name_len),
reinterpret_cast<char*>(&name_len) + sizeof(size_t));
buffer.insert(buffer.end(), action.i.host_name,
action.i.host_name + name_len);
size_t display_num = action.i.display_num;
buffer.insert(buffer.end(), reinterpret_cast<char*>(&display_num),
reinterpret_cast<char*>(&display_num) + sizeof(size_t));
for (size_t i = 0; i < display_num; ++i) {
const char* name = action.i.display_list[i];
size_t len = strlen(name);
buffer.insert(buffer.end(), reinterpret_cast<char*>(&len),
reinterpret_cast<char*>(&len) + sizeof(size_t));
buffer.insert(buffer.end(), reinterpret_cast<const char*>(name),
reinterpret_cast<const char*>(name) + len);
}
}
return buffer;
}
bool Render::DeserializeRemoteAction(const char* data, size_t size,
RemoteAction& out) {
size_t offset = 0;
if (size < 1) return false;
out.type = static_cast<ControlType>(data[offset]);
offset += 1;
if (out.type == ControlType::host_infomation) {
if (offset + sizeof(size_t) > size) return false;
size_t host_name_len = *reinterpret_cast<const size_t*>(data + offset);
offset += sizeof(size_t);
if (offset + host_name_len > size ||
host_name_len >= sizeof(out.i.host_name))
return false;
memcpy(out.i.host_name, data + offset, host_name_len);
out.i.host_name[host_name_len] = '\0';
out.i.host_name_size = host_name_len;
offset += host_name_len;
if (offset + sizeof(size_t) > size) return false;
size_t display_num = *reinterpret_cast<const size_t*>(data + offset);
out.i.display_num = display_num;
offset += sizeof(size_t);
out.i.display_list = (char**)malloc(display_num * sizeof(char*));
for (size_t i = 0; i < display_num; ++i) {
if (offset + sizeof(size_t) > size) return false;
size_t len = *reinterpret_cast<const size_t*>(data + offset);
offset += sizeof(size_t);
if (offset + len > size) return false;
out.i.display_list[i] = (char*)malloc(len + 1);
memcpy(out.i.display_list[i], data + offset, len);
out.i.display_list[i][len] = '\0';
offset += len;
}
}
return true;
}
void Render::FreeRemoteAction(RemoteAction& action) {
if (action.type == ControlType::host_infomation) {
for (size_t i = 0; i < action.i.display_num; ++i) {
free(action.i.display_list[i]);
}
free(action.i.display_list);
action.i.display_list = nullptr;
action.i.display_num = 0;
}
}
SDL_HitTestResult Render::HitTestCallback(SDL_Window* window,
const SDL_Point* area, void* data) {
Render* render = (Render*)data;
@@ -841,7 +912,7 @@ void Render::MainLoop() {
CreateConnection(peer_, client_id_, password_saved_) ? false : true;
}
if (!host_info_sent_ && screen_width_ > 0 && screen_height_ > 0) {
if (need_to_send_host_info_) {
RemoteAction remote_action;
if (screen_capturer_) {
display_info_list_ = screen_capturer_->GetDisplayInfoList();
@@ -864,11 +935,15 @@ void Render::MainLoop() {
std::string host_name = GetHostName();
remote_action.type = ControlType::host_infomation;
memcpy(&remote_action.i.host_name, host_name.data(), host_name.size());
remote_action.i.host_name[host_name.size()] = '\0';
remote_action.i.host_name_size = host_name.size();
int ret = SendDataFrame(peer_, (const char*)&remote_action,
sizeof(remote_action), data_label_.c_str());
std::vector<char> serialized = SerializeRemoteAction(remote_action);
int ret = SendDataFrame(peer_, serialized.data(), serialized.size(),
data_label_.c_str());
FreeRemoteAction(remote_action);
if (0 == ret) {
host_info_sent_ = true;
need_to_send_host_info_ = false;
}
}
}

View File

@@ -192,6 +192,13 @@ class Render {
static SDL_HitTestResult HitTestCallback(SDL_Window *window,
const SDL_Point *area, void *data);
static std::vector<char> SerializeRemoteAction(const RemoteAction &action);
static bool DeserializeRemoteAction(const char *data, size_t size,
RemoteAction &out);
static void FreeRemoteAction(RemoteAction &action);
private:
int SendKeyCommand(int key_code, bool is_down);
int ProcessMouseEvent(SDL_Event &event);
@@ -320,7 +327,7 @@ class Render {
bool need_to_rejoin_ = false;
bool just_created_ = false;
std::string controlled_remote_id_ = "";
bool host_info_sent_ = false;
bool need_to_send_host_info_ = false;
SDL_Event last_mouse_event;
// stream window render

View File

@@ -285,18 +285,26 @@ void Render::OnReceiveDataBufferCb(const char *data, size_t size,
render->client_properties_.end()) {
// local
auto props = render->client_properties_.find(remote_id)->second;
if (ControlType::host_infomation == remote_action.type) {
props->remote_host_name_ = std::string(remote_action.i.host_name,
remote_action.i.host_name_size);
RemoteAction host_info;
if (DeserializeRemoteAction(data, size, host_info)) {
if (ControlType::host_infomation == host_info.type) {
props->remote_host_name_ =
std::string(host_info.i.host_name, host_info.i.host_name_size);
LOG_INFO("Remote hostname: [{}]", props->remote_host_name_);
props->display_names_ = restore_display_list(remote_action.i.display_list,
remote_action.i.display_num);
for (int i = 0; i < props->display_names_.size(); i++) {
for (int i = 0; i < host_info.i.display_num; i++) {
props->display_names_.push_back(
std::string(host_info.i.display_list[i]));
LOG_INFO("Remote display [{}:{}]", i + 1, props->display_names_[i]);
}
}
} else {
props->remote_host_name_ = std::string(remote_action.i.host_name,
remote_action.i.host_name_size);
LOG_INFO("Remote hostname: [{}]", props->remote_host_name_);
LOG_ERROR("No remote display detected");
}
FreeRemoteAction(host_info);
} else {
// remote
if (ControlType::mouse == remote_action.type && render->mouse_controller_) {
@@ -409,7 +417,6 @@ void Render::OnConnectionStatusCb(ConnectionStatus status, const char *user_id,
render->start_mouse_controller_ = false;
render->start_keyboard_capturer_ = false;
render->control_mouse_ = false;
render->host_info_sent_ = false;
props->connection_established_ = false;
props->mouse_control_button_pressed_ = false;
if (props->dst_buffer_) {
@@ -444,7 +451,7 @@ void Render::OnConnectionStatusCb(ConnectionStatus status, const char *user_id,
switch (status) {
case ConnectionStatus::Connected:
render->host_info_sent_ = false;
render->need_to_send_host_info_ = true;
render->start_screen_capturer_ = true;
render->start_mouse_controller_ = true;
break;
@@ -452,7 +459,7 @@ void Render::OnConnectionStatusCb(ConnectionStatus status, const char *user_id,
render->start_screen_capturer_ = false;
render->start_mouse_controller_ = false;
render->start_keyboard_capturer_ = false;
render->host_info_sent_ = false;
render->need_to_send_host_info_ = false;
if (props) props->connection_established_ = false;
if (render->audio_capture_) {
render->StopSpeakerCapturer();