feat(libmsg): add pymsg C++ library with AsyncQueue, MsgServer, and JSON RPC layer

- Add libmsg module: pymsg protocol implementation (4-byte BE length prefix + UTF-8)
- Add AsyncQueue<T>: generic async batch queue with backpressure (aligned with Python AsyncRpcQueue)
- Add MsgServer: select-based multi-client TCP server with optional thread pool
- Add RpcClient/RpcServer: JSON RPC convention layer with automatic route injection
- Refactor gateway/LogC.hpp to use AsyncQueue for async batch log delivery
- Update gateway/DbC.hpp to add call_msg() using pymsg TCP protocol
- Remove duplicated gateway/inc/Msg.hpp (now provided by libmsg)
- Update gateway/Makefile and _inst.sh build order

Fixes included:
- pmsg_send size check uses kMaxMsgSize (64MiB) instead of 4GiB
- MsgClient._running uses std::atomic<bool> for thread safety
- Deadline-based recv timeout to prevent Slowloris (header+payload shared deadline)
This commit is contained in:
qing committed 2026-08-06 23:48:06 +08:00
1 parent 7ff73b7d07
commit 1d44e0dcb8
17 files changed
+2483 -482

No files matched your search

+79
View File
@@ -0,0 +1,79 @@
#pragma once
/** libmsg — UniServe 消息通信基础库
*
* 提供 pymsg 协议的 C++ 实现:4字节大端长度前缀 + UTF-8 载荷的 TCP 消息帧协议。
* 对应 Python 版 pymsg/pymsg/__init__.py,字节级兼容。
*
* 命名空间 pmc::net,库文件 libus_msg.so,头文件安装至 /usr/include/us/。
*/
#include <cstddef>
#include <cstdint>
#include <stdexcept>
#include <string>
#define MSG_CHECK(condition, message) \
if (!(condition)) { \
throw std::runtime_error(message); \
}
namespace pmc {
namespace net {
/// 协议常量
namespace msg_constants {
constexpr size_t kHeaderSize = 4; ///< 帧头长度(字节)
constexpr uint32_t kMaxMsgSize = 64u * 1024u * 1024u; ///< 消息最大长度 64 MiB
constexpr int kDefaultTimeout = 10; ///< 默认超时(秒)
constexpr int kSendTimeout = 3; ///< send() 默认超时(秒)
constexpr int kListenPollMs = 100'000; ///< listen() select 轮询间隔(微秒)
constexpr double kRetryLogEvery = 5.0; ///< 重试等待日志间隔(秒)
}
// ============================================================================
// 异常层次
// ============================================================================
/// 消息库基础异常
class MsgException : public std::runtime_error {
public:
explicit MsgException(const std::string &msg) : std::runtime_error(msg) {}
};
/// 消息收发超时
class MsgTimeoutException : public MsgException {
public:
explicit MsgTimeoutException(const std::string &msg) : MsgException(msg) {}
};
/// 连接异常(断开、重置、地址错误等)
class MsgConnectionException : public MsgException {
public:
explicit MsgConnectionException(const std::string &msg) : MsgException(msg) {}
};
/// 目标服务未启动(ECONNREFUSED),用于 retry_on_refused 重试机制
class MsgConnectionRefusedException : public MsgConnectionException {
public:
explicit MsgConnectionRefusedException(const std::string &msg)
: MsgConnectionException(msg) {}
};
// ============================================================================
// 基类(供 MsgClient 等内部工具方法继承使用)
// ============================================================================
class Msg_Basic {
public:
class MsgExp : public std::runtime_error {
public:
MsgExp(const std::string& msg) : std::runtime_error(msg) {}
};
protected:
/// 版本字符串(编译进 .so)
static const char* version();
};
} // namespace net
} // namespace pmc