X-Git-Url: http://git.localhorst.tv/?a=blobdiff_plain;f=src%2Fnet%2Fnet.cpp;h=1f4e0a951edd655d08547ad1b4dfad7f696a7503;hb=b4e5dad2ed5c7e77573de413f3bf5be88577856d;hp=58875e7d0a62e6f584050d67d78fdb6a9a4c73c9;hpb=933ca0fe6c660e482edd45742d981f2de59a32df;p=blank.git diff --git a/src/net/net.cpp b/src/net/net.cpp index 58875e7..1f4e0a9 100644 --- a/src/net/net.cpp +++ b/src/net/net.cpp @@ -1,10 +1,11 @@ +#include "CongestionControl.hpp" #include "Connection.hpp" #include "ConnectionHandler.hpp" #include "io.hpp" #include "Packet.hpp" #include "../app/init.hpp" -#include "../model/CompositeModel.hpp" +#include "../model/Model.hpp" #include "../world/Entity.hpp" #include "../world/EntityState.hpp" @@ -27,6 +28,170 @@ constexpr size_t Packet::PlayerCorrection::MAX_LEN; constexpr size_t Packet::ChunkBegin::MAX_LEN; constexpr size_t Packet::ChunkData::MAX_LEN; 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) +, mode(GOOD) +// rtt > 100ms or packet loss > 5% is BAD +, bad_rtt(100.0f) +, bad_loss(0.05f) +// rtt > 250ms or packet loss > 15% is UGLY +, ugly_rtt(250.0f) +, ugly_loss(0.15f) +, mode_keep_time(1000) { + Uint32 now = SDL_GetTicks(); + for (Uint32 &s : stamps) { + s = now; + } + next_sample += now; + mode_entered = now; + mode_reset = now; + mode_step = 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; + int16_t diff = int16_t(seq) - int16_t(stamp_last); + diff /= sample_skip; + 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; +} + +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) { + // not yet + return; + } + 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; + UpdateMode(); +} + +void CongestionControl::UpdateMode() noexcept { + Mode now_mode = Conditions(); + if (now_mode > mode) { + ChangeMode(now_mode); + } else if (now_mode < mode) { + CheckUpgrade(now_mode); + } else { + KeepMode(); + } +} + +void CongestionControl::CheckUpgrade(Mode m) noexcept { + Uint32 now = SDL_GetTicks(); + Uint32 time_in_mode = now - mode_entered; + if (time_in_mode < mode_keep_time) { + return; + } + ChangeMode(m); +} + +void CongestionControl::ChangeMode(Mode m) noexcept { + Uint32 now = SDL_GetTicks(); + if (m > mode) { + // changed to worse mode + // if we spent less than 10 seconds in better mode + // double keep time till up to 64 seconds + if (now - mode_entered < 10000) { + if (mode_keep_time < 64000) { + mode_keep_time *= 2; + } + } + } + mode = m; + mode_entered = now; + mode_reset = mode_entered; +} + +void CongestionControl::KeepMode() noexcept { + mode_reset = SDL_GetTicks(); + // if in good mode for 10 seconds, halve keep time till down to one second + if (mode == GOOD && mode_keep_time > 1000 && mode_step - mode_reset > 10000) { + mode_keep_time /= 2; + mode_step = mode_reset; + } +} + +CongestionControl::Mode CongestionControl::Conditions() const noexcept { + if (rtt > ugly_rtt || packet_loss > ugly_loss) { + return UGLY; + } else if (rtt > bad_rtt || packet_loss > bad_loss) { + return BAD; + } else { + return GOOD; + } +} + Connection::Connection(const IPaddress &addr) : handler(nullptr) @@ -82,6 +247,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; } @@ -109,6 +279,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) { @@ -147,31 +318,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); } @@ -214,6 +384,8 @@ const char *Packet::Type2String(uint8_t t) noexcept { return "ChunkData"; case BlockUpdate::TYPE: return "BlockUpdate"; + case Message::TYPE: + return "Message"; default: return "Unknown"; } @@ -257,6 +429,126 @@ void Packet::Payload::ReadString(string &dst, size_t off, size_t maxlen) const n } } +void Packet::Payload::Write(const glm::quat &val, size_t off) noexcept { + WritePackN(val.w, off); + WritePackN(val.x, off + 2); + WritePackN(val.y, off + 4); + WritePackN(val.z, off + 6); +} + +void Packet::Payload::Read(glm::quat &val, size_t off) const noexcept { + ReadPackN(val.w, off); + ReadPackN(val.x, off + 2); + ReadPackN(val.y, off + 4); + ReadPackN(val.z, off + 6); + val = normalize(val); +} + +void Packet::Payload::Write(const EntityState &state, size_t off) noexcept { + Write(state.chunk_pos, off); + WritePackU(state.block_pos * (1.0f / 16.0f), off + 12); + Write(state.velocity, off + 18); + Write(state.orient, off + 30); + 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 { + Read(state.chunk_pos, off); + ReadPackU(state.block_pos, off + 12); + Read(state.velocity, off + 18); + Read(state.orient, off + 30); + 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 { + WritePackB(state.chunk_pos - base, off); + WritePackU(state.block_pos * (1.0f / 16.0f), off + 3); + Write(state.velocity, off + 9); + Write(state.orient, off + 21); + 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 { + ReadPackB(state.chunk_pos, off); + ReadPackU(state.block_pos, off + 3); + Read(state.velocity, off + 9); + Read(state.orient, off + 21); + ReadPackN(state.pitch, off + 29); + ReadPackN(state.yaw, off + 31); + state.chunk_pos += base; + state.block_pos *= 16.0f; + state.pitch *= PI_0p5; + state.yaw *= PI; +} + +void Packet::Payload::WritePackB(const glm::ivec3 &val, size_t off) noexcept { + Write(int8_t(val.x), off); + Write(int8_t(val.y), off + 1); + Write(int8_t(val.z), off + 2); +} + +void Packet::Payload::ReadPackB(glm::ivec3 &val, size_t off) const noexcept { + int8_t conv = 0; + Read(conv, off); + val.x = conv; + Read(conv, off + 1); + val.y = conv; + Read(conv, off + 2); + 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); + val = raw * (1.0f/32767.0f); +} + +void Packet::Payload::WritePackN(const glm::vec3 &val, size_t off) noexcept { + WritePackN(val.x, off); + WritePackN(val.y, off + 2); + WritePackN(val.z, off + 4); +} + +void Packet::Payload::ReadPackN(glm::vec3 &val, size_t off) const noexcept { + ReadPackN(val.x, off); + ReadPackN(val.y, off + 2); + ReadPackN(val.z, off + 4); +} + +void Packet::Payload::WritePackU(float val, size_t off) noexcept { + uint16_t raw = glm::clamp(glm::round(val * 65535.0f), 0.0f, 65535.0f); + Write(raw, off); +} + +void Packet::Payload::ReadPackU(float &val, size_t off) const noexcept { + uint16_t raw = 0; + Read(raw, off); + val = raw * (1.0f/65535.0f); +} + +void Packet::Payload::WritePackU(const glm::vec3 &val, size_t off) noexcept { + WritePackU(val.x, off); + WritePackU(val.y, off + 2); + WritePackU(val.z, off + 4); +} + +void Packet::Payload::ReadPackU(glm::vec3 &val, size_t off) const noexcept { + ReadPackU(val.x, off); + ReadPackU(val.y, off + 2); + ReadPackU(val.z, off + 4); +} + void Packet::Login::WritePlayerName(const string &name) noexcept { WriteString(name, 0, 32); @@ -280,11 +572,11 @@ void Packet::Join::ReadPlayerState(EntityState &state) const noexcept { } void Packet::Join::WriteWorldName(const string &name) noexcept { - WriteString(name, 68, 32); + WriteString(name, 46, 32); } void Packet::Join::ReadWorldName(string &name) const noexcept { - ReadString(name, 68, 32); + ReadString(name, 46, 32); } void Packet::PlayerUpdate::WritePredictedState(const EntityState &state) noexcept { @@ -296,56 +588,27 @@ void Packet::PlayerUpdate::ReadPredictedState(EntityState &state) const noexcept } void Packet::PlayerUpdate::WriteMovement(const glm::vec3 &mov) noexcept { - glm::ivec3 conv = clamp(glm::ivec3(mov * 32767.0f), -32767, 32767); - Write(int16_t(conv.x), 64); - Write(int16_t(conv.y), 66); - Write(int16_t(conv.z), 68); + WritePackN(mov, 42); } void Packet::PlayerUpdate::ReadMovement(glm::vec3 &mov) const noexcept { - int16_t x, y, z; - Read(x, 64); - Read(y, 66); - Read(z, 68); - mov = glm::vec3(x, y, z) * .00003051850947599719f; -} - -void Packet::PlayerUpdate::WritePitch(float pitch) noexcept { - int16_t conv = pitch * 20860.12008116853786870640f; - Write(conv, 70); -} - -void Packet::PlayerUpdate::ReadPitch(float &pitch) const noexcept { - int16_t conv = 0; - Read(conv, 70); - pitch = conv * .00004793836258415163f; -} - -void Packet::PlayerUpdate::WriteYaw(float yaw) noexcept { - int16_t conv = yaw * 10430.06004058426893435320f; - Write(conv, 72); -} - -void Packet::PlayerUpdate::ReadYaw(float &yaw) const noexcept { - int16_t conv = 0; - Read(conv, 72); - yaw = conv * .00009587672516830326f; + ReadPackN(mov, 42); } void Packet::PlayerUpdate::WriteActions(uint8_t actions) noexcept { - Write(actions, 74); + Write(actions, 48); } void Packet::PlayerUpdate::ReadActions(uint8_t &actions) const noexcept { - Read(actions, 74); + Read(actions, 48); } void Packet::PlayerUpdate::WriteSlot(uint8_t slot) noexcept { - Write(slot, 75); + Write(slot, 49); } void Packet::PlayerUpdate::ReadSlot(uint8_t &slot) const noexcept { - Read(slot, 75); + Read(slot, 49); } void Packet::SpawnEntity::WriteEntity(const Entity &e) noexcept { @@ -356,20 +619,20 @@ void Packet::SpawnEntity::WriteEntity(const Entity &e) noexcept { Write(uint32_t(0), 4); } Write(e.GetState(), 8); - Write(e.Bounds(), 72); + Write(e.Bounds(), 50); uint32_t flags = 0; if (e.WorldCollidable()) { flags |= 1; } - Write(flags, 96); - WriteString(e.Name(), 100, 32); + Write(flags, 74); + WriteString(e.Name(), 78, 32); } void Packet::SpawnEntity::ReadEntityID(uint32_t &id) const noexcept { Read(id, 0); } -void Packet::SpawnEntity::ReadSkeletonID(uint32_t &id) const noexcept { +void Packet::SpawnEntity::ReadModelID(uint32_t &id) const noexcept { Read(id, 4); } @@ -380,9 +643,9 @@ void Packet::SpawnEntity::ReadEntity(Entity &e) const noexcept { string name; Read(state, 8); - Read(bounds, 72); - Read(flags, 96); - ReadString(name, 100, 32); + Read(bounds, 50); + Read(flags, 74); + ReadString(name, 78, 32); e.SetState(state); e.Bounds(bounds); @@ -406,11 +669,19 @@ void Packet::EntityUpdate::ReadEntityCount(uint32_t &count) const noexcept { Read(count, 0); } -void Packet::EntityUpdate::WriteEntity(const Entity &entity, uint32_t num) noexcept { +void Packet::EntityUpdate::WriteChunkBase(const glm::ivec3 &base) noexcept { + Write(base, 4); +} + +void Packet::EntityUpdate::ReadChunkBase(glm::ivec3 &base) const noexcept { + Read(base, 4); +} + +void Packet::EntityUpdate::WriteEntity(const Entity &entity, const glm::ivec3 &base, uint32_t num) noexcept { uint32_t off = GetSize(num); Write(entity.ID(), off); - Write(entity.GetState(), off + 4); + Write(entity.GetState(), base, off + 4); } void Packet::EntityUpdate::ReadEntityID(uint32_t &id, uint32_t num) const noexcept { @@ -418,9 +689,9 @@ void Packet::EntityUpdate::ReadEntityID(uint32_t &id, uint32_t num) const noexce Read(id, off); } -void Packet::EntityUpdate::ReadEntityState(EntityState &state, uint32_t num) const noexcept { +void Packet::EntityUpdate::ReadEntityState(EntityState &state, const glm::ivec3 &base, uint32_t num) const noexcept { uint32_t off = GetSize(num); - Read(state, off + 4); + Read(state, base, off + 4); } void Packet::PlayerCorrection::WritePacketSeq(std::uint16_t s) noexcept { @@ -541,6 +812,30 @@ void Packet::BlockUpdate::ReadBlock(Block &block, uint32_t num) const noexcept { Read(block, off); } +void Packet::Message::WriteType(uint8_t type) noexcept { + Write(type, 0); +} + +void Packet::Message::ReadType(uint8_t &type) const noexcept { + Read(type, 0); +} + +void Packet::Message::WriteReferral(uint32_t ref) noexcept { + Write(ref, 1); +} + +void Packet::Message::ReadReferral(uint32_t &ref) const noexcept { + Read(ref, 1); +} + +void Packet::Message::WriteMessage(const string &msg) noexcept { + WriteString(msg, 5, MAX_MESSAGE_LEN); +} + +void Packet::Message::ReadMessage(string &msg) const noexcept { + ReadString(msg, 5, MAX_MESSAGE_LEN); +} + void ConnectionHandler::Handle(const UDPpacket &udp_pack) { const Packet &pack = *reinterpret_cast(udp_pack.data); @@ -581,6 +876,9 @@ void ConnectionHandler::Handle(const UDPpacket &udp_pack) { case Packet::BlockUpdate::TYPE: On(Packet::As(udp_pack)); break; + case Packet::Message::TYPE: + On(Packet::As(udp_pack)); + break; default: // drop unknown or unhandled packets break;