You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

80 lines
1.7 KiB

#pragma once
#include <fstream>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <set>
#include <sstream>
#include <string>
#include <vector>
#include "xsync_errcode.hpp"
namespace xsync {
using namespace std;
class XsyncNetAdd {
public:
XsyncNetAdd(){};
XsyncNetAdd(string ip, uint32_t port) : ip(ip), port(port) {}
string ip;
uint32_t port;
};
class I_XSUDP {
public:
typedef function<void(XsyncNetAdd& from, uint8_t* data, size_t length)> onMessage_t;
public:
/**
* @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
* >0 发送成功,返回发送的字节数
* <0 发送失败
*/
virtual xs_error_code_t sendto(const XsyncNetAdd& to, const char* data, int32_t length, int32_t* sendlength) = 0;
/**
* @brief 接收UDP消息
*
* @param data
* @param length
* @param from
* @return int
* >0 接收成功,返回接收的字节数
* <0 接收失败
*/
virtual xs_error_code_t receive(char* data, int32_t& length, XsyncNetAdd& from, int overtimems) = 0;
/**
* @brief 开始接收UDP消息
*
* @param onMessage
* @return xs_error_code_t
*/
virtual xs_error_code_t startReceive(onMessage_t onMessage) = 0;
virtual xs_error_code_t stopReceive() = 0;
virtual xs_error_code_t clearRxBuffer() = 0;
virtual ~I_XSUDP() {}
};
class I_XSUDPFactory {
public:
virtual shared_ptr<I_XSUDP> createXSUDP() = 0;
};
} // namespace xsync