AuroraRuntime/Source/IO/Net/AuNetSocketOverlappedOperation.Unix.cpp
Jamie Reece Wilson 1fb36548ea [*] Minor net fixes
[*] Fix async shutdown / inline shutdown condition
[*] Fix multiple net triggers
[*] Fix spurious UDP Session Socket timeouts
2024-10-19 17:52:28 +01:00

201 lines
4.8 KiB
C++

/***
Copyright (C) 2022 J Reece Wilson (a/k/a "Reece"). All rights reserved.
File: AuNetSocketOverlappedOperation.Unix.cpp
Date: 2022-8-26
Author: Reece
***/
#include "Networking.hpp"
#include "AuNetSocketOverlappedOperation.hpp"
#include <Source/IO/Loop/LSEvent.hpp>
#include "AuNetSocket.Unix.hpp"
#include "AuNetError.hpp"
namespace Aurora::IO::Net
{
SocketOverlappedOperation::SocketOverlappedOperation(bool bMultipleTrigger) :
bMultipleTrigger(bMultipleTrigger)
{
this->pEvent = AuLoop::NewLSEventSlow(false, true);
if (!this->pEvent)
{
return;
}
}
SocketOverlappedOperation::SocketOverlappedOperation(const AuSPtr<AuLoop::ILoopSource> &pTrigger)
{
this->pEvent = pTrigger;
}
bool SocketOverlappedOperation::FinishOperation(const AuSPtr<void> &pThat,
const AuSPtr<INetWorker> &pWorker,
bool bReturnCode)
{
return this->FinishOperationEx(pThat, pWorker, bReturnCode, errno);
}
bool SocketOverlappedOperation::FinishOperationEx(const AuSPtr<void> &pThat,
const AuSPtr<INetWorker> &pWorker,
bool bReturnCode,
AuUInt32 dwErrorCode)
{
if (bReturnCode)
{
this->OnIOComplete();
return true;
}
else
{
if ((dwErrorCode == EAGAIN) ||
(dwErrorCode == EINPROGRESS))
{
return this->BeginOperation(pThat, pWorker);
}
else
{
// ERROR
SysPushErrorIO("Couldn't connect [overlapped] (0x{:x})", dwErrorCode);
this->uOsError = dwErrorCode;
return false;
}
}
}
bool SocketOverlappedOperation::BeginOperation(const AuSPtr<void> &that,
const AuSPtr<INetWorker> &pWorker)
{
if (!this->pEvent)
{
return false;
}
this->pSocket = that;
if (auto pOld = AuTryLockMemoryType(this->wpThat))
{
return true;
}
auto pThat = pWorker->ToProcessor()->StartSimpleLSWatchEx(this->pEvent,
AuSPtr<AuIO::IIOSimpleEventListener>(that, this),
!this->bMultipleTrigger);
if (!pThat)
{
this->pSocket.reset();
return false;
}
this->wpThat = pThat;
return true;
}
void SocketOverlappedOperation::ReleaseWatcher()
{
if (auto pOld = AuTryLockMemoryType(this->wpThat))
{
pOld->StopWatch();
}
AuResetMember(this->wpThat);
}
void SocketOverlappedOperation::UpdateTrigger(const AuSPtr<AuLoop::ILoopSource> &pTrigger)
{
this->pEvent = pTrigger;
}
bool SocketOverlappedOperation::IsValid()
{
return bool(this->pEvent);
}
void SocketOverlappedOperation::OnIOTick()
{
if (!this->bMultipleTrigger)
{
return;
}
auto pTemp = AuExchange(this->pSocket, {});
if (this->HasFailed())
{
this->OnOverlappedFailure(this->ToError());
}
else
{
this->OnOverlappedComplete();
}
}
void SocketOverlappedOperation::OnIOComplete()
{
if (this->bMultipleTrigger)
{
return;
}
if (AuExchange(this->bHasFlipped, true))
{
return;
}
if (this->HasFailed())
{
this->OnOverlappedFailure(this->ToError());
}
else
{
this->OnOverlappedComplete();
}
this->pSocket.reset();
}
void SocketOverlappedOperation::OnIOFailure()
{
if (AuExchange(this->bHasFlipped, true))
{
return;
}
this->OnOverlappedFailure(this->ToError());
this->pSocket.reset();
}
bool SocketOverlappedOperation::HasFailed()
{
return this->ToOsError() != 0;
}
AuUInt SocketOverlappedOperation::ToOsError()
{
if (this->uOsError)
{
return this->uOsError;
}
return this->uOsError;
}
bool SocketOverlappedOperation::HasComplete()
{
if (this->HasFailed())
{
return false;
}
return this->ToOsError() == 0;
}
NetError SocketOverlappedOperation::ToError()
{
NetError error;
NetError_SetOsError(error, this->ToOsError());
return error;
}
}