<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Hazel.UnitTests</RootNamespace>
<AssemblyName>Hazel.UnitTests</AssemblyName>
- <TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
+ <TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<ProjectTypeGuids>{3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">10.0</VisualStudioVersion>
<ReferencePath>$(ProgramFiles)\Common Files\microsoft shared\VSTT\$(VisualStudioVersion)\UITestExtensionPackages</ReferencePath>
<IsCodedUITest>False</IsCodedUITest>
<TestProjectType>UnitTest</TestProjectType>
+ <TargetFrameworkProfile />
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
/// Tests dual mode connectivity.
/// </summary>
[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();
}
{
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();
[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();
/// Tests dual mode connectivity.
/// </summary>
[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();
}
[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);
[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);
[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);
[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);
[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();
{
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)
[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);
[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);
using System.Linq;
using System.Net;
using System.Text;
-using System.Threading.Tasks;
+
namespace Hazel
{
using System.Linq;
using System.Text;
using System.Threading;
-using System.Threading.Tasks;
+
namespace Hazel
{
/// <include file="DocInclude/common.xml" path="docs/item[@name='Recyclable']/*" />
/// </remarks>
/// <threadsafety static="true" instance="true"/>
- public class DisconnectedEventArgs : IRecyclable
+ public class DisconnectedEventArgs : EventArgs, IRecyclable
{
/// <summary>
/// Object pool for this event.
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)
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)
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Hazel</RootNamespace>
<AssemblyName>Hazel</AssemblyName>
- <TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
+ <TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
+ <TargetFrameworkProfile />
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
- <Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
</ItemGroup>
using System.Collections.Generic;
using System.Linq;
using System.Text;
-using System.Threading.Tasks;
+
namespace Hazel
{
IPv4,
/// <summary>
- /// 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.
/// </summary>
- IPv4AndIPv6
+ IPv6
}
}
using System.Linq;
using System.Net;
using System.Text;
-using System.Threading.Tasks;
+
namespace Hazel
{
/// <see cref="ConnectionEndPoint"/> as the base <see cref="Connection"/> does.
/// </remarks>
public EndPoint RemoteEndPoint { get; protected set; }
+
+ /// <summary>
+ /// The <see cref="IPMode">IPMode</see> the client is connected using.
+ /// </summary>
+ public IPMode IPMode { get; protected set; }
}
}
using System.Linq;
using System.Net;
using System.Text;
-using System.Threading.Tasks;
+
namespace Hazel
{
public abstract class NetworkConnectionListener : ConnectionListener
{
/// <summary>
- /// The local IP address the listener is listening for new clients on.
+ /// The local end point the listener is listening for new clients on.
/// </summary>
- public IPAddress IPAddress { get; protected set; }
+ public EndPoint EndPoint { get; protected set; }
/// <summary>
- /// The port the listener is listening for new clients on.
+ /// The <see cref="IPMode">IPMode</see> the listener is listening for new clients on.
/// </summary>
- public int Port { get; protected set; }
+ public IPMode IPMode { get; protected set; }
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
-using System.Threading.Tasks;
+
using System.Net;
/// </summary>
/// <param name="endPoint">The end point to wrap.</param>
/// <param name="mode">The IP mode to use.</param>
- public NetworkEndPoint(EndPoint endPoint, IPMode mode = IPMode.IPv4AndIPv6)
+ public NetworkEndPoint(EndPoint endPoint, IPMode mode = IPMode.IPv4)
{
this.EndPoint = endPoint;
this.IPMode = mode;
/// <remarks>
/// When using this constructor <see cref="EndPoint"/> will contain an <see cref="IPEndPoint"/>.
/// </remarks>
- 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)
{
}
/// <remarks>
/// When using this constructor <see cref="EndPoint"/> will contain an <see cref="IPEndPoint"/>.
/// </remarks>
- 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)
{
}
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
{
/// <summary>
/// Our pool of objects
/// </summary>
+#if NET_45
ConcurrentBag<T> pool = new ConcurrentBag<T>();
+#else
+ Queue<T> pool = new Queue<T>();
+#endif
/// <summary>
/// The generator for creating new objects.
/// <returns>An instance of T.</returns>
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();
}
/// <param name="item">The item to return.</param>
internal void PutObject(T item)
{
+#if NET_45
pool.Add(item);
+#else
+ lock (pool)
+ pool.Enqueue(item);
+#endif
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
-using System.Threading.Tasks;
namespace Hazel
{
using System.Collections.Generic;
using System.Linq;
using System.Text;
-using System.Threading.Tasks;
+
namespace Hazel
{
using System.Collections.Generic;
using System.Linq;
using System.Text;
-using System.Threading.Tasks;
namespace Hazel.Tcp
{
this.EndPoint = remoteEndPoint;
this.RemoteEndPoint = remoteEndPoint.EndPoint;
+ this.IPMode = remoteEndPoint.IPMode;
//Create a socket
if (remoteEndPoint.IPMode == IPMode.IPv4)
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;
}
}
if (socket.Connected)
socket.Shutdown(SocketShutdown.Send);
- socket.Dispose();
+ socket.Close();
}
}
using System.Net;
using System.Net.Sockets;
using System.Text;
-using System.Threading.Tasks;
+
namespace Hazel.Tcp
{
/// <param name="IPAddress">The IPAddress to listen on.</param>
/// <param name="port">The port to listen on.</param>
/// <param name="mode">The <see cref="IPMode"/> to listen with.</param>
- 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))
+ {
+
+ }
+
+ /// <summary>
+ /// Creates a new TcpConnectionListener for the given <see cref="IPAddress"/>, port and <see cref="IPMode"/>.
+ /// </summary>
+ /// <param name="endPoint">The end point to listen on.</param>
+ 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
{
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;
}
/// <inheritdoc />
{
lock (listener)
{
- listener.Bind(new IPEndPoint(IPAddress, Port));
+ listener.Bind(EndPoint);
listener.Listen(1000);
listener.BeginAccept(AcceptConnection, null);
if (disposing)
{
lock (listener)
- listener.Dispose();
+ listener.Close();
}
base.Dispose(disposing);
using System.Net.Sockets;
using System.Text;
using System.Threading;
-using System.Threading.Tasks;
+
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
}
}
}
//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)
{
{
State = ConnectionState.NotConnected;
- socket.Dispose();
+ socket.Close();
}
}
using System.Linq;
using System.Text;
using System.Threading;
-using System.Threading.Tasks;
+
namespace Hazel.Udp
{
using System.Linq;
using System.Text;
using System.Threading;
-using System.Threading.Tasks;
+
namespace Hazel.Udp
{
using System.Net;
using System.Net.Sockets;
using System.Text;
-using System.Threading.Tasks;
+
namespace Hazel.Udp
{
/// </summary>
Socket listener;
- IPEndPoint bindOn;
-
/// <summary>
/// Buffer to store incoming data in.
/// </summary>
Dictionary<EndPoint, UdpServerConnection> connections = new Dictionary<EndPoint, UdpServerConnection>();
/// <summary>
- /// Creates a new ConnectionListener for the given <see cref="IPAddress"/>, port and <see cref="IPMode"/>.
+ /// Creates a new UdpConnectionListener for the given <see cref="IPAddress"/>, port and <see cref="IPMode"/>.
/// </summary>
/// <param name="IPAddress">The IPAddress to listen on.</param>
/// <param name="port">The port to listen on.</param>
/// <param name="mode">The <see cref="IPMode"/> to listen with.</param>
- 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);
+ }
+
+ /// <summary>
+ /// Creates a new UdpConnectionListener for the given <see cref="IPAddress"/>, port and <see cref="IPMode"/>.
+ /// </summary>
+ /// <param name="endPoint">The endpoint to listen on.</param>
+ 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);
}
}
try
{
lock (listener)
- listener.Bind(bindOn);
+ listener.Bind(EndPoint);
}
catch (SocketException e)
{
/// </summary>
void StartListeningForData()
{
- EndPoint remoteEP = (EndPoint)bindOn;
+ EndPoint remoteEP = EndPoint;
try
{
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
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...)
if (disposing)
{
lock (listener)
- listener.Dispose();
+ listener.Close();
}
base.Dispose(disposing);
using System.Linq;
using System.Net;
using System.Text;
-using System.Threading.Tasks;
+
namespace Hazel.Udp
{
/// </summary>
/// <param name="listener">The listener that created this connection.</param>
/// <param name="endPoint">The endpoint that we are connected to.</param>
- internal UdpServerConnection(UdpConnectionListener listener, EndPoint endPoint)
+ /// <param name="IPMode">The IPMode we are connected using.</param>
+ 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;
}