From e2a9666f356ab0a71ef10113ed0b0b696ddf1075 Mon Sep 17 00:00:00 2001 From: Aki Date: Wed, 30 Mar 2022 21:38:27 +0200 Subject: Removed windows header usage in NetClient --- NetEx/NetClient.cpp | 27 +++++++++++++++++++-------- NetEx/NetClient.h | 7 ++++--- 2 files changed, 23 insertions(+), 11 deletions(-) (limited to 'NetEx') diff --git a/NetEx/NetClient.cpp b/NetEx/NetClient.cpp index 8c9ee51..7a62160 100644 --- a/NetEx/NetClient.cpp +++ b/NetEx/NetClient.cpp @@ -13,7 +13,10 @@ #include "NetClient.h" -#include +#include +#include +#include +#include #include "NetAddr.h" #include "NetSock.h" @@ -23,8 +26,8 @@ NetClient::NetClient(const NetAddr& server_addr) : addr(server_addr), sock(nullptr), - delta(0), - time(0), + delta(std::chrono::high_resolution_clock::duration::zero()), + time(), err(0) { } @@ -45,8 +48,8 @@ NetClient::Send(Text msg) delete sock; sock = new NetSock(addr, true); - delta = 0; - time = timeGetTime(); + delta = std::chrono::high_resolution_clock::duration::zero(); + time = std::chrono::high_resolution_clock::now(); if (!sock) { err = ERR_NOBUFS; @@ -85,8 +88,8 @@ NetClient::Recv() if (sock) { int ready = sock->select(); - while (!ready && timeGetTime() - time < 2000) { - Sleep(5); + while (!ready && std::chrono::high_resolution_clock::now() - time < std::chrono::seconds(2000)) { + std::this_thread::sleep_for(std::chrono::milliseconds(5)); ready = sock->select(); } @@ -98,7 +101,7 @@ NetClient::Recv() msg = sock->recv(); } - delta = timeGetTime() - time; + delta = std::chrono::high_resolution_clock::now() - time; } delete sock; @@ -120,3 +123,11 @@ NetClient::SendRecv(Text msg) return response; } + + +std::uint32_t +NetClient::GetTime() const +{ + using target_duration = std::chrono::duration; + return std::chrono::duration_cast(delta).count(); +} diff --git a/NetEx/NetClient.h b/NetEx/NetClient.h index 5bd7d10..db5543d 100644 --- a/NetEx/NetClient.h +++ b/NetEx/NetClient.h @@ -14,6 +14,7 @@ #ifndef NetClient_h #define NetClient_h +#include #include #include "NetAddr.h" @@ -38,13 +39,13 @@ public: Text SendRecv(Text msg); int GetLastError() const { return err; } - std::uint32_t GetTime() const { return delta; } + std::uint32_t GetTime() const; protected: NetAddr addr; NetSock* sock; - std::uint32_t delta; - std::uint32_t time; + std::chrono::high_resolution_clock::duration delta; + std::chrono::high_resolution_clock::time_point time; int err; public: -- cgit v1.1