From 5e99b7c334eae1657ea128754db0d5cd04849c7d Mon Sep 17 00:00:00 2001 From: zhaohe Date: Thu, 4 Jan 2024 18:07:41 +0800 Subject: [PATCH] update --- xsync.cpp | 8 ++------ xsync.hpp | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++------- 2 files changed, 63 insertions(+), 13 deletions(-) diff --git a/xsync.cpp b/xsync.cpp index 6ec68ea..ee2d9b9 100644 --- a/xsync.cpp +++ b/xsync.cpp @@ -1,9 +1,5 @@ #include "xsync.hpp" using namespace iflytop; -Xsync::Xsync(/* args */) { - -} -Xsync::~Xsync() { - -} \ No newline at end of file +Xsync::Xsync(/* args */) {} +Xsync::~Xsync() {} \ No newline at end of file diff --git a/xsync.hpp b/xsync.hpp index f844cd2..b60a31a 100644 --- a/xsync.hpp +++ b/xsync.hpp @@ -12,21 +12,75 @@ namespace iflytop { using namespace std; -class I_XSyncUDP { +typedef enum { + kxs_ec_success = 0, + kxs_ec_overtime, + kxs_ec_socket_fail, + kxs_ec_bind_fail, + kxs_ec_send_fail, + kxs_ec_receive_fail, + kxs_ec_setsockopt_rx_timeout_fail, +} xs_error_code_t; + +class XsyncNetAdd { + public: + uint32_t ip; + uint32_t port; +}; + +class I_XSUDPListener { public: - typedef function onMessage_t; + typedef function onMessage_t; + + public: + /** + * @brief 初始化UDP + * + * @param ip localip default 0 + * @param localport localport + * @param onMessage UDP消息监听 + * @return int + */ + virtual xs_error_code_t initialize(string ip, int localport, onMessage_t onMessage) = 0; + virtual ~I_XSUDPListener() {} +}; +class I_XSUDP { public: - virtual int initialize(uint32_t ip, int localport, onMessage_t onMessage) = 0; - virtual void sendto(uint32_t ip, int32_t dest_port, const char* data, int32_t length) = 0; - virtual ~I_XSyncUDP() {} + /** + * @brief 初始化UDP + * + * @param ip localip default 0 + * @param localport localport + * @return int + */ + virtual xs_error_code_t initialize(string ip, int localport) = 0; + /** + * @brief 发送UDP消息 + * + * @param to + * @param data + * @param length + * @return int + */ + virtual xs_error_code_t sendto(const XsyncNetAdd& to, const char* data, int32_t length) = 0; + /** + * @brief 接收UDP消息 + * + * @param data + * @param length + * @param from + * @return int + */ + virtual xs_error_code_t receive(const char* data, int32_t length, XsyncNetAdd& from, int overtimems) = 0; + virtual ~I_XSUDP() {} }; class I_XSyncUDPFactory { public: - virtual shared_ptr create() = 0; - virtual ~I_XSyncUDPFactory() {} + virtual shared_ptr createXSUDPListener() = 0; + virtual shared_ptr createXSUDP() = 0; }; class Xsync {