X-Git-Url: http://git.localhorst.tv/?a=blobdiff_plain;f=src%2Fnet%2Fnet.cpp;h=fa9c381f15d4961dbf9ae6578c101e408fe99dfd;hb=8ab4ea13545cccbacbd1ed610968d3f481c1b3c8;hp=81de3e476e8286245e574905081b047d28b17308;hpb=8ae45b6555d55f301f83daf8c1337d332d8305ab;p=blank.git diff --git a/src/net/net.cpp b/src/net/net.cpp index 81de3e4..fa9c381 100644 --- a/src/net/net.cpp +++ b/src/net/net.cpp @@ -118,14 +118,14 @@ void Connection::Received(const UDPpacket &udp_pack) { if (diff > 0) { for (int i = 0; i < diff; ++i) { if (i > 32 || (i < 32 && (ctrl_in.hist & (1 << (31 - i))) == 0)) { - Handler().OnPacketLost(ctrl_in.ack - 32 + i); + Handler().PacketLost(ctrl_in.ack - 32 + i); } } } // check for newly ack'd packets for (uint16_t s = ctrl_new.AckBegin(); s != ctrl_new.AckEnd(); --s) { if (ctrl_new.Acks(s) && !ctrl_in.Acks(s)) { - Handler().OnPacketReceived(s); + Handler().PacketReceived(s); } } ctrl_in = ctrl_new; @@ -145,6 +145,35 @@ uint16_t Connection::SendPing(UDPpacket &udp_pack, UDPsocket sock) { } +ConnectionHandler::ConnectionHandler() +: packets_lost(0) +, packets_received(0) +, packet_loss(0.0f) { + +} + +void ConnectionHandler::PacketLost(uint16_t seq) { + OnPacketLost(seq); + ++packets_lost; + UpdatePacketLoss(); +} + +void ConnectionHandler::PacketReceived(uint16_t seq) { + OnPacketReceived(seq); + ++packets_received; + UpdatePacketLoss(); +} + +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; + } +} + + ostream &operator <<(ostream &out, const IPaddress &addr) { const unsigned char *host = reinterpret_cast(&addr.host); out << int(host[0]) @@ -322,18 +351,19 @@ void Packet::EntityUpdate::ReadEntityCount(uint32_t &count) const noexcept { } void Packet::EntityUpdate::WriteEntity(const Entity &entity, uint32_t num) noexcept { - uint32_t off = 4 + (num * 64); + uint32_t off = GetSize(num);; Write(entity.ID(), off); Write(entity.GetState(), off + 4); } void Packet::EntityUpdate::ReadEntityID(uint32_t &id, uint32_t num) const noexcept { - Read(id, 4 + (num * 64)); + uint32_t off = GetSize(num);; + Read(id, off); } void Packet::EntityUpdate::ReadEntityState(EntityState &state, uint32_t num) const noexcept { - uint32_t off = 4 + (num * 64); + uint32_t off = GetSize(num);; Read(state, off + 4); }