2022-08-28 19:02:06 +00:00
|
|
|
/***
|
|
|
|
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"
|
|
|
|
|
2022-12-12 23:50:05 +00:00
|
|
|
#include "SocketOverDatagram/SocketOverDatagram.hpp"
|
|
|
|
|
2022-08-28 19:02:06 +00:00
|
|
|
namespace Aurora::IO::Net
|
|
|
|
{
|
|
|
|
NetSrvSockets::NetSrvSockets(NetInterface *pParent) :
|
|
|
|
pParent_(pParent)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
AuSPtr<ISocket> NetSrvSockets::Connect(const NetSocketConnect &netConnect)
|
|
|
|
{
|
2023-09-23 16:10:00 +00:00
|
|
|
AuSPtr<Socket> pSocket;
|
|
|
|
|
2022-08-28 19:02:06 +00:00
|
|
|
auto pWorker = this->pParent_->TryScheduleEx();
|
|
|
|
if (!pWorker)
|
|
|
|
{
|
|
|
|
SysPushErrorNet("No Worker");
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2023-09-23 16:10:00 +00:00
|
|
|
if (netConnect.byEndpoint)
|
2023-01-26 21:43:19 +00:00
|
|
|
{
|
2023-09-23 16:10:00 +00:00
|
|
|
if (netConnect.byEndpoint.Value().transportProtocol != ETransportProtocol::eProtocolTCP)
|
2023-01-26 21:43:19 +00:00
|
|
|
{
|
|
|
|
SysPushErrorNet("Invalid transport protocol. Hint: Use ConnectManyEx for UDP.");
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
pSocket = AuMakeShared<Socket>(this->pParent_,
|
|
|
|
pWorker.get(),
|
|
|
|
netConnect.pDriver,
|
2023-09-23 16:10:00 +00:00
|
|
|
netConnect.byEndpoint.value());
|
2023-01-26 21:43:19 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-09-23 16:10:00 +00:00
|
|
|
auto &connectOptions = netConnect.byHost.Value();
|
2023-01-26 21:43:19 +00:00
|
|
|
|
2023-09-23 16:10:00 +00:00
|
|
|
if (connectOptions.netHostname.type != EHostnameType::eHostByDns)
|
2023-01-26 21:43:19 +00:00
|
|
|
{
|
2023-09-23 16:10:00 +00:00
|
|
|
SysPushErrorArg("Missing hostname or endpoint");
|
2023-01-26 21:43:19 +00:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
pSocket = AuMakeShared<Socket>(this->pParent_,
|
|
|
|
pWorker.get(),
|
|
|
|
netConnect.pDriver,
|
2023-09-23 16:10:00 +00:00
|
|
|
AuMakePair(connectOptions.netHostname, connectOptions.uPort),
|
|
|
|
connectOptions.protocol);
|
2023-01-26 21:43:19 +00:00
|
|
|
}
|
|
|
|
|
2022-08-28 19:02:06 +00:00
|
|
|
if (!pSocket)
|
|
|
|
{
|
|
|
|
SysPushErrorNet("No Memory");
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!pWorker->TryScheduleInternalTemplate<AuNullS>([=](const AuSPtr<AuAsync::PromiseCallback<AuNullS>> &info)
|
|
|
|
{
|
|
|
|
pSocket->FinishConstructAsync();
|
|
|
|
|
2023-01-26 21:43:19 +00:00
|
|
|
if (pSocket->bResolving_)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-08-28 19:02:06 +00:00
|
|
|
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)
|
|
|
|
{
|
2023-09-23 18:33:08 +00:00
|
|
|
// TODO: restore no udp check
|
2022-08-28 19:02:06 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2022-12-12 23:50:05 +00:00
|
|
|
auto uMaxSockets = netBind.uMaxConnections ? netBind.uMaxConnections : 512;
|
2022-08-28 19:02:06 +00:00
|
|
|
|
|
|
|
auto pWorker = this->pParent_->TryScheduleEx();
|
|
|
|
if (!pWorker)
|
|
|
|
{
|
|
|
|
SysPushErrorNet("No Worker");
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2022-12-12 23:50:05 +00:00
|
|
|
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 {};
|
|
|
|
}
|
2022-08-28 19:02:06 +00:00
|
|
|
|
|
|
|
auto pSocket = AuMakeShared<SocketServerImpl>(this->pParent_,
|
|
|
|
pWorker.get(),
|
|
|
|
netBind.pDriver,
|
|
|
|
netBind.pFactory,
|
2022-11-17 20:56:41 +00:00
|
|
|
uMaxSockets,
|
2022-12-16 17:18:12 +00:00
|
|
|
netBind.uDefaultInputStreamSize,
|
2022-12-12 23:50:05 +00:00
|
|
|
netBind.bMultiThreaded);
|
2022-08-28 19:02:06 +00:00
|
|
|
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)
|
|
|
|
{
|
2022-12-12 23:50:05 +00:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2022-08-28 19:02:06 +00:00
|
|
|
if (netBindEx.protocol == ETransportProtocol::eProtocolTCP)
|
|
|
|
{
|
|
|
|
return this->NewServer(netBindEx);
|
|
|
|
}
|
|
|
|
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
}
|