Skip to content

Commit

Permalink
refactor: removing Obsolete Notify code from networkplayer
Browse files Browse the repository at this point in the history
BREAKING CHANGE: removing notify code from networkplayer, notify is now part of peer
  • Loading branch information
James-Frowen committed May 24, 2021
1 parent 73b3e59 commit b2e5531
Show file tree
Hide file tree
Showing 4 changed files with 1 addition and 227 deletions.
32 changes: 1 addition & 31 deletions Assets/Mirage/Runtime/INetworkPlayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,40 +26,10 @@ public interface IMessageReceiver
void ClearHandlers();
}

/// <summary>
/// An object that can send notify messages
/// </summary>
public interface INotifySender
{
/// <summary>
/// Sends a message, but notify when it is delivered or lost
/// </summary>
/// <typeparam name="T">type of message to send</typeparam>
/// <param name="message">message to send</param>
/// <param name="token">a arbitrary object that the sender will receive with their notification</param>
void SendNotify<T>(T message, object token, int channelId = Channel.Unreliable);
}

/// <summary>
/// An object that can receive notify messages
/// </summary>
public interface INotifyReceiver
{
/// <summary>
/// Raised when a message is delivered
/// </summary>
event Action<INetworkPlayer, object> NotifyDelivered;

/// <summary>
/// Raised when a message is lost
/// </summary>
event Action<INetworkPlayer, object> NotifyLost;
}

/// <summary>
/// An object that can send and receive messages and notify messages
/// </summary>
public interface IMessageHandler : IMessageSender, IMessageReceiver, INotifySender, INotifyReceiver
public interface IMessageHandler : IMessageSender, IMessageReceiver
{
void HandleMessage(ArraySegment<byte> packet);
}
Expand Down
150 changes: 0 additions & 150 deletions Assets/Mirage/Runtime/NetworkPlayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,6 @@

namespace Mirage
{
[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.
/// </summary>
Expand Down Expand Up @@ -120,10 +115,6 @@ public NetworkPlayer(SocketLayer.IConnection connection)
{
Assert.IsNotNull(connection);
this.connection = connection;

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


Expand Down Expand Up @@ -345,146 +336,5 @@ public void DestroyOwnedObjects()
// clear the hashset because we destroyed them all
clientOwnedObjects.Clear();
}

#region Notify

internal struct PacketEnvelope
{
internal ushort Sequence;
internal object Token;
}
const int ACK_MASK_BITS = sizeof(ulong) * 8;
const int WINDOW_SIZE = 512;
// packages will be acked no longer than this time
[Obsolete("Use Peer instead", true)]
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;

// the first sequence will be 0, so
// we need the last received sequence to be just before that
// this is unsigned and wraps, so 0 - 1 == ushort.MaxValue
private ushort receiveSequence = ushort.MaxValue;
private ulong receiveMask;



/// <summary>
/// Sends a message, but notify when it is delivered or lost
/// </summary>
/// <typeparam name="T">type of message to send</typeparam>
/// <param name="message">message to send</param>
/// <param name="token">a arbitrary object that the sender will receive with their notification</param>
[Obsolete("Use Peer instead", true)]
public void SendNotify<T>(T message, object token, int channelId = Channel.Unreliable)
{
if (sendWindow.Count == WINDOW_SIZE)
{
NotifyLost?.Invoke(this, token);
return;
}

using (PooledNetworkWriter writer = NetworkWriterPool.GetWriter())
{
var notifyPacket = new NotifyPacket
{
Sequence = (ushort)sequencer.Next(),
ReceiveSequence = receiveSequence,
AckMask = receiveMask
};

sendWindow.Enqueue(new PacketEnvelope
{
Sequence = notifyPacket.Sequence,
Token = token
});

MessagePacker.Pack(notifyPacket, writer);
MessagePacker.Pack(message, writer);
NetworkDiagnostics.OnSend(message, channelId, writer.Length, 1);
Send(writer.ToArraySegment(), channelId);
lastNotifySentTime = Time.unscaledTime;
}

}

[Obsolete("Use Peer instead", true)]
internal void ReceiveNotify(NotifyPacket notifyPacket, NetworkReader networkReader, int channelId)
{
int sequenceDistance = (int)sequencer.Distance(notifyPacket.Sequence, receiveSequence);

// sequence is so far out of bounds we can't save, just kick him (or her!)
if (Math.Abs(sequenceDistance) > WINDOW_SIZE)
{
connection?.Disconnect();
return;
}

// this message is old, we already received
// a newer or duplicate packet. Discard it
if (sequenceDistance <= 0)
return;

receiveSequence = notifyPacket.Sequence;

if (sequenceDistance >= ACK_MASK_BITS)
receiveMask = 1;
else
receiveMask = (receiveMask << sequenceDistance) | 1;

AckPackets(notifyPacket.ReceiveSequence, notifyPacket.AckMask);

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
// and it told us the latest message it got
// and the ack mask
[Obsolete("Use Peer instead", true)]
private void AckPackets(ushort receiveSequence, ulong ackMask)
{
while (sendWindow.Count > 0)
{
PacketEnvelope envelope = sendWindow.Peek();

int distance = (int)sequencer.Distance(envelope.Sequence, receiveSequence);

if (distance > 0)
break;

sendWindow.Dequeue();

// if any of these cases trigger, packet is most likely lost
if ((distance <= -ACK_MASK_BITS) || ((ackMask & (1UL << -distance)) == 0UL))
{
NotifyLost?.Invoke(this, envelope.Token);
}
else
{
NotifyDelivered?.Invoke(this, envelope.Token);
}
}
}

/// <summary>
/// Raised when a message is delivered
/// </summary>
[Obsolete("Use Peer instead", true)]
public event Action<INetworkPlayer, object> NotifyDelivered;

/// <summary>
/// Raised when a message is lost
/// </summary>
[Obsolete("Use Peer instead", true)]
public event Action<INetworkPlayer, object> NotifyLost;
#endregion
}
}
35 changes: 0 additions & 35 deletions Assets/Mirage/Runtime/NotifyPacket.cs

This file was deleted.

11 changes: 0 additions & 11 deletions Assets/Mirage/Runtime/NotifyPacket.cs.meta

This file was deleted.

0 comments on commit b2e5531

Please sign in to comment.