From 9fe643eb99098567f60c86732db8b95e0a26e22f Mon Sep 17 00:00:00 2001 From: malloc Date: Thu, 1 Mar 2018 17:16:47 -0600 Subject: [PATCH] time functions in netlib do NOT look --- server/src/main.cpp | 11 +++++- server/src/utils/net.cpp | 85 ++++++++++++++++++++++++++++++++++++++++ server/src/utils/net.hpp | 22 ++++++++++- 3 files changed, 114 insertions(+), 4 deletions(-) diff --git a/server/src/main.cpp b/server/src/main.cpp index d753a19..6da04e9 100644 --- a/server/src/main.cpp +++ b/server/src/main.cpp @@ -2,15 +2,22 @@ #include #include "sock/tcpsock.hpp" #include "utils/string.hpp" +#include "utils/net.hpp" int main(int argc, char **argv) { //auto sock = sosc::TcpClient(); - std::string a = "this!!is!!a!!test"; + /*std::string a = "this!!is!!a!!test"; auto b = sosc::str::split(a, "!!"); std::for_each(b.begin(), b.end(), [](std::string& i) { std::cout << i << std::endl; - }); + });*/ + + uint32_t test = 0xDEADBEEF; + std::string net = HTONUL(test); + test = NTOHUL(net); + + std::cout << std::hex << test << std::hex << net << std::endl; return 0; } diff --git a/server/src/utils/net.cpp b/server/src/utils/net.cpp index c389e05..3ff1f0b 100644 --- a/server/src/utils/net.cpp +++ b/server/src/utils/net.cpp @@ -1,2 +1,87 @@ #include "net.hpp" +bool sosc::net::is_big_endian() { + static uint16_t check = 0xFF00; + if(check == 0xFF00) + check = *((uint8_t*)&check) == 0xFF; + + return check; +} + +template +std::string sosc::net::htonv(T host_var) { + int byte_count = sizeof(T); + std::string net_var(byte_count, 0); + + for(int b = 0; b < byte_count; ++b) { + int i = is_big_endian() + ? b : byte_count - b - 1; + + net_var[i] = ((uint8_t*)&host_var)[b]; + } + + return net_var; +} + +template +T sosc::net::ntohv(std::string net_var, size_t offset) { + int byte_count = sizeof(T); + T host_var = 0; + + for(int b = 0; b < byte_count; ++b) { + if(offset + b >= net_var.length()) + break; + + int i = is_big_endian() + ? b : byte_count - b - i; + + ((uint8_t*)&host_var)[i] = net_var[offset + b]; + } + + return host_var; +} + +std::tm to_utc(const time_t* time) { + static std::mutex mtx; + mtx.lock(); + + std::tm utc = tm(), + *utc_static = std::gmtime(time); + std::memcpy(&utc, utc_static, sizeof(std::tm)); + + mtx.unlock(); + return utc; +} + +bool sosc::net::is_error_time(std::string data, size_t offset) { + return is_error_time(unpack_time(data, offset)); +} + +bool sosc::net::is_error_time(sosc::time time) { + std::time_t ctime = sosc::clock::to_time_t(time); +} + +std::string sosc::net::pack_time() { + return pack_time(sosc::clock::now()); +} + +std::string sosc::net::pack_time(sosc::time time) { + std::time_t ctime = sosc::clock::to_time_t(time); + std::tm utc = to_utc(&ctime); + int year = utc.tm_year + 1900; + + std::stringstream stream; + stream << (char)((year & 0xFF0) >> 4) + << (char)(((year & 0x00F) << 4) | utc.tm_mon) + << (char)(utc.tm_mday - 1) + << (char)utc.tm_hour << (char)utc.tm_min << (char)utc.tm_sec; + return stream.str(); +} + +std::string sosc::net::pack_error_time() { + return std::string(6, 0); +} + +sosc::time unpack_time(std::string data, size_t offset = 0) { + +} diff --git a/server/src/utils/net.hpp b/server/src/utils/net.hpp index 1fedbad..593aa70 100644 --- a/server/src/utils/net.hpp +++ b/server/src/utils/net.hpp @@ -1,8 +1,13 @@ #ifndef SOSC_UTIL_NET_H #define SOSC_UTIL_NET_H +#include +#include #include #include +#include +#include +#include #undef htons #undef HTONS @@ -47,17 +52,30 @@ #define NTOHULL(X) sosc::net::ntohv(X) namespace sosc { -typedef std::chrono::system_clock time; +typedef std::chrono::system_clock clock; +typedef std::chrono::time_point time; namespace net { class IpAddress { +public: + +private: }; +bool is_big_endian(); template std::string htonv(T host_var); template -T ntohv(std::string net_var, size_t offset); +T ntohv(std::string net_var, size_t offset = 0); + +std::tm to_utc(const time_t* time); +bool is_error_time(std::string data, size_t offset = 0); +bool is_error_time(sosc::time time); +std::string pack_time(); +std::string pack_time(sosc::time time); +std::string pack_error_time(); +sosc::time unpack_time(std::string data, size_t offset = 0); }} #endif