Sleds/K2Daemon/global.h

135 lines
3.4 KiB
C
Raw Normal View History

2025-03-13 21:28:38 +00:00
/*
Copyright (c) 2013 IOnU Security Inc. All rights reserved
Created August 2013 by Kendrick Webster
K2Daemon/global.h - global data and functions and also a catchall for
globably-useful definitions.
*/
#pragma once
// Compile-time options for debugging/testing
//#define TEST_CHRONO
//#define TEST_MONGODB
#define DEBUG_MONGO_NQUEUE
#ifdef DEBUG_MONGO_NQUEUE
#define dbg_mongo_nqueue(...) logprintf(__VA_ARGS__)
#else
#define dbg_mongo_nqueue(...)
#endif
// global constants
constexpr unsigned int ONE_THOUSAND = 1000;
constexpr unsigned int ONE_MILLION = ONE_THOUSAND * ONE_THOUSAND;
constexpr unsigned int TIMER_TICKS_PER_SECOND = 10;
constexpr unsigned int TIMER_MILLISECONDS = ONE_THOUSAND / TIMER_TICKS_PER_SECOND;
constexpr unsigned int TIMER_MICROSECONDS = ONE_MILLION / TIMER_TICKS_PER_SECOND;
constexpr unsigned int MONGODB_FLUSH_TIME_LIMIT_MILLISECONDS = TIMER_MILLISECONDS / 5; // flush max time per try, 1/5 of a timer tick
constexpr unsigned int MONGODB_RECONNECT_DELAY_MILLISECONDS = 250;
// Common headers
#include <chrono>
//#include "../libionu/libionu.h"
#include "../K2Client/K2IPC.h"
#include "LogTrace.h"
#include "LogMemstats.h"
#include "Hash.h"
// Simple, human-readable return status ...
//
// Instead of passing detailed error codes and information to a caller,
// error details are reported directly to a global log facility, and the
// caller is only told whether an operation succeeded or failed.
//
// The enum below is equivalent to a bool return type, but doesn't burden
// a human reader with needing to know which state (true of false) represents
// a failure or success condition.
enum tag_eSuccessFailureType
{
SUCCESS,
FAILURE
};
typedef enum tag_eSuccessFailureType success_t;
// Common function pointer types
typedef void (* vv_callback_t)(void);
// -------------------------- Data and functions --------------------------
// random number source, incrementally seeded by main module
extern sc_hash_state_t rng_hash;
// the UDP socket
extern int sockfd;
// the UDP packet buffer for send
extern uint8_t tx_buf[K2IPC_MAX_PACKET_SIZE];
// the UDP packet buffer for receive
extern uint8_t rx_buf[K2IPC_MAX_PACKET_SIZE];
// the source address of the last received UDP packet
extern struct sockaddr_in recvaddr;
namespace Main
{
void PollDatabase(void);
}
// -------------------------- Utility classes --------------------------
class CStopWatch
{
protected:
std::chrono::steady_clock::time_point m_t0;
public:
CStopWatch()
: m_t0(std::chrono::steady_clock::now())
{
}
~CStopWatch()
{
}
void Reset(void)
{
m_t0 = std::chrono::steady_clock::now();
}
uint64_t u64_Microseconds(void)
{
return std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::steady_clock::now() - m_t0).count();
}
unsigned int Microseconds(void)
{
return static_cast<unsigned int>(u64_Microseconds());
}
uint64_t u64_Milliseconds(void)
{
return std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - m_t0).count();
}
unsigned int Milliseconds(void)
{
return static_cast<unsigned int>(u64_Milliseconds());
}
void DecreaseMilliseconds(unsigned int n)
{
m_t0 += std::chrono::milliseconds(n);
}
void DecreaseMicroseconds(unsigned int n)
{
m_t0 += std::chrono::microseconds(n);
}
};