mirror of
https://github.com/kunkundi/crossdesk.git
synced 2025-10-26 12:15:34 +08:00
Fix data parse error
This commit is contained in:
@@ -4,6 +4,7 @@
|
||||
#endif
|
||||
|
||||
#include <chrono>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <thread>
|
||||
@@ -34,11 +35,26 @@ std::string window_title = "Remote Desk Client";
|
||||
int thread_exit = 0;
|
||||
PeerPtr *peer = nullptr;
|
||||
|
||||
typedef enum { mouse = 0, keyboard } ControlType;
|
||||
typedef enum { move = 0, left_down, left_up, right_down, right_up } MouseFlag;
|
||||
typedef enum { key_down = 0, key_up } KeyFlag;
|
||||
typedef struct {
|
||||
SDL_KeyCode key;
|
||||
SDL_EventType action;
|
||||
int px;
|
||||
int py;
|
||||
long x;
|
||||
long y;
|
||||
MouseFlag flag;
|
||||
} Mouse;
|
||||
|
||||
typedef struct {
|
||||
long key_value;
|
||||
KeyFlag flag;
|
||||
} Key;
|
||||
|
||||
typedef struct {
|
||||
ControlType type;
|
||||
union {
|
||||
Mouse m;
|
||||
Key k;
|
||||
};
|
||||
} RemoteAction;
|
||||
|
||||
inline void FreshVideo() {
|
||||
@@ -55,13 +71,6 @@ inline void FreshVideo() {
|
||||
|
||||
inline int ProcessMouseKeyEven(SDL_Event &ev) {
|
||||
RemoteAction remote_action;
|
||||
remote_action.key = SDL_KeyCode(ev.key.keysym.sym);
|
||||
remote_action.action = SDL_EventType(ev.type);
|
||||
remote_action.px = ev.button.x;
|
||||
remote_action.py = ev.button.y;
|
||||
|
||||
SendData(peer, DATA_TYPE::DATA, (const char *)&remote_action,
|
||||
sizeof(remote_action));
|
||||
|
||||
if (SDL_KEYDOWN == ev.type) // SDL_KEYUP
|
||||
{
|
||||
@@ -84,10 +93,20 @@ inline int ProcessMouseKeyEven(SDL_Event &ev) {
|
||||
int py = ev.button.y;
|
||||
printf("SDL_MOUSEBUTTONDOWN x, y %d %d \n", px, py);
|
||||
|
||||
remote_action.type = ControlType::mouse;
|
||||
remote_action.m.flag = MouseFlag::left_down;
|
||||
remote_action.m.x = ev.button.x;
|
||||
remote_action.m.y = ev.button.y;
|
||||
|
||||
} else if (SDL_BUTTON_RIGHT == ev.button.button) {
|
||||
int px = ev.button.x;
|
||||
int py = ev.button.y;
|
||||
printf("SDL_BUTTON_RIGHT x, y %d %d \n", px, py);
|
||||
|
||||
remote_action.type = ControlType::mouse;
|
||||
remote_action.m.flag = MouseFlag::right_down;
|
||||
remote_action.m.x = ev.button.x;
|
||||
remote_action.m.y = ev.button.y;
|
||||
}
|
||||
} else if (SDL_MOUSEBUTTONUP == ev.type) {
|
||||
if (SDL_BUTTON_LEFT == ev.button.button) {
|
||||
@@ -95,16 +114,31 @@ inline int ProcessMouseKeyEven(SDL_Event &ev) {
|
||||
int py = ev.button.y;
|
||||
printf("SDL_MOUSEBUTTONUP x, y %d %d \n", px, py);
|
||||
|
||||
remote_action.type = ControlType::mouse;
|
||||
remote_action.m.flag = MouseFlag::left_up;
|
||||
remote_action.m.x = ev.button.x;
|
||||
remote_action.m.y = ev.button.y;
|
||||
|
||||
} else if (SDL_BUTTON_RIGHT == ev.button.button) {
|
||||
int px = ev.button.x;
|
||||
int py = ev.button.y;
|
||||
printf("SDL_MOUSEBUTTONUP x, y %d %d \n", px, py);
|
||||
|
||||
remote_action.type = ControlType::mouse;
|
||||
remote_action.m.flag = MouseFlag::right_up;
|
||||
remote_action.m.x = ev.button.x;
|
||||
remote_action.m.y = ev.button.y;
|
||||
}
|
||||
} else if (SDL_MOUSEMOTION == ev.type) {
|
||||
int px = ev.motion.x;
|
||||
int py = ev.motion.y;
|
||||
|
||||
printf("SDL_MOUSEMOTION x, y %d %d \n", px, py);
|
||||
|
||||
remote_action.type = ControlType::mouse;
|
||||
remote_action.m.flag = MouseFlag::move;
|
||||
remote_action.m.x = ev.button.x;
|
||||
remote_action.m.y = ev.button.y;
|
||||
} else if (SDL_QUIT == ev.type) {
|
||||
SDL_Event event;
|
||||
event.type = SDL_QUIT;
|
||||
@@ -113,10 +147,15 @@ inline int ProcessMouseKeyEven(SDL_Event &ev) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
SendData(peer, DATA_TYPE::DATA, (const char *)&remote_action,
|
||||
sizeof(remote_action));
|
||||
// std::cout << remote_action.type << " " << remote_action.type << " "
|
||||
// << remote_action.px << " " << remote_action.py << std::endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void GuestReceiveBuffer(const char *data, size_t size, const char *user_id,
|
||||
void ReceiveVideoBuffer(const char *data, size_t size, const char *user_id,
|
||||
size_t user_id_size) {
|
||||
// std::cout << "Receive: [" << user_id << "] " << std::endl;
|
||||
memcpy(dst_buffer, data, size);
|
||||
@@ -136,6 +175,18 @@ void GuestReceiveBuffer(const char *data, size_t size, const char *user_id,
|
||||
}
|
||||
}
|
||||
|
||||
void ReceiveAudioBuffer(const char *data, size_t size, const char *user_id,
|
||||
size_t user_id_size) {
|
||||
std::cout << "Receive audio, size " << size << ", user [" << user_id << "] "
|
||||
<< std::endl;
|
||||
}
|
||||
|
||||
void ReceiveDataBuffer(const char *data, size_t size, const char *user_id,
|
||||
size_t user_id_size) {
|
||||
std::cout << "Receive data, size " << size << ", user [" << user_id << "] "
|
||||
<< std::endl;
|
||||
}
|
||||
|
||||
std::string GetMac() {
|
||||
IP_ADAPTER_INFO adapterInfo[16];
|
||||
DWORD bufferSize = sizeof(adapterInfo);
|
||||
@@ -157,11 +208,16 @@ std::string GetMac() {
|
||||
}
|
||||
|
||||
int main() {
|
||||
Params params;
|
||||
params.cfg_path = "../../../../config/config.ini";
|
||||
params.on_receive_buffer = GuestReceiveBuffer;
|
||||
std::string default_cfg_path = "../../../../config/config.ini";
|
||||
std::ifstream f(default_cfg_path.c_str());
|
||||
|
||||
std::string transmission_id = "000000";
|
||||
Params params;
|
||||
params.cfg_path = f.good() ? "../../../../config/config.ini" : "config.ini";
|
||||
params.on_receive_video_buffer = ReceiveVideoBuffer;
|
||||
params.on_receive_audio_buffer = ReceiveAudioBuffer;
|
||||
params.on_receive_data_buffer = ReceiveDataBuffer;
|
||||
|
||||
std::string transmission_id = "000001";
|
||||
std::string user_id = GetMac();
|
||||
|
||||
peer = CreatePeer(¶ms);
|
||||
@@ -199,7 +255,18 @@ int main() {
|
||||
if (event.type == REFRESH_EVENT) {
|
||||
FreshVideo();
|
||||
} else if (event.type == SDL_WINDOWEVENT) {
|
||||
SDL_GetWindowSize(screen, &screen_w, &screen_h);
|
||||
int new_screen_w = 0;
|
||||
int new_screen_h = 0;
|
||||
SDL_GetWindowSize(screen, &new_screen_w, &new_screen_h);
|
||||
|
||||
if (new_screen_w != screen_w) {
|
||||
screen_w = new_screen_w;
|
||||
screen_h = new_screen_w * 9 / 16;
|
||||
} else if (new_screen_h != screen_h) {
|
||||
screen_w = new_screen_h * 16 / 9;
|
||||
screen_h = new_screen_h;
|
||||
}
|
||||
SDL_SetWindowSize(screen, screen_w, screen_h);
|
||||
printf("Resize windows: %dx%d\n", screen_w, screen_h);
|
||||
} else if (event.type == SDL_QUIT) {
|
||||
return 0;
|
||||
|
||||
@@ -3,10 +3,16 @@
|
||||
#ifdef _WIN32
|
||||
#include <Winsock2.h>
|
||||
#include <iphlpapi.h>
|
||||
#include <windows.h>
|
||||
#endif
|
||||
|
||||
#define SDL_MAIN_HANDLED
|
||||
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
|
||||
#include "SDL2/SDL.h"
|
||||
|
||||
extern "C" {
|
||||
#include <libavformat/avformat.h>
|
||||
#include <libavutil/imgutils.h>
|
||||
@@ -15,6 +21,31 @@ extern "C" {
|
||||
|
||||
#define NV12_BUFFER_SIZE 1280 * 720 * 3 / 2
|
||||
|
||||
int screen_w = 0;
|
||||
int screen_h = 0;
|
||||
|
||||
typedef enum { mouse = 0, keyboard } ControlType;
|
||||
typedef enum { move = 0, left_down, left_up, right_down, right_up } MouseFlag;
|
||||
typedef enum { key_down = 0, key_up } KeyFlag;
|
||||
typedef struct {
|
||||
long x;
|
||||
long y;
|
||||
MouseFlag flag;
|
||||
} Mouse;
|
||||
|
||||
typedef struct {
|
||||
long key_value;
|
||||
KeyFlag flag;
|
||||
} Key;
|
||||
|
||||
typedef struct {
|
||||
ControlType type;
|
||||
union {
|
||||
Mouse m;
|
||||
Key k;
|
||||
};
|
||||
} RemoteAction;
|
||||
|
||||
RemoteDeskServer ::RemoteDeskServer() {}
|
||||
|
||||
RemoteDeskServer ::~RemoteDeskServer() {
|
||||
@@ -48,7 +79,7 @@ int BGRAToNV12FFmpeg(unsigned char *src_buffer, int width, int height,
|
||||
return 0;
|
||||
}
|
||||
|
||||
void RemoteDeskServer::HostReceiveBuffer(const char *data, size_t size,
|
||||
void RemoteDeskServer::ReceiveVideoBuffer(const char *data, size_t size,
|
||||
const char *user_id,
|
||||
size_t user_id_size) {
|
||||
std::string msg(data, size);
|
||||
@@ -57,6 +88,45 @@ void RemoteDeskServer::HostReceiveBuffer(const char *data, size_t size,
|
||||
std::cout << "Receive: [" << user << "] " << msg << std::endl;
|
||||
}
|
||||
|
||||
void RemoteDeskServer::ReceiveAudioBuffer(const char *data, size_t size,
|
||||
const char *user_id,
|
||||
size_t user_id_size) {}
|
||||
|
||||
void RemoteDeskServer::ReceiveDataBuffer(const char *data, size_t size,
|
||||
const char *user_id,
|
||||
size_t user_id_size) {
|
||||
std::string user(user_id, user_id_size);
|
||||
|
||||
RemoteAction remote_action;
|
||||
memcpy(&remote_action, data, sizeof(remote_action));
|
||||
|
||||
INPUT ip;
|
||||
|
||||
if (remote_action.type == ControlType::mouse) {
|
||||
ip.type = INPUT_MOUSE;
|
||||
ip.mi.dx = remote_action.m.x * screen_w / 1280;
|
||||
ip.mi.dy = remote_action.m.y * screen_h / 720;
|
||||
if (remote_action.m.flag == MouseFlag::left_down) {
|
||||
ip.mi.dwFlags = MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_ABSOLUTE;
|
||||
} else if (remote_action.m.flag == MouseFlag::left_up) {
|
||||
ip.mi.dwFlags = MOUSEEVENTF_LEFTUP | MOUSEEVENTF_ABSOLUTE;
|
||||
} else if (remote_action.m.flag == MouseFlag::right_down) {
|
||||
ip.mi.dwFlags = MOUSEEVENTF_RIGHTDOWN | MOUSEEVENTF_ABSOLUTE;
|
||||
} else if (remote_action.m.flag == MouseFlag::right_up) {
|
||||
ip.mi.dwFlags = MOUSEEVENTF_RIGHTUP | MOUSEEVENTF_ABSOLUTE;
|
||||
}
|
||||
ip.mi.mouseData = 0;
|
||||
ip.mi.time = 0;
|
||||
|
||||
// Send the press
|
||||
SendInput(1, &ip, sizeof(INPUT));
|
||||
|
||||
std::cout << "Receive data from [" << user << "], " << ip.type << " "
|
||||
<< ip.mi.dwFlags << " " << ip.mi.dx << " " << ip.mi.dy
|
||||
<< std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
std::string GetMac() {
|
||||
IP_ADAPTER_INFO adapterInfo[16];
|
||||
DWORD bufferSize = sizeof(adapterInfo);
|
||||
@@ -78,17 +148,16 @@ std::string GetMac() {
|
||||
}
|
||||
|
||||
int RemoteDeskServer::Init() {
|
||||
std::string default_cfg_path = "../../../../config/config.ini";
|
||||
std::ifstream f(default_cfg_path.c_str());
|
||||
|
||||
Params params;
|
||||
params.cfg_path = "../../../../config/config.ini";
|
||||
params.on_receive_buffer = [](const char *data, size_t size,
|
||||
const char *user_id, size_t user_id_size) {
|
||||
// std::string msg(data, size);
|
||||
// std::string user(user_id, user_id_size);
|
||||
params.cfg_path = f.good() ? "../../../../config/config.ini" : "config.ini";
|
||||
params.on_receive_video_buffer = ReceiveVideoBuffer;
|
||||
params.on_receive_audio_buffer = ReceiveAudioBuffer;
|
||||
params.on_receive_data_buffer = ReceiveDataBuffer;
|
||||
|
||||
// std::cout << "Receive: [" << user << "] " << msg << std::endl;
|
||||
};
|
||||
|
||||
std::string transmission_id = "000000";
|
||||
std::string transmission_id = "000001";
|
||||
std::string user_id = "Server-" + GetMac();
|
||||
peer = CreatePeer(¶ms);
|
||||
CreateConnection(peer, transmission_id.c_str(), user_id.c_str());
|
||||
@@ -103,6 +172,9 @@ int RemoteDeskServer::Init() {
|
||||
rect.right = GetSystemMetrics(SM_CXSCREEN);
|
||||
rect.bottom = GetSystemMetrics(SM_CYSCREEN);
|
||||
|
||||
screen_w = GetSystemMetrics(SM_CXSCREEN);
|
||||
screen_h = GetSystemMetrics(SM_CYSCREEN);
|
||||
|
||||
last_frame_time_ = std::chrono::high_resolution_clock::now();
|
||||
screen_capture->Init(
|
||||
rect, 60,
|
||||
|
||||
@@ -12,7 +12,11 @@ class RemoteDeskServer {
|
||||
public:
|
||||
int Init();
|
||||
|
||||
static void HostReceiveBuffer(const char* data, size_t size,
|
||||
static void ReceiveVideoBuffer(const char* data, size_t size,
|
||||
const char* user_id, size_t user_id_size);
|
||||
static void ReceiveAudioBuffer(const char* data, size_t size,
|
||||
const char* user_id, size_t user_id_size);
|
||||
static void ReceiveDataBuffer(const char* data, size_t size,
|
||||
const char* user_id, size_t user_id_size);
|
||||
|
||||
private:
|
||||
|
||||
17
xmake.lua
17
xmake.lua
@@ -37,27 +37,22 @@ target("screen_capture")
|
||||
|
||||
target("remote_desk_server")
|
||||
set_kind("binary")
|
||||
add_packages("log", "vcpkg::ffmpeg")
|
||||
add_packages("log", "vcpkg::ffmpeg", "vcpkg::sdl2")
|
||||
add_deps("projectx", "screen_capture")
|
||||
add_files("remote_desk_server/*.cpp")
|
||||
add_includedirs("../../src/interface")
|
||||
add_links("swscale", "avutil")
|
||||
add_defines("WIN32_LEAN_AND_MEAN")
|
||||
-- if is_os("windows") then
|
||||
-- add_links("iphlpapi")
|
||||
-- add_includedirs("../../thirdparty/ffmpeg/include")
|
||||
-- if is_mode("debug") then
|
||||
-- add_linkdirs("../../thirdparty/ffmpeg/lib/debug/win")
|
||||
-- else
|
||||
-- add_linkdirs("../../thirdparty/ffmpeg/lib/release/win")
|
||||
-- end
|
||||
-- end
|
||||
if is_os("windows") then
|
||||
add_links("SDL2-static", "SDL2main", "gdi32", "winmm",
|
||||
"setupapi", "version", "Imm32", "iphlpapi")
|
||||
end
|
||||
|
||||
|
||||
target("remote_desk_client")
|
||||
set_kind("binary")
|
||||
add_deps("projectx")
|
||||
add_packages("log")
|
||||
add_packages("ffmpeg")
|
||||
add_packages("vcpkg::sdl2")
|
||||
add_files("remote_desk_client/*.cpp")
|
||||
add_includedirs("../../src/interface")
|
||||
|
||||
Reference in New Issue
Block a user