Add basic unconditional KeepAlive

This commit is contained in:
DankParrot 2020-08-05 18:25:36 -07:00
parent a5f37c33a6
commit 9c87657e2c
2 changed files with 53 additions and 0 deletions

View File

@ -3,6 +3,8 @@
#include "PacketReader.h"
#include <chrono>
namespace Feather
{
DedicatedServer::DedicatedServer(ServerProperties* properties) :
@ -30,6 +32,32 @@ namespace Feather
}
data.clear();
}
// TODO: Move to tick thread !!
{
using namespace Protocol;
using namespace std::chrono;
// TODO: high_resolution_clock on mingw is innacurate and slow
static auto lastKeepAlive = high_resolution_clock::now();
auto now = high_resolution_clock::now();
milliseconds td = duration_cast<milliseconds>(now - lastKeepAlive);
if (td >= 1000ms) {
lastKeepAlive = now;
Play::ClientboundKeepAlive keepAlive =
{
// MC probably sends time since epoch
.id = duration_cast<milliseconds>(now.time_since_epoch()).count(),
};
for (auto &client : clients)
{
client.SendMessage(keepAlive);
}
}
}
}
}
@ -166,4 +194,10 @@ namespace Feather
client.SendMessage(playerPos);
}
template <>
void DedicatedServer::HandlePacket(MinecraftClient& client, const Play::ServerboundKeepAlive& keepAlive)
{
// TODO: check these and kick clients that don't send em right or at all
}
}

View File

@ -137,6 +137,17 @@
play :
{
serverbound:
{
KeepAlive:
{
id: 0x0F
vars: {
id: int64
}
}
}
clientbound :
{
JoinGame :
@ -155,6 +166,14 @@
enableRespawnScreen : bool
}
}
KeepAlive:
{
id: 0x21
vars: {
id: int64
}
}
PlayerPositionAndLook:
{