From 0c64847028c1e39d44f76189d9ac215726442f9b Mon Sep 17 00:00:00 2001 From: Aki Date: Wed, 30 Mar 2022 22:07:03 +0200 Subject: Brought NetLayer closer to standard --- NetEx/NetLayer.cpp | 65 ++++++++++++++++++++++++++++++++---------------------- NetEx/NetLayer.h | 14 ++++++------ 2 files changed, 46 insertions(+), 33 deletions(-) (limited to 'NetEx') diff --git a/NetEx/NetLayer.cpp b/NetEx/NetLayer.cpp index 12458be..5992fbe 100644 --- a/NetEx/NetLayer.cpp +++ b/NetEx/NetLayer.cpp @@ -3,81 +3,94 @@ Copyright (c) 2011-2012, Starshatter OpenSource Distribution Contributors Copyright (c) 1997-2006, Destroyer Studios LLC. - AUTHOR: John DiCamillo + AUTHOR: John DiCamillo - OVERVIEW - ======== - Wrapper for WinSock Library + OVERVIEW + ======== + Wrapper for WinSock Library */ -#include #include "NetLayer.h" -#include -#include +#ifdef _WIN32 +#include +#else +#include +#include +#endif -// +-------------------------------------------------------------------+ +#include +#include +#include +#include -static DWORD baseTime = timeGetTime(); -// +-------------------------------------------------------------------+ +static const auto base_time = std::chrono::high_resolution_clock::now(); -NetLayer::NetLayer() -{ - fail = false; +NetLayer::NetLayer() : + fail{false} +{ +#ifdef _WIN32 WSADATA info; WORD ver = MAKEWORD(2,2); int err = WSAStartup(ver, &info); if (err) fail = true; +#endif } + NetLayer::~NetLayer() { +#ifdef _WIN32 WSACleanup(); +#endif } + bool NetLayer::OK() const { return !fail; } + int NetLayer::GetLastError() { +#ifdef _WIN32 return WSAGetLastError(); +#else + return errno; +#endif } -DWORD + +std::uint32_t NetLayer::GetTime() { - DWORD msec = timeGetTime(); - - if (msec >= baseTime) { - return msec - baseTime; - } - - else { - DWORD extra = 0xffffffff; - return msec + extra - baseTime; - } + const auto now = std::chrono::high_resolution_clock::now(); + const auto diff = now - base_time; + using target_duration = std::chrono::duration; + return std::chrono::duration_cast(diff).count(); } + long NetLayer::GetUTC() { - return (long) time(0); + return static_cast(std::time(nullptr)); } + Text NetLayer::GetHostName() { char hostname[256]; - ZeroMemory(hostname, sizeof(hostname)); + std::memset(hostname, 0, sizeof(hostname)); ::gethostname(hostname, sizeof(hostname)); return hostname; diff --git a/NetEx/NetLayer.h b/NetEx/NetLayer.h index 0d21d36..0ffb197 100644 --- a/NetEx/NetLayer.h +++ b/NetEx/NetLayer.h @@ -3,21 +3,21 @@ Copyright (c) 2011-2012, Starshatter OpenSource Distribution Contributors Copyright (c) 1997-2006, Destroyer Studios LLC. - AUTHOR: John DiCamillo + AUTHOR: John DiCamillo - OVERVIEW - ======== - Wrapper for WinSock Library + OVERVIEW + ======== + Wrapper for WinSock Library */ #ifndef NetLayer_h #define NetLayer_h -#include +#include + #include "Text.h" -// +-------------------------------------------------------------------+ class NetLayer { @@ -31,7 +31,7 @@ public: const char* Description() const; static int GetLastError(); - static DWORD GetTime(); + static std::uint32_t GetTime(); static long GetUTC(); static Text GetHostName(); -- cgit v1.1