kunlang_video/include/tunnel_client.hpp
2026-01-21 15:00:01 +08:00

43 lines
998 B
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#pragma once
#include <atomic>
#include <nlohmann/json.hpp>
#include <string>
#include <thread>
#include <websocketpp/client.hpp>
#include <websocketpp/config/asio_no_tls_client.hpp>
// websocketpp 客户端类型
using ws_client = websocketpp::client<websocketpp::config::asio_client>;
class TunnelClient
{
public:
TunnelClient(const std::string& vid, const std::string& server_ws_url, int local_http_port);
void start();
void stop();
private:
void run_loop();
// 处理 Server 的 JSON 请求GET / POST / 大文件流)
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);
private:
std::string vid_;
std::string ws_url_;
int local_port_ = 0;
std::atomic<bool> running_{false};
std::thread th_;
// websocketpp
ws_client* client_ = nullptr;
websocketpp::connection_hdl hdl_;
};