1 #include "ClientConnection.hpp"
2 #include "ChunkTransmitter.hpp"
5 #include "../app/init.hpp"
6 #include "../io/WorldSave.hpp"
7 #include "../model/CompositeModel.hpp"
8 #include "../world/ChunkIndex.hpp"
9 #include "../world/Entity.hpp"
10 #include "../world/World.hpp"
14 #include <glm/gtx/io.hpp>
22 ChunkTransmitter::ChunkTransmitter(ClientConnection &conn)
25 , buffer_size(Chunk::BlockSize() + 10)
26 , buffer(new uint8_t[buffer_size])
28 , packet_len(Packet::ChunkData::MAX_DATA_LEN)
39 ChunkTransmitter::~ChunkTransmitter() {
43 bool ChunkTransmitter::Idle() const noexcept {
44 return !Transmitting() && !Waiting();
47 bool ChunkTransmitter::Transmitting() const noexcept {
48 return cursor < num_packets;
51 void ChunkTransmitter::Transmit() {
52 if (cursor < num_packets) {
58 bool ChunkTransmitter::Waiting() const noexcept {
59 return confirm_wait > 0;
62 void ChunkTransmitter::Ack(uint16_t seq) {
66 if (seq == begin_packet) {
74 for (int i = 0, end = data_packets.size(); i < end; ++i) {
75 if (seq == data_packets[i]) {
86 void ChunkTransmitter::Nack(uint16_t seq) {
90 if (seq == begin_packet) {
94 for (size_t i = 0, end = data_packets.size(); i < end; ++i) {
95 if (seq == data_packets[i]) {
102 void ChunkTransmitter::Abort() {
103 if (!current) return;
108 data_packets.clear();
112 void ChunkTransmitter::Send(Chunk &chunk) {
113 // abort current chunk, if any
119 // load new chunk data
121 buffer_len = buffer_size;
122 if (compress(buffer.get(), &buffer_len, reinterpret_cast<const Bytef *>(chunk.BlockData()), Chunk::BlockSize()) != Z_OK) {
123 // compression failed, send it uncompressed
124 buffer_len = Chunk::BlockSize();
125 memcpy(buffer.get(), chunk.BlockData(), buffer_len);
129 num_packets = (buffer_len / packet_len) + (buffer_len % packet_len != 0);
130 data_packets.resize(num_packets, -1);
136 void ChunkTransmitter::SendBegin() {
137 uint32_t flags = compressed;
138 auto pack = conn.Prepare<Packet::ChunkBegin>();
139 pack.WriteTransmissionId(trans_id);
140 pack.WriteFlags(flags);
141 pack.WriteChunkCoords(current->Position());
142 pack.WriteDataSize(buffer_len);
143 if (begin_packet == -1) {
146 begin_packet = conn.Send();
149 void ChunkTransmitter::SendData(size_t i) {
150 int pos = i * packet_len;
151 int len = min(packet_len, buffer_len - pos);
152 const uint8_t *data = &buffer[pos];
154 auto pack = conn.Prepare<Packet::ChunkData>();
155 pack.WriteTransmissionId(trans_id);
156 pack.WriteDataOffset(pos);
157 pack.WriteDataSize(len);
158 pack.WriteData(data, len);
160 if (data_packets[i] == -1) {
163 data_packets[i] = conn.Send();
166 void ChunkTransmitter::Release() {
174 ClientConnection::ClientConnection(Server &server, const IPaddress &addr)
178 , player_model(nullptr)
182 , player_update_state()
183 , player_update_pack(0)
184 , player_update_timer(1500)
189 conn.SetHandler(this);
192 ClientConnection::~ClientConnection() {
196 void ClientConnection::Update(int dt) {
198 if (Disconnected()) {
203 auto global_iter = server.GetWorld().Entities().begin();
204 auto global_end = server.GetWorld().Entities().end();
205 auto local_iter = spawns.begin();
206 auto local_end = spawns.end();
208 while (global_iter != global_end && local_iter != local_end) {
209 if (global_iter->ID() == local_iter->entity->ID()) {
211 if (CanDespawn(*global_iter)) {
212 SendDespawn(*local_iter);
215 QueueUpdate(*local_iter);
219 } else if (global_iter->ID() < local_iter->entity->ID()) {
220 // global entity was inserted
221 if (CanSpawn(*global_iter)) {
222 auto spawned = spawns.emplace(local_iter, *global_iter);
227 // global entity was removed
228 SendDespawn(*local_iter);
234 while (global_iter != global_end) {
235 if (CanSpawn(*global_iter)) {
236 spawns.emplace_back(*global_iter);
237 SendSpawn(spawns.back());
243 while (local_iter != local_end) {
244 SendDespawn(*local_iter);
253 if (conn.ShouldPing()) {
254 conn.SendPing(server.GetPacket(), server.GetSocket());
258 ClientConnection::SpawnStatus::SpawnStatus(Entity &e)
265 ClientConnection::SpawnStatus::~SpawnStatus() {
269 bool ClientConnection::CanSpawn(const Entity &e) const noexcept {
271 &e != &PlayerEntity() &&
273 manhattan_radius(e.ChunkCoords() - PlayerEntity().ChunkCoords()) < 7;
276 bool ClientConnection::CanDespawn(const Entity &e) const noexcept {
279 manhattan_radius(e.ChunkCoords() - PlayerEntity().ChunkCoords()) > 7;
282 uint16_t ClientConnection::Send() {
283 return conn.Send(server.GetPacket(), server.GetSocket());
286 uint16_t ClientConnection::Send(size_t len) {
287 server.GetPacket().len = sizeof(Packet::Header) + len;
291 void ClientConnection::SendSpawn(SpawnStatus &status) {
292 // don't double spawn
293 if (status.spawn_pack != -1) return;
295 auto pack = Prepare<Packet::SpawnEntity>();
296 pack.WriteEntity(*status.entity);
297 status.spawn_pack = Send();
301 void ClientConnection::SendDespawn(SpawnStatus &status) {
302 // don't double despawn
303 if (status.despawn_pack != -1) return;
305 auto pack = Prepare<Packet::DespawnEntity>();
306 pack.WriteEntityID(status.entity->ID());
307 status.despawn_pack = Send();
311 void ClientConnection::QueueUpdate(SpawnStatus &status) {
312 // don't send updates while spawn not ack'd or despawn sent
313 if (status.spawn_pack == -1 && status.despawn_pack == -1) {
314 entity_updates.push_back(&status);
318 void ClientConnection::SendUpdates() {
319 auto pack = Prepare<Packet::EntityUpdate>();
321 for (SpawnStatus *status : entity_updates) {
322 pack.WriteEntity(*status->entity, entity_pos);
324 if (entity_pos == Packet::EntityUpdate::MAX_ENTITIES) {
325 pack.WriteEntityCount(entity_pos);
326 Send(Packet::EntityUpdate::GetSize(entity_pos));
327 pack = Prepare<Packet::EntityUpdate>();
331 if (entity_pos > 0) {
332 pack.WriteEntityCount(entity_pos);
333 Send(Packet::EntityUpdate::GetSize(entity_pos));
335 entity_updates.clear();
338 void ClientConnection::CheckPlayerFix() {
339 // player_update_state's position holds the client's most recent prediction
340 glm::vec3 diff = player_update_state.Diff(PlayerEntity().GetState());
341 float dist_squared = dot(diff, diff);
343 // if client's prediction is off by more than 1cm, send
344 // our (authoritative) state back so it can fix it
345 constexpr float fix_thresh = 0.0001f;
347 if (dist_squared > fix_thresh) {
348 auto pack = Prepare<Packet::PlayerCorrection>();
349 pack.WritePacketSeq(player_update_pack);
350 pack.WritePlayer(PlayerEntity());
355 void ClientConnection::CheckChunkQueue() {
356 if (PlayerChunks().Base() != old_base) {
357 Chunk::Pos begin = PlayerChunks().CoordsBegin();
358 Chunk::Pos end = PlayerChunks().CoordsEnd();
359 for (Chunk::Pos pos = begin; pos.z < end.z; ++pos.z) {
360 for (pos.y = begin.y; pos.y < end.y; ++pos.y) {
361 for (pos.x = begin.x; pos.x < end.x; ++pos.x) {
362 if (manhattan_radius(pos - old_base) > PlayerChunks().Extent()) {
363 chunk_queue.push_back(pos);
368 old_base = PlayerChunks().Base();
370 if (transmitter.Transmitting()) {
371 transmitter.Transmit();
374 if (transmitter.Idle()) {
376 constexpr int max = 64;
377 while (count < max && !chunk_queue.empty()) {
378 Chunk::Pos pos = chunk_queue.front();
379 chunk_queue.pop_front();
380 if (PlayerChunks().InRange(pos)) {
381 Chunk *chunk = PlayerChunks().Get(pos);
383 transmitter.Send(*chunk);
386 chunk_queue.push_back(pos);
394 void ClientConnection::AttachPlayer(Player &player) {
396 input.reset(new DirectInput(server.GetWorld(), player, server));
397 PlayerEntity().Ref();
398 if (server.GetWorldSave().Exists(player)) {
399 server.GetWorldSave().Read(player);
402 old_base = PlayerChunks().Base();
403 Chunk::Pos begin = PlayerChunks().CoordsBegin();
404 Chunk::Pos end = PlayerChunks().CoordsEnd();
405 for (Chunk::Pos pos = begin; pos.z < end.z; ++pos.z) {
406 for (pos.y = begin.y; pos.y < end.y; ++pos.y) {
407 for (pos.x = begin.x; pos.x < end.x; ++pos.x) {
408 chunk_queue.push_back(pos);
412 if (HasPlayerModel()) {
413 GetPlayerModel().Instantiate(PlayerEntity().GetModel());
416 cout << "player \"" << player.Name() << "\" joined" << endl;
419 void ClientConnection::DetachPlayer() {
420 if (!HasPlayer()) return;
421 cout << "player \"" << input->GetPlayer().Name() << "\" left" << endl;
422 server.GetWorldSave().Write(input->GetPlayer());
423 PlayerEntity().Kill();
424 PlayerEntity().UnRef();
431 void ClientConnection::SetPlayerModel(const CompositeModel &m) noexcept {
434 m.Instantiate(PlayerEntity().GetModel());
438 bool ClientConnection::HasPlayerModel() const noexcept {
442 const CompositeModel &ClientConnection::GetPlayerModel() const noexcept {
443 return *player_model;
446 void ClientConnection::OnPacketReceived(uint16_t seq) {
447 if (transmitter.Waiting()) {
448 transmitter.Ack(seq);
450 if (!confirm_wait) return;
451 for (auto iter = spawns.begin(), end = spawns.end(); iter != end; ++iter) {
452 if (seq == iter->spawn_pack) {
453 iter->spawn_pack = -1;
457 if (seq == iter->despawn_pack) {
465 void ClientConnection::OnPacketLost(uint16_t seq) {
466 if (transmitter.Waiting()) {
467 transmitter.Nack(seq);
469 if (!confirm_wait) return;
470 for (SpawnStatus &status : spawns) {
471 if (seq == status.spawn_pack) {
472 status.spawn_pack = -1;
477 if (seq == status.despawn_pack) {
478 status.despawn_pack = -1;
486 void ClientConnection::On(const Packet::Login &pack) {
488 pack.ReadPlayerName(name);
490 Player *new_player = server.GetWorld().AddPlayer(name);
494 AttachPlayer(*new_player);
495 cout << "accepted login from player \"" << name << '"' << endl;
496 auto response = Prepare<Packet::Join>();
497 response.WritePlayer(new_player->GetEntity());
498 response.WriteWorldName(server.GetWorld().Name());
500 // set up update tracking
501 player_update_state = new_player->GetEntity().GetState();
502 player_update_pack = pack.Seq();
503 player_update_timer.Reset();
504 player_update_timer.Start();
507 cout << "rejected login from player \"" << name << '"' << endl;
508 Prepare<Packet::Part>();
514 void ClientConnection::On(const Packet::Part &) {
518 void ClientConnection::On(const Packet::PlayerUpdate &pack) {
519 if (!HasPlayer()) return;
520 int pack_diff = int16_t(pack.Seq()) - int16_t(player_update_pack);
521 bool overdue = player_update_timer.HitOnce();
522 player_update_timer.Reset();
523 if (pack_diff <= 0 && !overdue) {
524 // drop old packets if we have a fairly recent state
527 glm::vec3 movement(0.0f);
533 player_update_pack = pack.Seq();
534 pack.ReadPredictedState(player_update_state);
535 pack.ReadMovement(movement);
536 pack.ReadPitch(pitch);
538 pack.ReadActions(new_actions);
541 input->SetMovement(movement);
542 input->TurnHead(pitch - input->GetPitch(), yaw - input->GetYaw());
543 input->SelectInventory(slot);
545 if ((new_actions & 0x01) && !(old_actions & 0x01)) {
546 input->StartPrimaryAction();
547 } else if (!(new_actions & 0x01) && (old_actions & 0x01)) {
548 input->StopPrimaryAction();
550 if ((new_actions & 0x02) && !(old_actions & 0x02)) {
551 input->StartSecondaryAction();
552 } else if (!(new_actions & 0x02) && (old_actions & 0x02)) {
553 input->StopSecondaryAction();
555 if ((new_actions & 0x04) && !(old_actions & 0x04)) {
556 input->StartTertiaryAction();
557 } else if (!(new_actions & 0x04) && (old_actions & 0x04)) {
558 input->StopTertiaryAction();
560 old_actions = new_actions;
563 bool ClientConnection::ChunkInRange(const glm::ivec3 &pos) const noexcept {
564 return HasPlayer() && PlayerChunks().InRange(pos);
568 Server::Server(const Config::Network &conf, World &world, const WorldSave &save)
570 , serv_pack{ -1, nullptr, 0 }
574 , player_model(nullptr) {
575 serv_sock = SDLNet_UDP_Open(conf.port);
577 throw NetError("SDLNet_UDP_Open");
580 serv_pack.data = new Uint8[sizeof(Packet)];
581 serv_pack.maxlen = sizeof(Packet);
585 delete[] serv_pack.data;
586 SDLNet_UDP_Close(serv_sock);
590 void Server::Handle() {
591 int result = SDLNet_UDP_Recv(serv_sock, &serv_pack);
593 HandlePacket(serv_pack);
594 result = SDLNet_UDP_Recv(serv_sock, &serv_pack);
597 // a boo boo happened
598 throw NetError("SDLNet_UDP_Recv");
602 void Server::HandlePacket(const UDPpacket &udp_pack) {
603 if (udp_pack.len < int(sizeof(Packet::Header))) {
604 // packet too small, drop
607 const Packet &pack = *reinterpret_cast<const Packet *>(udp_pack.data);
608 if (pack.header.tag != Packet::TAG) {
609 // mistagged packet, drop
613 ClientConnection &client = GetClient(udp_pack.address);
614 client.GetConnection().Received(udp_pack);
617 ClientConnection &Server::GetClient(const IPaddress &addr) {
618 for (ClientConnection &client : clients) {
619 if (client.Matches(addr)) {
623 clients.emplace_back(*this, addr);
624 if (HasPlayerModel()) {
625 clients.back().SetPlayerModel(GetPlayerModel());
627 return clients.back();
630 void Server::Update(int dt) {
631 for (list<ClientConnection>::iterator client(clients.begin()), end(clients.end()); client != end;) {
633 if (client->Disconnected()) {
634 client = clients.erase(client);
641 void Server::SetPlayerModel(const CompositeModel &m) noexcept {
643 for (ClientConnection &client : clients) {
644 client.SetPlayerModel(m);
648 bool Server::HasPlayerModel() const noexcept {
652 const CompositeModel &Server::GetPlayerModel() const noexcept {
653 return *player_model;
656 void Server::SetBlock(Chunk &chunk, int index, const Block &block) {
657 chunk.SetBlock(index, block);
658 // TODO: batch chunk changes
659 auto pack = Packet::Make<Packet::BlockUpdate>(GetPacket());
660 pack.WriteChunkCoords(chunk.Position());
661 pack.WriteBlockCount(uint32_t(1));
662 pack.WriteIndex(index, 0);
663 pack.WriteBlock(chunk.BlockAt(index), 0);
664 GetPacket().len = sizeof(Packet::Header) + Packet::BlockUpdate::GetSize(1);
665 for (ClientConnection &client : clients) {
666 if (client.ChunkInRange(chunk.Position())) {