-
-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: adding option to pass channel type to socket
useful when using steam or epic relay and you want to use their reliability
- Loading branch information
1 parent
2e4149f
commit 5418d39
Showing
12 changed files
with
611 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
149 changes: 149 additions & 0 deletions
149
Assets/Mirage/Runtime/SocketLayer/Connection/PassthroughConnection.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
using System; | ||
using UnityEngine; | ||
|
||
namespace Mirage.SocketLayer | ||
{ | ||
internal class PassthroughConnection : Connection, IRawConnection | ||
{ | ||
private const int HEADER_SIZE = 1 + Batch.MESSAGE_LENGTH_SIZE; | ||
|
||
private readonly Batch _reliableBatch; | ||
private readonly Batch _unreliableBatch; | ||
private readonly AckSystem _ackSystem; | ||
|
||
public PassthroughConnection(Peer peer, IEndPoint endPoint, IDataHandler dataHandler, Config config, int maxPacketSize, Time time, Pool<ByteBuffer> bufferPool, ILogger logger, Metrics metrics) | ||
: base(peer, endPoint, dataHandler, config, maxPacketSize, time, logger, metrics) | ||
{ | ||
_reliableBatch = new ArrayBatch(maxPacketSize, this, PacketType.Reliable); | ||
_unreliableBatch = new ArrayBatch(maxPacketSize, this, PacketType.Unreliable); | ||
_ackSystem = new AckSystem(this, config, maxPacketSize, time, bufferPool, logger, metrics); | ||
|
||
if (maxPacketSize > ushort.MaxValue) | ||
{ | ||
throw new ArgumentException($"Max package size can not bigger than {ushort.MaxValue}. NoReliableConnection uses 2 bytes for message length, maxPacketSize over that value will mean that message will be incorrectly batched."); | ||
} | ||
} | ||
|
||
public override void SendUnreliable(byte[] message, int offset, int length) | ||
{ | ||
ThrowIfNotConnectedOrConnecting(); | ||
|
||
if (length + HEADER_SIZE > _maxPacketSize) | ||
{ | ||
throw new ArgumentException($"Message is bigger than MTU, size:{length} but max message size is {_maxPacketSize - HEADER_SIZE}"); | ||
} | ||
|
||
_unreliableBatch.AddMessage(message, offset, length); | ||
_metrics?.OnSendMessageUnreliable(length); | ||
} | ||
|
||
/// <summary> | ||
/// Use <see cref="INotifyCallBack"/> version for non-alloc | ||
/// </summary> | ||
public override INotifyToken SendNotify(byte[] packet, int offset, int length) | ||
{ | ||
ThrowIfNotConnectedOrConnecting(); | ||
var token = _ackSystem.SendNotify(packet, offset, length); | ||
_metrics?.OnSendMessageNotify(length); | ||
return token; | ||
} | ||
|
||
/// <summary> | ||
/// Use <see cref="INotifyCallBack"/> version for non-alloc | ||
/// </summary> | ||
public override void SendNotify(byte[] packet, int offset, int length, INotifyCallBack callBacks) | ||
{ | ||
ThrowIfNotConnectedOrConnecting(); | ||
_ackSystem.SendNotify(packet, offset, length, callBacks); | ||
_metrics?.OnSendMessageNotify(length); | ||
} | ||
|
||
/// <summary> | ||
/// single message, batched by AckSystem | ||
/// </summary> | ||
/// <param name="message"></param> | ||
public override void SendReliable(byte[] message, int offset, int length) | ||
{ | ||
ThrowIfNotConnectedOrConnecting(); | ||
|
||
if (length + HEADER_SIZE > _maxPacketSize) | ||
{ | ||
throw new ArgumentException($"Message is bigger than MTU, size:{length} but max message size is {_maxPacketSize - HEADER_SIZE}"); | ||
} | ||
|
||
_reliableBatch.AddMessage(message, offset, length); | ||
_metrics?.OnSendMessageReliable(length); | ||
} | ||
|
||
internal override void ReceiveUnreliablePacket(Packet packet) | ||
{ | ||
HandleReliableBatched(packet.Buffer.array, 1, packet.Length, PacketType.Unreliable); | ||
} | ||
|
||
internal override void ReceiveReliablePacket(Packet packet) | ||
{ | ||
HandleReliableBatched(packet.Buffer.array, 1, packet.Length, PacketType.Reliable); | ||
} | ||
|
||
internal override void ReceiveReliableFragment(Packet packet) => throw new NotSupportedException(); | ||
|
||
internal override void ReceiveNotifyPacket(Packet packet) | ||
{ | ||
var segment = _ackSystem.ReceiveNotify(packet.Buffer.array, packet.Length); | ||
if (segment != default) | ||
{ | ||
_metrics?.OnReceiveMessageNotify(packet.Length); | ||
_dataHandler.ReceiveMessage(this, segment); | ||
} | ||
} | ||
|
||
internal override void ReceiveNotifyAck(Packet packet) | ||
{ | ||
_ackSystem.ReceiveAck(packet.Buffer.array); | ||
} | ||
|
||
public override void FlushBatch() | ||
{ | ||
_ackSystem.Update(); | ||
_reliableBatch.Flush(); | ||
_unreliableBatch.Flush(); | ||
} | ||
|
||
internal override bool IsValidSize(Packet packet) | ||
{ | ||
const int minPacketSize = 1; | ||
|
||
var length = packet.Length; | ||
if (length < minPacketSize) | ||
return false; | ||
|
||
// Min size of message given to Mirage | ||
const int minMessageSize = 2; | ||
|
||
const int minCommandSize = 2; | ||
const int minUnreliableSize = 1 + minMessageSize; | ||
|
||
switch (packet.Type) | ||
{ | ||
case PacketType.Command: | ||
return length >= minCommandSize; | ||
|
||
case PacketType.Reliable: | ||
case PacketType.Unreliable: | ||
return length >= minUnreliableSize; | ||
|
||
case PacketType.Notify: | ||
return length >= AckSystem.NOTIFY_HEADER_SIZE + minMessageSize; | ||
case PacketType.Ack: | ||
return length >= AckSystem.ACK_HEADER_SIZE; | ||
case PacketType.ReliableFragment: | ||
// not supported | ||
return false; | ||
|
||
default: | ||
case PacketType.KeepAlive: | ||
return true; | ||
} | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
Assets/Mirage/Runtime/SocketLayer/Connection/PassthroughConnection.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.