[+] Linux datagrams

[+] NetSocketBind::uDefaultInputStreamSize
[+] NetDatagramBind::uDefaultInputStreamSize
This commit is contained in:
Reece Wilson 2022-12-16 17:18:12 +00:00
parent 42d02b185c
commit 1e4082c02f
25 changed files with 236 additions and 42 deletions

View File

@ -16,6 +16,7 @@ namespace Aurora::IO::Net
{
IPAddress ip;
AuUInt16 uPort;
AuUInt32 uDefaultInputStreamSize;
AuSPtr<IDatagramDriver> pDriver;
};

View File

@ -34,8 +34,9 @@ namespace Aurora::IO::Net
AuUInt16 uPort {};
AuSPtr<ISocketDriverFactory> pFactory;
AuSPtr<ISocketServerDriver> pDriver;
AuUInt uMaxConnections {};
AuUInt uMaxAcceptBacklog {};
AuUInt32 uMaxConnections {};
AuUInt32 uMaxAcceptBacklog {};
AuUInt32 uDefaultInputStreamSize /* also: abs max datagram size of datagrams over sockets. note that each sessions buffer could be raised if tick/fragement future-buffering rejection. */ {};
bool bMultiThreaded {};
};

View File

@ -13,15 +13,18 @@ namespace Aurora::IO::Net
{
DatagramServerImpl::DatagramServerImpl(NetInterface *pInterface,
NetWorker *pWorker,
const AuSPtr<IDatagramDriver> &pDriver) :
const AuSPtr<IDatagramDriver> &pDriver,
AuUInt32 uDefaultPacketSize) :
DatagramServer(pInterface,
pWorker,
pDriver),
SocketServerImpl(pInterface,
pWorker,
{},
{},
4096,
pDriver,
uDefaultPacketSize),
SocketServerImpl(pInterface, // struct NetInterface *pInterface
pWorker, // struct NetWorker *pWorker
{}, // const AuSPtr<ISocketServerDriver> &pDriver
{}, // const AuSPtr<ISocketDriverFactory> &pFactory
4096, // uMaxConnections
uDefaultPacketSize, // uDefaultInputStreamSize
true),
Socket(pInterface, pWorker, {}, -1)
{

View File

@ -13,22 +13,82 @@ namespace Aurora::IO::Net
{
DatagramServerImpl::DatagramServerImpl(NetInterface *pInterface,
NetWorker *pWorker,
const AuSPtr<IDatagramDriver> &pDriver) :
const AuSPtr<IDatagramDriver> &pDriver,
AuUInt32 uDefaultPacketSize) :
DatagramServer(pInterface,
pWorker,
pDriver),
SocketServerImpl(pInterface,
pWorker,
{},
{},
4096,
true),
pDriver,
uDefaultPacketSize),
SocketServerImpl(pInterface, // struct NetInterface *pInterface
pWorker, // struct NetWorker *pWorker
{}, // const AuSPtr<ISocketServerDriver> &pDriver
{}, // const AuSPtr<ISocketDriverFactory> &pFactory
4096, // uMaxConnections
uDefaultPacketSize, // uDefaultInputStreamSize
true), // bMultiThreaded
Socket(pInterface, pWorker, {}, -1)
{
}
void DatagramServerImpl::DoPosixTick()
{
// We dont use this tick under NT
do
{
auto uLength = this->uDefaultInputStreamSize ? this->uDefaultInputStreamSize : kDefaultStreamSize;
auto pBuffer = AuMakeShared<AuByteBuffer>(uLength);
if (!pBuffer)
{
SysPushErrorMemory();
return;
}
if (!pBuffer->IsValid())
{
SysPushErrorMemory();
return;
}
NetEndpoint endpoint;
socklen_t addressLength { sizeof(sockaddr_storage) };
auto sRet = ::recvfrom(this->ToPlatformHandle(), pBuffer->writePtr, uLength, MSG_DONTWAIT, (struct sockaddr *)endpoint.hint, &addressLength);
if (sRet <= 0)
{
if (errno == EAGAIN || errno == EWOULDBLOCK)
{
break;
}
SysPushErrorNet("recvfrom: {}", sRet);
// TODO: report and teardown if critical
break;
}
else
{
pBuffer->writePtr += sRet;
DeoptimizeEndpoint(endpoint);
endpoint.transportProtocol = ETransportProtocol::eProtocolUDP;
if (this->pDriver_)
{
try
{
this->pDriver_->OnPacket(endpoint, pBuffer);
}
catch (...)
{
SysPushErrorCatch();
if (this->pDriver_)
{
this->pDriver_->OnError(AuNet::NetError(ENetworkError::eCatchException));
}
}
}
}
}
while (true);
}
}

