[+] Added NetSocketConnectBase

[*] ded60591 cont
This commit is contained in:
Reece Wilson 2023-09-23 19:33:08 +01:00
parent ded605910f
commit 3c8442d8ce
5 changed files with 66 additions and 33 deletions

View File

@ -18,23 +18,44 @@ namespace Aurora::IO::Net
ETransportProtocol protocol; ETransportProtocol protocol;
}; };
struct NetSocketConnect struct NetSocketConnectBase
{ {
inline NetSocketConnectBase() = default;
inline NetSocketConnectBase(IPAddress ip, AuUInt16 uPort) : byEndpoint(NetEndpoint { ip, uPort })
{ }
inline NetSocketConnectBase(NetSocketConnectByHost byHost) : byHost(byHost)
{ }
// connect by protocol, address:port // connect by protocol, address:port
AuOptionalEx<NetEndpoint> byEndpoint; AuOptionalEx<NetEndpoint> byEndpoint;
// or connect by [string/ip family, ip address], port // or connect by [string/ip family, ip address], port
AuOptionalEx<NetSocketConnectByHost> byHost; AuOptionalEx<NetSocketConnectByHost> byHost;
};
struct NetSocketConnect : NetSocketConnectBase
{
inline NetSocketConnect() :
NetSocketConnectBase()
{ }
inline NetSocketConnect(IPAddress ip, AuUInt16 uPort) :
NetSocketConnectBase(ip, uPort)
{ }
inline NetSocketConnect(NetSocketConnectByHost byHost) :
NetSocketConnectBase(byHost)
{ }
//
AuSPtr<ISocketDriver> pDriver; AuSPtr<ISocketDriver> pDriver;
AuUInt32 uMaxConnectTimeMs {}; AuUInt32 uMaxConnectTimeMs {};
}; };
struct NetSocketConnectMany struct NetSocketConnectMany
{ {
ETransportProtocol protocol {}; AuList<NetSocketConnectBase> names;
AuList<AuPair<IPAddress, AuUInt16>> ips;
AuSPtr<ISocketDriver> pDriver; AuSPtr<ISocketDriver> pDriver;
AuUInt32 uMaxConnectTimeMs {}; AuUInt32 uMaxConnectTimeMs {};
}; };

View File

@ -81,12 +81,17 @@ namespace Aurora::IO::Net
return; return;
} }
if (this->bHasRemoteMany_ && this->connectMany_.ips.size()) if (this->bHasRemoteMany_ && this->connectMany_.names.size())
{ {
this->remoteEndpoint_ = this->connectMany_.ips[0].first; if (this->connectMany_.names[0].byEndpoint)
if (this->remoteEndpoint_.transportProtocol == ETransportProtocol::kEnumInvalid)
{ {
this->remoteEndpoint_.transportProtocol = this->connectMany_.protocol; this->remoteEndpoint_ = this->connectMany_.names[0].byEndpoint.value();
}
else
{
auto val = this->connectMany_.names[0].byHost.value();
this->remoteEndpoint_.ip = val.netHostname.address;
this->remoteEndpoint_.transportProtocol = val.protocol;
} }
} }

View File

