#include "Server.hpp"
#include "../app/init.hpp"
+#include "../geometry/distance.hpp"
#include "../io/WorldSave.hpp"
#include "../model/Model.hpp"
#include "../world/ChunkIndex.hpp"
if (data_packets[i] == -1) {
++confirm_wait;
}
- data_packets[i] = conn.Send();
+ data_packets[i] = conn.Send(Packet::ChunkData::GetSize(len));
}
void ChunkTransmitter::Release() {
, spawns()
, confirm_wait(0)
, entity_updates()
+, entity_updates_skipped(0)
, player_update_state()
, player_update_pack(0)
, player_update_timer(1500)
, old_actions(0)
, transmitter(*this)
, chunk_queue()
-, old_base() {
+, old_base()
+, chunk_blocks_skipped(0) {
conn.SetHandler(this);
}
return;
}
if (HasPlayer()) {
- // sync entities
- auto global_iter = server.GetWorld().Entities().begin();
- auto global_end = server.GetWorld().Entities().end();
- auto local_iter = spawns.begin();
- auto local_end = spawns.end();
-
- while (global_iter != global_end && local_iter != local_end) {
- if (global_iter->ID() == local_iter->entity->ID()) {
- // they're the same
- if (CanDespawn(*global_iter)) {
- SendDespawn(*local_iter);
- } else {
- // update
- QueueUpdate(*local_iter);
- }
- ++global_iter;
- ++local_iter;
- } else if (global_iter->ID() < local_iter->entity->ID()) {
- // global entity was inserted
- if (CanSpawn(*global_iter)) {
- auto spawned = spawns.emplace(local_iter, *global_iter);
- SendSpawn(*spawned);
- }
- ++global_iter;
- } else {
- // global entity was removed
+ CheckPlayerFix();
+ CheckChunkQueue();
+ CheckEntities();
+ SendUpdates();
+ }
+ if (conn.ShouldPing()) {
+ conn.SendPing(server.GetPacket(), server.GetSocket());
+ }
+}
+
+void ClientConnection::CheckEntities() {
+ auto global_iter = server.GetWorld().Entities().begin();
+ auto global_end = server.GetWorld().Entities().end();
+ auto local_iter = spawns.begin();
+ auto local_end = spawns.end();
+
+ while (global_iter != global_end && local_iter != local_end) {
+ if (global_iter->ID() == local_iter->entity->ID()) {
+ // they're the same
+ if (CanDespawn(*global_iter)) {
SendDespawn(*local_iter);
- ++local_iter;
+ } else if (SendingUpdates()) {
+ // update
+ QueueUpdate(*local_iter);
}
- }
-
- // leftover spawns
- while (global_iter != global_end) {
+ ++global_iter;
+ ++local_iter;
+ } else if (global_iter->ID() < local_iter->entity->ID()) {
+ // global entity was inserted
if (CanSpawn(*global_iter)) {
- spawns.emplace_back(*global_iter);
- SendSpawn(spawns.back());
+ auto spawned = spawns.emplace(local_iter, *global_iter);
+ SendSpawn(*spawned);
}
++global_iter;
- }
-
- // leftover despawns
- while (local_iter != local_end) {
+ } else {
+ // global entity was removed
SendDespawn(*local_iter);
++local_iter;
}
- SendUpdates();
+ }
- CheckPlayerFix();
- CheckChunkQueue();
+ // leftover spawns
+ while (global_iter != global_end) {
+ if (CanSpawn(*global_iter)) {
+ spawns.emplace_back(*global_iter);
+ SendSpawn(spawns.back());
+ }
+ ++global_iter;
}
- if (conn.ShouldPing()) {
- conn.SendPing(server.GetPacket(), server.GetSocket());
+
+ // leftover despawns
+ while (local_iter != local_end) {
+ SendDespawn(*local_iter);
+ ++local_iter;
}
}
++confirm_wait;
}
+bool ClientConnection::SendingUpdates() const noexcept {
+ return entity_updates_skipped >= NetStat().SuggestedPacketHold();
+}
+
void ClientConnection::QueueUpdate(SpawnStatus &status) {
// don't send updates while spawn not ack'd or despawn sent
if (status.spawn_pack == -1 && status.despawn_pack == -1) {
}
void ClientConnection::SendUpdates() {
+ if (!SendingUpdates()) {
+ entity_updates.clear();
+ ++entity_updates_skipped;
+ return;
+ }
auto base = PlayerChunks().Base();
auto pack = Prepare<Packet::EntityUpdate>();
pack.WriteChunkBase(base);
Send(Packet::EntityUpdate::GetSize(entity_pos));
}
entity_updates.clear();
+ entity_updates_skipped = 0;
}
void ClientConnection::CheckPlayerFix() {
void ClientConnection::CheckChunkQueue() {
if (PlayerChunks().Base() != old_base) {
- Chunk::Pos begin = PlayerChunks().CoordsBegin();
- Chunk::Pos end = PlayerChunks().CoordsEnd();
- for (Chunk::Pos pos = begin; pos.z < end.z; ++pos.z) {
+ ExactLocation::Coarse begin = PlayerChunks().CoordsBegin();
+ ExactLocation::Coarse end = PlayerChunks().CoordsEnd();
+ for (ExactLocation::Coarse pos = begin; pos.z < end.z; ++pos.z) {
for (pos.y = begin.y; pos.y < end.y; ++pos.y) {
for (pos.x = begin.x; pos.x < end.x; ++pos.x) {
if (manhattan_radius(pos - old_base) > PlayerChunks().Extent()) {
old_base = PlayerChunks().Base();
sort(chunk_queue.begin(), chunk_queue.end(), QueueCompare(old_base));
}
+ // don't push entity updates and chunk data in the same tick
+ if (chunk_blocks_skipped >= NetStat().SuggestedPacketHold() && !SendingUpdates()) {
+ ++chunk_blocks_skipped;
+ return;
+ }
if (transmitter.Transmitting()) {
transmitter.Transmit();
+ chunk_blocks_skipped = 0;
return;
}
if (transmitter.Idle()) {
int count = 0;
constexpr int max = 64;
while (count < max && !chunk_queue.empty()) {
- Chunk::Pos pos = chunk_queue.front();
+ ExactLocation::Coarse pos = chunk_queue.front();
chunk_queue.pop_front();
if (PlayerChunks().InRange(pos)) {
Chunk *chunk = PlayerChunks().Get(pos);
if (chunk) {
transmitter.Send(*chunk);
+ chunk_blocks_skipped = 0;
return;
} else {
chunk_queue.push_back(pos);
PlayerEntity().Ref();
old_base = PlayerChunks().Base();
- Chunk::Pos begin = PlayerChunks().CoordsBegin();
- Chunk::Pos end = PlayerChunks().CoordsEnd();
- for (Chunk::Pos pos = begin; pos.z < end.z; ++pos.z) {
+ ExactLocation::Coarse begin = PlayerChunks().CoordsBegin();
+ ExactLocation::Coarse end = PlayerChunks().CoordsEnd();
+ for (ExactLocation::Coarse pos = begin; pos.z < end.z; ++pos.z) {
for (pos.y = begin.y; pos.y < end.y; ++pos.y) {
for (pos.x = begin.x; pos.x < end.x; ++pos.x) {
chunk_queue.push_back(pos);
pack.ReadActions(new_actions);
pack.ReadSlot(slot);
+ // accept client's orientation as is
+ input->GetPlayer().GetEntity().Orientation(player_update_state.orient);
+ // simulate movement
input->SetMovement(movement);
+ // rotate head to match client's "prediction"
input->TurnHead(player_update_state.pitch - input->GetPitch(), player_update_state.yaw - input->GetYaw());
+ // select the given inventory slot
input->SelectInventory(slot);
+ // check if any actions have been started or stopped
if ((new_actions & 0x01) && !(old_actions & 0x01)) {
input->StartPrimaryAction();
} else if (!(new_actions & 0x01) && (old_actions & 0x01)) {
pack.ReadMessage(msg);
if (type == 1 && HasPlayer()) {
- server.DistributeMessage(1, PlayerEntity().ID(), msg);
+ server.DispatchMessage(input->GetPlayer(), msg);
}
}
const WorldSave &save)
: serv_sock(nullptr)
, serv_pack{ -1, nullptr, 0 }
+, serv_set(SDLNet_AllocSocketSet(1))
, clients()
, world(world)
, spawn_index(world.Chunks().MakeIndex(wc.spawn, 3))
, save(save)
-, player_model(nullptr) {
+, player_model(nullptr)
+, cli(world) {
+ if (!serv_set) {
+ throw NetError("SDLNet_AllocSocketSet");
+ }
+
serv_sock = SDLNet_UDP_Open(conf.port);
if (!serv_sock) {
+ SDLNet_FreeSocketSet(serv_set);
throw NetError("SDLNet_UDP_Open");
}
+ if (SDLNet_UDP_AddSocket(serv_set, serv_sock) == -1) {
+ SDLNet_UDP_Close(serv_sock);
+ SDLNet_FreeSocketSet(serv_set);
+ throw NetError("SDLNet_UDP_AddSocket");
+ }
+
serv_pack.data = new Uint8[sizeof(Packet)];
serv_pack.maxlen = sizeof(Packet);
}
Server::~Server() {
+ for (ClientConnection &client : clients) {
+ client.Disconnected();
+ }
+ clients.clear();
world.Chunks().UnregisterIndex(spawn_index);
delete[] serv_pack.data;
+ SDLNet_UDP_DelSocket(serv_set, serv_sock);
SDLNet_UDP_Close(serv_sock);
+ SDLNet_FreeSocketSet(serv_set);
+}
+
+
+void Server::Wait(int dt) noexcept {
+ SDLNet_CheckSockets(serv_set, dt);
}
+bool Server::Ready() noexcept {
+ return SDLNet_CheckSockets(serv_set, 0) > 0;
+}
void Server::Handle() {
int result = SDLNet_UDP_Recv(serv_sock, &serv_pack);
}
}
+void Server::DispatchMessage(Player &player, const string &msg) {
+ if (msg.empty()) {
+ return;
+ }
+ if (msg[0] == '/' && msg.size() > 1 && msg[1] != '/') {
+ cli.Execute(player, msg.substr(1));
+ } else {
+ DistributeMessage(1, player.GetEntity().ID(), msg);
+ }
+}
+
void Server::DistributeMessage(uint8_t type, uint32_t ref, const string &msg) {
auto pack = Packet::Make<Packet::Message>(serv_pack);
pack.WriteType(type);