View File

@ -9,11 +9,12 @@
namespace Aurora::IO::Net
{
struct DatagramServerImpl : virtual DatagramServer
struct DatagramServerImpl : DatagramServer
{
DatagramServerImpl(struct NetInterface *pInterface,
struct NetWorker *pWorker,
const AuSPtr<IDatagramDriver> &pDriver);
const AuSPtr<IDatagramDriver> &pDriver,
AuUInt32 uDefaultPacketSize);
void DoPosixTick() override;
};

View File

@ -20,12 +20,14 @@ namespace Aurora::IO::Net
{
DatagramServer::DatagramServer(NetInterface *pInterface,
NetWorker *pWorker,
const AuSPtr<IDatagramDriver> &pDriver) :
SocketServerImpl(pInterface,
pWorker,
{},
{},
4096,
const AuSPtr<IDatagramDriver> &pDriver,
AuUInt32 uDefaultPacketSize) :
SocketServerImpl(pInterface, // struct NetInterface *pInterface
pWorker, // struct NetWorker *pWorker
{}, // const AuSPtr<ISocketServerDriver> &pDriver
{}, // const AuSPtr<ISocketDriverFactory> &pFactory
4096, // uMaxConnections
uDefaultPacketSize, // uDefaultInputStreamSize
true),
pDriver_(pDriver)
{

View File

@ -19,7 +19,8 @@ namespace Aurora::IO::Net
DatagramServer(struct NetInterface *pInterface,
struct NetWorker *pWorker,
const AuSPtr<IDatagramDriver> &pDriver);
const AuSPtr<IDatagramDriver> &pDriver,
AuUInt32 uDefaultPacketSize);
void BeginUDPLoop();
@ -35,7 +36,7 @@ namespace Aurora::IO::Net
virtual void DoPosixTick() = 0;
private:
protected:
AuSPtr<IDatagramDriver> pDriver_;
};
}

View File

@ -54,6 +54,11 @@ namespace Aurora::IO::Net
};
}
return EndpointToLength(ep);
}
AuUInt8 EndpointToLength(const NetEndpoint &ep)
{
switch (ep.ip.ip)
{
case EIPProtocol::eIPProtocolV6:

View File

@ -17,4 +17,6 @@ namespace Aurora::IO::Net
AuUInt TransportToPlatformType(ETransportProtocol protocol);
AuUInt IPToDomain(const NetEndpoint &ep);
AuUInt8 EndpointToLength(const NetEndpoint &ep);
}

View File

@ -11,6 +11,7 @@
#include "AuNetWorker.hpp"
#include "AuIPAddress.hpp"
#include "AuNetError.hpp"
#include "AuNetSocketServer.hpp"
#if defined(AURORA_IS_MODERNNT_DERIVED)
#include "AuNetStream.NT.hpp"
@ -341,8 +342,13 @@ namespace Aurora::IO::Net
return this->pWorker_;
}
bool SocketBase::SendPreestablish()
bool SocketBase::SendPreestablish(SocketServer *pServer)
{
if (pServer && pServer->uDefaultInputStreamSize)
{
this->socketChannel_.uBytesInputBuffer = pServer->uDefaultInputStreamSize;
}
this->socketChannel_.inputChannel.WarmOnEstablish(); // Allocate stream resources, in case we need to start working with the source buffer
// think: setting up protocol stacks, accessing the base bytebuffer, without the pipe being
// allocated already

View File

@ -76,7 +76,7 @@ namespace Aurora::IO::Net
INetWorker *ToWorker();
NetWorker *ToWorkerEx();
bool SendPreestablish();
bool SendPreestablish(struct SocketServer *pServer = nullptr);
void SendEnd();
void SendFinalize();

View File

@ -22,7 +22,8 @@ namespace Aurora::IO::Net
pWorker,
pDriver,
pSocketDriverFactory,
maxConnections,
uMaxConnections,
uDefaultInputStreamSize,
bMultiThreaded),
Socket(pInterface,
pWorker,

View File

@ -17,7 +17,8 @@ namespace Aurora::IO::Net
struct NetWorker *pWorker,
const AuSPtr<ISocketServerDriver> &pDriver,
const AuSPtr<ISocketDriverFactory> &pSocketDriverFactory,
AuUInt32 maxConnections,
AuUInt32 uMaxConnections,
AuUInt32 uDefaultInputStreamSize,
bool bMultiThreaded);

