]> git.deb.at Git - rhonda/impostor.hazel.git/commitdiff
Abstracted NetworkConnections
authorJamJar00 <jamster.30@btinternet.com>
Sun, 15 May 2016 22:05:13 +0000 (23:05 +0100)
committerJamJar00 <jamster.30@btinternet.com>
Sun, 15 May 2016 22:05:13 +0000 (23:05 +0100)
Hazel.UnitTests/UdpConnectionTests.cs
Hazel/Hazel.csproj
Hazel/NetworkConnection.cs [new file with mode: 0644]
Hazel/NetworkConnectionListener.cs [new file with mode: 0644]
Hazel/TcpConnection.cs
Hazel/TcpConnectionListener.cs
Hazel/UdpConnection.cs
Hazel/UdpConnectionListener.cs
Performance1.psess [deleted file]

index a6ce448a9a56f4954e69f8e399e432d4d5e3dedc..fff0b4a10ca3a01cd761fd1849787d566be82ca0 100644 (file)
@@ -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();
                 };
 
index efadf841ae9decf6ffde90ed399b955f7b4adb78..b77da7b9ee6f52a4d2bfffd8bab506b829930ec9 100644 (file)
@@ -52,6 +52,8 @@
     <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" />
diff --git a/Hazel/NetworkConnection.cs b/Hazel/NetworkConnection.cs
new file mode 100644 (file)
index 0000000..7280000
--- /dev/null
@@ -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 <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; }
+    }
+}
diff --git a/Hazel/NetworkConnectionListener.cs b/Hazel/NetworkConnectionListener.cs
new file mode 100644 (file)
index 0000000..db52d69
--- /dev/null
@@ -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 <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; }
+    }
+}
index 04e8598077b739215ea59747ec2cf73e7256384b..71eec3a092ef97488c934d0c808b60c73ed2717f 100644 (file)
@@ -17,18 +17,13 @@ namespace Hazel
     /// <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>
index 09af39e7b0e7a592941724b8fdfa058d7cd7324b..f651008bcf3cb482f36ad946658575a848eacd0d 100644 (file)
@@ -18,22 +18,12 @@ namespace Hazel
     /// <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.
@@ -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);
         }
 
         /// <summary>
@@ -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
         /// <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)
                 {
@@ -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);
index 4e880c0d6b2baa4ae21dcb6490b8dcdfe775606e..f5ac140896fc8d63f2cd1b184bc12e9f7218592c 100644 (file)
@@ -18,13 +18,8 @@ namespace Hazel
     /// <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>
index da2c06b112262b59626c5a476e416f1b41ce59b5..69981a21fe1ca3c8090518e14fe0344c7bb4a857 100644 (file)
@@ -16,20 +16,10 @@ using System.Threading.Tasks;
 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>
diff --git a/Performance1.psess b/Performance1.psess
deleted file mode 100644 (file)
index 7dc9d39..0000000
+++ /dev/null
@@ -1,21 +0,0 @@
-<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>