public bool resolve(PacketReader packet, ushort blockId) { // Check if this is an older block id if (Overflow.le(blockId, oldestResolutionBlockId)) { ResetResolutionTable(blockId); //client->flag_desync(); return(false); } // Otherwise, it might be newer ushort diff = Overflow.sub(blockId, oldestResolutionBlockId); ushort idx = (ushort)(Overflow.add(oldestResolutionPosition, diff) % ResolutionTableSize); if (diff >= ResolutionTableSize) { // We have to move oldest so that newest points to blockId ushort move_amount = Overflow.sub(diff, ResolutionTableDiff); oldestResolutionBlockId = Overflow.add(oldestResolutionBlockId, move_amount); oldestResolutionPosition = (ushort)(Overflow.add(oldestResolutionPosition, move_amount) % ResolutionTableSize); // Fix diff so we don't overrun the new position idx = (ushort)(Overflow.add(oldestResolutionPosition, Overflow.sub(diff, move_amount)) % ResolutionTableSize); // Clean position, as it is a newer packet that hasn't been parsed yet resolutionTable[idx] = 0; } // Compute packet mask ulong mask = (ulong)(1) << packet.getCounter(); // Get blockId position, bitmask, and compute if ((resolutionTable[idx] & mask) != 0) { // The packet is already in return(false); } resolutionTable[idx] |= mask; return(true); }
public Buffer update(ushort tickId, IBaseClient client, SuperPacket <PQ> superpacket) { ++sinceLastPing; if (needsPing()) { sinceLastPing = 0; superpacket.SetFlag(SuperPacketFlags.Ping); } // TODO(gpascualg): Lock superpacket bool first_packet = true; superpacket.prepare(); if (superpacket.finish(tickId, first_packet)) { Buffer buffer = new Buffer(superpacket.getBuffer()); // Register time for ping purposes ushort packetIdDiff = Overflow.sub(buffer.readUshort(2), timestampsHeadId); timestampsHeadPosition = Overflow.mod(Overflow.add(timestampsHeadPosition, packetIdDiff), ResolutionTableSize); UnityEngine.Debug.Log($"< SET {buffer.readUshort(2)} AT {timestampsHeadPosition} WITH DIFF {packetIdDiff}"); timestamps[timestampsHeadPosition] = DateTimeExtensions.now(); timestampsHeadId = buffer.readUshort(2); // Update estimate sendKbpsEstimateAcc += buffer.getPosition(); lastSendSize = (ushort)buffer.getPosition(); first_packet = false; return(buffer); } lastSendSize = 0; return(null); }