X-Git-Url: http://git.localhorst.tv/?a=blobdiff_plain;f=src%2Fnet%2Fnet.cpp;h=7de901e31f58b7f842461e2a2414e677db8ceca5;hb=f506afdcb23fed22f2c7f345b94cb411487f89c9;hp=5d062343b8976f28425b3c45fd7b704dc00e0e2b;hpb=4727825186798902f68df5b99a6a32f0ef618454;p=blank.git diff --git a/src/net/net.cpp b/src/net/net.cpp index 5d06234..7de901e 100644 --- a/src/net/net.cpp +++ b/src/net/net.cpp @@ -41,7 +41,6 @@ CongestionControl::CongestionControl() , packets_lost(0) , packets_received(0) , packet_loss(0.0f) -, stamp_cursor(15) , stamp_last(0) , rtt(64.0f) , next_sample(1000) @@ -50,11 +49,11 @@ CongestionControl::CongestionControl() , tx_kbps(0.0f) , rx_kbps(0.0f) , mode(GOOD) -// rtt > 100ms or packet loss > 5% is BAD -, bad_rtt(100.0f) +// rtt > 75ms or packet loss > 5% is BAD +, bad_rtt(75.0f) , bad_loss(0.05f) -// rtt > 250ms or packet loss > 15% is UGLY -, ugly_rtt(250.0f) +// rtt > 150ms or packet loss > 15% is UGLY +, ugly_rtt(150.0f) , ugly_loss(0.15f) , mode_keep_time(1000) { Uint32 now = SDL_GetTicks(); @@ -71,8 +70,7 @@ void CongestionControl::PacketSent(uint16_t seq) noexcept { if (!SamplePacket(seq)) { return; } - stamp_cursor = (stamp_cursor + 1) % 16; - stamps[stamp_cursor] = SDL_GetTicks(); + stamps[SampleIndex(seq)] = SDL_GetTicks(); stamp_last = seq; } @@ -97,22 +95,25 @@ void CongestionControl::UpdatePacketLoss() noexcept { } } -void CongestionControl::UpdateRTT(std::uint16_t seq) noexcept { +void CongestionControl::UpdateRTT(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 + int16_t diff = int16_t(stamp_last) - int16_t(seq); + if (diff < 0 || diff > int(15 * sample_skip)) { + // packet outside observed frame return; } - int cur_rtt = SDL_GetTicks() - stamps[(stamp_cursor + diff + 16) % 16]; + int cur_rtt = SDL_GetTicks() - stamps[SampleIndex(seq)]; rtt += (cur_rtt - rtt) * 0.1f; } -bool CongestionControl::SamplePacket(std::uint16_t seq) const noexcept { +bool CongestionControl::SamplePacket(uint16_t seq) const noexcept { return seq % sample_skip == 0; } +size_t CongestionControl::SampleIndex(uint16_t seq) const noexcept { + return (seq / sample_skip) % 16; +} + void CongestionControl::PacketIn(const UDPpacket &pack) noexcept { rx_bytes += pack.len + packet_overhead; UpdateStats(); @@ -197,7 +198,9 @@ CongestionControl::Mode CongestionControl::Conditions() const noexcept { Connection::Connection(const IPaddress &addr) : handler(nullptr) , addr(addr) -, send_timer(500) +// make sure a packet is sent at least every 50ms since packets contains +// acks that the remote end will use to measure RTT +, send_timer(50) , recv_timer(10000) , ctrl_out{ 0, 0xFFFF, 0xFFFFFFFF } , ctrl_in{ 0, 0xFFFF, 0xFFFFFFFF } @@ -431,18 +434,67 @@ 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); + // find the largest component + float largest = 0.0f; + int largest_index = -1; + for (int i = 0; i < 4; ++i) { + float iabs = abs(val[i]); + if (iabs > largest) { + largest = iabs; + largest_index = i; + } + } + // make sure it's positive + const glm::quat q(val[largest_index] < 0.0f ? -val : val); + // move index to the two most significant bits + uint64_t packed = uint64_t(largest_index) << 62; + // we have to map from [-0.7072,0.7072] to [-524287,524287] and move into positive range + constexpr float conv = 524287.0f / 0.7072f; + // if largest is 1, the other three are 0 + // precision of comparison is the interval of our mapping + if (abs(1.0 - largest) < (1.0f / conv)) { + packed |= 0x7FFFF7FFFF7FFFF; + } else { + // pack the three smaller components into 20bit ints each + int shift = 40; + for (int i = 0; i < 4; ++i) { + if (i != largest_index) { + packed |= uint64_t(int(q[i] * conv) + 524287) << shift; + shift -= 20; + } + } + } + // and write it out + Write(packed, off); } 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); + // extract the 8 byte packed value + uint64_t packed = 0; + Read(packed, off); + // two most significant bits are the index of the largest (omitted) component + int largest_index = packed >> 62; + // if all other three are 0, largest is 1 and we can omit the conversion + if ((packed & 0xFFFFFFFFFFFFFFF) == 0x7FFFF7FFFF7FFFF) { + val = { 0.0f, 0.0f, 0.0f, 0.0f }; + val[largest_index] = 1.0f; + return; + } + // we have to map from [-524287,524287] to [-0.7072,0.7072] + constexpr float conv = 0.7072f / 524287.0f; + int shift = 40; + for (int i = 0; i < 4; ++i) { + if (i != largest_index) { + val[i] = float(int((packed >> shift) & 0xFFFFF) - 524287) * conv; + shift -= 20; + } else { + // set to zero so the length of the other three can be determined + val[i] = 0.0f; + } + } + // omitted component squared is 1 - length squared of others + val[largest_index] = sqrt(1.0f - dot(val, val)); + // and already normalized } void Packet::Payload::Write(const EntityState &state, size_t off) noexcept {