AuroraRuntime/Source/IO/Net/AuNetSrvSockets.cpp
Jamie Reece Wilson dcf94a8b23 [+] (dummy!) ISocketServerLimits.hpp
[+] (dummy!) ISocketChannelLimits.hpp
[+] ISocketChannel::GetChannelLimits
[+] ISocketBase::GetLockedWorkerThread
[+] ISocket::GetSocketServer
2023-11-29 13:55:01 +00:00

293 lines
9.7 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)
{
AuSPtr<Socket> pSocket;
auto pWorker = this->pParent_->TryScheduleEx();
if (!pWorker)
{
SysPushErrorNet("No Worker");
return {};
}
if (netConnect.byEndpoint)
{
if (netConnect.byEndpoint.Value().transportProtocol != ETransportProtocol::eProtocolTCP)
{
SysPushErrorNet("Invalid transport protocol. Hint: Use ConnectManyEx for UDP.");
return {};
}
pSocket = AuMakeShared<Socket>(this->pParent_,
pWorker.get(),
netConnect.pDriver,
netConnect.byEndpoint.value());
}
else
{
auto &connectOptions = netConnect.byHost.Value();
if (connectOptions.netHostname.type != EHostnameType::eHostByDns)
{
SysPushErrorArg("Missing hostname or endpoint");
return {};
}
pSocket = AuMakeShared<Socket>(this->pParent_,
pWorker.get(),
netConnect.pDriver,
AuMakePair(connectOptions.netHostname, connectOptions.uPort),
connectOptions.protocol);
}
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)
{
// TODO: restore no udp check
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 (netBind.uMaxAcceptBacklog)
{
pSocket->uBacklog = netBind.uMaxAcceptBacklog;
}
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 {};
}
}