]> git.deb.at Git - rhonda/impostor.hazel.git/commitdiff
Various updates
authorJamJar00 <jamster.30@btinternet.com>
Mon, 30 May 2016 20:10:09 +0000 (21:10 +0100)
committerJamJar00 <jamster.30@btinternet.com>
Mon, 30 May 2016 20:10:09 +0000 (21:10 +0100)
Hazel.UnitTests/TcpConnectionTests.cs
Hazel.UnitTests/TestHelper.cs
Hazel.UnitTests/UdpConnectionTests.cs
Hazel/DataEventArgs.cs
Hazel/DocInclude/TcpListenerExample.cs [new file with mode: 0644]
Hazel/IPMode.cs [new file with mode: 0644]
Hazel/UdpClientConnection.cs

index e30ef29277159f47e0aa8d95a1be741730955598..66d9958684139ac7ac69ae8354c4807abce4693b 100644 (file)
@@ -32,6 +32,43 @@ namespace Hazel.UnitTests
             }
         }
 
+        /// <summary>
+        ///     Tests IPv4 connectivity.
+        /// </summary>
+        [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));
+            }
+        }
+
+        /// <summary>
+        ///     Tests dual mode connectivity.
+        /// </summary>
+        [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));
+                }
+            }
+        }
+
         /// <summary>
         ///     Tests sending and receiving on the TcpConnection.
         /// </summary>
@@ -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);
             }
         }
 
index 5708a49a96dd06dcdea413c373406849b412fd18..7434fbddda592ca542d8f7604399269962662001 100644 (file)
@@ -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();
index fff0b4a10ca3a01cd761fd1849787d566be82ca0..b93e6fb3a26f796179295daf7b991d3acbd15f7e 100644 (file)
@@ -32,6 +32,43 @@ namespace Hazel.UnitTests
             }
         }
 
+        /// <summary>
+        ///     Tests IPv4 connectivity.
+        /// </summary>
+        [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));
+            }
+        }
+
+        /// <summary>
+        ///     Tests dual mode connectivity.
+        /// </summary>
+        [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));
+                }
+            }
+        }
+
         /// <summary>
         ///     Tests server to client unreliable communication on the UdpConnection.
         /// </summary>
index fd56a6b1190ef9d46ad9c93b64830e02d87a533f..72522a39457f0fd43c2157258783fdd11fe14f3b 100644 (file)
@@ -39,7 +39,7 @@ namespace Hazel
         /// <summary>
         ///     The <see cref="SendOption"/> the data was sent with.
         /// </summary>
-        public object SendOption { get; private set; }
+        public SendOption SendOption { get; private set; }
 
         /// <summary>
         ///     Private constructor for object pool.
diff --git a/Hazel/DocInclude/TcpListenerExample.cs b/Hazel/DocInclude/TcpListenerExample.cs
new file mode 100644 (file)
index 0000000..bac1fee
--- /dev/null
@@ -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 (file)
index 0000000..1c8c482
--- /dev/null
@@ -0,0 +1,29 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace Hazel
+{
+    /// <summary>
+    ///     Represents the IP version that a connection or listener will use.
+    /// </summary>
+    /// <remarks>
+    ///     If you wand a client to connect or be able to connect using IPv6 then you should use <see cref="IPv4AndIPv6"/>, 
+    ///     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.
+    /// </remarks>
+    public enum IPMode
+    {
+        /// <summary>
+        ///     Instruction to use IPv4 only, IPv6 connections will not be able to connect.
+        /// </summary>
+        IPv4,
+
+        /// <summary>
+        ///     Instruction to use dual mode sockets, both IPv4 and IPv6 connections will be able to connect.
+        /// </summary>
+        IPv4AndIPv6
+    }
+}
index b867fd1bf821fcb9b7b5a9f2739ae45e1b044128..5c151d315c85b2861de29f089b458a0661be6adf 100644 (file)
@@ -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)
                 {