]> git.localhorst.tv Git - blank.git/commitdiff
move client update throttling to state
authorDaniel Karbach <daniel.karbach@localhorst.tv>
Mon, 14 Sep 2015 10:49:39 +0000 (12:49 +0200)
committerDaniel Karbach <daniel.karbach@localhorst.tv>
Mon, 14 Sep 2015 10:49:39 +0000 (12:49 +0200)
src/client/InteractiveState.hpp
src/client/client.cpp
src/net/Client.hpp
src/net/net.cpp

index d36472d4855a2cd34feddf76174966589cd50910..9b1ce52236309d23751286b037e2d80ead35701f 100644 (file)
@@ -1,6 +1,7 @@
 #ifndef BLANK_CLIENT_INTERACTIVESTATE_HPP_
 #define BLANK_CLIENT_INTERACTIVESTATE_HPP_
 
+#include "../app/IntervalTimer.hpp"
 #include "../app/State.hpp"
 #include "../io/WorldSave.hpp"
 #include "../model/Skeletons.hpp"
@@ -42,6 +43,7 @@ private:
        Interface interface;
        ChunkRenderer chunk_renderer;
        Skeletons skeletons;
+       IntervalTimer update_timer;
 
 };
 
index 757bc3a265ab6cfd883a9f9fbdfc45020c73ae80..bd436358b9af5163766121bf07981f7d5fdc60a9 100644 (file)
@@ -55,7 +55,8 @@ InteractiveState::InteractiveState(MasterState &master, uint32_t player_id)
        world.AddPlayer(master.GetInterfaceConf().player_name, player_id)
 )
 , chunk_renderer(*interface.GetPlayer().chunks)
-, skeletons() {
+, skeletons()
+, update_timer(16) {
        TextureIndex tex_index;
        master.GetEnv().loader.LoadBlockTypes("default", block_types, tex_index);
        chunk_renderer.LoadTextures(master.GetEnv().loader, tex_index);
@@ -63,6 +64,7 @@ InteractiveState::InteractiveState(MasterState &master, uint32_t player_id)
        skeletons.Load();
        // TODO: better solution for initializing HUD
        interface.SelectNext();
+       update_timer.Start();
 }
 
 void InteractiveState::OnEnter() {
@@ -104,9 +106,13 @@ void InteractiveState::Update(int dt) {
        world.Update(dt);
        chunk_renderer.Update(dt);
 
+       update_timer.Update(dt);
+
        Entity &player = *interface.GetPlayer().entity;
 
-       master.GetClient().SendPlayerUpdate(player);
+       if (update_timer.Hit()) {
+               master.GetClient().SendPlayerUpdate(player);
+       }
 
        glm::mat4 trans = player.Transform(player.ChunkCoords());
        glm::vec3 dir(trans * glm::vec4(0.0f, 0.0f, -1.0f, 0.0f));
index ee848c0d74729db9976cf382ccfe8ad1d83b512d..2848aedb342a37ebcac5ebd8c7052089c0696397 100644 (file)
@@ -2,7 +2,6 @@
 #define BLANK_NET_CLIENT_HPP_
 
 #include "Connection.hpp"
-#include "../app/IntervalTimer.hpp"
 
 #include <string>
 #include <SDL_net.h>
@@ -34,8 +33,7 @@ public:
        std::uint16_t SendPing();
        std::uint16_t SendLogin(const std::string &);
        std::uint16_t SendPart();
-       // this may not send the update at all, in which case it returns -1
-       int SendPlayerUpdate(const Entity &);
+       std::uint16_t SendPlayerUpdate(const Entity &);
 
 private:
        void HandlePacket(const UDPpacket &);
@@ -44,7 +42,6 @@ private:
        Connection conn;
        UDPsocket client_sock;
        UDPpacket client_pack;
-       IntervalTimer update_timer;
 
 };
 
index 4e6a63a9ad07a31330e004de4171f813aa848c40..18abb9250a7912b264829060cc11bdafba725551 100644 (file)
@@ -51,13 +51,11 @@ IPaddress client_resolve(const char *host, Uint16 port) {
 Client::Client(const Config &conf)
 : conn(client_resolve(conf.host.c_str(), conf.port))
 , client_sock(client_bind(0))
-, client_pack{ -1, nullptr, 0 }
-, update_timer(16) {
+, client_pack{ -1, nullptr, 0 } {
        client_pack.data = new Uint8[sizeof(Packet)];
        client_pack.maxlen = sizeof(Packet);
        // establish connection
        SendPing();
-       update_timer.Start();
 }
 
 Client::~Client() {
@@ -93,7 +91,6 @@ void Client::HandlePacket(const UDPpacket &udp_pack) {
 }
 
 void Client::Update(int dt) {
-       update_timer.Update(dt);
        conn.Update(dt);
        if (conn.ShouldPing()) {
                SendPing();
@@ -110,9 +107,7 @@ uint16_t Client::SendLogin(const string &name) {
        return conn.Send(client_pack, client_sock);
 }
 
-int Client::SendPlayerUpdate(const Entity &player) {
-       // don't send all too many updates
-       if (!update_timer.Hit()) return -1;
+uint16_t Client::SendPlayerUpdate(const Entity &player) {
        auto pack = Packet::Make<Packet::PlayerUpdate>(client_pack);
        pack.WritePlayer(player);
        return conn.Send(client_pack, client_sock);