time functions in netlib do NOT look

This commit is contained in:
malloc 2018-03-01 17:16:47 -06:00
parent 9b267df5e8
commit 9fe643eb99
3 changed files with 114 additions and 4 deletions

View file

@ -2,15 +2,22 @@
#include <string> #include <string>
#include "sock/tcpsock.hpp" #include "sock/tcpsock.hpp"
#include "utils/string.hpp" #include "utils/string.hpp"
#include "utils/net.hpp"
int main(int argc, char **argv) { int main(int argc, char **argv) {
//auto sock = sosc::TcpClient(); //auto sock = sosc::TcpClient();
std::string a = "this!!is!!a!!test"; /*std::string a = "this!!is!!a!!test";
auto b = sosc::str::split(a, "!!"); auto b = sosc::str::split(a, "!!");
std::for_each(b.begin(), b.end(), [](std::string& i) { std::for_each(b.begin(), b.end(), [](std::string& i) {
std::cout << i << std::endl; 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; return 0;
} }

View file

@ -1,2 +1,87 @@
#include "net.hpp" #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<typename T>
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<typename T>
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) {
}

View file

@ -1,8 +1,13 @@
#ifndef SOSC_UTIL_NET_H #ifndef SOSC_UTIL_NET_H
#define SOSC_UTIL_NET_H #define SOSC_UTIL_NET_H
#include <sstream>
#include <algorithm>
#include <string> #include <string>
#include <chrono> #include <chrono>
#include <ctime>
#include <mutex>
#include <cstring>
#undef htons #undef htons
#undef HTONS #undef HTONS
@ -47,17 +52,30 @@
#define NTOHULL(X) sosc::net::ntohv<uint64_t>(X) #define NTOHULL(X) sosc::net::ntohv<uint64_t>(X)
namespace sosc { namespace sosc {
typedef std::chrono::system_clock time; typedef std::chrono::system_clock clock;
typedef std::chrono::time_point<sosc::clock> time;
namespace net { namespace net {
class IpAddress { class IpAddress {
public:
private:
}; };
bool is_big_endian();
template<typename T = uint32_t> template<typename T = uint32_t>
std::string htonv(T host_var); std::string htonv(T host_var);
template<typename T = uint32_t> template<typename T = uint32_t>
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 #endif