X-Git-Url: http://git.localhorst.tv/?a=blobdiff_plain;f=src%2Fnet%2Fnet.cpp;h=4de800e2429eb4c1c65e4a87ef0ec17567692058;hb=a957788426ff011acf662e29ec5fc0525c1a578f;hp=86098f4d4a3e087c6b16f03e0546df6f45bfbf59;hpb=7c5e04a4af82947ae8e26db3d03f13a555c10aba;p=blank.git diff --git a/src/net/net.cpp b/src/net/net.cpp index 86098f4..4de800e 100644 --- a/src/net/net.cpp +++ b/src/net/net.cpp @@ -1,3 +1,4 @@ +#include "CongestionControl.hpp" #include "Connection.hpp" #include "ConnectionHandler.hpp" #include "io.hpp" @@ -30,6 +31,102 @@ constexpr size_t Packet::BlockUpdate::MAX_LEN; constexpr size_t Packet::Message::MAX_LEN; constexpr size_t Packet::Message::MAX_MESSAGE_LEN; + +CongestionControl::CongestionControl() +// I know, I know, it's an estimate (about 20 for IPv4, 48 for IPv6) +: packet_overhead(20) +// only sample every eighth packet for measuring RTT +, sample_skip(8) +, packets_lost(0) +, packets_received(0) +, packet_loss(0.0f) +, stamp_cursor(15) +, stamp_last(0) +, rtt(64.0f) +, next_sample(1000) +, tx_bytes(0) +, rx_bytes(0) +, tx_kbps(0.0f) +, rx_kbps(0.0f) { + Uint32 now = SDL_GetTicks(); + for (Uint32 &s : stamps) { + s = now; + } + next_sample += now; +} + +void CongestionControl::PacketSent(uint16_t seq) noexcept { + if (!SamplePacket(seq)) { + return; + } + stamp_cursor = (stamp_cursor + 1) % 16; + stamps[stamp_cursor] = SDL_GetTicks(); + stamp_last = seq; +} + +void CongestionControl::PacketLost(uint16_t seq) noexcept { + ++packets_lost; + UpdatePacketLoss(); + UpdateRTT(seq); +} + +void CongestionControl::PacketReceived(uint16_t seq) noexcept { + ++packets_received; + UpdatePacketLoss(); + UpdateRTT(seq); +} + +void CongestionControl::UpdatePacketLoss() noexcept { + unsigned int packets_total = packets_lost + packets_received; + if (packets_total >= 256) { + packet_loss = float(packets_lost) / float(packets_total); + packets_lost = 0; + packets_received = 0; + } +} + +void CongestionControl::UpdateRTT(std::uint16_t seq) noexcept { + if (!SamplePacket(seq)) return; + int diff = HeadDiff(seq); + if (diff > 0 || diff < -15) { + // packet outside observed time frame + return; + } + int cur_rtt = SDL_GetTicks() - stamps[(stamp_cursor + diff + 16) % 16]; + rtt += (cur_rtt - rtt) * 0.1f; +} + +bool CongestionControl::SamplePacket(std::uint16_t seq) const noexcept { + return seq % sample_skip == 0; +} + +int CongestionControl::HeadDiff(std::uint16_t seq) const noexcept { + int16_t diff = int16_t(seq) - int16_t(stamp_last); + return diff / sample_skip; +} + +void CongestionControl::PacketIn(const UDPpacket &pack) noexcept { + rx_bytes += pack.len + packet_overhead; + UpdateStats(); +} + +void CongestionControl::PacketOut(const UDPpacket &pack) noexcept { + tx_bytes += pack.len + packet_overhead; + UpdateStats(); +} + +void CongestionControl::UpdateStats() noexcept { + Uint32 now = SDL_GetTicks(); + if (now >= next_sample) { + tx_kbps = float(tx_bytes) * (1.0f / 1024.0f); + rx_kbps = float(rx_bytes) * (1.0f / 1024.0f); + tx_bytes = 0; + rx_bytes = 0; + next_sample += 1000; + } +} + + Connection::Connection(const IPaddress &addr) : handler(nullptr) , addr(addr) @@ -84,6 +181,11 @@ uint16_t Connection::Send(UDPpacket &udp_pack, UDPsocket sock) { throw NetError("SDLNet_UDP_Send"); } + if (HasHandler()) { + Handler().PacketOut(udp_pack); + Handler().PacketSent(seq); + } + FlagSend(); return seq; } @@ -111,6 +213,7 @@ void Connection::Received(const UDPpacket &udp_pack) { } Packet::TControl ctrl_new = pack.header.ctrl; + Handler().PacketIn(udp_pack); Handler().Handle(udp_pack); if (diff > 0) { @@ -149,31 +252,30 @@ uint16_t Connection::SendPing(UDPpacket &udp_pack, UDPsocket sock) { ConnectionHandler::ConnectionHandler() -: packets_lost(0) -, packets_received(0) -, packet_loss(0.0f) { +: cc() { + +} +void ConnectionHandler::PacketSent(uint16_t seq) noexcept { + cc.PacketSent(seq); } void ConnectionHandler::PacketLost(uint16_t seq) { OnPacketLost(seq); - ++packets_lost; - UpdatePacketLoss(); + cc.PacketLost(seq); } void ConnectionHandler::PacketReceived(uint16_t seq) { OnPacketReceived(seq); - ++packets_received; - UpdatePacketLoss(); + cc.PacketReceived(seq); } -void ConnectionHandler::UpdatePacketLoss() noexcept { - unsigned int packets_total = packets_lost + packets_received; - if (packets_total >= 256) { - packet_loss = float(packets_lost) / float(packets_total); - packets_lost = 0; - packets_received = 0; - } +void ConnectionHandler::PacketIn(const UDPpacket &pack) noexcept { + cc.PacketIn(pack); +} + +void ConnectionHandler::PacketOut(const UDPpacket &pack) noexcept { + cc.PacketOut(pack); } @@ -281,7 +383,8 @@ void Packet::Payload::Write(const EntityState &state, size_t off) noexcept { WritePackU(state.block_pos * (1.0f / 16.0f), off + 12); Write(state.velocity, off + 18); Write(state.orient, off + 30); - Write(state.ang_vel, off + 38); + WritePackN(state.pitch * PI_0p5_inv, off + 38); + WritePackN(state.yaw * PI_inv, off + 40); } void Packet::Payload::Read(EntityState &state, size_t off) const noexcept { @@ -289,8 +392,11 @@ void Packet::Payload::Read(EntityState &state, size_t off) const noexcept { ReadPackU(state.block_pos, off + 12); Read(state.velocity, off + 18); Read(state.orient, off + 30); - Read(state.ang_vel, off + 38); + ReadPackN(state.pitch, off + 38); + ReadPackN(state.yaw, off + 40); state.block_pos *= 16.0f; + state.pitch *= PI_0p5; + state.yaw *= PI; } void Packet::Payload::Write(const EntityState &state, const glm::ivec3 &base, size_t off) noexcept { @@ -298,7 +404,8 @@ void Packet::Payload::Write(const EntityState &state, const glm::ivec3 &base, si WritePackU(state.block_pos * (1.0f / 16.0f), off + 3); Write(state.velocity, off + 9); Write(state.orient, off + 21); - Write(state.ang_vel, off + 29); + WritePackN(state.pitch * PI_0p5_inv, off + 29); + WritePackN(state.yaw * PI_inv, off + 31); } void Packet::Payload::Read(EntityState &state, const glm::ivec3 &base, size_t off) const noexcept { @@ -306,14 +413,12 @@ void Packet::Payload::Read(EntityState &state, const glm::ivec3 &base, size_t of ReadPackU(state.block_pos, off + 3); Read(state.velocity, off + 9); Read(state.orient, off + 21); - Read(state.ang_vel, off + 29); + ReadPackN(state.pitch, off + 29); + ReadPackN(state.yaw, off + 31); state.chunk_pos += base; state.block_pos *= 16.0f; -} - -void Packet::Payload::WritePackN(float val, size_t off) noexcept { - int16_t raw = glm::clamp(glm::round(val * 32767.0f), -32767.0f, 32767.0f); - Write(raw, off); + state.pitch *= PI_0p5; + state.yaw *= PI; } void Packet::Payload::WritePackB(const glm::ivec3 &val, size_t off) noexcept { @@ -332,6 +437,11 @@ void Packet::Payload::ReadPackB(glm::ivec3 &val, size_t off) const noexcept { val.z = conv; } +void Packet::Payload::WritePackN(float val, size_t off) noexcept { + int16_t raw = glm::clamp(glm::round(val * 32767.0f), -32767.0f, 32767.0f); + Write(raw, off); +} + void Packet::Payload::ReadPackN(float &val, size_t off) const noexcept { int16_t raw = 0; Read(raw, off); @@ -396,11 +506,11 @@ void Packet::Join::ReadPlayerState(EntityState &state) const noexcept { } void Packet::Join::WriteWorldName(const string &name) noexcept { - WriteString(name, 54, 32); + WriteString(name, 46, 32); } void Packet::Join::ReadWorldName(string &name) const noexcept { - ReadString(name, 54, 32); + ReadString(name, 46, 32); } void Packet::PlayerUpdate::WritePredictedState(const EntityState &state) noexcept { @@ -412,47 +522,27 @@ void Packet::PlayerUpdate::ReadPredictedState(EntityState &state) const noexcept } void Packet::PlayerUpdate::WriteMovement(const glm::vec3 &mov) noexcept { - WritePackN(mov, 50); + WritePackN(mov, 42); } void Packet::PlayerUpdate::ReadMovement(glm::vec3 &mov) const noexcept { - ReadPackN(mov, 50); -} - -void Packet::PlayerUpdate::WritePitch(float pitch) noexcept { - float conv = pitch * PI_0p5_inv; - WritePackN(conv, 56); -} - -void Packet::PlayerUpdate::ReadPitch(float &pitch) const noexcept { - ReadPackN(pitch, 56); - pitch *= PI_0p5; -} - -void Packet::PlayerUpdate::WriteYaw(float yaw) noexcept { - float conv = yaw * PI_inv; - WritePackN(conv, 58); -} - -void Packet::PlayerUpdate::ReadYaw(float &yaw) const noexcept { - ReadPackN(yaw, 58); - yaw *= PI; + ReadPackN(mov, 42); } void Packet::PlayerUpdate::WriteActions(uint8_t actions) noexcept { - Write(actions, 60); + Write(actions, 48); } void Packet::PlayerUpdate::ReadActions(uint8_t &actions) const noexcept { - Read(actions, 60); + Read(actions, 48); } void Packet::PlayerUpdate::WriteSlot(uint8_t slot) noexcept { - Write(slot, 61); + Write(slot, 49); } void Packet::PlayerUpdate::ReadSlot(uint8_t &slot) const noexcept { - Read(slot, 61); + Read(slot, 49); } void Packet::SpawnEntity::WriteEntity(const Entity &e) noexcept { @@ -463,13 +553,13 @@ void Packet::SpawnEntity::WriteEntity(const Entity &e) noexcept { Write(uint32_t(0), 4); } Write(e.GetState(), 8); - Write(e.Bounds(), 58); + Write(e.Bounds(), 50); uint32_t flags = 0; if (e.WorldCollidable()) { flags |= 1; } - Write(flags, 82); - WriteString(e.Name(), 86, 32); + Write(flags, 74); + WriteString(e.Name(), 78, 32); } void Packet::SpawnEntity::ReadEntityID(uint32_t &id) const noexcept { @@ -487,9 +577,9 @@ void Packet::SpawnEntity::ReadEntity(Entity &e) const noexcept { string name; Read(state, 8); - Read(bounds, 58); - Read(flags, 82); - ReadString(name, 86, 32); + Read(bounds, 50); + Read(flags, 74); + ReadString(name, 78, 32); e.SetState(state); e.Bounds(bounds);