connection.Connect(new NetworkEndPoint(IPAddress.Loopback, 4296));
connection.KeepAliveInterval = 100;
- System.Threading.Thread.Sleep(1100); //Enough time for 10 keep alive packets
+ System.Threading.Thread.Sleep(1100); //Enough time for ~10 keep alive packets
- Assert.AreEqual(33, connection.Statistics.TotalBytesSent);
+ Assert.IsTrue(
+ connection.Statistics.TotalBytesSent >= 27
+ && connection.Statistics.TotalBytesSent <= 33,
+ "Received: " + connection.Statistics.TotalBytesSent
+ );
}
}
{
((UdpConnection)args.Connection).KeepAliveInterval = 100;
- Thread.Sleep(1100); //Enough time for 10 keep alive packets
+ Thread.Sleep(1100); //Enough time for ~10 keep alive packets
+
+ Assert.IsTrue(
+ args.Connection.Statistics.TotalBytesSent >= 27
+ && args.Connection.Statistics.TotalBytesSent <= 33,
+ "Received: " + args.Connection.Statistics.TotalBytesSent
+ );
- Assert.AreEqual(30, args.Connection.Statistics.TotalBytesSent);
mutex.Set();
};
<Compile Include="DataEventArgs.cs" />
<Compile Include="DisconnectedEventArgs.cs" />
<Compile Include="HazelException.cs" />
+ <Compile Include="NetworkConnection.cs" />
+ <Compile Include="NetworkConnectionListener.cs" />
<Compile Include="NetworkEndPoint.cs" />
<Compile Include="NewConnectionEventArgs.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
--- /dev/null
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Net;
+using System.Text;
+using System.Threading.Tasks;
+
+/*
+* Copyright (C) Jamie Read - All Rights Reserved
+* Unauthorized copying of this file, via any medium is strictly prohibited
+* Proprietary and confidential
+* Written by Jamie Read <jamie.read@outlook.com>, January 2016
+*/
+
+namespace Hazel
+{
+ /// <summary>
+ /// A connection to a remote end point via a network protocol.
+ /// </summary>
+ public abstract class NetworkConnection : Connection
+ {
+ /// <summary>
+ /// The remote end point of this connection.
+ /// </summary>
+ public EndPoint RemoteEndPoint { get; protected set; }
+ }
+}
--- /dev/null
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Net;
+using System.Text;
+using System.Threading.Tasks;
+
+/*
+* Copyright (C) Jamie Read - All Rights Reserved
+* Unauthorized copying of this file, via any medium is strictly prohibited
+* Proprietary and confidential
+* Written by Jamie Read <jamie.read@outlook.com>, January 2016
+*/
+
+namespace Hazel
+{
+ /// <summary>
+ /// Connection listener for network based connections.
+ /// </summary>
+ public abstract class NetworkConnectionListener : ConnectionListener
+ {
+ /// <summary>
+ /// The IP address we're listening on.
+ /// </summary>
+ public IPAddress IPAddress { get; protected set; }
+
+ /// <summary>
+ /// The port we're listening on.
+ /// </summary>
+ public int Port { get; protected set; }
+ }
+}
/// <summary>
/// Represents a connection that uses the TCP protocol.
/// </summary>
- public class TcpConnection : Connection
+ public class TcpConnection : NetworkConnection
{
/// <summary>
/// The socket we're managing.
/// </summary>
public Socket Socket { get; private set; }
- /// <summary>
- /// The remote end point of this connection.
- /// </summary>
- public EndPoint RemoteEndPoint { get; protected set; }
-
/// <summary>
/// Creates a TcpConnection from a given TCP Socket.
/// </summary>
/// <summary>
/// Listens for new TCP connections and creates TCPConnections for them.
/// </summary>
- public class TcpConnectionListener : ConnectionListener
+ public class TcpConnectionListener : NetworkConnectionListener
{
- /// <summary>
- /// The IP address we're listening on.
- /// </summary>
- public IPAddress IPAddress { get; private set; }
-
- /// <summary>
- /// The port we're listening on.
- /// </summary>
- public int Port { get; private set; }
-
/// <summary>
/// The socket listening for connections.
/// </summary>
- public Socket Listener { get; private set; }
+ Socket listener;
/// <summary>
/// Creates a new ConnectionListener for the given IP and port.
this.IPAddress = IPAddress;
this.Port = port;
- this.Listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
+ this.listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
}
/// <summary>
{
try
{
- lock (Listener)
+ lock (listener)
{
- Listener.Bind(new IPEndPoint(IPAddress, Port));
- Listener.Listen(1000);
+ listener.Bind(new IPEndPoint(IPAddress, Port));
+ listener.Listen(1000);
- Listener.BeginAccept(AcceptConnection, null);
+ listener.BeginAccept(AcceptConnection, null);
}
}
catch (SocketException e)
/// <param name="result">The asyncronous operation's result.</param>
void AcceptConnection(IAsyncResult result)
{
- lock (Listener)
+ lock (listener)
{
//Accept Tcp socket
Socket tcpSocket;
try
{
- tcpSocket = Listener.EndAccept(result);
+ tcpSocket = listener.EndAccept(result);
}
catch (ObjectDisposedException)
{
}
//Start listening for the next connection
- Listener.BeginAccept(new AsyncCallback(AcceptConnection), null);
+ listener.BeginAccept(new AsyncCallback(AcceptConnection), null);
//Sort the event out
TcpConnection tcpConnection = new TcpConnection(tcpSocket);
{
if (disposing)
{
- lock (Listener)
- Listener.Dispose();
+ lock (listener)
+ listener.Dispose();
}
base.Dispose(disposing);
/// <summary>
/// Represents a connection that uses the UDP protocol.
/// </summary>
- public abstract partial class UdpConnection : Connection
+ public abstract partial class UdpConnection : NetworkConnection
{
- /// <summary>
- /// The remote end point of this connection.
- /// </summary>
- public EndPoint RemoteEndPoint { get; protected set; }
-
/// <summary>
/// Writes the given bytes to the connection.
/// </summary>
namespace Hazel
{
/// <summary>
- /// Listens for new UDP connections and creates UdpConnection for them.
+ /// Listens for new UDP connections and creates UdpConnections for them.
/// </summary>
- public class UdpConnectionListener : ConnectionListener
+ public class UdpConnectionListener : NetworkConnectionListener
{
- /// <summary>
- /// The IP address we're listening on.
- /// </summary>
- public IPAddress IPAddress { get; private set; }
-
- /// <summary>
- /// The port we're listening on.
- /// </summary>
- public int Port { get; private set; }
-
/// <summary>
/// The socket listening for connections.
/// </summary>
+++ /dev/null
-<VSPerformanceSession Version="1.00">
- <Options>
- <CollectionMethod>Sampling</CollectionMethod>
- <AllocationMethod>None</AllocationMethod>
- <AddReport>true</AddReport>
- <UniqueReport>Timestamp</UniqueReport>
- <SamplingMethod>Cycles</SamplingMethod>
- <CycleCount>10000000</CycleCount>
- <PageFaultCount>10</PageFaultCount>
- <SysCallCount>10</SysCallCount>
- <SamplingCounter PlatformID="00000000" CounterID="0000000000000000" ReloadValue="000000000000000a" />
- <RelocateBinaries>false</RelocateBinaries>
- <HardwareCounters EnableHWCounters="false" />
- </Options>
- <PreinstrumentEvent>
- <InstrEventExclude>false</InstrEventExclude>
- </PreinstrumentEvent>
- <PostinstrumentEvent>
- <InstrEventExclude>false</InstrEventExclude>
- </PostinstrumentEvent>
-</VSPerformanceSession>