}
}
+ /// <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>
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);
}
}
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);
}
}
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);
//Connect
connection.Connect(new NetworkEndPoint(IPAddress.Loopback, 4296));
- connection.WriteBytes(data, sendOption);
+ connection.SendBytes(data, sendOption);
//Wait until data is received
mutex.WaitOne();
}
}
+ /// <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>
/// <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.
--- /dev/null
+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();
+ }
+ }
+}
--- /dev/null
+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
+ }
+}
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)
{