tube socks soon

This commit is contained in:
malloc 2018-03-05 17:23:07 -06:00
parent b2ca8cc142
commit e98512193f
6 changed files with 152 additions and 100 deletions

View file

@ -6,8 +6,7 @@ file(GLOB_RECURSE server_src
"src/*.cpp" "src/*.cpp"
) )
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -static -g") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -static")
add_executable(server ${server_src}) add_executable(server ${server_src})
install(TARGETS server RUNTIME DESTINATION bin) install(TARGETS server RUNTIME DESTINATION bin)

View file

@ -3,6 +3,7 @@
#include "sock/tcpsock.hpp" #include "sock/tcpsock.hpp"
#include "utils/string.hpp" #include "utils/string.hpp"
#include "utils/net.hpp" #include "utils/net.hpp"
#include "utils/time.hpp"
int main(int argc, char **argv) { int main(int argc, char **argv) {
//auto sock = sosc::TcpClient(); //auto sock = sosc::TcpClient();
@ -17,7 +18,7 @@ int main(int argc, char **argv) {
std::string net = HTONUL(test); std::string net = HTONUL(test);
test = NTOHUL(net); test = NTOHUL(net);
std::cout << std::hex << test << std::hex << net << std::endl; std::string time = sosc::clk::pack_time();
return 0; return 0;
} }

View file

@ -7,86 +7,3 @@ bool sosc::net::is_big_endian() {
return check; 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) {
if(offset + 6 >= data.length())
return pack_error_time();
struct tm time = tm();
time.tm_year = ((data[offset] &
}

View file

@ -42,23 +42,44 @@ class IpAddress {
public: public:
private: private:
typedef std::pair<uint16_t, uint8_t> addrpart_t;
addrpart_t parts[8] = {};
static addrpart_t ParsePart(std::string part);
}; };
bool is_big_endian(); bool is_big_endian();
template<typename T = uint32_t> template<typename T>
std::string htonv(T host_var); std::string htonv(T host_var) {
template<typename T = uint32_t> int byte_count = sizeof(T);
T ntohv(std::string net_var, size_t offset = 0); std::string net_var(byte_count, 0);
std::tm to_utc(const time_t* time); for(int b = 0; b < byte_count; ++b) {
sosc::time error_time(); int i = is_big_endian()
bool is_error_time(std::string data, size_t offset = 0); ? b : byte_count - b - 1;
bool is_error_time(sosc::time time);
std::string pack_time(); net_var[i] = ((uint8_t*)&host_var)[b];
std::string pack_time(sosc::time time); }
std::string pack_error_time();
sosc::time unpack_time(std::string data, size_t offset = 0); return net_var;
}
template<typename T>
T ntohv(std::string net_var, size_t offset = 0) {
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 - 1;
((uint8_t*)&host_var)[i] = net_var[offset + b];
}
return host_var;
}
}} }}
#endif #endif

View file

@ -1,2 +1,100 @@
#include "time.hpp" #include "time.hpp"
std::tm sosc::clk::to_utc(sosc::time time) {
time_t ctime = sosc::clock::to_time_t(time);
return to_utc(&ctime);
}
std::tm sosc::clk::to_utc(const time_t* time) {
static std::mutex mtx;
mtx.lock();
std::tm utc = std::tm(),
*utc_static = std::gmtime(time);
std::memcpy(&utc, utc_static, sizeof(std::tm));
mtx.unlock();
return utc;
}
std::tm sosc::clk::to_local(sosc::time time) {
time_t ctime = sosc::clock::to_time_t(time);
return to_local(&ctime);
}
std::tm sosc::clk::to_local(const time_t* time) {
static std::mutex mtx;
mtx.lock();
std::tm ctm = std::tm(),
*tm_static = std::localtime(time);
std::memcpy(&ctm, tm_static, sizeof(std::tm));
mtx.unlock();
return ctm;
}
std::tm sosc::clk::to_tm(sosc::time time) {
return to_local(time);
}
std::tm sosc::clk::to_tm(const time_t* time) {
return to_local(time);
}
sosc::time sosc::clk::error_time() {
std::tm error = std::tm();
error.tm_year = -1900;
error.tm_mday = 1;
error.tm_mon = error.tm_hour
= error.tm_min = error.tm_sec = 0;
return sosc::clock::from_time_t(std::mktime(&error));
}
bool sosc::clk::is_error_time(std::string data, size_t offset) {
return (((data[offset] & 0xFF) << 4) |
((data[offset + 1] & 0xF0) >> 4)) == 0;
}
bool sosc::clk::is_error_time(sosc::time time) {
std::tm err_check = to_tm(time);
return err_check.tm_year == -1900;
}
sosc::time sosc::clk::unpack_time(std::string data, size_t offset) {
if(offset + 6 >= data.length())
return error_time();
struct tm time = tm();
time.tm_year = ((data[offset] & 0xFF) << 4 |
(data[offset + 1] & 0xF0) >> 4) - 1900;
time.tm_mon = data[offset + 1] & 0x0F;
time.tm_mday = (data[offset + 2] & 0xFF) + 1;
time.tm_hour = data[offset + 3] & 0xFF;
time.tm_min = data[offset + 4] & 0xFF;
time.tm_sec = data[offset + 5] & 0xFF;
return sosc::clock::from_time_t(std::mktime(&time));
}
std::string sosc::clk::pack_time() {
return pack_time(sosc::clock::now());
}
std::string sosc::clk::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::clk::pack_error_time() {
return std::string(6, 0);
}

View file

@ -14,8 +14,24 @@ typedef std::chrono::system_clock clock;
typedef std::chrono::time_point<sosc::clock> time; typedef std::chrono::time_point<sosc::clock> time;
namespace clk { namespace clk {
std::tm to_utc(sosc::time time);
std::tm to_utc(const time_t* time);
std::tm to_local(sosc::time time);
std::tm to_local(const time_t* time);
std::tm to_tm(sosc::time time);
std::tm to_tm(const time_t* time);
sosc::time error_time();
bool is_error_time(std::string data, size_t offset = 0);
bool is_error_time(sosc::time time);
sosc::time unpack_time(std::string data, size_t offset = 0);
std::string pack_time();
std::string pack_time(sosc::time time);
std::string pack_error_time();
}} }}
#endif #endif