@ -81,10 +81,15 @@ namespace Aurora::IO::Net
if (this->bHasRemoteMany_ && this->connectMany_.ips.size()) if (this->bHasRemoteMany_ && this->connectMany_.ips.size())
{ {
this->remoteEndpoint_ = this->connectMany_.ips[0].first; if (this->connectMany_.names[0].byEndpoint)
if (this->remoteEndpoint_.transportProtocol == ETransportProtocol::kEnumInvalid)
{ {
this->remoteEndpoint_.transportProtocol = this->connectMany_.protocol; this->remoteEndpoint_ = this->connectMany_.names[0].byEndpoint.value();
}
else
{
auto val = this->connectMany_.names[0].byHost.value();
this->remoteEndpoint_.ip = val.netHostname.address;
this->remoteEndpoint_.transportProtocol = val.protocol;
} }
} }
@ -132,7 +137,6 @@ namespace Aurora::IO::Net
void Socket::FinishConstructAsync() void Socket::FinishConstructAsync()
{ {
if (this->resolveLater.size() || this->bResolving_) if (this->resolveLater.size() || this->bResolving_)
{ {
if (!this->TryStartResolve()) if (!this->TryStartResolve())
@ -147,7 +151,6 @@ namespace Aurora::IO::Net
RenewSocket(); RenewSocket();
} }
bool Socket::PrepareConnectOperations() bool Socket::PrepareConnectOperations()
{ {
return this->MakeNonblocking(); return this->MakeNonblocking();

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_.protocol = eProtocol;
} }
this->osHandleOwner_ = AuIO::IOHandleShared(); this->osHandleOwner_ = AuIO::IOHandleShared();
@ -167,22 +166,19 @@ namespace Aurora::IO::Net
auto pResolver = this->pInterface_->GetResolveService()->SimpleAllResolve(address, auto pResolver = this->pInterface_->GetResolveService()->SimpleAllResolve(address,
AuMakeSharedThrow<AuAsync::PromiseCallbackFunctional<AuList<AuNet::IPAddress>, AuMakeSharedThrow<AuAsync::PromiseCallbackFunctional<AuList<AuNet::IPAddress>,
AuNet::NetError>>( AuNet::NetError>>(
[=](const AuSPtr<AuList<AuNet::IPAddress>> &ips) [=](const AuSPtr<AuList<AuNet::IPAddress>> &ips)
{ {
pThat->bResolving_ = false; pThat->bResolving_ = false;
if (pThat->remoteEndpoint_.transportProtocol == ETransportProtocol::kEnumInvalid) AuList<NetSocketConnectBase> names;
{
pThat->remoteEndpoint_.transportProtocol = this->connectMany_.protocol;
}
for (auto &ip : *ips) for (auto &ip : *ips)
{ {
pThat->connectMany_.ips.push_back(AuMakePair(ip, pThat->remoteEndpoint_.uPort)); names.push_back(NetSocketConnectBase(ip, pThat->remoteEndpoint_.uPort));
} }
pThat->connectMany_.names = names;
pThat->bHasRemoteMany_ = true; pThat->bHasRemoteMany_ = true;
pThat->RenewSocket(); pThat->RenewSocket();
pThat->ConnectNext(); pThat->ConnectNext();
@ -197,18 +193,30 @@ namespace Aurora::IO::Net
bool SocketBase::ConnectNext() bool SocketBase::ConnectNext()
{ {
if (this->connectMany_.ips.empty()) if (this->connectMany_.names.empty())
{ {
return false; return false;
} }
auto [topLevelEntryIp, topLevelEntryPort] = this->connectMany_.ips[0]; auto base = this->connectMany_.names[0];
this->connectMany_.ips.erase(this->connectMany_.ips.begin()); this->connectMany_.names.erase(this->connectMany_.names.begin());
NetEndpoint endpoint; NetEndpoint endpoint;
endpoint.uPort = topLevelEntryPort; if (base.byEndpoint)
endpoint.ip = topLevelEntryIp; {
endpoint.transportProtocol = this->connectMany_.protocol; endpoint = base.byEndpoint.value();
}
else
{
auto val = base.byHost.value();
if (val.netHostname.type == EHostnameType::eHostByDns)
{
return false;
}
endpoint.uPort = val.uPort;
endpoint.ip = val.netHostname.address;
endpoint.transportProtocol = val.protocol;
}
return this->Connect(endpoint); return this->Connect(endpoint);
} }

View File

@ -99,11 +99,7 @@ namespace Aurora::IO::Net
AuSPtr<ISocket> NetSrvSockets::ConnectMany(const NetSocketConnectMany &netConnectMany) AuSPtr<ISocket> NetSrvSockets::ConnectMany(const NetSocketConnectMany &netConnectMany)
{ {
if (netConnectMany.protocol != ETransportProtocol::eProtocolTCP) // TODO: restore no udp check
{
SysPushErrorNet("Invalid transport protocol. Hint: Use ConnectManyEx for UDP.");
return {};
}
auto pWorker = this->pParent_->TryScheduleEx(); auto pWorker = this->pParent_->TryScheduleEx();
if (!pWorker) if (!pWorker)