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

View File

@ -83,7 +83,7 @@ namespace Aurora::IO::Net
if (this->bHasRemoteMany_ && this->connectMany_.ips.size()) 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) if (this->remoteEndpoint_.transportProtocol == ETransportProtocol::kEnumInvalid)
{ {
this->remoteEndpoint_.transportProtocol = this->connectMany_.protocol; this->remoteEndpoint_.transportProtocol = this->connectMany_.protocol;

View File

@ -81,7 +81,7 @@ namespace Aurora::IO::Net
if (this->bHasRemoteMany_ && this->connectMany_.ips.size()) 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) if (this->remoteEndpoint_.transportProtocol == ETransportProtocol::kEnumInvalid)
{ {
this->remoteEndpoint_.transportProtocol = this->connectMany_.protocol; this->remoteEndpoint_.transportProtocol = this->connectMany_.protocol;

View File

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

View File

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