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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#ifndef _FUNCTIONALUDPPEER_H_
#define _FUNCTIONALUDPPEER_H_
 
#include "SimpleUDPPeer.h"
#include "PreAllocBufferPriorityQueue.h"
 
#include <map>
#include <set>
 
#define FUP_MAGIC 0xB52D
 
#pragma pack(1)
typedef uint16_t fuph_seq_t;
typedef uint8_t fuph_grpseq_t;
 
typedef void(*proc_func_t)(int a,int b);
 
struct FUPHeader
{
private:
    //uint8_t _padding0;
 
public:
    uint16_t magic;
    fuph_seq_t seq;
    fuph_grpseq_t grpseq;
    uint8_t parts;
    uint16_t size;
    uint8_t data[0];
 
    FUPHeader() : 
        //_padding0(0), 
        magic(FUP_MAGIC), seq(0), grpseq(0), parts(0), size(0), data()
    {}
 
    void hton();
    void ntoh();
};
#pragma pack()
 
class FunctionalUDPPeer
{
public:
    struct PartitionWrapper
    {
        const fuph_grpseq_t grpseq;
        const size_t maxGrpSeq;
        size_t packetizedCount;
        
        explicit PartitionWrapper() : grpseq(0), maxGrpSeq(0), packetizedCount(0)
        {}
        
        explicit PartitionWrapper(fuph_grpseq_t _grpseq, size_t _maxGrpSeq) : grpseq(_grpseq), maxGrpSeq(_maxGrpSeq), packetizedCount(0)
        {}
    };
    
    struct Config
    {
        size_t maxEagainSpin;
        size_t maxSeqInPartition;
        size_t maxPartitions;
        size_t maxPacketize;
        PreAllocBufferPriorityQueue::Config recvQueueCfg;
        SimpleUDPPeer::Config simpleUDPPeerCfg;
        
        Config() : maxEagainSpin(2), maxSeqInPartition(10), maxPartitions(4), simpleUDPPeerCfg()
        {}
    };
 
    FunctionalUDPPeer();
    ~FunctionalUDPPeer();
    
    void set_config(const Config& _cfg);
    //const Config& get_config() const;
    
    bool listen(const std::string& ip, short port);
    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 recv_async_stop();
    //#todo error callback
 
private:
    Config cfg;
    SimpleUDPPeer simpleUDPPeer;
    //PreAllocBufferPriorityQueue* recvQueue;
    
    typedef std::set<PreAllocBufferPriorityQueue::Buffer*> partitions_set_t;
    typedef std::map<PartitionWrapper, partitions_set_t> partitions_map_t;
    partitions_map_t partitions;
    
    uint8_t* mySendBuffer;
    size_t mySendBuffSize;
    size_t mySendBuffMaxSize;
    
    uint8_t* myRecvBuffer;
    size_t myRecvBuffSize;
    size_t myRecvBuffMaxSize;
    
    fuph_seq_t mySeq;
    fuph_grpseq_t myGrpSeq;
    
    proc_func_t recv_async_proc_user;
    
    void turnover_seq();
    bool insert_partitions(PreAllocBufferPriorityQueue::Buffer* qbuff);
    bool packetize(uint8_t* buffer, size_t& buffSize);
    static void recv_async_proc(void* args, uint8_t* buffer, size_t buffSize);
};
 
#endif