[*] Refactor AuNet::NetSocketConnect

This commit is contained in:
Reece Wilson 2023-09-23 17:10:00 +01:00
parent 7e9bfd8215
commit ded605910f
5 changed files with 28 additions and 35 deletions

View File

@ -13,18 +13,18 @@ namespace Aurora::IO::Net
struct NetSocketConnectByHost
{
AuOptionalEx<NetHostname> netHostname;
AuOptionalEx<AuUInt16> uPort;
AuOptionalEx<ETransportProtocol> protocol;
NetHostname netHostname;
AuUInt16 uPort;
ETransportProtocol protocol;
};
struct NetSocketConnect
{
// connect by protocol, address:port
AuOptionalEx<NetEndpoint> endpoint;
AuOptionalEx<NetEndpoint> byEndpoint;
// or connect by [string/ip family, ip address], port
NetSocketConnectByHost byHost;
AuOptionalEx<NetSocketConnectByHost> byHost;
//
AuSPtr<ISocketDriver> pDriver;
@ -34,8 +34,7 @@ namespace Aurora::IO::Net
struct NetSocketConnectMany
{
ETransportProtocol protocol {};
AuList<IPAddress> ips;
AuUInt16 uPort {};
AuList<AuPair<IPAddress, AuUInt16>> ips;
AuSPtr<ISocketDriver> pDriver;
AuUInt32 uMaxConnectTimeMs {};
};

View File

@ -83,7 +83,7 @@ namespace Aurora::IO::Net
if (this->bHasRemoteMany_ && this->connectMany_.ips.size())
{
this->remoteEndpoint_ = this->connectMany_.ips[0];
this->remoteEndpoint_ = this->connectMany_.ips[0].first;
if (this->remoteEndpoint_.transportProtocol == ETransportProtocol::kEnumInvalid)
{
this->remoteEndpoint_.transportProtocol = this->connectMany_.protocol;

View File

@ -81,7 +81,7 @@ namespace Aurora::IO::Net
if (this->bHasRemoteMany_ && this->connectMany_.ips.size())
{
this->remoteEndpoint_ = this->connectMany_.ips[0];
this->remoteEndpoint_ = this->connectMany_.ips[0].first;
if (this->remoteEndpoint_.transportProtocol == ETransportProtocol::kEnumInvalid)
{
this->remoteEndpoint_.transportProtocol = this->connectMany_.protocol;

View File

@ -101,7 +101,6 @@ namespace Aurora::IO::Net
this->resolveLater = host.hostname;
this->remoteEndpoint_.uPort = uPort;
this->remoteEndpoint_.transportProtocol = eProtocol;
this->connectMany_.uPort = uPort;
this->connectMany_.protocol = eProtocol;
}
@ -173,12 +172,17 @@ namespace Aurora::IO::Net
{
pThat->bResolving_ = false;
pThat->connectMany_.uPort = pThat->remoteEndpoint_.uPort;
pThat->connectMany_.ips.insert(pThat->connectMany_.ips.end(), ips->begin(), ips->end());
if (pThat->remoteEndpoint_.transportProtocol == ETransportProtocol::kEnumInvalid)
{
pThat->remoteEndpoint_.transportProtocol = this->connectMany_.protocol;
}
for (auto &ip : *ips)
{
pThat->connectMany_.ips.push_back(AuMakePair(ip, pThat->remoteEndpoint_.uPort));
}
pThat->bHasRemoteMany_ = true;
pThat->RenewSocket();
pThat->ConnectNext();
@ -198,12 +202,12 @@ namespace Aurora::IO::Net
return false;
}
auto topLevelEntry = this->connectMany_.ips[0];
auto [topLevelEntryIp, topLevelEntryPort] = this->connectMany_.ips[0];
this->connectMany_.ips.erase(this->connectMany_.ips.begin());
NetEndpoint endpoint;
endpoint.uPort = this->connectMany_.uPort;
endpoint.ip = topLevelEntry;
endpoint.uPort = topLevelEntryPort;
endpoint.ip = topLevelEntryIp;
endpoint.transportProtocol = this->connectMany_.protocol;
return this->Connect(endpoint);
}

View File

@ -25,6 +25,8 @@ namespace Aurora::IO::Net
AuSPtr<ISocket> NetSrvSockets::Connect(const NetSocketConnect &netConnect)
{
AuSPtr<Socket> pSocket;
auto pWorker = this->pParent_->TryScheduleEx();
if (!pWorker)
{
@ -32,11 +34,9 @@ namespace Aurora::IO::Net
return {};
}
AuSPtr<Socket> pSocket;
if (netConnect.endpoint)
if (netConnect.byEndpoint)
{
if (netConnect.endpoint.Value().transportProtocol != ETransportProtocol::eProtocolTCP)
if (netConnect.byEndpoint.Value().transportProtocol != ETransportProtocol::eProtocolTCP)
{
SysPushErrorNet("Invalid transport protocol. Hint: Use ConnectManyEx for UDP.");
return {};
@ -45,33 +45,23 @@ namespace Aurora::IO::Net
pSocket = AuMakeShared<Socket>(this->pParent_,
pWorker.get(),
netConnect.pDriver,
netConnect.endpoint.value());
netConnect.byEndpoint.value());
}
else
{
if (!netConnect.byHost.netHostname)
auto &connectOptions = netConnect.byHost.Value();
if (connectOptions.netHostname.type != EHostnameType::eHostByDns)
{
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());
AuMakePair(connectOptions.netHostname, connectOptions.uPort),
connectOptions.protocol);
}
if (!pSocket)