2019-06-06 07:02:48 +00:00
|
|
|
#ifndef UDPC_DEFINES_HPP
|
|
|
|
#define UDPC_DEFINES_HPP
|
|
|
|
|
|
|
|
#define UDPC_CONTEXT_IDENTIFIER 0x902F4DB3
|
2019-07-21 05:29:31 +00:00
|
|
|
#define UDPC_TIMEOUT_SECONDS 10.0f
|
2019-07-25 11:51:08 +00:00
|
|
|
#define UDPC_GOOD_MODE_SEND_INTERVAL (1.0f / 30.0f)
|
|
|
|
#define UDPC_BAD_MODE_SEND_INTERVAL (1.0f / 10.0f)
|
|
|
|
#define UDPC_SENT_PKTS_MAX_SIZE 33
|
2019-08-18 10:14:14 +00:00
|
|
|
#define UDPC_QUEUED_PKTS_MAX_SIZE 32
|
2019-07-25 11:51:08 +00:00
|
|
|
|
|
|
|
#define UDPC_ID_CONNECT 0x80000000
|
|
|
|
#define UDPC_ID_PING 0x40000000
|
|
|
|
#define UDPC_ID_NO_REC_CHK 0x20000000
|
|
|
|
#define UDPC_ID_RESENDING 0x10000000
|
2019-06-06 07:02:48 +00:00
|
|
|
|
|
|
|
#include <atomic>
|
2019-06-06 07:42:07 +00:00
|
|
|
#include <bitset>
|
2019-07-25 11:51:08 +00:00
|
|
|
#include <chrono>
|
2019-06-06 07:42:07 +00:00
|
|
|
#include <cstdint>
|
2019-07-21 05:29:31 +00:00
|
|
|
#include <deque>
|
|
|
|
#include <unordered_map>
|
2019-08-18 10:14:14 +00:00
|
|
|
#include <random>
|
2019-06-06 07:02:48 +00:00
|
|
|
|
2019-07-21 05:29:31 +00:00
|
|
|
#include "TSQueue.hpp"
|
2019-07-25 11:51:08 +00:00
|
|
|
#include "UDPConnection.h"
|
2019-06-06 07:02:48 +00:00
|
|
|
|
|
|
|
namespace UDPC {
|
|
|
|
|
2019-07-25 11:51:08 +00:00
|
|
|
static uint32_t LOCAL_ADDR = 0;
|
|
|
|
static const auto INIT_PKT_INTERVAL_DT = std::chrono::seconds(5);
|
|
|
|
static const auto HEARTBEAT_PKT_INTERVAL_DT = std::chrono::milliseconds(150);
|
2019-08-18 10:14:14 +00:00
|
|
|
static const auto PACKET_TIMEOUT_TIME = std::chrono::seconds(1);
|
|
|
|
|
|
|
|
struct Context;
|
2019-07-25 11:51:08 +00:00
|
|
|
|
2019-07-21 05:29:31 +00:00
|
|
|
struct ConnectionData {
|
2019-08-18 10:14:14 +00:00
|
|
|
ConnectionData();
|
|
|
|
ConnectionData(bool isServer, Context *ctx);
|
|
|
|
|
2019-07-21 05:29:31 +00:00
|
|
|
/*
|
|
|
|
* 0 - trigger send
|
|
|
|
* 1 - is good mode
|
|
|
|
* 2 - is good rtt
|
2019-07-25 11:51:08 +00:00
|
|
|
* 3 - initiating connection
|
2019-07-21 05:29:31 +00:00
|
|
|
* 4 - is id set
|
|
|
|
*/
|
|
|
|
std::bitset<32> flags;
|
|
|
|
uint32_t id;
|
|
|
|
uint32_t lseq;
|
|
|
|
uint32_t rseq;
|
|
|
|
uint32_t ack;
|
|
|
|
float timer;
|
|
|
|
float toggleT;
|
|
|
|
float toggleTimer;
|
|
|
|
float toggledTimer;
|
|
|
|
uint32_t addr; // in network order
|
|
|
|
uint16_t port;
|
2019-07-25 11:51:08 +00:00
|
|
|
std::deque<UDPC_PacketInfo> sentPkts;
|
|
|
|
TSQueue<UDPC_PacketInfo> sendPkts;
|
|
|
|
TSQueue<UDPC_PacketInfo> priorityPkts;
|
2019-07-21 05:29:31 +00:00
|
|
|
std::chrono::steady_clock::time_point received;
|
|
|
|
std::chrono::steady_clock::time_point sent;
|
|
|
|
float rtt;
|
|
|
|
}; // struct ConnectionData
|
|
|
|
|
2019-06-06 07:02:48 +00:00
|
|
|
struct Context {
|
|
|
|
Context(bool isThreaded);
|
|
|
|
|
|
|
|
uint_fast32_t _contextIdentifier;
|
2019-08-18 10:14:14 +00:00
|
|
|
char recvBuf[UDPC_PACKET_MAX_SIZE];
|
2019-06-06 07:02:48 +00:00
|
|
|
/*
|
2019-07-25 11:51:08 +00:00
|
|
|
* 0 - is threaded
|
|
|
|
* 1 - is client
|
2019-08-18 10:14:14 +00:00
|
|
|
* 2 - is accepting new connections
|
2019-06-06 07:02:48 +00:00
|
|
|
*/
|
|
|
|
std::bitset<32> flags;
|
|
|
|
std::atomic_bool isAcceptNewConnections;
|
|
|
|
std::atomic_uint32_t protocolID;
|
|
|
|
std::atomic_uint_fast8_t loggingType;
|
|
|
|
char atostrBuf[16];
|
2019-07-21 05:29:31 +00:00
|
|
|
|
|
|
|
int socketHandle;
|
|
|
|
struct sockaddr_in socketInfo;
|
|
|
|
|
|
|
|
std::chrono::steady_clock::time_point lastUpdated;
|
2019-08-18 10:14:14 +00:00
|
|
|
// ipv4 address to ConnectionData
|
2019-07-21 05:29:31 +00:00
|
|
|
std::unordered_map<uint32_t, ConnectionData> conMap;
|
2019-08-18 10:14:14 +00:00
|
|
|
// id to ipv4 address
|
2019-07-25 11:51:08 +00:00
|
|
|
std::unordered_map<uint32_t, uint32_t> idMap;
|
|
|
|
|
2019-08-18 10:14:14 +00:00
|
|
|
std::default_random_engine rng_engine;
|
|
|
|
|
2019-06-06 07:02:48 +00:00
|
|
|
}; // struct Context
|
|
|
|
|
2019-07-25 11:51:08 +00:00
|
|
|
Context *verifyContext(void *ctx);
|
2019-06-06 07:02:48 +00:00
|
|
|
|
2019-07-07 05:44:37 +00:00
|
|
|
bool isBigEndian();
|
|
|
|
|
2019-07-25 11:51:08 +00:00
|
|
|
/*
|
|
|
|
* flags:
|
|
|
|
* - 0x1 - connect
|
|
|
|
* - 0x2 - ping
|
|
|
|
* - 0x4 - no_rec_chk
|
|
|
|
* - 0x8 - resending
|
|
|
|
*/
|
|
|
|
void preparePacket(char *data, uint32_t protocolID, uint32_t conID,
|
|
|
|
uint32_t rseq, uint32_t ack, uint32_t *seqID, int flags);
|
|
|
|
|
2019-08-18 10:14:14 +00:00
|
|
|
uint32_t generateConnectionID(Context &ctx);
|
|
|
|
|
2019-06-06 07:02:48 +00:00
|
|
|
} // namespace UDPC
|
|
|
|
|
|
|
|
#endif
|