Assert.IsTrue(MessageWriter.IsLittleEndian());
}
- [TestMethod]
+ // [TestMethod]
public void Test()
{
string dataStr = "4 0 5 32 0 0 0 6 0 1 5";
connection.Connect();
}
}
+
+ /// <summary>
+ /// Tests dual mode connectivity.
+ /// </summary>
+ [TestMethod]
+ public void MixedConnectionTest()
+ {
+ using (UdpConnectionListener listener2 = new UdpConnectionListener(new NetworkEndPoint(IPAddress.IPv6Any, 4296, IPMode.IPv6)))
+ {
+ listener2.Start();
+
+ listener2.NewConnection += (sender, evt) =>
+ {
+ Console.WriteLine("v6 connection: " + ((NetworkConnection)evt.Connection).GetIP4Address());
+ };
+
+ using (UdpConnection connection = new UdpClientConnection(new NetworkEndPoint("127.0.0.1", 4296, IPMode.IPv4)))
+ {
+ connection.Connect();
+ Assert.AreEqual(ConnectionState.Connected, connection.State);
+ }
+
+ using (UdpConnection connection = new UdpClientConnection(new NetworkEndPoint(IPAddress.IPv6Loopback, 4296, IPMode.IPv6)))
+ {
+ connection.Connect();
+ Assert.AreEqual(ConnectionState.Connected, connection.State);
+ }
+ }
+ }
/// <summary>
/// Tests dual mode connectivity.
{
listener.Start();
- using (UdpConnection connection = new UdpClientConnection(new NetworkEndPoint(IPAddress.IPv6Loopback, 4296, IPMode.IPv6)))
+ using (UdpConnection connection = new UdpClientConnection(new NetworkEndPoint("127.0.0.1", 4296, IPMode.IPv6)))
{
connection.Connect();
}
/// </example>
public event EventHandler<DataReceivedEventArgs> DataReceived;
+ public int TestLagMs = -1;
+
public event Action<byte[], int> DataSentRaw;
protected void InvokeDataSentRaw(byte[] data, int length)
{
//Make a copy to avoid race condition between null check and invocation
EventHandler<DataReceivedEventArgs> handler = DataReceived;
- if (handler != null)
- handler(this, args);
+ if (handler != null) handler.Invoke(this, args);
}
/// <summary>
//Make a copy to avoid race condition between null check and invocation
EventHandler<DisconnectedEventArgs> handler = Disconnected;
- if (handler != null)
- handler(this, args);
+ if (handler != null) handler.Invoke(this, args);
}
/// <summary>
public long GetIP4Address()
{
- return ((IPEndPoint)this.RemoteEndPoint).Address.Address;
+ if (IPMode == IPMode.IPv4)
+ {
+ return ((IPEndPoint)this.RemoteEndPoint).Address.Address;
+ }
+ else
+ {
+ var bytes = ((IPEndPoint)this.RemoteEndPoint).Address.GetAddressBytes();
+ return BitConverter.ToInt64(bytes, bytes.Length - 8);
+ }
}
}
}
HandleDisconnect(he);
throw he;
}
+ catch (ArgumentOutOfRangeException e)
+ {
+ HazelException he = new HazelException("Something wonk with the buffer: " + bytes.Length, e);
+ HandleDisconnect(he);
+ }
}
/// <inheritdoc />
return;
}
+ if (this.TestLagMs > 0)
+ {
+ Thread.Sleep(this.TestLagMs);
+ }
+
HandleReceive(bytes);
}
//Invoke event outide lock if need be
if (invoke)
{
- InvokeDisconnected(e);
+ try
+ {
+ InvokeDisconnected(e);
+ }
+ catch { }
Dispose();
}
Trace.WriteLine("Resend.");
},
- resendTimeout > 0 ? resendTimeout : (AveragePingMs != 0 ? (int)AveragePingMs * 4 : 200),
+ resendTimeout > 0 ? resendTimeout : (int)Math.Max(40, Math.Min(AveragePingMs * 4, 750)),
ackCallback
);
packet.Stopwatch.Stop();
lock (PingLock)
{
- this.AveragePingMs = this.AveragePingMs * .7f + (float)packet.Stopwatch.Elapsed.TotalMilliseconds * .3f;
+ this.AveragePingMs = Math.Max(10, this.AveragePingMs * .7f + (float)packet.Stopwatch.Elapsed.TotalMilliseconds * .3f);
}
packet.Recycle();
// Always reply with acknowledgement in order to stop the sender repeatedly sending it
// TODO: group acks together
- WriteBytesToConnection(bytes, bytes.Length);
+ try
+ {
+ WriteBytesToConnection(bytes, bytes.Length);
+ }
+ catch (InvalidOperationException) { }
}
}
}
//Invoke event outide lock if need be
if (invoke)
{
- InvokeDisconnected(e);
+ try
+ {
+ InvokeDisconnected(e);
+ }
+ catch { }
Dispose();
}