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 ++++++++++++++++++++++++++++++++---------------------- 1 file changed, 39 insertions(+), 26 deletions(-) (limited to 'NetEx/NetLayer.cpp') 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; -- cgit v1.1