AuroraRuntime/Source/IO/Net/AuNetSocketServer.cpp
Reece Wilson 67905a4192 [+] Network + Protocol + TLS - Initial Commit
=============================================================================
Network ]====================================================================
=============================================================================

[+] Added (very) early Aurora::IO::Net implementation
[+] AuNet::EHostnameType
[+] AuNet::EIPProtocol
[+] AuNet::ENetworkError
[+] AuNet::ETransportProtocol
[+] AuNet::INetInterface
[+] AuNet::INetSrvDatagram
[+] AuNet::INetSrvResolve
[+] AuNet::INetSrvSockets
[+] AuNet::INetSrvWorkers
[+] AuNet::INetWorker
[+] AuNet::IPAddress
[+] AuNet::IResolver
[+] AuNet::ISocket
[+] AuNet::IResolver
[+] AuNet::ISocketBase
[+] AuNet::ISocketChannel
[+] AuNet::ISocketDriver
[+] AuNet::ISocketDriverFactory
[+] AuNet::ISocketServer
[+] AuNet::ISocketServerDriver
[+] AuNet::NetEndpoint
[+] AuNet::NetError
[+] AuNet::NetHostname
(+implementation)

=============================================================================
Protocol ]===================================================================
=============================================================================

[+] IProtocolInterceptor
[+] IProtocolInterceptorEx
[+] IProtocolStack
(+implementation)

=============================================================================
TLS ]========================================================================
=============================================================================

[+] ITLSContext
[+] TLSProtocolRecv
[+] TLSProtocolSend
(+implementation)

=============================================================================
IO Bug Fixes ]===============================================================
=============================================================================

[*] IOProcessor::SubmitIOWorkItem should signal the CvEvent, forcing at least once future tick (wont optimize with if in tick & not yet dispatched work items)
[*] Split IOPipeWork in into IOPipeProcessor header
[+] IOPipeWork::GetBuffer (internal reallocation)
[*] Harden against IAsyncTransactions without a loop source
[*] Missing null `if (processor->listener)` in IOProcessor
[*] Solved some soft-lock conditions under Linux's LoopQueue (added deferred commits)
[*] Quick hack: IOProcessor::HasItems() should OR the early can-tick check function.

=============================================================================
Other ]======================================================================
=============================================================================

[+] Linux: LSSignalCatcher
[+] `static void AuResetMember(Aurora::Memory::ByteBuffer &ref)` for AuROXTL
[*] Attempt to enforce a normalization and don't overwrite-readptr-under-istreamwriters policy in ByteBuffer_ReadWrite (circular buffers)
[*] Bad ECC ctors

=============================================================================
Known issues ]===============================================================
=============================================================================

> Linux net is nowhere near done
> UDP socket emulation layer isn't implemented
> Ciphersuite API is a stub
> Private key API is a stub
> ...therefore no TLS servers
> Missing thread safety precautions under net
> Net implementation is still beri early
2022-08-28 20:02:06 +01:00

245 lines
5.6 KiB
C++

