summaryrefslogtreecommitdiffhomepage
path: root/NetEx
diff options
context:
space:
mode:
authorAki <please@ignore.pl>2022-03-30 22:07:03 +0200
committerAki <please@ignore.pl>2022-03-30 22:07:03 +0200
commit0c64847028c1e39d44f76189d9ac215726442f9b (patch)
tree4263e82fdf79dcf92bb62e59c41a7bdb72c8f86a /NetEx
parente2a9666f356ab0a71ef10113ed0b0b696ddf1075 (diff)
downloadstarshatter-0c64847028c1e39d44f76189d9ac215726442f9b.zip
starshatter-0c64847028c1e39d44f76189d9ac215726442f9b.tar.gz
starshatter-0c64847028c1e39d44f76189d9ac215726442f9b.tar.bz2
Brought NetLayer closer to standard
Diffstat (limited to 'NetEx')
-rw-r--r--NetEx/NetLayer.cpp65
-rw-r--r--NetEx/NetLayer.h14
2 files changed, 46 insertions, 33 deletions
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 <windows.h>
#include "NetLayer.h"
-#include <mmsystem.h>
-#include <time.h>
+#ifdef _WIN32
+#include <winsock2.h>
+#else
+#include <errno.h>
+#include <unistd.h>
+#endif
-// +-------------------------------------------------------------------+
+#include <chrono>
+#include <cstring>
+#include <ctime>
+#include <ratio>
-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<std::uint32_t, std::milli>;
+ return std::chrono::duration_cast<target_duration>(diff).count();
}
+
long
NetLayer::GetUTC()
{
- return (long) time(0);
+ return static_cast<long>(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 <windows.h>
+#include <cstdint>
+
#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();