Files
vslam_cpp/libs/vslam_web/include/vslam/web_server.h
trung09122002 48bd5377dc upload
2026-09-18 18:14:42 +07:00

96 lines
3.2 KiB
C++

#ifndef VSLAM_WEB_SERVER_H
#define VSLAM_WEB_SERVER_H
// Web UI mot trang thay RViz. HTTP + WebSocket tren Boost.Beast.
//
// KIEN TRUC:
// * io_context chay tren `io_threads` luong (mac dinh 1 -- moi viec nang
// nam o luong khac).
// * Publish() goi tu luong web_tx o 10 Hz, KHONG BAO GIO CHAN: no serialize
// roi asio::post sang strand cua tung phien.
// * Luong web KHONG BAO GIO sua state cua estimator. Moi POST /api/* di qua
// CommandHandler -> hang doi lenh -> luong so huu tai nguyen drain.
//
// GIAO THUC: mot WebSocket /ws, hai loai khung.
// Text = JSON, du lieu tan so thap, nguoi doc duoc.
// Binary = scan / cloud / map / path, header 16 byte (xem binary_codec.h).
#include <cstddef>
#include <functional>
#include <memory>
#include <string>
#include "vslam/web_config.h"
#include "vslam/web_snapshot.h"
#include "vslam/vslam_common.h"
namespace vslam {
// Trang thai dung chung cua server. Dinh nghia trong web_server.cc.
//
// KHONG lam struct long trong WebServer: cac lop phien (HttpSession,
// WsSession) o file .cc phai truy cap duoc, ma mot kieu long private thi ho
// khong goi ten duoc. Day la cung lop loi da gap o ConfigNode::Impl.
class WebServerImpl;
class WebServer {
public:
// Xu ly lenh tu POST /api/<cmd>. `json_args` la body tho (co the rong).
// Duoc goi TREN LUONG IO -- phai tra ve ngay, khong duoc chan. Cach dung
// dung la day vao hang doi roi tra Ok.
using CommandHandler =
std::function<StatusResponse(const std::string& cmd, const std::string& json_args)>;
// Teleop tu joypad cam ung, gia tri chuan hoa [-1, 1] o 20 Hz.
// TOAN BO shaping (ramp gia toc, deadman) lam o C++ trong teleop_shaper,
// KHONG o JS: khong bao gio tin browser ve dinh dang chuyen dong, vi WS co
// the lag hoac mat ket noi giua chung.
using TeleopHandler = std::function<void(double vx_norm, double wz_norm)>;
WebServer(WebConfig cfg, CommandHandler on_command, TeleopHandler on_teleop);
~WebServer();
WebServer(const WebServer&) = delete;
WebServer& operator=(const WebServer&) = delete;
StatusResponse Start();
void Stop();
// Khong chan. An toan goi tu bat ky luong nao.
void Publish(const WebSnapshot& snap);
// Khoi hinh hoc 3D cua the gioi, phuc vu tai `GET /api/world.bin`.
//
// Dat MOT lan luc khoi dong roi thoi -- the gioi khong doi trong mot phien.
// Trinh duyet tai no mot lan de dung do dac THAT thay vi khoi hop dung tu
// luoi chiem dung. Rong = khong co, trinh duyet quay ve khoi hop.
void SetWorldMesh(std::string blob);
size_t client_count() const;
// Cong thuc su dang nghe. Khac cfg.port khi cau hinh port=0 (test).
uint16_t port() const;
struct Stats {
uint64_t connections_total = 0;
uint64_t frames_sent = 0;
uint64_t frames_dropped = 0; // hang doi phien day
uint64_t bytes_sent = 0;
uint64_t commands_handled = 0;
uint64_t auth_failures = 0;
};
Stats stats() const;
private:
void DoAccept();
// shared_ptr chu khong unique_ptr: moi phien HTTP/WS giu mot tham chieu toi
// trang thai nay va co the song lau hon loi goi Stop() mot chut (async
// handler dang bay). unique_ptr o day la use-after-free luc tat may.
std::shared_ptr<WebServerImpl> impl_;
};
} // namespace vslam
#endif // VSLAM_WEB_SERVER_H