2022-08-28 19:02:06 +00:00
|
|
|
/***
|
|
|
|
Copyright (C) 2022 J Reece Wilson (a/k/a "Reece"). All rights reserved.
|
|
|
|
|
|
|
|
File: AuNetSocket.NT.cpp
|
|
|
|
Date: 2022-8-16
|
|
|
|
Author: Reece
|
|
|
|
***/
|
|
|
|
#include "Networking.hpp"
|
|
|
|
#include "AuNetSocket.hpp"
|
|
|
|
#include "AuNetEndpoint.hpp"
|
|
|
|
#include "AuNetWorker.hpp"
|
|
|
|
#include "AuIPAddress.hpp"
|
|
|
|
#include "AuNetError.hpp"
|
|
|
|
|
|
|
|
namespace Aurora::IO::Net
|
|
|
|
{
|
|
|
|
static NetError GetLastNetError()
|
|
|
|
{
|
|
|
|
NetError error;
|
|
|
|
NetError_SetCurrent(error);
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
Socket::Socket(struct NetInterface *pInterface,
|
|
|
|
struct NetWorker *pWorker,
|
|
|
|
const AuSPtr<ISocketDriver> &pSocketDriver,
|
2023-11-29 13:55:01 +00:00
|
|
|
AuUInt osHandle,
|
|
|
|
AuSPtr<ISocketServer> pParent,
|
|
|
|
SocketServer *pParent2) :
|
|
|
|
SocketBase(pInterface, pWorker, pSocketDriver, osHandle, pParent, pParent2)
|
2022-08-28 19:02:06 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
2023-01-26 21:43:19 +00:00
|
|
|
|
|
|
|
Socket::Socket(struct NetInterface *pInterface,
|
|
|
|
struct NetWorker *pWorker,
|
|
|
|
const AuSPtr<ISocketDriver> &pSocketDriver,
|
|
|
|
const NetEndpoint &endpoint) :
|
|
|
|
SocketBase(pInterface, pWorker, pSocketDriver, endpoint)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-08-28 19:02:06 +00:00
|
|
|
Socket::Socket(struct NetInterface *pInterface,
|
|
|
|
struct NetWorker *pWorker,
|
|
|
|
const AuSPtr<ISocketDriver> &pSocketDriver,
|
2023-01-26 21:43:19 +00:00
|
|
|
const AuPair<NetHostname, AuUInt16> &endpoint,
|
|
|
|
AuNet::ETransportProtocol eProtocol) :
|
|
|
|
SocketBase(pInterface, pWorker, pSocketDriver, endpoint, eProtocol)
|
2022-08-28 19:02:06 +00:00
|
|
|
{
|
2023-01-26 21:43:19 +00:00
|
|
|
|
2022-08-28 19:02:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Socket::Socket(NetInterface *pInterface,
|
|
|
|
NetWorker *pWorker,
|
|
|
|
const AuSPtr<ISocketDriver> &pSocketDriver,
|
|
|
|
const NetSocketConnectMany &connectMany) :
|
|
|
|
SocketBase(pInterface, pWorker, pSocketDriver, connectMany)
|
|
|
|
{
|
2023-01-26 21:43:19 +00:00
|
|
|
|
2022-08-28 19:02:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Socket::~Socket()
|
|
|
|
{
|
|
|
|
CloseSocket();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Socket::CloseSocket()
|
|
|
|
{
|
|
|
|
if (this->osHandle_ &&
|
|
|
|
this->osHandle_ != -1)
|
|
|
|
{
|
2023-12-07 11:13:32 +00:00
|
|
|
if (pclosesocket)
|
|
|
|
{
|
|
|
|
pclosesocket(this->osHandle_);
|
|
|
|
}
|
2022-08-28 19:02:06 +00:00
|
|
|
this->osHandle_ = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-26 21:43:19 +00:00
|
|
|
void Socket::RenewSocket()
|
2022-08-28 19:02:06 +00:00
|
|
|
{
|
|
|
|
if (!this->SendPreestablish())
|
|
|
|
{
|
|
|
|
SysPushErrorIO("Preestablish drop");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-09-23 18:33:08 +00:00
|
|
|
if (this->bHasRemoteMany_ && this->connectMany_.names.size())
|
2023-01-26 21:43:19 +00:00
|
|
|
{
|
2023-09-23 18:33:08 +00:00
|
|
|
if (this->connectMany_.names[0].byEndpoint)
|
2023-09-23 08:25:18 +00:00
|
|
|
{
|
2023-09-23 18:33:08 +00:00
|
|
|
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;
|
2023-09-23 08:25:18 +00:00
|
|
|
}
|
2023-01-26 21:43:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (this->osHandle_ &&
|
|
|
|
this->osHandle_ != -1)
|
|
|
|
{
|
2023-12-07 11:13:32 +00:00
|
|
|
if (pclosesocket)
|
|
|
|
{
|
|
|
|
pclosesocket(this->osHandle_);
|
|
|
|
}
|
2023-01-26 21:43:19 +00:00
|
|
|
this->osHandle_ = 0;
|
|
|
|
}
|
|
|
|
|
2023-12-07 11:13:32 +00:00
|
|
|
if (!pWSASocketW)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this->osHandle_ = pWSASocketW(
|
2022-08-28 19:02:06 +00:00
|
|
|
IPToDomain(this->remoteEndpoint_),
|
|
|
|
TransportToPlatformType(this->remoteEndpoint_),
|
|
|
|
IPPROTO_IP,
|
|
|
|
nullptr,
|
|
|
|
0,
|
|
|
|
WSA_FLAG_OVERLAPPED
|
|
|
|
);
|
|
|
|
|
|
|
|
if (this->osHandle_ == -1)
|
|
|
|
{
|
|
|
|
this->SendErrorNoStream(GetLastNetError());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!this->PrepareConnectOperations())
|
|
|
|
{
|
|
|
|
this->bForceFailConstruct_ = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if !defined(AURORA_IS_MODERNNT_DERIVED)
|
2023-01-26 21:43:19 +00:00
|
|
|
|
|
|
|
this->osHandleOwner_ = AuMakeShared<AuFS::FileHandle>();
|
|
|
|
if (!this->osHandle_)
|
|
|
|
{
|
|
|
|
this->SendErrorNoStream(GetLastNetError());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-08-28 19:02:06 +00:00
|
|
|
this->osHandleOwner_->Init((int)this->osHandle_, (int)this->osHandle_);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2023-01-26 21:43:19 +00:00
|
|
|
void Socket::FinishConstructAsync()
|
|
|
|
{
|
|
|
|
if (this->resolveLater.size() || this->bResolving_)
|
|
|
|
{
|
|
|
|
if (!this->TryStartResolve())
|
|
|
|
{
|
|
|
|
this->SendErrorNoStream(GetLastNetError());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
RenewSocket();
|
|
|
|
}
|
|
|
|
|
2022-08-28 19:02:06 +00:00
|
|
|
bool Socket::PrepareConnectOperations()
|
|
|
|
{
|
2023-12-07 11:13:32 +00:00
|
|
|
if (psetsockopt)
|
|
|
|
{
|
|
|
|
// we'll soon find out if this truly failed later on...
|
|
|
|
(void)psetsockopt(this->osHandle_,
|
|
|
|
SOL_SOCKET,
|
|
|
|
SO_UPDATE_CONNECT_CONTEXT,
|
|
|
|
nullptr,
|
|
|
|
0);
|
|
|
|
}
|
2022-08-28 19:02:06 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Socket::UpdateNagleAnyThread(bool bDisableNagle)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Socket::UpdateLocalEndpoint()
|
|
|
|
{
|
|
|
|
int iLen { (int)this->endpointSize_ };
|
|
|
|
|
2023-12-07 11:13:32 +00:00
|
|
|
if (!pgetsockname)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pgetsockname(this->osHandle_, (sockaddr *)this->localEndpoint_.hint, &iLen) == -1)
|
2022-08-28 19:02:06 +00:00
|
|
|
{
|
|
|
|
SysPushErrorIO();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
DeoptimizeEndpoint(this->localEndpoint_);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Socket::UpdateRemoteEndpoint()
|
|
|
|
{
|
|
|
|
int iLen { (int)this->endpointSize_ };
|
|
|
|
|
2023-12-07 11:13:32 +00:00
|
|
|
if (!pgetpeername)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pgetpeername(this->osHandle_, (sockaddr *)this->remoteEndpoint_.hint, &iLen) == -1)
|
2022-08-28 19:02:06 +00:00
|
|
|
{
|
|
|
|
SysPushErrorIO();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
DeoptimizeEndpoint(this->remoteEndpoint_);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Socket::TryBindAnyLocal()
|
|
|
|
{
|
|
|
|
struct sockaddr_in addr {};
|
|
|
|
addr.sin_family = EIPProtocolIsValid(this->remoteEndpoint_.ip.ip) ?
|
|
|
|
IPToDomain(this->remoteEndpoint_) :
|
|
|
|
AF_UNSPEC;
|
|
|
|
addr.sin_addr.s_addr = INADDR_ANY;
|
|
|
|
addr.sin_port = 0;
|
2023-12-07 11:13:32 +00:00
|
|
|
if (!pbind)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return pbind(this->osHandle_,
|
|
|
|
(SOCKADDR *)&addr, sizeof(addr)) == 0;
|
2022-08-28 19:02:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Socket::ConnectOverlapped()
|
|
|
|
{
|
|
|
|
DWORD dwNumBytes {};
|
|
|
|
GUID guid = WSAID_CONNECTEX;
|
|
|
|
LPFN_CONNECTEX lpConnectEx {};
|
2023-12-07 11:13:32 +00:00
|
|
|
|
|
|
|
if (!pWSAIoctl)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2022-08-28 19:02:06 +00:00
|
|
|
|
2023-12-07 11:13:32 +00:00
|
|
|
if (pWSAIoctl(this->osHandle_,
|
2022-08-28 19:02:06 +00:00
|
|
|
SIO_GET_EXTENSION_FUNCTION_POINTER,
|
|
|
|
&guid,
|
|
|
|
sizeof(guid),
|
|
|
|
&lpConnectEx,
|
|
|
|
sizeof(lpConnectEx),
|
|
|
|
&dwNumBytes,
|
|
|
|
nullptr,
|
|
|
|
nullptr) != 0)
|
|
|
|
{
|
|
|
|
SysPushErrorNet("Couldn't yoink extension function pointer");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!TryBindAnyLocal())
|
|
|
|
{
|
|
|
|
SysPushErrorNet("Couldn't bind locally");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool bStatus = lpConnectEx(this->osHandle_,
|
|
|
|
AuReinterpretCast<sockaddr>(this->remoteEndpoint_.hint),
|
|
|
|
this->endpointSize_,
|
|
|
|
nullptr,
|
|
|
|
0,
|
|
|
|
nullptr,
|
|
|
|
&this->connectOperation.overlapped);
|
|
|
|
|
|
|
|
return this->connectOperation.FinishOperation(AuSharedFromThis(),
|
|
|
|
AuSPtr<INetWorker>(AuSharedFromThis(), this->ToWorker()),
|
|
|
|
bStatus);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Socket::ConnectNonblocking()
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Socket::ConnectBlocking()
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Socket::MakeNonblocking()
|
|
|
|
{
|
|
|
|
u_long iMode { 1 };
|
2023-12-07 11:13:32 +00:00
|
|
|
return pioctlsocket &&
|
|
|
|
pioctlsocket(this->osHandle_, FIONBIO, &iMode) == 0;
|
2022-08-28 19:02:06 +00:00
|
|
|
}
|
|
|
|
|
2022-11-18 04:15:05 +00:00
|
|
|
void Socket::Shutdown(bool bNow)
|
2022-08-28 19:02:06 +00:00
|
|
|
{
|
2022-11-18 04:15:05 +00:00
|
|
|
if (bNow)
|
|
|
|
{
|
|
|
|
this->SendEnd();
|
2023-12-07 11:13:32 +00:00
|
|
|
if (pshutdown)
|
|
|
|
{
|
|
|
|
pshutdown(this->osHandle_, SD_BOTH);
|
|
|
|
}
|
2022-11-18 04:15:05 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (!this->socketChannel_.outputChannel.AsWritableByteBuffer()->RemainingBytes())
|
|
|
|
{
|
|
|
|
this->Shutdown(true);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this->socketChannel_.outputChannel.bShutdownOnComplete = true;
|
|
|
|
this->socketChannel_.ScheduleOutOfFrameWrite();
|
|
|
|
}
|
|
|
|
}
|
2022-08-28 19:02:06 +00:00
|
|
|
}
|
|
|
|
}
|