yy1717
2021-01-19 87156fa3adfa2e3232a6f6e612584aa8a4ebaea1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
//
// Created by YY on 2019/9/29.
//
 
#ifndef RTKBASESTATION_NET_H
#define RTKBASESTATION_NET_H
 
#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