yy1717
2021-01-19 87156fa3adfa2e3232a6f6e612584aa8a4ebaea1
lib/src/main/cpp/common/net.h
@@ -5,13 +5,53 @@
#ifndef RTKBASESTATION_NET_H
#define RTKBASESTATION_NET_H
int WriteTCP(int fd, const uint8_t * buf, uint32_t len);
int ReadTCP(int fd, uint8_t * buf, uint32_t len);
int ConnectTCP(const char *ip, uint16_t port);
void DisconnectTCP(int fd);
int EstablishUDP(const char *ip, uint16_t port);
void RemoveUDP(int fd);
int WriteUDP(int fd, char *ip, uint16_t port, const uint8_t * buf, uint32_t len);
int ReadUDP(int fd, uint8_t * buf, uint32_t len);
#include <string>
enum SERVICE_TYPE {
    TYPE_SERVER,
    TYPE_CLIENT
};
class CTcpPort
{
public:
    SERVICE_TYPE m_iServiceType;
    std::string m_sIp;
    int m_nPort;
    bool m_connected;
    int m_pServerSocket;
    int m_pClientSocket;
public:
    CTcpPort();
    virtual ~CTcpPort();
    bool IsOpen(void);
    bool CloseTcpPort();
    bool OpenTcpPort(const char *ip, int port);
    void set_event_callback(void (*callback)(int, void *, void *), void *context);
    void set_data_callback(void (*callback)(void *, int, void *, void *), void *context);
    int WriteTCP(const uint8_t * buf, uint32_t len);
private:
    int GetHostIP(const char *host_name, char *net_addr);
    bool is_domain_name(const char *ip);
    int socket_set_keepalive(int fd);
    int tcp_connect(char *ip, uint16_t port);
    void(*event_func)(int, void *, void *);   //数据回调函数指针
    void *event_func_context;   //数据回调函数上下文
    void(*receive_data_func)(void *, int, void *, void *);   //数据回调函数指针
    void *receive_data_func_context;   //数据回调函数上下文
    static void *TcpConnectThread(void *p);
    static void *TcpListenThread(void *p);
};
//int WriteTCP(int fd, const uint8_t * buf, uint32_t len);
//int ReadTCP(int fd, uint8_t * buf, uint32_t len);
//int ConnectTCP(const char *ip, uint16_t port);
//void DisconnectTCP(int fd);
#endif //RTKBASESTATION_NET_H