Skip to content

Commit

Permalink
[Server] Now able to process more than one packet per tick.
Browse files Browse the repository at this point in the history
  • Loading branch information
Unarelith committed Oct 16, 2023
1 parent 91f3883 commit 6e045a9
Showing 1 changed file with 27 additions and 22 deletions.
49 changes: 27 additions & 22 deletions source/server/network/Server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ void Server::handleNewConnections() {
Network::Packet outPacket;
outPacket << Network::Command::ClientOk << client.id << m_isSingleplayer;
client.tcpSocket->send(outPacket);
// client.tcpSocket->setBlocking(false);
client.tcpSocket->setBlocking(false);

if (m_connectionCallback)
m_connectionCallback(client, packet);
Expand All @@ -108,31 +108,36 @@ void Server::handleClientMessages() {
ClientInfo &client = m_info.clients()[i];
if (m_selector.isReady(*client.tcpSocket)) {
Network::Packet packet;
sf::Socket::Status status = client.tcpSocket->receive(packet);
if (status == sf::Socket::Done) {
Network::Command command;
packet >> command;

// gkDebug() << "TCP message received:" << Network::commandToString(command);

if (m_isRunning) {
for (auto &it : m_commands) {
if (command == it.first)
it.second(client, packet);

if (command == Network::Command::ClientDisconnect) {
disconnectClient(client);
--i;

break;
sf::Socket::Status status;
int packetsProcessed = 0;
do {
status = client.tcpSocket->receive(packet);
if (status == sf::Socket::Done) {
Network::Command command;
packet >> command;

// gkDebug() << "TCP message received:" << Network::commandToString(command);

if (m_isRunning) {
for (auto &it : m_commands) {
if (command == it.first)
it.second(client, packet);

if (command == Network::Command::ClientDisconnect) {
disconnectClient(client);
--i;

break;
}
}
}
}
else if (status == sf::Socket::Disconnected) {
disconnectClient(client);
--i;
}
}
else if (status == sf::Socket::Disconnected) {
disconnectClient(client);
--i;
}
while ((status == sf::Socket::Done || status == sf::Socket::Disconnected) && packetsProcessed++ < 100);
}
}
}
Expand Down

0 comments on commit 6e045a9

Please sign in to comment.