UDPConnection/cpp_impl/src/UDPC_Defines.hpp

183 lines
5.3 KiB
C++
Raw Normal View History

2019-06-06 07:02:48 +00:00
#ifndef UDPC_DEFINES_HPP
#define UDPC_DEFINES_HPP
#define UDPC_CONTEXT_IDENTIFIER 0x902F4DB3
2019-07-25 11:51:08 +00:00
#define UDPC_SENT_PKTS_MAX_SIZE 33
#define UDPC_QUEUED_PKTS_MAX_SIZE 32
2019-08-27 04:06:15 +00:00
#define UDPC_RECEIVED_PKTS_MAX_SIZE 50
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>
#include <unordered_set>
#include <queue>
#include <random>
#include <memory>
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 {
static const auto ONE_SECOND = std::chrono::seconds(1);
static const auto TEN_SECONDS = std::chrono::seconds(10);
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);
static const auto PACKET_TIMEOUT_TIME = ONE_SECOND;
static const auto GOOD_RTT_LIMIT = std::chrono::milliseconds(250);
static const auto CONNECTION_TIMEOUT = TEN_SECONDS;
static const auto GOOD_MODE_SEND_RATE = std::chrono::microseconds(33333);
static const auto BAD_MODE_SEND_RATE = std::chrono::milliseconds(100);
// forward declaration
struct Context;
2019-07-25 11:51:08 +00:00
struct SentPktInfo {
typedef std::shared_ptr<SentPktInfo> Ptr;
SentPktInfo();
uint32_t id;
std::chrono::steady_clock::time_point sentTime;
};
struct ConnectionIdentifier {
ConnectionIdentifier();
ConnectionIdentifier(uint32_t addr, uint16_t port);
// copy
ConnectionIdentifier(const ConnectionIdentifier& other) = default;
ConnectionIdentifier& operator =(const ConnectionIdentifier& other) = default;
// move
ConnectionIdentifier(ConnectionIdentifier&& other) = default;
ConnectionIdentifier& operator =(ConnectionIdentifier&& other) = default;
uint64_t id;
/// expects address to be in network byte order (big-endian)
void setAddr(uint32_t addr);
/// expects port to be in native order (not network byte order)
void setPort(uint16_t port);
/// returns address as a 4 byte unsigned int in network byte order (big-endian)
uint32_t getAddr() const;
/// returns port as a 2 byte unsigned int in native order (not network byte order)
uint16_t getPort() const;
bool operator ==(const ConnectionIdentifier& other) const;
struct Hasher {
std::size_t operator()(const ConnectionIdentifier& key) const;
};
};
2019-07-21 05:29:31 +00:00
struct ConnectionData {
ConnectionData();
ConnectionData(bool isServer, Context *ctx);
2019-08-22 07:08:05 +00:00
// copy
ConnectionData(const ConnectionData& other) = delete;
ConnectionData& operator=(const ConnectionData& other) = delete;
// move
ConnectionData(ConnectionData&& other) = default;
ConnectionData& operator=(ConnectionData&& other) = default;
void cleanupSentPkts();
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;
std::chrono::steady_clock::duration timer;
std::chrono::steady_clock::duration toggleT;
std::chrono::steady_clock::duration toggleTimer;
std::chrono::steady_clock::duration toggledTimer;
2019-07-21 05:29:31 +00:00
uint32_t addr; // in network order
2019-08-22 07:08:05 +00:00
uint16_t port; // in native order
2019-07-25 11:51:08 +00:00
std::deque<UDPC_PacketInfo> sentPkts;
TSQueue<UDPC_PacketInfo> sendPkts;
TSQueue<UDPC_PacketInfo> priorityPkts;
2019-08-27 04:03:38 +00:00
TSQueue<UDPC_PacketInfo> receivedPkts;
// pkt id to pkt shared_ptr
std::unordered_map<uint32_t, SentPktInfo::Ptr> sentInfoMap;
2019-07-21 05:29:31 +00:00
std::chrono::steady_clock::time_point received;
std::chrono::steady_clock::time_point sent;
std::chrono::steady_clock::duration rtt;
2019-07-21 05:29:31 +00:00
}; // struct ConnectionData
2019-06-06 07:02:48 +00:00
struct Context {
Context(bool isThreaded);
uint_fast32_t _contextIdentifier;
2019-08-27 05:06:27 +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
* 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;
// ipv4 address and port (as ConnectionIdentifier) to ConnectionData
std::unordered_map<ConnectionIdentifier, ConnectionData, ConnectionIdentifier::Hasher> conMap;
// ipv4 address to all connected ConnectionIdentifiers
std::unordered_map<uint32_t, std::unordered_set<ConnectionIdentifier, ConnectionIdentifier::Hasher> > addrConMap;
// id to ipv4 address and port (as ConnectionIdentifier)
std::unordered_map<uint32_t, ConnectionIdentifier> idMap;
2019-07-25 11:51:08 +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
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);
uint32_t generateConnectionID(Context &ctx);
float durationToFSec(
const std::chrono::steady_clock::time_point& older,
const std::chrono::steady_clock::time_point& newer);
2019-06-06 07:02:48 +00:00
} // namespace UDPC
#endif