/***
Copyright (C) 2022 J Reece Wilson (a/k/a "Reece"). All rights reserved.
File: AuNetSocketServer.cpp
Date: 2022-8-22
Author: Reece
***/
#include "Networking.hpp"
#include "AuNetSocketServer.hpp"
#include "AuNetEndpoint.hpp"
#include "AuNetError.hpp"
#include "AuNetWorker.hpp"
namespace Aurora::IO::Net
{
SocketServer::SocketServer(NetInterface *pInterface,
NetWorker *pWorker,
const AuSPtr<ISocketServerDriver> &pDriver,
const AuSPtr<ISocketDriverFactory> &pFactory,
AuUInt32 maxConnections)
: Socket(pInterface,
pWorker,
AuSPtr<ISocketDriver>{},
-1),
pDriver_(pDriver),
pFactory_(pFactory),
uMaxConnections_(maxConnections)
{
}
void SocketServer::Init(const NetEndpoint &localAddress)
{
if (!this->InitSocket(localAddress))
{
this->SendErrorNoStream(ENetworkError::eInitSocketFailed);
return;
}
}
void SocketServer::Listen(const NetEndpoint &localAddress, bool bBind, bool bListen)
{
if (this->bHasErrored_)
{
return;
}
this->localEndpoint_ = localAddress;
this->endpointSize_ = OptimizeEndpoint(this->localEndpoint_);
if (!this->endpointSize_)
{
this->SendErrorNoStream(ENetworkError::eBadAddress);
return;
}
if (bBind)
{
if (!this->ImplBind())
{
this->SendErrorNoStream(ENetworkError::eUnknown);
return;
}
}
if (bListen)
{
if (!this->ImplListen())
{
this->SendErrorNoStream(ENetworkError::eUnknown);
return;
}
}
if ((!AuBuild::kIsNtDerived) || // NT accept doesnt need it
(this->localEndpoint_.transportProtocol == ETransportProtocol::eProtocolUDP)) // only the recvfrom loop does
{
if (!this->MakeNonblocking())
{
NetError error;
NetError_SetCurrent(error);
this->SendErrorNoStream(error);
return;
}
}
if (this->pDriver_)
{
try
{
this->pDriver_->OnBind();
}
catch (...)
{
SysPushErrorCatch();
this->SendErrorNoStream(ENetworkError::eAsyncError);
}
}
}
void SocketServer::Accept()
{
if (this->bHasErrored_)
{
return;
}
if (!this->BeginAcceptLoop())
{
this->SendErrorNoStream(NetError(ENetworkError::eCantAccept));
return;
}
}
/////////////////////////////////////////////////////////////////////////////////////
// ISocketServer
AuSPtr<ISocketServerDriver> SocketServer::GetServerDriver()
{
return this->pDriver_;
}
AuSPtr<ISocketDriverFactory> SocketServer::GetFactory()
{
return this->pFactory_;
}
/////////////////////////////////////////////////////////////////////////////////////
// ISocketDriver
bool SocketServer::OnPreestablish(const AuSPtr<ISocket> &pInforming)
{
return true;
}
void SocketServer::OnEstablish()
{
}
void SocketServer::OnStreamUpdated()
{
}
void SocketServer::OnFatalErrorReported(const NetError &error)
{
if (this->pDriver_)
{
this->pDriver_->OnFatalErrorReported(error);
}
}
void SocketServer::OnEnd()
{
}
void SocketServer::OnFinalize()
{
if (this->pDriver_)
{
this->pDriver_->OnFinalize();
}
}
/////////////////////////////////////////////////////////////////////////////////////
// SocketBase
void SocketServer::FinishConstructAsync()
{
}
void SocketServer::Shutdown()
{
}
void SocketServer::Destroy()
{
}
void SocketServer::ScheduleAcceptTick()
{
// WARNING: Accepts cannot be on the same tick as on-accept
// We must reschedule to prevent add-source, under on tick callback, prior to removal eval of the very same event handle
// (loopqueue constraint)
auto shared = AuDynamicCast<SocketServer>(this->SharedFromThis()); // C++ is cringe. cant static-up-cast a virtual base
if (!this->ToWorkerEx()->TryScheduleInternalTemplate<AuNullS>([that = shared](const AuSPtr<AuAsync::PromiseCallback<AuNullS>> &info)
{
that->DoNonblockingReadTick();
}, AuSPtr<AuAsync::PromiseCallback<AuNullS, AuNullS>>{}))
{
this->SendErrorBeginShutdown({});
}
}
bool SocketServer::PrepareConnectOperations()
{
return false;
}
bool SocketServer::UpdateLocalEndpoint()
{
int iLen { (int)this->endpointSize_ };
if (::getsockname(this->osHandle_, (sockaddr *)this->localEndpoint_.hint, &iLen) == -1)
{
SysPushErrorIO();
return false;
}
DeoptimizeEndpoint(this->localEndpoint_);
return true;
}
bool SocketServer::UpdateRemoteEndpoint()
{
return false;
}
bool SocketServer::TryBindAnyLocal()
{
return false;
}
bool SocketServer::ConnectOverlapped()
{
return false;
}
bool SocketServer::ConnectNonblocking()
{
return false;
}
bool SocketServer::ConnectBlocking()
{
return false;
}
}