AuroraRuntime/Source/IO/Net/AuNetSrvSockets.cpp
Reece Wilson 04aca5fcf2 [+] Aurora::IO::Net::NetSocketConnectByHost
[+] Aurora::IO::FS::DirDeleterEx
[+] Aurora::IO::Compress
[+] Aurora::IO::Decompress
[*] Aurora::Memory::ByteBuffer zero-alloc fixes
[*] Aurora::Memory::ByteBuffer linear read of begin/end should return (`const AuUInt8 *`)'s
[*] Changed NT file CREATE flags
[*] Fix linux regression
[*] Update logger sink DirLogArchive
... [+] DirectoryLogger::uMaxLogsOrZeroBeforeDelete
... [+] DirectoryLogger::uMaxCumulativeFileSizeInMiBOrZeroBeforeDelete
... [+] DirectoryLogger::uMaxCumulativeFileSizeInMiBOrZeroBeforeCompress
... [+] DirectoryLogger::uMaxFileTimeInDeltaMSOrZeroBeforeCompress
... [+] DirectoryLogger::uMaxFileTimeInDeltaMSOrZeroBeforeDelete
[*] FIX: BufferedLineReader was taking the wrong end head
(prep) LZMACompressor
[*] Updated build-script for LZMA (when i can be bothered to impl it)
(prep) FSOverlappedUtilities
(prep) FSDefaultOverlappedWorkerThread | default worker pool / apc dispatcher / auasync dispatcher concept for higher level overlapped ops
(stub) [+] Aurora::IO::FS::OverlappedForceDelegatedIO
(stub) [+] Aurora::IO::FS::OverlappedCompress
(stub) [+] Aurora::IO::FS::OverlappedDecompress
(stub) [+] Aurora::IO::FS::OverlappedWrite
(stub) [+] Aurora::IO::FS::OverlappedRead
(stub) [+] Aurora::IO::FS::OverlappedStat
(stub) [+] Aurora::IO::FS::OverlappedCopy
(stub) [+] Aurora::IO::FS::OverlappedRelink
(stub) [+] Aurora::IO::FS::OverlappedTrustFile
(stub) [+] Aurora::IO::FS::OverlappedBlockFile
(stub) [+] Aurora::IO::FS::OverlappedUnblockFile
(stub) [+] Aurora::IO::FS::OverlappedDelete
2023-01-26 21:43:19 +00:00

302 lines
10 KiB
C++

