2022-08-28 19:02:06 +00:00
|
|
|
/***
|
|
|
|
Copyright (C) 2022 J Reece Wilson (a/k/a "Reece"). All rights reserved.
|
|
|
|
|
|
|
|
File: AuNetSocketServer.Linux.cpp
|
|
|
|
Date: 2022-8-26
|
|
|
|
Author: Reece
|
|
|
|
***/
|
|
|
|
#include "Networking.hpp"
|
|
|
|
#include "AuNetSocketServer.hpp"
|
|
|
|
#include "AuNetError.hpp"
|
|
|
|
#include "AuNetEndpoint.hpp"
|
|
|
|
|
2023-08-19 14:05:14 +00:00
|
|
|
#if !defined(SOCK_CLOEXEC)
|
|
|
|
#define SOCK_CLOEXEC 0
|
|
|
|
#endif
|
|
|
|
|
2022-08-28 19:02:06 +00:00
|
|
|
namespace Aurora::IO::Net
|
|
|
|
{
|
|
|
|
SocketServerImpl::SocketServerImpl(NetInterface *pInterface,
|
|
|
|
NetWorker *pWorker,
|
|
|
|
const AuSPtr<ISocketServerDriver> &pDriver,
|
|
|
|
const AuSPtr<ISocketDriverFactory> &pSocketDriverFactory,
|
2022-12-16 17:18:12 +00:00
|
|
|
AuUInt32 uMaxConnections,
|
|
|
|
AuUInt32 uDefaultInputStreamSize,
|
2022-12-16 00:41:01 +00:00
|
|
|
bool bMultiThreaded) :
|
2022-08-28 19:02:06 +00:00
|
|
|
SocketServer(pInterface,
|
|
|
|
pWorker,
|
|
|
|
pDriver,
|
|
|
|
pSocketDriverFactory,
|
2022-12-16 17:18:12 +00:00
|
|
|
uMaxConnections,
|
|
|
|
uDefaultInputStreamSize,
|
2022-12-16 00:41:01 +00:00
|
|
|
bMultiThreaded),
|
2022-08-28 19:02:06 +00:00
|
|
|
Socket(pInterface,
|
|
|
|
pWorker,
|
|
|
|
AuSPtr<ISocketDriver>{},
|
|
|
|
-1),
|
|
|
|
acceptOperation_(pInterface, this),
|
|
|
|
pSocketDriverFactory_(pSocketDriverFactory)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void SocketServerImpl::DoNonblockingReadTick()
|
|
|
|
{
|
|
|
|
if (!this->acceptOperation_.DoTick())
|
|
|
|
{
|
|
|
|
SysPushErrorIO("Accept tick failed");
|
|
|
|
this->SendFinalize();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SocketServerImpl::InitSocket(const NetEndpoint &localAddress)
|
|
|
|
{
|
|
|
|
this->pSocketDriver_ = AuDynamicCast<SocketServerImpl>(this->SharedFromThis());
|
|
|
|
this->localEndpoint_ = localAddress;
|
|
|
|
|
|
|
|
if (!this->SendPreestablish())
|
|
|
|
{
|
|
|
|
SysPushErrorIO("Preestablish drop");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-08-19 14:05:14 +00:00
|
|
|
bool bMakeCloseExec { !bool(SOCK_CLOEXEC) };
|
2022-08-28 19:02:06 +00:00
|
|
|
this->osHandle_ = ::socket(
|
|
|
|
IPToDomain(localAddress),
|
2023-08-19 14:05:14 +00:00
|
|
|
TransportToPlatformType(localAddress) | SOCK_CLOEXEC,
|
2022-08-28 19:02:06 +00:00
|
|
|
0
|
|
|
|
);
|
|
|
|
|
2023-08-19 14:05:14 +00:00
|
|
|
if (this->osHandle_ == -1)
|
|
|
|
{
|
|
|
|
this->osHandle_ = ::socket(
|
|
|
|
IPToDomain(localAddress),
|
|
|
|
TransportToPlatformType(localAddress),
|
|
|
|
0
|
|
|
|
);
|
|
|
|
|
|
|
|
bMakeCloseExec = true;
|
|
|
|
}
|
|
|
|
|
2022-08-28 19:02:06 +00:00
|
|
|
if (this->osHandle_ == -1)
|
|
|
|
{
|
|
|
|
NetError error;
|
|
|
|
NetError_SetCurrent(error);
|
|
|
|
this->SendErrorNoStream(error);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-08-19 14:05:14 +00:00
|
|
|
if (bMakeCloseExec)
|
|
|
|
{
|
|
|
|
this->MakeCloseonexec();
|
|
|
|
}
|
|
|
|
|
|
|
|
return this->MakeNonblocking();
|
2022-08-28 19:02:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool SocketServerImpl::ImplBind()
|
|
|
|
{
|
|
|
|
if (::bind(this->osHandle_,
|
|
|
|
(sockaddr *)this->localEndpoint_.hint, this->endpointSize_) != 0)
|
|
|
|
{
|
|
|
|
NetError error;
|
|
|
|
NetError_SetCurrent(error);
|
|
|
|
this->SendErrorNoStream(error);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SocketServerImpl::ImplListen()
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
Picking 512 because....
|
|
|
|
|
|
|
|
"The maximum length of the queue of pending connections. If set to SOMAXCONN, the underlying service provider responsible for socket s
|
|
|
|
will set the backlog to a maximum reasonable value. If set to SOMAXCONN_HINT(N) (where N is a number), the backlog value will be N,
|
|
|
|
adjusted to be within the range (200, 65535). Note that SOMAXCONN_HINT can be used to set the backlog to a larger value than possible
|
|
|
|
with SOMAXCONN." - MSDN
|
|
|
|
"the backlog value will be N, adjusted to be within the range (200, 65535)." - MSDN
|
|
|
|
|
|
|
|
"If the backlog argument is greater than the value in /proc/sys/net/core/somaxconn, then it is silently capped to that value.
|
|
|
|
Since Linux 5.4, the default in this file is 4096" - da man pages
|
|
|
|
|
|
|
|
We need a sane number between the two. 512 seems reasonable
|
|
|
|
*/
|
|
|
|
if (::listen(this->osHandle_, 512) != 0)
|
|
|
|
{
|
|
|
|
NetError error;
|
|
|
|
NetError_SetCurrent(error);
|
|
|
|
this->SendErrorNoStream(error);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SocketServerImpl::BeginAcceptLoop()
|
|
|
|
{
|
|
|
|
return this->acceptOperation_.InitOnce();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SocketServerImpl::DetroyServer()
|
|
|
|
{
|
|
|
|
this->acceptOperation_.Destroy();
|
|
|
|
}
|
|
|
|
}
|