#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
|