View File

@ -16,13 +16,15 @@ namespace Aurora::IO::Net
NetWorker *pWorker,
const AuSPtr<ISocketServerDriver> &pDriver,
const AuSPtr<ISocketDriverFactory> &pSocketDriverFactory,
AuUInt32 maxConnections,
AuUInt32 uMaxConnections,
AuUInt32 uDefaultInputStreamSize,
bool bMultiThreaded) :
SocketServer(pInterface,
pWorker,
pDriver,
pSocketDriverFactory,
maxConnections,
uMaxConnections,
uDefaultInputStreamSize,
bMultiThreaded),
Socket(pInterface,
pWorker,

View File

@ -17,7 +17,8 @@ namespace Aurora::IO::Net
struct NetWorker *pWorker,
const AuSPtr<ISocketServerDriver> &pDriver,
const AuSPtr<ISocketDriverFactory> &pSocketDriverFactory,
AuUInt32 maxConnections,
AuUInt32 uMaxConnections,
AuUInt32 uDefaultInputStreamSize,
bool bMultiThreaded);

View File

@ -17,7 +17,8 @@ namespace Aurora::IO::Net
NetWorker *pWorker,
const AuSPtr<ISocketServerDriver> &pDriver,
const AuSPtr<ISocketDriverFactory> &pFactory,
AuUInt32 maxConnections,
AuUInt32 uMaxConnections,
AuUInt32 uDefaultInputStreamSize,
bool bMultiThreaded)
: Socket(pInterface,
pWorker,
@ -25,7 +26,8 @@ namespace Aurora::IO::Net
-1),
pDriver_(pDriver),
pFactory_(pFactory),
uMaxConnections_(maxConnections),
uMaxConnections_(uMaxConnections),
uDefaultInputStreamSize(uDefaultInputStreamSize),
bMultiThreaded(bMultiThreaded)
{

View File

@ -23,7 +23,8 @@ namespace Aurora::IO::Net
struct NetWorker *pWorker,
const AuSPtr<ISocketServerDriver> &pDriver,
const AuSPtr<ISocketDriverFactory> &pFactory,
AuUInt32 maxConnections,
AuUInt32 uMaxConnections,
AuUInt32 uDefaultInputStreamSize,
bool bMultiThreaded);
void Init(const NetEndpoint &localAddress);
@ -49,6 +50,7 @@ namespace Aurora::IO::Net
virtual void DetroyServer() = 0;
const bool bMultiThreaded;
const AuUInt32 uDefaultInputStreamSize;
protected:
// INTERFACE: base os socket

View File

@ -0,0 +1,56 @@
/***
Copyright (C) 2022 J Reece Wilson (a/k/a "Reece"). All rights reserved.
File: AuNetSocketServerOnRead.Unix.cpp
Date: 2022-12-16
Author: Reece
***/
#if 0
#include "Networking.hpp"
#include "AuNetSocketServer.hpp"
#include "AuNetSocket.hpp"
#include "AuNetEndpoint.hpp"
#include "AuNetInterface.hpp"
#include "AuNetWorker.hpp"
#include "AuNetEndpoint.hpp"
#include "AuNetSocketServerOnRead.Unix.hpp"
namespace Aurora::IO::Net
{
NetSocketServerOnRead::NetSocketServerOnRead(NetInterface *pInterface,
Socket *pParent) :
pInterface_(pInterface),
SocketServerAcceptReadOperationBase(pParent)
{
}
void NetSocketServerOnRead::Destroy()
{
if (this->pWatch_)
{
this->pWatch_->StopWatch();
}
}
bool NetSocketServerOnRead::DoTick()
{
// this->pParent_->DoPosixTick();
return true;
}
bool NetSocketServerOnRead::InitOnce()
{
auto pWaitHandle = AuMakeShared<Loop::LSHandle>(this->pParent_->ToPlatformHandle(), -1);
if (!pWaitHandle)
{
return false;
}
this->pWatch_ = this->pParent_->ToWorker()->ToProcessor()->StartSimpleLSWatchEx(pWaitHandle,
AuSPtr<IIOSimpleEventListener>(this->pParent_->SharedFromThis(), this),
false);
return bool(this->pWatch_);
}
}
#endif

