sweeper_video/include/tunnel_client.hpp

36 lines
845 B
C++
Raw Normal View History

2026-01-21 14:02:52 +08:00
#pragma once
2026-01-21 15:00:01 +08:00
2026-01-21 14:02:52 +08:00
#include <atomic>
2026-01-21 15:00:01 +08:00
#include <nlohmann/json.hpp>
2026-01-21 14:02:52 +08:00
#include <string>
#include <thread>
2026-01-21 15:00:01 +08:00
#include <websocketpp/client.hpp>
#include <websocketpp/config/asio_no_tls_client.hpp>
2026-01-21 14:58:32 +08:00
2026-01-21 15:00:01 +08:00
using ws_client = websocketpp::client<websocketpp::config::asio_client>;
2026-01-21 14:02:52 +08:00
class TunnelClient
{
public:
TunnelClient(const std::string& vid, const std::string& server_ws_url, int local_http_port);
2026-01-21 15:00:01 +08:00
2026-01-21 14:02:52 +08:00
void start();
void stop();
private:
void run_loop();
2026-01-21 14:58:32 +08:00
void handle_request_and_reply(const nlohmann::json& req);
void send_text_safe(const std::string& s);
void send_binary_safe(const void* data, size_t len);
2026-01-21 14:02:52 +08:00
private:
std::string vid_;
std::string ws_url_;
2026-01-21 14:58:32 +08:00
int local_port_ = 0;
2026-01-21 14:02:52 +08:00
std::atomic<bool> running_{false};
std::thread th_;
2026-01-21 14:58:32 +08:00
2026-01-21 15:00:01 +08:00
ws_client* client_ = nullptr;
2026-01-21 14:58:32 +08:00
websocketpp::connection_hdl hdl_;
2026-03-19 10:14:40 +08:00
};