pans
2017-08-30 71c92f101b6c8b4a678a8c3cfe2d8edbf488efa4
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
#ifndef _SIMPLEUDPPEER_H_
#define _SIMPLEUDPPEER_H_
 
#include <cstdint>
#include <string>
#include <linux/in.h>
 
struct AsyncStub;
 
class SimpleUDPPeer
{
    friend struct AsyncStub;
 
public:
    struct Config
    {
        timeval defaultTimeout;
        size_t maxBuffSize;
        std::string local_ip;
        short local_port;
        std::string remote_ip;
        short remote_port;
        
        Config() : defaultTimeout(), maxBuffSize(60000), local_ip(), local_port(0), remote_ip(), remote_port(0)
        {
            defaultTimeout.tv_sec = 0;
            defaultTimeout.tv_usec = 0;
        }
    };
    
    typedef void (*proc_func_t)(void* args, uint8_t* buffer, size_t buffSize);
 
    SimpleUDPPeer();
    ~SimpleUDPPeer();
    
    void set_config(const Config& _cfg);
    const Config& get_config() const;
 
    bool init();
    void teardown();
    bool send_sync(const uint8_t* buffer, size_t& buffSize);
    bool send_async(const uint8_t* buffer, size_t buffSize);
    bool recv_sync(uint8_t* buffer, size_t& buffSize);
    bool recv_async_start(proc_func_t proc, void* args = nullptr);
    void recv_async_stop();
    //#todo error callback
 
private:
    Config cfg;
    sockaddr_in local_address;
    sockaddr_in remote_address;
    int fd;
    AsyncStub* asyncStub;
};
 
#endif