View File

@ -0,0 +1,32 @@
/***
Copyright (C) 2022 J Reece Wilson (a/k/a "Reece"). All rights reserved.
File: AuNetSocketServerOnRead.Unix.hpp
Date: 2022-12-16
Author: Reece
***/
#pragma once
#include "AuNetSocketServerAcceptReadOperation.hpp"
namespace Aurora::IO::Net
{
struct NetInterface;
struct Socket;
struct NetSocketServerOnRead :
virtual SocketServerAcceptReadOperationBase
{
NetSocketServerOnRead(NetInterface *pInterface,
Socket *pParent);
bool DoTick();
bool InitOnce();
void Destroy();
protected:
NetInterface *pInterface_;
Socket *pParent_;
AuSPtr<IIOProcessorItem> pWatch_;
};
}

View File

@ -42,7 +42,8 @@ namespace Aurora::IO::Net
auto pServer = AuMakeShared<DatagramServerImpl>(this->pParent_,
pWorker.get(),
bind.pDriver);
bind.pDriver,
bind.uDefaultInputStreamSize);
if (!pServer)
{
SysPushErrorNet("No Memory");

View File

@ -178,6 +178,7 @@ namespace Aurora::IO::Net
netBind.pDriver,
netBind.pFactory,
uMaxSockets,
netBind.uDefaultInputStreamSize,
netBind.bMultiThreaded);
if (!pSocket)
{

View File

@ -9,6 +9,7 @@
#include "AuNetStream.Linux.hpp"
#include "AuNetSocket.hpp"
#include "AuNetWorker.hpp"
#include "AuNetEndpoint.hpp"
#include <Source/IO/Loop/LSEvent.hpp>
namespace Aurora::IO::Net
@ -159,12 +160,23 @@ namespace Aurora::IO::Net
}
else
{
this->iSocketLength = this->pSocket->endpointSize_;
#if 0
AuLogDbg("{} -> {} {}, {}, {} {}", this->GetSocket(),
memoryView->ptr,
memoryView->length,
0,
(void *)netEndpoint.hint,
this->iSocketLength);
#endif
if (::sendto(this->GetSocket(),
memoryView->ptr,
memoryView->length,
0,
(struct sockaddr *)netEndpoint.hint,
this->iSocketLength) != memoryView->length)
EndpointToLength(netEndpoint)) != memoryView->length)
{
LIOS_SendProcess(0, false, errno);
return true;

View File

@ -229,7 +229,7 @@ namespace Aurora::IO::Net
NULL,
this->dwRecvFlags,
(sockaddr *)netEndpoint.hint,
this->iSocketLength,
EndpointToLength(netEndpoint),
&this->overlap,
WsaOverlappedCompletionRoutine);
}

View File

@ -56,6 +56,7 @@ namespace Aurora::IO::Net
AuNet::NetDatagramBind nsDatagramBind;
nsDatagramBind.ip = this->localEndpoint.ip;
nsDatagramBind.uPort = this->localEndpoint.uPort;
nsDatagramBind.uDefaultInputStreamSize = this->uDefaultPacketSize;
nsDatagramBind.pDriver = this->ToDriver();
this->pDatagramServer_ = this->pInterface_->GetDatagramService()->NewDatagramServer(nsDatagramBind);

View File

@ -187,7 +187,7 @@ namespace Aurora::RNG
void EntropyDeinit()
{
#if defined(AURORA_IS_POSIX_DERIVED)
if (gDevURand <= 0)
if (gDevURand > 0)
{
::close(gDevURand);
}