Skip to content

Commit

Permalink
netplay: extract NETacceptIncomingConnections() function
Browse files Browse the repository at this point in the history
This is a host-only routine to handle incoming client
request while actively hosting (`allow_joining == true`).

Signed-off-by: Pavel Solodovnikov <pavel.al.solodovnikov@gmail.com>
  • Loading branch information
ManManson authored and past-due committed Jun 19, 2024
1 parent 5134fb5 commit fb57881
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 58 deletions.
137 changes: 79 additions & 58 deletions lib/netplay/netplay.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2954,6 +2954,7 @@ bool NETrecvNet(NETQUEUE *queue, uint8_t *type)
if (NetPlay.isHost)
{
NETfixPlayerCount();
NETacceptIncomingConnections();
NETallowJoining();
lobbyConnectionHandler.run();
NETcheckPlayers(); // make sure players are still alive & well
Expand Down Expand Up @@ -3943,64 +3944,6 @@ static void NETallowJoining()
NetPlay.ShowedMOTD = true;
}

if (tmp_socket_set == nullptr)
{
// initialize server socket set
// FIXME: why is this not done in NETinit()?? - Per
tmp_socket_set = allocSocketSet();
if (tmp_socket_set == nullptr)
{
debug(LOG_ERROR, "Cannot create socket set: %s", strSockError(getSockErr()));
return;
}
// FIXME: I guess initialization of allowjoining is here now... - FlexCoral
for (auto& tmpState : tmp_connectState)
{
tmpState.reset();
}
tmp_pendingIPs.clear();
// NOTE: Do *NOT* call tmp_badIPs.clear() here - we want to preserve it until quit
}

// Find the first empty socket slot
for (i = 0; i < MAX_TMP_SOCKETS; ++i)
{
if (tmp_socket[i] == nullptr)
{
break;
}
}
if (i == MAX_TMP_SOCKETS)
{
debug(LOG_NET, "all temp sockets are currently used up!");
}

// See if there's an incoming connection (if we have space to handle it!)
if (i < MAX_TMP_SOCKETS && tmp_socket[i] == nullptr // Make sure that we're not out of sockets
&& (tmp_socket[i] = socketAccept(server_listen_socket)) != nullptr)
{
NETinitQueue(NETnetTmpQueue(i));
SocketSet_AddSocket(*tmp_socket_set, tmp_socket[i]);

std::string rIP = getSocketTextAddress(*tmp_socket[i]);
connectFailed = quickRejectConnection(rIP);
tmp_pendingIPs[rIP]++;

std::string rIPLogEntry = "Incoming connection from:";
rIPLogEntry.append(rIP);
NETlogEntry(rIPLogEntry.c_str(), SYNC_FLAG, i);

tmp_connectState[i].ip = rIP;
tmp_connectState[i].connectTime = std::chrono::steady_clock::now();
tmp_connectState[i].connectState = TmpSocketInfo::TmpConnectState::PendingInitialConnect;

if (bEnableTCPNoDelay)
{
// Enable TCP_NODELAY
socketSetTCPNoDelay(*tmp_socket[i], true);
}
}

if (checkSockets(*tmp_socket_set, NET_READ_TIMEOUT) > 0)
{
for (i = 0; i < MAX_TMP_SOCKETS; ++i)
Expand Down Expand Up @@ -5181,3 +5124,81 @@ const char *messageTypeToString(unsigned messageType_)
}
return "(UNUSED)";
}


void NETacceptIncomingConnections()
{
ASSERT_HOST_ONLY(return);
if (!allow_joining)
{
return;
}

size_t i = 0;
// Find the first empty socket slot
for (; i < MAX_TMP_SOCKETS; ++i)
{
if (tmp_socket[i] == nullptr)
{
break;
}
}
if (i == MAX_TMP_SOCKETS)
{
debug(LOG_NET, "all temp sockets are currently used up!");
return;
}

// See if there's an incoming connection
tmp_socket[i] = socketAccept(server_listen_socket);
if (!tmp_socket[i])
{
return;
}
const std::string rIP = getSocketTextAddress(*tmp_socket[i]);
if (quickRejectConnection(rIP))
{
debug(LOG_NET, "freeing temp socket %p (%d)", static_cast<void*>(tmp_socket[i]), __LINE__);
NETcloseTempSocket(i);
return;
}

// First-time initialize `tmp_socket_set` if needed.
if (tmp_socket_set == nullptr)
{
// initialize temporary server socket set
// FIXME: why is this not done in NETinit()?? - Per
tmp_socket_set = allocSocketSet();
if (tmp_socket_set == nullptr)
{
debug(LOG_ERROR, "Cannot create socket set: %s", strSockError(getSockErr()));
return;
}
// FIXME: I guess initialization of allowjoining is here now... - FlexCoral
for (auto& tmpState : tmp_connectState)
{
tmpState.reset();
}
tmp_pendingIPs.clear();
// NOTE: Do *NOT* call tmp_badIPs.clear() here - we want to preserve it until quit
}

NETinitQueue(NETnetTmpQueue(i));
SocketSet_AddSocket(*tmp_socket_set, tmp_socket[i]);

tmp_pendingIPs[rIP]++;

std::string rIPLogEntry = "Incoming connection from:";
rIPLogEntry.append(rIP);
NETlogEntry(rIPLogEntry.c_str(), SYNC_FLAG, i);

tmp_connectState[i].ip = rIP;
tmp_connectState[i].connectTime = std::chrono::steady_clock::now();
tmp_connectState[i].connectState = TmpSocketInfo::TmpConnectState::PendingInitialConnect;

if (bEnableTCPNoDelay)
{
// Enable TCP_NODELAY
socketSetTCPNoDelay(*tmp_socket[i], true);
}
}
2 changes: 2 additions & 0 deletions lib/netplay/netplay.h
Original file line number Diff line number Diff line change
Expand Up @@ -540,4 +540,6 @@ struct PlayerReference
uint32_t index;
};

void NETacceptIncomingConnections();

#endif

0 comments on commit fb57881

Please sign in to comment.