Skip to content

Commit

Permalink
feat: Notify acks messages in one way messages
Browse files Browse the repository at this point in the history
If only one peer is sending notify messages,  we still want
the acks to come back,  so if no messages are being sent back,
we will send an empty message with the acks.
  • Loading branch information
paulpach committed Feb 10, 2021
1 parent d3663bf commit 07ca15d
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion Assets/Mirror/Runtime/NetworkConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@

namespace Mirror
{
[NetworkMessage]
public struct NotifyAck
{
}

/// <summary>
/// A High level network connection. This is used for connections from client-to-server and for connection from server-to-client.
Expand Down Expand Up @@ -83,6 +87,9 @@ public NetworkConnection(IConnection connection)
{
Assert.IsNotNull(connection);
this.connection = connection;

// a black message to ensure a notify timeout
RegisterHandler<NotifyAck>(msg => { });
}

/// <summary>
Expand Down Expand Up @@ -377,13 +384,17 @@ internal struct PacketEnvelope
}
const int ACK_MASK_BITS = sizeof(ulong) * 8;
const int WINDOW_SIZE = 512;
// packages will be acked no longer than this time;
public float NOTIFY_ACK_TIMEOUT = 0.3f;

private Sequencer sequencer = new Sequencer(16);
readonly Queue<PacketEnvelope> sendWindow = new Queue<PacketEnvelope>(WINDOW_SIZE);
private float lastNotifySentTime;

private ushort receiveSequence;
private ulong receiveMask;



/// <summary>
/// Sends a message, but notify when it is delivered or lost
Expand All @@ -398,7 +409,7 @@ public void SendNotify<T>(T msg, object token, int channelId = Channel.Unreliabl
NotifyLost?.Invoke(this, token);
return;
}

using (PooledNetworkWriter writer = NetworkWriterPool.GetWriter())
{
var notifyPacket = new NotifyPacket
Expand All @@ -418,6 +429,7 @@ public void SendNotify<T>(T msg, object token, int channelId = Channel.Unreliabl
MessagePacker.Pack(msg, writer);
NetworkDiagnostics.OnSend(msg, channelId, writer.Length, 1);
SendAsync(writer.ToArraySegment(), channelId).Forget();
lastNotifySentTime = Time.unscaledTime;
}

}
Expand Down Expand Up @@ -449,6 +461,11 @@ internal void ReceiveNotify(NotifyPacket notifyPacket, NetworkReader networkRead

int msgType = MessagePacker.UnpackId(networkReader);
InvokeHandler(msgType, networkReader, channelId);

if (Time.unscaledTime - lastNotifySentTime > NOTIFY_ACK_TIMEOUT)
{
SendNotify(new NotifyAck(), null, channelId);
}
}

// the other end just sent us a message
Expand Down

0 comments on commit 07ca15d

Please sign in to comment.