Optimize PacketId

This commit is contained in:
Sofia 2020-08-11 00:20:58 +03:00
parent 1962212ec0
commit de3a50ff2e
4 changed files with 15 additions and 20 deletions

View File

@ -15,13 +15,11 @@ namespace NeonTea.Quakeball.TeaNet.Packets {
/// <summary>Reads packet meta-information from the buffer.</summary>
public void ReadMeta(ByteBuffer buffer) {
PacketId = buffer.ReadInt();
PacketIsReliable = buffer.ReadBool();
}
/// <summary>Writes packet meta-information to the buffer.</summary>
public void WriteMeta(ByteBuffer buffer) {
buffer.Write(PacketId);
buffer.Write(PacketIsReliable);
}

View File

@ -37,7 +37,7 @@ namespace NeonTea.Quakeball.TeaNet.Packets {
return id;
}
public ByteBuffer BuildMessage(Connection connection) {
public ByteBuffer BuildMessage(Connection connection, int firstId) {
ByteBuffer buffer = new ByteBuffer();
foreach (byte b in Peer.Fingerprint) {
buffer.Write(b);
@ -54,7 +54,7 @@ namespace NeonTea.Quakeball.TeaNet.Packets {
} else if (connection.Status == ConnectionStatus.Ready) {
buffer.Write((byte)PacketStage.Ready);
buffer.Write(connection.Internal.LatestInwardReliable);
buffer.Write(connection.Internal.CurrentFirstPacket);
buffer.Write(firstId);
}
return buffer;
}

View File

@ -57,9 +57,6 @@ namespace NeonTea.Quakeball.TeaNet.Peers {
/// <summary>Last unreliable Packet ID we've received from the connection</summary>
public int LatestInwardUnreliable;
/// <summary>The id of the first packet in the queue to be sent</summary>
public int CurrentFirstPacket;
/// <summary>Reliable Packet ID counter for packets we're sending them</summary>
public int ReliablePacketIDCounter;
/// <summary>Unreliable Packet ID counter for packets we're sending them</summary>

View File

@ -92,8 +92,12 @@ namespace NeonTea.Quakeball.TeaNet.Peers {
Connection conn = Connections[uid];
Protocol protocol = Peer.GetProtocol(conn.Internal.AssignedProtocol);
if (protocol != null && conn.IsReady()) {
ByteBuffer buffer = protocol.BuildMessage(conn);
Packet[] list = PacketQueue[uid].ToArray();
int firstId = Int32.MaxValue;
if (list.Length > 0) {
firstId = list[0].PacketId;
}
ByteBuffer buffer = protocol.BuildMessage(conn, firstId);
buffer.Write(list.Length);
foreach (Packet p in list) {
buffer.WritePacket(protocol, p);
@ -118,7 +122,7 @@ namespace NeonTea.Quakeball.TeaNet.Peers {
p.PacketIsReliable = false;
Protocol protocol = Peer.GetProtocol(conn.Internal.AssignedProtocol);
if (protocol != null && conn.IsReady()) {
ByteBuffer buffer = protocol.BuildMessage(conn);
ByteBuffer buffer = protocol.BuildMessage(conn, p.PacketId);
buffer.Write(1);
buffer.WritePacket(protocol, p);
Send(conn, buffer);
@ -170,7 +174,7 @@ namespace NeonTea.Quakeball.TeaNet.Peers {
private void SendPlain(Connection conn) {
Protocol protocol = Peer.GetProtocol(conn.Internal.AssignedProtocol);
if (protocol != null) {
ByteBuffer buffer = protocol.BuildMessage(conn);
ByteBuffer buffer = protocol.BuildMessage(conn, Int32.MaxValue);
Send(conn, buffer);
}
}
@ -244,23 +248,19 @@ namespace NeonTea.Quakeball.TeaNet.Peers {
ConcurrentQueue<Packet> queue = PacketQueue[conn.uid];
Packet peeked;
while (queue.TryPeek(out peeked)) {
if (peeked.PacketId <= conn.Internal.LatestOutwardReliable) {
Packet discarded;
queue.TryDequeue(out discarded);
continue;
if (peeked.PacketId > conn.Internal.LatestOutwardReliable) {
break;
} else {
Packet LastRemoved;
queue.TryDequeue(out LastRemoved);
}
conn.Internal.CurrentFirstPacket = peeked.PacketId;
break;
}
PacketQueue[conn.uid] = queue;
int PacketAmount = buffer.ReadInt();
for (int i = 0; i < PacketAmount; i++) {
Packet p = buffer.ReadPacket(protocol);
//p.PacketId = FirstPacketId + i;
if (i == 0 && p.PacketId == FirstPacketId) {
//Debug.Log("Matched!");
}
p.PacketId = FirstPacketId + i;
if (p.PacketIsReliable) {
if (p.PacketId > conn.Internal.LatestInwardReliable) {
conn.Internal.LatestInwardReliable = p.PacketId;