From: JamJar00 Date: Sun, 15 May 2016 22:05:13 +0000 (+0100) Subject: Abstracted NetworkConnections X-Git-Tag: 1.0.0~158 X-Git-Url: https://git.deb.at/?a=commitdiff_plain;h=82ab70bc285d8e43134ee750f791f5251b367d36;p=rhonda%2Fimpostor.hazel.git Abstracted NetworkConnections --- diff --git a/Hazel.UnitTests/UdpConnectionTests.cs b/Hazel.UnitTests/UdpConnectionTests.cs index a6ce448..fff0b4a 100644 --- a/Hazel.UnitTests/UdpConnectionTests.cs +++ b/Hazel.UnitTests/UdpConnectionTests.cs @@ -98,9 +98,13 @@ namespace Hazel.UnitTests 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 + ); } } @@ -119,9 +123,14 @@ namespace Hazel.UnitTests { ((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(); }; diff --git a/Hazel/Hazel.csproj b/Hazel/Hazel.csproj index efadf84..b77da7b 100644 --- a/Hazel/Hazel.csproj +++ b/Hazel/Hazel.csproj @@ -52,6 +52,8 @@ + + diff --git a/Hazel/NetworkConnection.cs b/Hazel/NetworkConnection.cs new file mode 100644 index 0000000..7280000 --- /dev/null +++ b/Hazel/NetworkConnection.cs @@ -0,0 +1,27 @@ +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 , January 2016 +*/ + +namespace Hazel +{ + /// + /// A connection to a remote end point via a network protocol. + /// + public abstract class NetworkConnection : Connection + { + /// + /// The remote end point of this connection. + /// + public EndPoint RemoteEndPoint { get; protected set; } + } +} diff --git a/Hazel/NetworkConnectionListener.cs b/Hazel/NetworkConnectionListener.cs new file mode 100644 index 0000000..db52d69 --- /dev/null +++ b/Hazel/NetworkConnectionListener.cs @@ -0,0 +1,32 @@ +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 , January 2016 +*/ + +namespace Hazel +{ + /// + /// Connection listener for network based connections. + /// + public abstract class NetworkConnectionListener : ConnectionListener + { + /// + /// The IP address we're listening on. + /// + public IPAddress IPAddress { get; protected set; } + + /// + /// The port we're listening on. + /// + public int Port { get; protected set; } + } +} diff --git a/Hazel/TcpConnection.cs b/Hazel/TcpConnection.cs index 04e8598..71eec3a 100644 --- a/Hazel/TcpConnection.cs +++ b/Hazel/TcpConnection.cs @@ -17,18 +17,13 @@ namespace Hazel /// /// Represents a connection that uses the TCP protocol. /// - public class TcpConnection : Connection + public class TcpConnection : NetworkConnection { /// /// The socket we're managing. /// public Socket Socket { get; private set; } - /// - /// The remote end point of this connection. - /// - public EndPoint RemoteEndPoint { get; protected set; } - /// /// Creates a TcpConnection from a given TCP Socket. /// diff --git a/Hazel/TcpConnectionListener.cs b/Hazel/TcpConnectionListener.cs index 09af39e..f651008 100644 --- a/Hazel/TcpConnectionListener.cs +++ b/Hazel/TcpConnectionListener.cs @@ -18,22 +18,12 @@ namespace Hazel /// /// Listens for new TCP connections and creates TCPConnections for them. /// - public class TcpConnectionListener : ConnectionListener + public class TcpConnectionListener : NetworkConnectionListener { - /// - /// The IP address we're listening on. - /// - public IPAddress IPAddress { get; private set; } - - /// - /// The port we're listening on. - /// - public int Port { get; private set; } - /// /// The socket listening for connections. /// - public Socket Listener { get; private set; } + Socket listener; /// /// Creates a new ConnectionListener for the given IP and port. @@ -45,7 +35,7 @@ namespace Hazel 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); } /// @@ -55,12 +45,12 @@ namespace Hazel { 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) @@ -75,13 +65,13 @@ namespace Hazel /// The asyncronous operation's result. void AcceptConnection(IAsyncResult result) { - lock (Listener) + lock (listener) { //Accept Tcp socket Socket tcpSocket; try { - tcpSocket = Listener.EndAccept(result); + tcpSocket = listener.EndAccept(result); } catch (ObjectDisposedException) { @@ -90,7 +80,7 @@ namespace Hazel } //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); @@ -111,8 +101,8 @@ namespace Hazel { if (disposing) { - lock (Listener) - Listener.Dispose(); + lock (listener) + listener.Dispose(); } base.Dispose(disposing); diff --git a/Hazel/UdpConnection.cs b/Hazel/UdpConnection.cs index 4e880c0..f5ac140 100644 --- a/Hazel/UdpConnection.cs +++ b/Hazel/UdpConnection.cs @@ -18,13 +18,8 @@ namespace Hazel /// /// Represents a connection that uses the UDP protocol. /// - public abstract partial class UdpConnection : Connection + public abstract partial class UdpConnection : NetworkConnection { - /// - /// The remote end point of this connection. - /// - public EndPoint RemoteEndPoint { get; protected set; } - /// /// Writes the given bytes to the connection. /// diff --git a/Hazel/UdpConnectionListener.cs b/Hazel/UdpConnectionListener.cs index da2c06b..69981a2 100644 --- a/Hazel/UdpConnectionListener.cs +++ b/Hazel/UdpConnectionListener.cs @@ -16,20 +16,10 @@ using System.Threading.Tasks; namespace Hazel { /// - /// Listens for new UDP connections and creates UdpConnection for them. + /// Listens for new UDP connections and creates UdpConnections for them. /// - public class UdpConnectionListener : ConnectionListener + public class UdpConnectionListener : NetworkConnectionListener { - /// - /// The IP address we're listening on. - /// - public IPAddress IPAddress { get; private set; } - - /// - /// The port we're listening on. - /// - public int Port { get; private set; } - /// /// The socket listening for connections. /// diff --git a/Performance1.psess b/Performance1.psess deleted file mode 100644 index 7dc9d39..0000000 --- a/Performance1.psess +++ /dev/null @@ -1,21 +0,0 @@ - - - Sampling - None - true - Timestamp - Cycles - 10000000 - 10 - 10 - - false - - - - false - - - false - -