Skip to content

Commit

Permalink
Replace INFINITE and TO_INF by StdSync::Infinite and remove unnecessa…
Browse files Browse the repository at this point in the history
…ry GetTimeout overrides
  • Loading branch information
Fulgen301 committed Dec 25, 2024
1 parent a1abd30 commit b13c483
Show file tree
Hide file tree
Showing 7 changed files with 16 additions and 37 deletions.
2 changes: 0 additions & 2 deletions src/C4Chrono.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@

#ifndef _WIN32

#define INFINITE 0xFFFFFFFF

unsigned long timeGetTime(void);

#endif
Expand Down
25 changes: 6 additions & 19 deletions src/C4NetIO.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,6 @@
#include <functional>
#include <utility>

// constants definition
const int C4NetIO::TO_INF = -1;

// simulate packet loss (loss probability in percent)
// #define C4NETIO_SIMULATE_PACKETLOSS 10

Expand Down Expand Up @@ -575,7 +572,7 @@ bool C4NetIOTCP::Execute(int iMaxTime) // (mt-safe)

#ifdef _WIN32
// wait for something to happen
if (WaitForSingleObject(Event, iMaxTime == C4NetIO::TO_INF ? INFINITE : iMaxTime) == WAIT_TIMEOUT)
if (WaitForSingleObject(Event, iMaxTime) == WAIT_TIMEOUT)
// timeout -> nothing happened
return true;
WSAResetEvent(Event);
Expand Down Expand Up @@ -1013,11 +1010,6 @@ void C4NetIOTCP::GetFDs(std::vector<pollfd> &fds)

#endif

int C4NetIOTCP::GetTimeout() // (mt-safe)
{
return TO_INF;
}