/***
Copyright (C) 2022 J Reece Wilson (a/k/a "Reece"). All rights reserved.
File: AuNetSrvSockets.cpp
Date: 2022-8-16
Author: Reece
***/
#include "Networking.hpp"
#include "AuNetSrvSockets.hpp"
#include "AuNetInterface.hpp"
#include "AuNetWorker.hpp"
#include "AuNetSocket.hpp"
#include "AuNetSocketServer.hpp"
#include "SocketOverDatagram/SocketOverDatagram.hpp"
namespace Aurora::IO::Net
{
NetSrvSockets::NetSrvSockets(NetInterface *pParent) :
pParent_(pParent)
{
}
AuSPtr<ISocket> NetSrvSockets::Connect(const NetSocketConnect &netConnect)
{
auto pWorker = this->pParent_->TryScheduleEx();
if (!pWorker)
{
SysPushErrorNet("No Worker");
return {};
}
AuSPtr<Socket> pSocket;
if (netConnect.endpoint)
{
if (netConnect.endpoint.Value().transportProtocol != ETransportProtocol::eProtocolTCP)
{
SysPushErrorNet("Invalid transport protocol. Hint: Use ConnectManyEx for UDP.");
return {};
}
pSocket = AuMakeShared<Socket>(this->pParent_,
pWorker.get(),
netConnect.pDriver,
netConnect.endpoint.value());
}
else
{
if (!netConnect.byHost.netHostname)
{
SysPushErrorArg("Missing hostname or endpoint");
return {};
}
if (!netConnect.byHost.protocol)
{
SysPushErrorArg("Missing protocol");
return {};
}
if (!netConnect.byHost.uPort)
{
SysPushErrorArg("Missing port");
return {};
}
pSocket = AuMakeShared<Socket>(this->pParent_,
pWorker.get(),
netConnect.pDriver,
AuMakePair(netConnect.byHost.netHostname.value(), netConnect.byHost.uPort.value()),
netConnect.byHost.protocol.value());
}
if (!pSocket)
{
SysPushErrorNet("No Memory");
return {};
}
if (!pWorker->TryScheduleInternalTemplate<AuNullS>([=](const AuSPtr<AuAsync::PromiseCallback<AuNullS>> &info)
{
pSocket->FinishConstructAsync();
if (pSocket->bResolving_)
{
return;
}
if (!pSocket->IsValid())
{
pSocket->SendErrorNoStream({});
return;
}
if (!pSocket->Connect(pSocket->GetRemoteEndpoint()))
{
SysPushErrorIO("An asynchronous connect failed [root level]");
}
}, AuSPtr<AuAsync::PromiseCallback<AuNullS, AuNullS>>{}))
{
return {};
}
return pSocket;
}
AuSPtr<ISocket> NetSrvSockets::ConnectMany(const NetSocketConnectMany &netConnectMany)
{
if (netConnectMany.protocol != ETransportProtocol::eProtocolTCP)
{
SysPushErrorNet("Invalid transport protocol. Hint: Use ConnectManyEx for UDP.");
return {};
}
auto pWorker = this->pParent_->TryScheduleEx();
if (!pWorker)
{
SysPushErrorNet("No Worker");
return {};
}
auto pSocket = AuMakeShared<Socket>(this->pParent_,
pWorker.get(),
netConnectMany.pDriver,
netConnectMany);
if (!pSocket)
{
SysPushErrorNet("No Memory");
return {};
}
if (!pWorker->TryScheduleInternalTemplate<AuNullS>([=](const AuSPtr<AuAsync::PromiseCallback<AuNullS>> &info)
{
pSocket->FinishConstructAsync();
if (!pSocket->IsValid())
{
pSocket->SendErrorNoStream({});
return;
}
if (!pSocket->ConnectNext())
{
SysPushErrorIO("An asynchronous connect failed [root level]");
}
}, AuSPtr<AuAsync::PromiseCallback<AuNullS, AuNullS>>{}))
{
return {};
}
return pSocket;
}
AuSPtr<ISocketServer> NetSrvSockets::NewServer(const NetSocketBind &netBind)
{
auto uMaxSockets = netBind.uMaxConnections ? netBind.uMaxConnections : 512;
auto pWorker = this->pParent_->TryScheduleEx();
if (!pWorker)
{
SysPushErrorNet("No Worker");
return {};
}
if (netBind.protocol == ETransportProtocol::eProtocolUDP)
{
auto pSocket = AuMakeShared<NetDatagramSocketServer>(this->pParent_,
pWorker.get(),
netBind.pDriver,
netBind.pFactory,
uMaxSockets,
0,
AuSToMS<AuUInt32>(60),
netBind.bMultiThreaded,
false,
kDefaultStreamSize);
if (!pSocket)
{
SysPushErrorNet("No Memory");
return {};
}
if (!pSocket->Init())
{
SysPushErrorNested("no socket");
return {};
}
if (!pWorker->TryScheduleInternalTemplate<AuNullS>([=](const AuSPtr<AuAsync::PromiseCallback<AuNullS>> &info)
{
NetEndpoint endpoint;
endpoint.ip = netBind.ip;
endpoint.uPort = netBind.uPort;
endpoint.transportProtocol = netBind.protocol;
pSocket->Start(endpoint);
}, AuSPtr<AuAsync::PromiseCallback<AuNullS, AuNullS>>{}))
{
return {};
}
return pSocket->ToSocketServer();
}
if (netBind.protocol != ETransportProtocol::eProtocolTCP)
{
SysPushErrorNet("Unknown transport protocol");
return {};
}
auto pSocket = AuMakeShared<SocketServerImpl>(this->pParent_,
pWorker.get(),
netBind.pDriver,
netBind.pFactory,
uMaxSockets,
netBind.uDefaultInputStreamSize,
netBind.bMultiThreaded);
if (!pSocket)
{
SysPushErrorNet("No Memory");
return {};
}
if (!pWorker->TryScheduleInternalTemplate<AuNullS>([=](const AuSPtr<AuAsync::PromiseCallback<AuNullS>> &info)
{
pSocket->FinishConstructAsync();
NetEndpoint endpoint;
endpoint.ip = netBind.ip;
endpoint.uPort = netBind.uPort;
endpoint.transportProtocol = netBind.protocol;
pSocket->Init(endpoint);
pSocket->Listen(endpoint);
pSocket->Accept();
}, AuSPtr<AuAsync::PromiseCallback<AuNullS, AuNullS>>{}))
{
return {};
}
return pSocket;
}
AuSPtr<ISocketServer> NetSrvSockets::NewServerEx(const NetSocketBindEx &netBindEx)
{
auto uMaxSockets = netBindEx.uMaxConnections ? netBindEx.uMaxConnections : 512;
auto pWorker = this->pParent_->TryScheduleEx();
if (!pWorker)
{
SysPushErrorNet("No Worker");
return {};
}
if (netBindEx.protocol == ETransportProtocol::eProtocolUDP)
{
auto pSocket = AuMakeShared<NetDatagramSocketServer>(this->pParent_,
pWorker.get(),
netBindEx.pDriver,
netBindEx.pFactory,
uMaxSockets,
0,
AuSToMS<AuUInt32>(netBindEx.uUDPTimeoutMs),
netBindEx.bMultiThreaded,
false,
kDefaultStreamSize);
if (!pSocket)
{
SysPushErrorNet("No Memory");
return {};
}
if (!pSocket->Init())
{
SysPushErrorNested("no socket");
return {};
}
if (!pWorker->TryScheduleInternalTemplate<AuNullS>([=](const AuSPtr<AuAsync::PromiseCallback<AuNullS>> &info)
{
NetEndpoint endpoint;
endpoint.ip = netBindEx.ip;
endpoint.uPort = netBindEx.uPort;
endpoint.transportProtocol = netBindEx.protocol;
pSocket->Start(endpoint);
}, AuSPtr<AuAsync::PromiseCallback<AuNullS, AuNullS>>{}))
{
return {};
}
return pSocket->ToSocketServer();
}
if (netBindEx.protocol == ETransportProtocol::eProtocolTCP)
{
return this->NewServer(netBindEx);
}
return {};
}
}