From 26387b2e9d7e0a2296a2b4bf45a3e46580380edc Mon Sep 17 00:00:00 2001 From: JamJar00 Date: Mon, 30 May 2016 21:10:09 +0100 Subject: [PATCH] Various updates --- Hazel.UnitTests/TcpConnectionTests.cs | 41 ++++++++++++++++++++++++-- Hazel.UnitTests/TestHelper.cs | 4 +-- Hazel.UnitTests/UdpConnectionTests.cs | 37 +++++++++++++++++++++++ Hazel/DataEventArgs.cs | 2 +- Hazel/DocInclude/TcpListenerExample.cs | 23 +++++++++++++++ Hazel/IPMode.cs | 29 ++++++++++++++++++ Hazel/UdpClientConnection.cs | 11 +------ 7 files changed, 132 insertions(+), 15 deletions(-) create mode 100644 Hazel/DocInclude/TcpListenerExample.cs create mode 100644 Hazel/IPMode.cs diff --git a/Hazel.UnitTests/TcpConnectionTests.cs b/Hazel.UnitTests/TcpConnectionTests.cs index e30ef29..66d9958 100644 --- a/Hazel.UnitTests/TcpConnectionTests.cs +++ b/Hazel.UnitTests/TcpConnectionTests.cs @@ -32,6 +32,43 @@ namespace Hazel.UnitTests } } + /// + /// Tests IPv4 connectivity. + /// + [TestMethod] + public void TcpIPv4ConnectionTest() + { + using (TcpConnectionListener listener = new TcpConnectionListener(IPAddress.Any, 4296, IPMode.IPv4)) + using (TcpConnection connection = new TcpConnection()) + { + listener.Start(); + + connection.Connect(new NetworkEndPoint(IPAddress.Loopback, 4296, IPMode.IPv4)); + } + } + + /// + /// Tests dual mode connectivity. + /// + [TestMethod] + public void TcpDualModeConnectionTest() + { + using (TcpConnectionListener listener = new TcpConnectionListener(IPAddress.Any, 4296, IPMode.IPv4AndIPv6)) + { + listener.Start(); + + using (TcpConnection connection = new TcpConnection()) + { + connection.Connect(new NetworkEndPoint(IPAddress.Loopback, 4296, IPMode.IPv4)); + } + + using (TcpConnection connection = new TcpConnection()) + { + connection.Connect(new NetworkEndPoint(IPAddress.Loopback, 4296, IPMode.IPv4AndIPv6)); + } + } + } + /// /// Tests sending and receiving on the TcpConnection. /// @@ -41,7 +78,7 @@ namespace Hazel.UnitTests using (TcpConnectionListener listener = new TcpConnectionListener(IPAddress.Any, 4296)) using (TcpConnection connection = new TcpConnection()) { - TestHelper.RunServerToClientTest(listener, connection, 4, 0, SendOption.OrderedFragmentedReliable); + TestHelper.RunServerToClientTest(listener, connection, 4, 0, SendOption.FragmentedReliable); } } @@ -54,7 +91,7 @@ namespace Hazel.UnitTests using (TcpConnectionListener listener = new TcpConnectionListener(IPAddress.Any, 4296)) using (TcpConnection connection = new TcpConnection()) { - TestHelper.RunClientToServerTest(listener, connection, 4, 0, SendOption.OrderedFragmentedReliable); + TestHelper.RunClientToServerTest(listener, connection, 4, 0, SendOption.FragmentedReliable); } } diff --git a/Hazel.UnitTests/TestHelper.cs b/Hazel.UnitTests/TestHelper.cs index 5708a49..7434fbd 100644 --- a/Hazel.UnitTests/TestHelper.cs +++ b/Hazel.UnitTests/TestHelper.cs @@ -28,7 +28,7 @@ namespace Hazel.UnitTests Assert.AreEqual(0, args.Connection.Statistics.DataBytesReceived); Assert.AreEqual(0, args.Connection.Statistics.TotalBytesReceived); - args.Connection.WriteBytes(data, sendOption); + args.Connection.SendBytes(data, sendOption); Assert.AreEqual(data.Length, args.Connection.Statistics.DataBytesSent); Assert.AreEqual(data.Length + headerSize, args.Connection.Statistics.TotalBytesSent); @@ -100,7 +100,7 @@ namespace Hazel.UnitTests //Connect connection.Connect(new NetworkEndPoint(IPAddress.Loopback, 4296)); - connection.WriteBytes(data, sendOption); + connection.SendBytes(data, sendOption); //Wait until data is received mutex.WaitOne(); diff --git a/Hazel.UnitTests/UdpConnectionTests.cs b/Hazel.UnitTests/UdpConnectionTests.cs index fff0b4a..b93e6fb 100644 --- a/Hazel.UnitTests/UdpConnectionTests.cs +++ b/Hazel.UnitTests/UdpConnectionTests.cs @@ -32,6 +32,43 @@ namespace Hazel.UnitTests } } + /// + /// Tests IPv4 connectivity. + /// + [TestMethod] + public void UdpIPv4ConnectionTest() + { + using (UdpConnectionListener listener = new UdpConnectionListener(IPAddress.Any, 4296, IPMode.IPv4)) + using (UdpConnection connection = new UdpClientConnection()) + { + listener.Start(); + + connection.Connect(new NetworkEndPoint(IPAddress.Loopback, 4296, IPMode.IPv4)); + } + } + + /// + /// Tests dual mode connectivity. + /// + [TestMethod] + public void TcpDualModeConnectionTest() + { + using (UdpConnectionListener listener = new UdpConnectionListener(IPAddress.Any, 4296, IPMode.IPv4AndIPv6)) + { + listener.Start(); + + using (UdpConnection connection = new UdpClientConnection()) + { + connection.Connect(new NetworkEndPoint(IPAddress.Loopback, 4296, IPMode.IPv4)); + } + + using (UdpConnection connection = new UdpClientConnection()) + { + connection.Connect(new NetworkEndPoint(IPAddress.Loopback, 4296, IPMode.IPv4AndIPv6)); + } + } + } + /// /// Tests server to client unreliable communication on the UdpConnection. /// diff --git a/Hazel/DataEventArgs.cs b/Hazel/DataEventArgs.cs index fd56a6b..72522a3 100644 --- a/Hazel/DataEventArgs.cs +++ b/Hazel/DataEventArgs.cs @@ -39,7 +39,7 @@ namespace Hazel /// /// The the data was sent with. /// - public object SendOption { get; private set; } + public SendOption SendOption { get; private set; } /// /// Private constructor for object pool. diff --git a/Hazel/DocInclude/TcpListenerExample.cs b/Hazel/DocInclude/TcpListenerExample.cs new file mode 100644 index 0000000..bac1fee --- /dev/null +++ b/Hazel/DocInclude/TcpListenerExample.cs @@ -0,0 +1,23 @@ +class TcpListenerExample +{ + static void Main(string[] args) + { + //Setup listener + using (TcpConnectionListener listener = new TcpConnectionListener(IPAddress.Any, 4296)) + { + //Start listening for new connection events + listener.NewConnection += delegate(object sender, NewConnectionEventArgs a) + { + //Send the client some data + a.Connection.SendBytes(new byte[] { 0, 1, 2, 3, 4, 5, 6, 7 }, SendOption.Reliable); + + //Disconnect from the client + a.Connection.Close(); + }; + + listener.Start(); + + Console.ReadKey(); + } + } +} diff --git a/Hazel/IPMode.cs b/Hazel/IPMode.cs new file mode 100644 index 0000000..1c8c482 --- /dev/null +++ b/Hazel/IPMode.cs @@ -0,0 +1,29 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Hazel +{ + /// + /// Represents the IP version that a connection or listener will use. + /// + /// + /// If you wand a client to connect or be able to connect using IPv6 then you should use , + /// this sets the underlying sockets to use IPv6 but still allow IPv4 sockets to connect for backwards compatability + /// and hence it is the default IPMode in most cases. + /// + public enum IPMode + { + /// + /// Instruction to use IPv4 only, IPv6 connections will not be able to connect. + /// + IPv4, + + /// + /// Instruction to use dual mode sockets, both IPv4 and IPv6 connections will be able to connect. + /// + IPv4AndIPv6 + } +} diff --git a/Hazel/UdpClientConnection.cs b/Hazel/UdpClientConnection.cs index b867fd1..5c151d3 100644 --- a/Hazel/UdpClientConnection.cs +++ b/Hazel/UdpClientConnection.cs @@ -98,19 +98,10 @@ namespace Hazel State = ConnectionState.Connecting; - //Calculate local end point - EndPoint localEndPoint; - if (nep.EndPoint is IPEndPoint) - localEndPoint = new IPEndPoint(((IPEndPoint)nep.EndPoint).Address, 0); - else if (nep.EndPoint is IPEndPoint) - localEndPoint = new DnsEndPoint(((DnsEndPoint)nep.EndPoint).Host, 0); - else - throw new ArgumentException("Can only connect using an IPEndPoint or DnsEndpoint"); - //Begin listening try { - socket.Bind(localEndPoint); + socket.Bind(new IPEndPoint(IPAddress.Any, 0)); } catch (SocketException e) { -- 2.39.5