Assert.AreEqual(65534, reader.ReadInt32()); // Content
var sub = reader.ReadMessageAsNewBuffer();
+ Assert.AreEqual(0, sub.Position);
+ Assert.AreEqual(0, sub.Offset);
+
Assert.AreEqual(3, sub.Length);
Assert.AreEqual(2, sub.Tag);
Assert.AreEqual("HO", sub.ReadString());
sub.Recycle();
sub = reader.ReadMessageAsNewBuffer();
+ Assert.AreEqual(0, sub.Position);
+ Assert.AreEqual(0, sub.Offset);
+
Assert.AreEqual(0, sub.Length);
Assert.AreEqual(232, sub.Tag);
sub.Recycle();
internal void SendDataRaw(byte[] response, EndPoint remoteEndPoint)
{
- this.sendQueue.Add(new SendMessageInfo() { Buffer = response, Recipient = remoteEndPoint });
+ this.sendQueue.TryAdd(new SendMessageInfo() { Buffer = response, Recipient = remoteEndPoint });
}
/// <summary>
this.sendThread.Join();
this.receiveThread.Join();
this.processThreads.Join();
+
+ this.receiveQueue.Dispose();
+ this.sendQueue.Dispose();
}
public void Dispose()
<DebugType>portable</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
- <DefineConstants>TRACE;HAZEL_BAG</DefineConstants>
+ <DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<DocumentationFile>
public static MessageReader GetSized(int minSize)
{
var output = ReaderPool.GetObject();
+
if (output.Buffer == null || output.Buffer.Length < minSize)
{
output.Buffer = new byte[minSize];
}
+ else
+ {
+ Array.Clear(output.Buffer, 0, output.Buffer.Length);
+ }
output.Offset = 0;
output.Position = 0;
public static MessageReader Get(MessageReader source)
{
- var output = GetSized(source.Buffer.Length);
+ var output = MessageReader.GetSized(source.Buffer.Length);
System.Buffer.BlockCopy(source.Buffer, 0, output.Buffer, 0, source.Buffer.Length);
output.Offset = source.Offset;
output.Offset += 3;
output.Position = 0;
- if (this.BytesRemaining < output.Length + 3) throw new InvalidDataException($"Message length is longer than message length: {output.Length + 3} of {this.BytesRemaining}");
+ if (this.BytesRemaining < output.Length + 3) throw new InvalidDataException($"Message Length at Position {this.readHead} is longer than message length: {output.Length + 3} of {this.BytesRemaining}");
this.Position += output.Length + 3;
return output;
var len = this.ReadUInt16();
var tag = this.ReadByte();
- if (this.BytesRemaining < len) throw new InvalidDataException($"Message length is longer than message length: {len} of {this.BytesRemaining}");
+ if (this.BytesRemaining < len) throw new InvalidDataException($"Message Length at Position {this.readHead} is longer than message length: {len} of {this.BytesRemaining}");
var output = MessageReader.GetSized(len);
public void Clear(SendOption sendOption)
{
+ Array.Clear(this.Buffer, 0, this.Buffer.Length);
this.messageStarts.Clear();
this.SendOption = sendOption;
this.Buffer[0] = (byte)sendOption;
/// </remarks>
public EndPoint RemoteEndPoint { get; protected set; }
+ public virtual float AveragePingMs { get; }
+
public long GetIP4Address()
{
if (IPMode == IPMode.IPv4)
protected override void SetState(ConnectionState state)
{
- if (state == ConnectionState.Connected)
- connectWaitLock.Set();
- else
- connectWaitLock.Reset();
+ try
+ {
+ if (state == ConnectionState.Connected)
+ connectWaitLock.Set();
+ else
+ connectWaitLock.Reset();
+ }
+ catch (ObjectDisposedException)
+ {
+
+ }
}
/// <summary>
/// This returns the average ping for a one-way trip as calculated from the reliable packets that have been sent
/// and acknowledged by the endpoint.
/// </remarks>
- public float AveragePingMs = 500;
+ private float _pingMs = 500;
/// <summary>
/// The maximum times a message should be resent before marking the endpoint as disconnected.
this,
buffer,
buffer.Length,
- ResendTimeout > 0 ? ResendTimeout : (int)Math.Min(AveragePingMs * this.ResendPingMultiplier, 300),
+ ResendTimeout > 0 ? ResendTimeout : (int)Math.Min(_pingMs * this.ResendPingMultiplier, 300),
ackCallback);
if (!reliableDataPacketsSent.TryAdd(id, packet))
lock (PingLock)
{
- this.AveragePingMs = Math.Max(50, this.AveragePingMs * .7f + rt * .3f);
+ this._pingMs = Math.Max(50, this._pingMs * .7f + rt * .3f);
}
}
else if (this.activePingPackets.TryRemove(id, out PingPacket pingPkt))
lock (PingLock)
{
- this.AveragePingMs = Math.Max(50, this.AveragePingMs * .7f + rt * .3f);
+ this._pingMs = Math.Max(50, this._pingMs * .7f + rt * .3f);
}
}
}
/// <inheritdoc />
public abstract partial class UdpConnection : NetworkConnection
{
+ public override float AveragePingMs => this._pingMs;
+
private const int SioUdpConnectionReset = -1744830452;
public static readonly byte[] EmptyDisconnectBytes = new byte[] { (byte)UdpSendOption.Disconnect };
}
catch { }
-
try
{
const int SIO_UDP_CONNRESET = -1744830452;