bool C4NetIOTCP::GetStatistic(int *pBroadcastRate) // (mt-safe)
{
// no broadcast
Expand Down Expand Up @@ -1821,7 +1813,7 @@ HANDLE C4NetIOSimpleUDP::GetEvent() // (mt-safe)
enum C4NetIOSimpleUDP::WaitResult C4NetIOSimpleUDP::WaitForSocket(int iTimeout)
{
// wait for anything to happen
DWORD ret = WaitForSingleObject(hEvent, iTimeout == TO_INF ? INFINITE : iTimeout);
DWORD ret = WaitForSingleObject(hEvent, iTimeout);
if (ret == WAIT_TIMEOUT)
return WR_Timeout;
if (ret == WAIT_FAILED)
Expand Down Expand Up @@ -1888,11 +1880,6 @@ enum C4NetIOSimpleUDP::WaitResult C4NetIOSimpleUDP::WaitForSocket(int iTimeout)

#endif

int C4NetIOSimpleUDP::GetTimeout()
{
return C4NetIO::TO_INF;
}

bool C4NetIOSimpleUDP::SetMCLoopback(int fLoopback)
{
// enable/disable MC loopback
Expand Down Expand Up @@ -2303,7 +2290,7 @@ bool C4NetIOUDP::Execute(int iMaxTime) // (mt-safe)

// adjust maximum block time
int iMaxBlock = GetTimeout();
if (iMaxTime == TO_INF || iMaxTime > iMaxBlock) iMaxTime = iMaxBlock;
if (iMaxTime == StdSync::Infinite || iMaxTime > iMaxBlock) iMaxTime = iMaxBlock;

// execute subclass
if (!C4NetIOSimpleUDP::Execute(iMaxBlock))
Expand Down Expand Up @@ -3160,7 +3147,7 @@ bool C4NetIOUDP::Peer::SendDirect(C4NetIOPacket &&rPacket) // (mt-safe)
void C4NetIOUDP::Peer::OnConn()
{
// reset timeout
SetTimeout(TO_INF);
SetTimeout(StdSync::Infinite);
// set status
eStatus = CS_Works;
// do callback
Expand Down Expand Up @@ -3229,7 +3216,7 @@ void C4NetIOUDP::Peer::CheckCompleteIPackets()

void C4NetIOUDP::Peer::SetTimeout(int iLength, int iRetryCnt) // (mt-safe)
{
if (iLength != TO_INF)
if (iLength != StdSync::Infinite)
iTimeout = timeGetTime() + iLength;
else
iTimeout = 0;
Expand All @@ -3254,7 +3241,7 @@ void C4NetIOUDP::Peer::OnTimeout()
Close("connection timeout");
}
// reset timeout
SetTimeout(TO_INF);
SetTimeout(StdSync::Infinite);
}

// * C4NetIOUDP: implementation
Expand Down
11 changes: 3 additions & 8 deletions src/C4NetIO.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,6 @@ class C4NetIO : public StdSchedulerProc
C4NetIO();
virtual ~C4NetIO();

// *** constants / types
static const int TO_INF; // = -1;

using addr_t = C4Network2EndpointAddress;

static std::vector<C4Network2HostAddress> GetLocalAddresses(bool unsorted = false);
Expand Down Expand Up @@ -190,7 +187,7 @@ class C4NetIOTCP : public C4NetIO, protected CStdCSecExCallback
virtual bool Close() override;
virtual bool CloseBroadcast();

virtual bool Execute(int iMaxTime = TO_INF) override;
virtual bool Execute(int iMaxTime = StdSync::Infinite) override;

// * multithreading safe
std::unique_ptr<Socket> Bind(const addr_t &addr);
Expand All @@ -208,7 +205,6 @@ class C4NetIOTCP : public C4NetIO, protected CStdCSecExCallback
#else
virtual void GetFDs(std::vector<pollfd> &fds) override;
#endif
virtual int GetTimeout() override;

// statistics
virtual bool GetStatistic(int *pBroadcastRate) override;
Expand Down Expand Up @@ -360,7 +356,7 @@ class C4NetIOSimpleUDP : public C4NetIO
virtual bool Close() override;
virtual bool CloseBroadcast();

virtual bool Execute(int iMaxTime = TO_INF) override;
virtual bool Execute(int iMaxTime = StdSync::Infinite) override;

virtual bool Send(const C4NetIOPacket &rPacket) override;
virtual bool Broadcast(const C4NetIOPacket &rPacket) override;
Expand All @@ -371,7 +367,6 @@ class C4NetIOSimpleUDP : public C4NetIO
#else
virtual void GetFDs(std::vector<pollfd> &fds) override;
#endif
virtual int GetTimeout() override;

// not implemented
virtual bool Connect(const addr_t &addr) override { assert(false); return false; }
Expand Down Expand Up @@ -451,7 +446,7 @@ class C4NetIOUDP : public C4NetIOSimpleUDP, protected CStdCSecExCallback
virtual bool Close() override;
virtual bool CloseBroadcast() override;

virtual bool Execute(int iMaxTime = TO_INF) override;
virtual bool Execute(int iMaxTime = StdSync::Infinite) override;

virtual bool Connect(const addr_t &addr) override;
virtual bool Close(const addr_t &addr) override;
Expand Down
7 changes: 3 additions & 4 deletions src/C4Network2Reference.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ class C4Network2HTTPClient::Impl
virtual void SetNotify(C4InteractiveThread *thread) = 0;

virtual bool Execute(int maxTime) = 0;
virtual int GetTimeout() = 0;
virtual int GetTimeout() { return StdSync::Infinite; }

#ifdef _WIN32
virtual HANDLE GetEvent() = 0;
Expand Down Expand Up @@ -274,8 +274,7 @@ class C4Network2HTTPClientImplCurl : public C4Network2HTTPClient::Impl
bool SetServer(std::string_view serverAddress, std::uint16_t defaultPort) override;
void SetNotify(class C4InteractiveThread *thread) override { this->thread.store(thread, std::memory_order_release); }

bool Execute(int iMaxTime = C4NetIO::TO_INF) override { return true; }
int GetTimeout() override { return C4NetIO::TO_INF; }
bool Execute(int iMaxTime = StdSync::Infinite) override { return true; }

#ifdef _WIN32
HANDLE GetEvent() override { return nullptr; }
Expand Down Expand Up @@ -385,7 +384,7 @@ class C4Network2HTTPClientImplNetIO : public C4Network2HTTPClient::Impl, public
void SetNotify(class C4InteractiveThread *pnNotify) override { pNotify = pnNotify; }

// Overridden
bool Execute(int iMaxTime = TO_INF) override;
bool Execute(int iMaxTime = StdSync::Infinite) override;
int GetTimeout() override;

#ifdef _WIN32
Expand Down
2 changes: 1 addition & 1 deletion src/C4Network2Reference.h
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ class C4Network2HTTPClient : public StdSchedulerProc
bool SetServer(std::string_view serverAddress, std::uint16_t defaultPort = 0);
void SetNotify(class C4InteractiveThread *thread);

bool Execute(int iMaxTime = C4NetIO::TO_INF) override;
bool Execute(int iMaxTime = StdSync::Infinite) override;
int GetTimeout() override;

#ifdef _WIN32
Expand Down
4 changes: 2 additions & 2 deletions src/StdApp.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@

#include "C4Log.h"
#include "C4Windows.h"
#include "StdSync.h"
#include "StdWindow.h"

#ifdef _WIN32
#include "StdSync.h"

const int SEC1_TIMER = 1, SEC1_MSEC = 1000;
#elif defined(USE_X11)
Expand Down Expand Up @@ -263,7 +263,7 @@ class CStdApp
virtual int32_t &ScreenWidth() = 0;
virtual int32_t &ScreenHeight() = 0;
virtual float GetScale() = 0;
C4AppHandleResult HandleMessage(unsigned int iTimeout = INFINITE, bool fCheckTimer = true);
C4AppHandleResult HandleMessage(unsigned int iTimeout = StdSync::Infinite, bool fCheckTimer = true);
void SetDisplayMode(DisplayMode mode) { pWindow->SetDisplayMode(mode); }
void ResetTimer(unsigned int uDelay);
CStdWindow *pWindow;
Expand Down
2 changes: 1 addition & 1 deletion src/StdScheduler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ bool StdScheduler::Execute(int iTimeout)
{
if (const int procTimeout{proc->GetTimeout()}; procTimeout >= 0)
{
if (iTimeout == -1 || iTimeout > procTimeout)
if (iTimeout == StdSync::Infinite || iTimeout > procTimeout)
{
iTimeout = procTimeout;
}
Expand Down

0 comments on commit b13c483

Please sign in to comment.