|
|
@ -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<void(uint32_t ip, int32_t fromport, uint8_t* data, size_t length)> onMessage_t; |
|
|
|
typedef function<void(XsyncNetAdd& from, uint8_t* data, size_t length)> 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<I_XSyncUDP> create() = 0; |
|
|
|
virtual ~I_XSyncUDPFactory() {} |
|
|
|
virtual shared_ptr<I_XSUDPListener> createXSUDPListener() = 0; |
|
|
|
virtual shared_ptr<I_XSUDP> createXSUDP() = 0; |
|
|
|
}; |
|
|
|
|
|
|
|
class Xsync { |
|
|
|