From 705e16fa10fae4126b30d6ba37481921e7bf99fd Mon Sep 17 00:00:00 2001 From: JamJar00 Date: Sun, 17 Jul 2016 15:31:46 +0100 Subject: [PATCH] Reduced to .NET target version 3.5 and refactored IPv6 support to fit. --- Hazel.UnitTests/Hazel.UnitTests.csproj | 3 +- Hazel.UnitTests/TcpConnectionTests.cs | 11 ++----- Hazel.UnitTests/UdpConnectionTests.cs | 31 +++++++++----------- Hazel/ConnectionListener.cs | 2 +- Hazel/ConnectionStatistics.cs | 2 +- Hazel/DisconnectedEventArgs.cs | 2 +- Hazel/DocInclude/TcpListenerExample.cs | 2 +- Hazel/DocInclude/UdpListenerExample.cs | 2 +- Hazel/Hazel.csproj | 4 +-- Hazel/IPMode.cs | 7 +++-- Hazel/NetworkConnection.cs | 7 ++++- Hazel/NetworkConnectionListener.cs | 10 +++---- Hazel/NetworkEndPoint.cs | 12 ++++---- Hazel/ObjectPool.cs | 22 ++++++++++++-- Hazel/SendOption.cs | 1 - Hazel/SendOptionInternal.cs | 2 +- Hazel/Tcp/StateObject.cs | 1 - Hazel/Tcp/TcpConnection.cs | 8 ++---- Hazel/Tcp/TcpConnectionListener.cs | 29 ++++++++++++------- Hazel/Udp/UdpClientConnection.cs | 15 +++++++--- Hazel/Udp/UdpConnection.KeepAlive.cs | 2 +- Hazel/Udp/UdpConnection.Reliable.cs | 2 +- Hazel/Udp/UdpConnectionListener.cs | 40 ++++++++++++++++---------- Hazel/Udp/UdpServerConnection.cs | 6 ++-- 24 files changed, 131 insertions(+), 92 deletions(-) diff --git a/Hazel.UnitTests/Hazel.UnitTests.csproj b/Hazel.UnitTests/Hazel.UnitTests.csproj index 62520fd..466c334 100644 --- a/Hazel.UnitTests/Hazel.UnitTests.csproj +++ b/Hazel.UnitTests/Hazel.UnitTests.csproj @@ -8,7 +8,7 @@ Properties Hazel.UnitTests Hazel.UnitTests - v4.5 + v3.5 512 {3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} 10.0 @@ -16,6 +16,7 @@ $(ProgramFiles)\Common Files\microsoft shared\VSTT\$(VisualStudioVersion)\UITestExtensionPackages False UnitTest + true diff --git a/Hazel.UnitTests/TcpConnectionTests.cs b/Hazel.UnitTests/TcpConnectionTests.cs index ccdf314..b092b5e 100644 --- a/Hazel.UnitTests/TcpConnectionTests.cs +++ b/Hazel.UnitTests/TcpConnectionTests.cs @@ -54,18 +54,13 @@ namespace Hazel.UnitTests /// Tests dual mode connectivity. /// [TestMethod] - public void TcpDualModeConnectionTest() + public void TcpIPv6ConnectionTest() { - using (TcpConnectionListener listener = new TcpConnectionListener(IPAddress.Any, 4296, IPMode.IPv4AndIPv6)) + using (TcpConnectionListener listener = new TcpConnectionListener(IPAddress.IPv6Any, 4296, IPMode.IPv6)) { listener.Start(); - using (TcpConnection connection = new TcpConnection(new NetworkEndPoint(IPAddress.Loopback, 4296, IPMode.IPv4))) - { - connection.Connect(); - } - - using (TcpConnection connection = new TcpConnection(new NetworkEndPoint(IPAddress.Loopback, 4296, IPMode.IPv4AndIPv6))) + using (TcpConnection connection = new TcpConnection(new NetworkEndPoint(IPAddress.IPv6Loopback, 4296, IPMode.IPv6))) { connection.Connect(); } diff --git a/Hazel.UnitTests/UdpConnectionTests.cs b/Hazel.UnitTests/UdpConnectionTests.cs index 3db82c5..003cc22 100644 --- a/Hazel.UnitTests/UdpConnectionTests.cs +++ b/Hazel.UnitTests/UdpConnectionTests.cs @@ -18,7 +18,7 @@ namespace Hazel.UnitTests { NetworkEndPoint ep = new NetworkEndPoint(IPAddress.Loopback, 4296); - using (UdpConnectionListener listener = new UdpConnectionListener(IPAddress.Any, 4296)) + using (UdpConnectionListener listener = new UdpConnectionListener(new NetworkEndPoint(IPAddress.Any, 4296))) using (UdpConnection connection = new UdpClientConnection(ep)) { listener.Start(); @@ -41,7 +41,7 @@ namespace Hazel.UnitTests [TestMethod] public void UdpIPv4ConnectionTest() { - using (UdpConnectionListener listener = new UdpConnectionListener(IPAddress.Any, 4296, IPMode.IPv4)) + using (UdpConnectionListener listener = new UdpConnectionListener(new NetworkEndPoint(IPAddress.Any, 4296, IPMode.IPv4))) using (UdpConnection connection = new UdpClientConnection(new NetworkEndPoint(IPAddress.Loopback, 4296, IPMode.IPv4))) { listener.Start(); @@ -54,18 +54,13 @@ namespace Hazel.UnitTests /// Tests dual mode connectivity. /// [TestMethod] - public void UdpDualModeConnectionTest() + public void UdpIPv6ConnectionTest() { - using (UdpConnectionListener listener = new UdpConnectionListener(IPAddress.Any, 4296, IPMode.IPv4AndIPv6)) + using (UdpConnectionListener listener = new UdpConnectionListener(new NetworkEndPoint(IPAddress.IPv6Any, 4296, IPMode.IPv6))) { listener.Start(); - using (UdpConnection connection = new UdpClientConnection(new NetworkEndPoint(IPAddress.Loopback, 4296, IPMode.IPv4))) - { - connection.Connect(); - } - - using (UdpConnection connection = new UdpClientConnection(new NetworkEndPoint(IPAddress.Loopback, 4296, IPMode.IPv4AndIPv6))) + using (UdpConnection connection = new UdpClientConnection(new NetworkEndPoint(IPAddress.IPv6Loopback, 4296, IPMode.IPv6))) { connection.Connect(); } @@ -78,7 +73,7 @@ namespace Hazel.UnitTests [TestMethod] public void UdpUnreliableServerToClientTest() { - using (UdpConnectionListener listener = new UdpConnectionListener(IPAddress.Any, 4296)) + using (UdpConnectionListener listener = new UdpConnectionListener(new NetworkEndPoint(IPAddress.Any, 4296))) using (UdpConnection connection = new UdpClientConnection(new NetworkEndPoint(IPAddress.Loopback, 4296))) { TestHelper.RunServerToClientTest(listener, connection, 1, 3, SendOption.None); @@ -91,7 +86,7 @@ namespace Hazel.UnitTests [TestMethod] public void UdpReliableServerToClientTest() { - using (UdpConnectionListener listener = new UdpConnectionListener(IPAddress.Any, 4296)) + using (UdpConnectionListener listener = new UdpConnectionListener(new NetworkEndPoint(IPAddress.Any, 4296))) using (UdpConnection connection = new UdpClientConnection(new NetworkEndPoint(IPAddress.Loopback, 4296))) { TestHelper.RunServerToClientTest(listener, connection, 3, 3, SendOption.Reliable); @@ -104,7 +99,7 @@ namespace Hazel.UnitTests [TestMethod] public void UdpUnreliableClientToServerTest() { - using (UdpConnectionListener listener = new UdpConnectionListener(IPAddress.Any, 4296)) + using (UdpConnectionListener listener = new UdpConnectionListener(new NetworkEndPoint(IPAddress.Any, 4296))) using (UdpConnection connection = new UdpClientConnection(new NetworkEndPoint(IPAddress.Loopback, 4296))) { TestHelper.RunClientToServerTest(listener, connection, 1, 3, SendOption.None); @@ -117,7 +112,7 @@ namespace Hazel.UnitTests [TestMethod] public void UdpReliableClientToServerTest() { - using (UdpConnectionListener listener = new UdpConnectionListener(IPAddress.Any, 4296)) + using (UdpConnectionListener listener = new UdpConnectionListener(new NetworkEndPoint(IPAddress.Any, 4296))) using (UdpConnection connection = new UdpClientConnection(new NetworkEndPoint(IPAddress.Loopback, 4296))) { TestHelper.RunClientToServerTest(listener, connection, 3, 3, SendOption.Reliable); @@ -130,7 +125,7 @@ namespace Hazel.UnitTests [TestMethod] public void KeepAliveClientTest() { - using (UdpConnectionListener listener = new UdpConnectionListener(IPAddress.Any, 4296)) + using (UdpConnectionListener listener = new UdpConnectionListener(new NetworkEndPoint(IPAddress.Any, 4296))) using (UdpConnection connection = new UdpClientConnection(new NetworkEndPoint(IPAddress.Loopback, 4296))) { listener.Start(); @@ -156,7 +151,7 @@ namespace Hazel.UnitTests { ManualResetEvent mutex = new ManualResetEvent(false); - using (UdpConnectionListener listener = new UdpConnectionListener(IPAddress.Any, 4296)) + using (UdpConnectionListener listener = new UdpConnectionListener(new NetworkEndPoint(IPAddress.Any, 4296))) using (UdpConnection connection = new UdpClientConnection(new NetworkEndPoint(IPAddress.Loopback, 4296))) { listener.NewConnection += delegate(object sender, NewConnectionEventArgs args) @@ -188,7 +183,7 @@ namespace Hazel.UnitTests [TestMethod] public void ClientDisconnectTest() { - using (UdpConnectionListener listener = new UdpConnectionListener(IPAddress.Any, 4296)) + using (UdpConnectionListener listener = new UdpConnectionListener(new NetworkEndPoint(IPAddress.Any, 4296))) using (UdpConnection connection = new UdpClientConnection(new NetworkEndPoint(IPAddress.Loopback, 4296))) { TestHelper.RunClientDisconnectTest(listener, connection); @@ -201,7 +196,7 @@ namespace Hazel.UnitTests [TestMethod] public void ServerDisconnectTest() { - using (UdpConnectionListener listener = new UdpConnectionListener(IPAddress.Any, 4296)) + using (UdpConnectionListener listener = new UdpConnectionListener(new NetworkEndPoint(IPAddress.Any, 4296))) using (UdpConnection connection = new UdpClientConnection(new NetworkEndPoint(IPAddress.Loopback, 4296))) { TestHelper.RunServerDisconnectTest(listener, connection); diff --git a/Hazel/ConnectionListener.cs b/Hazel/ConnectionListener.cs index 4cd01a8..e24962a 100644 --- a/Hazel/ConnectionListener.cs +++ b/Hazel/ConnectionListener.cs @@ -3,7 +3,7 @@ using System.Collections.Generic; using System.Linq; using System.Net; using System.Text; -using System.Threading.Tasks; + namespace Hazel { diff --git a/Hazel/ConnectionStatistics.cs b/Hazel/ConnectionStatistics.cs index 450c3d6..85f40e7 100644 --- a/Hazel/ConnectionStatistics.cs +++ b/Hazel/ConnectionStatistics.cs @@ -3,7 +3,7 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; -using System.Threading.Tasks; + namespace Hazel { diff --git a/Hazel/DisconnectedEventArgs.cs b/Hazel/DisconnectedEventArgs.cs index 21e29f5..36ec2e5 100644 --- a/Hazel/DisconnectedEventArgs.cs +++ b/Hazel/DisconnectedEventArgs.cs @@ -16,7 +16,7 @@ namespace Hazel /// /// /// - public class DisconnectedEventArgs : IRecyclable + public class DisconnectedEventArgs : EventArgs, IRecyclable { /// /// Object pool for this event. diff --git a/Hazel/DocInclude/TcpListenerExample.cs b/Hazel/DocInclude/TcpListenerExample.cs index bac1fee..468452b 100644 --- a/Hazel/DocInclude/TcpListenerExample.cs +++ b/Hazel/DocInclude/TcpListenerExample.cs @@ -3,7 +3,7 @@ static void Main(string[] args) { //Setup listener - using (TcpConnectionListener listener = new TcpConnectionListener(IPAddress.Any, 4296)) + using (TcpConnectionListener listener = new TcpConnectionListener(new NetworkEndPoint(IPAddress.Any, 4296))) { //Start listening for new connection events listener.NewConnection += delegate(object sender, NewConnectionEventArgs a) diff --git a/Hazel/DocInclude/UdpListenerExample.cs b/Hazel/DocInclude/UdpListenerExample.cs index 73c2432..4c26c49 100644 --- a/Hazel/DocInclude/UdpListenerExample.cs +++ b/Hazel/DocInclude/UdpListenerExample.cs @@ -3,7 +3,7 @@ static void Main(string[] args) { //Setup listener - using (UdpConnectionListener listener = new UdpConnectionListener(IPAddress.Any, 4296)) + using (UdpConnectionListener listener = new UdpConnectionListener(new NetworkEndPoint(IPAddress.Any, 4296))) { //Start listening for new connection events listener.NewConnection += delegate(object sender, NewConnectionEventArgs a) diff --git a/Hazel/Hazel.csproj b/Hazel/Hazel.csproj index 1bf7a70..2b143af 100644 --- a/Hazel/Hazel.csproj +++ b/Hazel/Hazel.csproj @@ -9,8 +9,9 @@ Properties Hazel Hazel - v4.5 + v3.5 512 + true @@ -44,7 +45,6 @@ - diff --git a/Hazel/IPMode.cs b/Hazel/IPMode.cs index 1c8c482..04c8c38 100644 --- a/Hazel/IPMode.cs +++ b/Hazel/IPMode.cs @@ -2,7 +2,7 @@ using System.Collections.Generic; using System.Linq; using System.Text; -using System.Threading.Tasks; + namespace Hazel { @@ -22,8 +22,9 @@ namespace Hazel IPv4, /// - /// Instruction to use dual mode sockets, both IPv4 and IPv6 connections will be able to connect. + /// Instruction to use IPv6 only, IPv4 connections will not be able to connect. IPv4 addresses can be connected + /// by converting to IPv6 addresses. /// - IPv4AndIPv6 + IPv6 } } diff --git a/Hazel/NetworkConnection.cs b/Hazel/NetworkConnection.cs index 18a660e..7aef3e4 100644 --- a/Hazel/NetworkConnection.cs +++ b/Hazel/NetworkConnection.cs @@ -3,7 +3,7 @@ using System.Collections.Generic; using System.Linq; using System.Net; using System.Text; -using System.Threading.Tasks; + namespace Hazel { @@ -21,5 +21,10 @@ namespace Hazel /// as the base does. /// public EndPoint RemoteEndPoint { get; protected set; } + + /// + /// The IPMode the client is connected using. + /// + public IPMode IPMode { get; protected set; } } } diff --git a/Hazel/NetworkConnectionListener.cs b/Hazel/NetworkConnectionListener.cs index 30be505..1e5e237 100644 --- a/Hazel/NetworkConnectionListener.cs +++ b/Hazel/NetworkConnectionListener.cs @@ -3,7 +3,7 @@ using System.Collections.Generic; using System.Linq; using System.Net; using System.Text; -using System.Threading.Tasks; + namespace Hazel { @@ -14,13 +14,13 @@ namespace Hazel public abstract class NetworkConnectionListener : ConnectionListener { /// - /// The local IP address the listener is listening for new clients on. + /// The local end point the listener is listening for new clients on. /// - public IPAddress IPAddress { get; protected set; } + public EndPoint EndPoint { get; protected set; } /// - /// The port the listener is listening for new clients on. + /// The IPMode the listener is listening for new clients on. /// - public int Port { get; protected set; } + public IPMode IPMode { get; protected set; } } } diff --git a/Hazel/NetworkEndPoint.cs b/Hazel/NetworkEndPoint.cs index 7e71171..b6b6427 100644 --- a/Hazel/NetworkEndPoint.cs +++ b/Hazel/NetworkEndPoint.cs @@ -2,7 +2,7 @@ using System.Collections.Generic; using System.Linq; using System.Text; -using System.Threading.Tasks; + using System.Net; @@ -32,7 +32,7 @@ namespace Hazel /// /// The end point to wrap. /// The IP mode to use. - public NetworkEndPoint(EndPoint endPoint, IPMode mode = IPMode.IPv4AndIPv6) + public NetworkEndPoint(EndPoint endPoint, IPMode mode = IPMode.IPv4) { this.EndPoint = endPoint; this.IPMode = mode; @@ -47,8 +47,8 @@ namespace Hazel /// /// When using this constructor will contain an . /// - public NetworkEndPoint(IPAddress address, int port, IPMode mode = IPMode.IPv4AndIPv6) - : this(new IPEndPoint(address, port)) + public NetworkEndPoint(IPAddress address, int port, IPMode mode = IPMode.IPv4) + : this(new IPEndPoint(address, port), mode) { } @@ -62,8 +62,8 @@ namespace Hazel /// /// When using this constructor will contain an . /// - public NetworkEndPoint(string IP, int port, IPMode mode = IPMode.IPv4AndIPv6) - : this(IPAddress.Parse(IP), port) + public NetworkEndPoint(string IP, int port, IPMode mode = IPMode.IPv4) + : this(IPAddress.Parse(IP), port, mode) { } diff --git a/Hazel/ObjectPool.cs b/Hazel/ObjectPool.cs index 7a410e4..a4d8d68 100644 --- a/Hazel/ObjectPool.cs +++ b/Hazel/ObjectPool.cs @@ -1,9 +1,11 @@ using System; +#if NET_45 using System.Collections.Concurrent; +#else using System.Collections.Generic; +#endif using System.Linq; using System.Text; -using System.Threading.Tasks; namespace Hazel { @@ -17,7 +19,11 @@ namespace Hazel /// /// Our pool of objects /// +#if NET_45 ConcurrentBag pool = new ConcurrentBag(); +#else + Queue pool = new Queue(); +#endif /// /// The generator for creating new objects. @@ -39,10 +45,17 @@ namespace Hazel /// An instance of T. internal T GetObject() { +#if NET_45 T item; if (pool.TryTake(out item)) return item; - +#else + lock (pool) + { + if (pool.Count > 0) + return pool.Dequeue(); + } +#endif return objectFactory.Invoke(); } @@ -52,7 +65,12 @@ namespace Hazel /// The item to return. internal void PutObject(T item) { +#if NET_45 pool.Add(item); +#else + lock (pool) + pool.Enqueue(item); +#endif } } } diff --git a/Hazel/SendOption.cs b/Hazel/SendOption.cs index 2629711..a81fded 100644 --- a/Hazel/SendOption.cs +++ b/Hazel/SendOption.cs @@ -2,7 +2,6 @@ using System.Collections.Generic; using System.Linq; using System.Text; -using System.Threading.Tasks; namespace Hazel { diff --git a/Hazel/SendOptionInternal.cs b/Hazel/SendOptionInternal.cs index e39e7e5..2a7a707 100644 --- a/Hazel/SendOptionInternal.cs +++ b/Hazel/SendOptionInternal.cs @@ -2,7 +2,7 @@ using System.Collections.Generic; using System.Linq; using System.Text; -using System.Threading.Tasks; + namespace Hazel { diff --git a/Hazel/Tcp/StateObject.cs b/Hazel/Tcp/StateObject.cs index c5878f9..8c4ac20 100644 --- a/Hazel/Tcp/StateObject.cs +++ b/Hazel/Tcp/StateObject.cs @@ -2,7 +2,6 @@ using System.Collections.Generic; using System.Linq; using System.Text; -using System.Threading.Tasks; namespace Hazel.Tcp { diff --git a/Hazel/Tcp/TcpConnection.cs b/Hazel/Tcp/TcpConnection.cs index 62e32cb..2507870 100644 --- a/Hazel/Tcp/TcpConnection.cs +++ b/Hazel/Tcp/TcpConnection.cs @@ -58,6 +58,7 @@ namespace Hazel.Tcp this.EndPoint = remoteEndPoint; this.RemoteEndPoint = remoteEndPoint.EndPoint; + this.IPMode = remoteEndPoint.IPMode; //Create a socket if (remoteEndPoint.IPMode == IPMode.IPv4) @@ -68,12 +69,9 @@ namespace Hazel.Tcp throw new HazelException("IPV6 not supported!"); socket = new Socket(AddressFamily.InterNetworkV6, SocketType.Stream, System.Net.Sockets.ProtocolType.Tcp); + socket.SetSocketOption(SocketOptionLevel.IPv6, (SocketOptionName)27, false); } - //Set parameters of socket - if (remoteEndPoint.IPMode == IPMode.IPv4AndIPv6) - socket.DualMode = true; - socket.NoDelay = true; } } @@ -349,7 +347,7 @@ namespace Hazel.Tcp if (socket.Connected) socket.Shutdown(SocketShutdown.Send); - socket.Dispose(); + socket.Close(); } } diff --git a/Hazel/Tcp/TcpConnectionListener.cs b/Hazel/Tcp/TcpConnectionListener.cs index 055febe..78b7fa0 100644 --- a/Hazel/Tcp/TcpConnectionListener.cs +++ b/Hazel/Tcp/TcpConnectionListener.cs @@ -4,7 +4,7 @@ using System.Linq; using System.Net; using System.Net.Sockets; using System.Text; -using System.Threading.Tasks; + namespace Hazel.Tcp { @@ -25,12 +25,23 @@ namespace Hazel.Tcp /// The IPAddress to listen on. /// The port to listen on. /// The to listen with. - public TcpConnectionListener(IPAddress IPAddress, int port, IPMode mode = IPMode.IPv4AndIPv6) + [Obsolete("Temporary constructor in beta only, use NetworkEndPoint constructor instead.")] + public TcpConnectionListener(IPAddress IPAddress, int port, IPMode mode = IPMode.IPv4) + : this (new NetworkEndPoint(IPAddress, port, mode)) + { + + } + + /// + /// Creates a new TcpConnectionListener for the given , port and . + /// + /// The end point to listen on. + public TcpConnectionListener(NetworkEndPoint endPoint) { - this.IPAddress = IPAddress; - this.Port = port; + this.EndPoint = endPoint.EndPoint; + this.IPMode = endPoint.IPMode; - if (mode == IPMode.IPv4) + if (endPoint.IPMode == IPMode.IPv4) this.listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); else { @@ -38,10 +49,8 @@ namespace Hazel.Tcp throw new HazelException("IPV6 not supported!"); this.listener = new Socket(AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp); + this.listener.SetSocketOption(SocketOptionLevel.IPv6, (SocketOptionName)27, false); } - - if (mode == IPMode.IPv4AndIPv6) - this.listener.DualMode = true; } /// @@ -51,7 +60,7 @@ namespace Hazel.Tcp { lock (listener) { - listener.Bind(new IPEndPoint(IPAddress, Port)); + listener.Bind(EndPoint); listener.Listen(1000); listener.BeginAccept(AcceptConnection, null); @@ -102,7 +111,7 @@ namespace Hazel.Tcp if (disposing) { lock (listener) - listener.Dispose(); + listener.Close(); } base.Dispose(disposing); diff --git a/Hazel/Udp/UdpClientConnection.cs b/Hazel/Udp/UdpClientConnection.cs index c0df18b..f8836fb 100644 --- a/Hazel/Udp/UdpClientConnection.cs +++ b/Hazel/Udp/UdpClientConnection.cs @@ -5,7 +5,7 @@ using System.Net; using System.Net.Sockets; using System.Text; using System.Threading; -using System.Threading.Tasks; + namespace Hazel.Udp { @@ -41,13 +41,17 @@ namespace Hazel.Udp { this.EndPoint = remoteEndPoint; this.RemoteEndPoint = remoteEndPoint.EndPoint; + this.IPMode = remoteEndPoint.IPMode; if (remoteEndPoint.IPMode == IPMode.IPv4) socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); else { + if (!Socket.OSSupportsIPv6) + throw new HazelException("IPV6 not supported!"); + socket = new Socket(AddressFamily.InterNetworkV6, SocketType.Dgram, ProtocolType.Udp); - socket.DualMode = true; + socket.SetSocketOption(SocketOptionLevel.IPv6, (SocketOptionName)27, false); //TODO these lines shouldn't be needed anymore } } } @@ -96,7 +100,10 @@ namespace Hazel.Udp //Begin listening try { - socket.Bind(new IPEndPoint(IPAddress.Any, 0)); + if (IPMode == IPMode.IPv4) + socket.Bind(new IPEndPoint(IPAddress.Any, 0)); + else + socket.Bind(new IPEndPoint(IPAddress.IPv6Any, 0)); } catch (SocketException e) { @@ -232,7 +239,7 @@ namespace Hazel.Udp { State = ConnectionState.NotConnected; - socket.Dispose(); + socket.Close(); } } diff --git a/Hazel/Udp/UdpConnection.KeepAlive.cs b/Hazel/Udp/UdpConnection.KeepAlive.cs index f2864f7..d789a48 100644 --- a/Hazel/Udp/UdpConnection.KeepAlive.cs +++ b/Hazel/Udp/UdpConnection.KeepAlive.cs @@ -4,7 +4,7 @@ using System.Diagnostics; using System.Linq; using System.Text; using System.Threading; -using System.Threading.Tasks; + namespace Hazel.Udp { diff --git a/Hazel/Udp/UdpConnection.Reliable.cs b/Hazel/Udp/UdpConnection.Reliable.cs index 05d6b89..f3217f4 100644 --- a/Hazel/Udp/UdpConnection.Reliable.cs +++ b/Hazel/Udp/UdpConnection.Reliable.cs @@ -4,7 +4,7 @@ using System.Diagnostics; using System.Linq; using System.Text; using System.Threading; -using System.Threading.Tasks; + namespace Hazel.Udp { diff --git a/Hazel/Udp/UdpConnectionListener.cs b/Hazel/Udp/UdpConnectionListener.cs index 6d0c9c8..066975d 100644 --- a/Hazel/Udp/UdpConnectionListener.cs +++ b/Hazel/Udp/UdpConnectionListener.cs @@ -4,7 +4,7 @@ using System.Linq; using System.Net; using System.Net.Sockets; using System.Text; -using System.Threading.Tasks; + namespace Hazel.Udp { @@ -19,8 +19,6 @@ namespace Hazel.Udp /// Socket listener; - IPEndPoint bindOn; - /// /// Buffer to store incoming data in. /// @@ -32,24 +30,36 @@ namespace Hazel.Udp Dictionary connections = new Dictionary(); /// - /// Creates a new ConnectionListener for the given , port and . + /// Creates a new UdpConnectionListener for the given , port and . /// /// The IPAddress to listen on. /// The port to listen on. /// The to listen with. - public UdpConnectionListener(IPAddress IPAddress, int port, IPMode mode = IPMode.IPv4AndIPv6) + [Obsolete("Temporary constructor in beta only, use NetworkEndPoint constructor instead.")] + public UdpConnectionListener(IPAddress IPAddress, int port, IPMode mode = IPMode.IPv4) + : this (new NetworkEndPoint(IPAddress, port, mode)) { - this.IPAddress = IPAddress; - this.Port = port; - this.bindOn = new IPEndPoint(IPAddress, Port); + } + + /// + /// Creates a new UdpConnectionListener for the given , port and . + /// + /// The endpoint to listen on. + public UdpConnectionListener(NetworkEndPoint endPoint) + { + this.EndPoint = endPoint.EndPoint; + this.IPMode = endPoint.IPMode; - if (mode == IPMode.IPv4) + if (endPoint.IPMode == IPMode.IPv4) this.listener = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); else { + if (!Socket.OSSupportsIPv6) + throw new HazelException("IPV6 not supported!"); + this.listener = new Socket(AddressFamily.InterNetworkV6, SocketType.Dgram, ProtocolType.Udp); - this.listener.DualMode = true; + this.listener.SetSocketOption(SocketOptionLevel.IPv6, (SocketOptionName)27, false); } } @@ -59,7 +69,7 @@ namespace Hazel.Udp try { lock (listener) - listener.Bind(bindOn); + listener.Bind(EndPoint); } catch (SocketException e) { @@ -74,7 +84,7 @@ namespace Hazel.Udp /// void StartListeningForData() { - EndPoint remoteEP = (EndPoint)bindOn; + EndPoint remoteEP = EndPoint; try { @@ -94,7 +104,7 @@ namespace Hazel.Udp void ReadCallback(IAsyncResult result) { int bytesReceived; - EndPoint remoteEndPoint = new IPEndPoint(IPAddress.Any, 0); + EndPoint remoteEndPoint = new IPEndPoint(IPMode == IPMode.IPv4 ? IPAddress.Any : IPAddress.IPv6Any, 0); //End the receive operation try @@ -142,7 +152,7 @@ namespace Hazel.Udp if (buffer[0] != (byte)SendOptionInternal.Hello || buffer.Length != 3) return; - connection = new UdpServerConnection(this, remoteEndPoint); + connection = new UdpServerConnection(this, remoteEndPoint, IPMode); connections.Add(remoteEndPoint, connection); //Then ping back an ack to make sure they're happy (unless we rejected them...) @@ -200,7 +210,7 @@ namespace Hazel.Udp if (disposing) { lock (listener) - listener.Dispose(); + listener.Close(); } base.Dispose(disposing); diff --git a/Hazel/Udp/UdpServerConnection.cs b/Hazel/Udp/UdpServerConnection.cs index eca3c3e..4d3419a 100644 --- a/Hazel/Udp/UdpServerConnection.cs +++ b/Hazel/Udp/UdpServerConnection.cs @@ -3,7 +3,7 @@ using System.Collections.Generic; using System.Linq; using System.Net; using System.Text; -using System.Threading.Tasks; + namespace Hazel.Udp { @@ -32,12 +32,14 @@ namespace Hazel.Udp /// /// The listener that created this connection. /// The endpoint that we are connected to. - internal UdpServerConnection(UdpConnectionListener listener, EndPoint endPoint) + /// The IPMode we are connected using. + internal UdpServerConnection(UdpConnectionListener listener, EndPoint endPoint, IPMode IPMode) : base() { this.Listener = listener; this.RemoteEndPoint = endPoint; this.EndPoint = new NetworkEndPoint(endPoint); + this.IPMode = IPMode; State = ConnectionState.Connected; } -- 2.39.5