From 79d1dc1c50da09aaa87b9a7b988a203ade5816f1 Mon Sep 17 00:00:00 2001 From: Scott Lyons Date: Mon, 3 Apr 2017 09:09:19 +0100 Subject: [PATCH 1/2] Added interfaces to allow for mocks in unit testing --- SimpleTCP/Message.cs | 12 +++++++++++- SimpleTCP/SimpleTcpClient.cs | 15 ++++++++++++++- SimpleTCP/SimpleTcpServer.cs | 23 ++++++++++++++++++++++- 3 files changed, 47 insertions(+), 3 deletions(-) diff --git a/SimpleTCP/Message.cs b/SimpleTCP/Message.cs index d9d9a6a..130a84d 100644 --- a/SimpleTCP/Message.cs +++ b/SimpleTCP/Message.cs @@ -7,7 +7,17 @@ namespace SimpleTCP { - public class Message + public interface IMessage + { + byte[] Data { get; } + string MessageString { get; } + void Reply(byte[] data); + void Reply(string data); + void ReplyLine(string data); + TcpClient TcpClient { get; } + } + + public class Message : IMessage { private TcpClient _tcpClient; private System.Text.Encoding _encoder = null; diff --git a/SimpleTCP/SimpleTcpClient.cs b/SimpleTCP/SimpleTcpClient.cs index 5c66a93..a20ff90 100644 --- a/SimpleTCP/SimpleTcpClient.cs +++ b/SimpleTCP/SimpleTcpClient.cs @@ -9,7 +9,20 @@ namespace SimpleTCP { - public class SimpleTcpClient : IDisposable + public interface ISimpleTcpClient + { + byte Delimiter { get; set; } + Encoding StringEncoder { get; set; } + bool AutoTrimStrings { get; set; } + SimpleTcpClient Connect(string hostNameOrIpAddress, int port); + SimpleTcpClient Disconnect(); + TcpClient TcpClient { get; } + void Write(byte[] data); + void Write(string data); + Message WriteLineAndGetReply(string data, TimeSpan timeout); + } + + public class SimpleTcpClient : ISimpleTcpClient, IDisposable { public SimpleTcpClient() { diff --git a/SimpleTCP/SimpleTcpServer.cs b/SimpleTCP/SimpleTcpServer.cs index 15d1509..b13518d 100644 --- a/SimpleTCP/SimpleTcpServer.cs +++ b/SimpleTCP/SimpleTcpServer.cs @@ -11,7 +11,28 @@ namespace SimpleTCP { - public class SimpleTcpServer + public interface ISimpleTcpServer + { + byte Delimiter { get; set; } + Encoding StringEncoder { get; set; } + bool AutoTrimStrings { get; set; } + event EventHandler ClientConnected; + event EventHandler ClientDisconnected; + event EventHandler DelimiterDataReceived; + event EventHandler DataReceived; + IEnumerable GetIPAddresses(); + List GetListeningIPs(); + void Broadcast(byte[] data); + void Broadcast(string data); + void BroadcastLine(string data); + SimpleTcpServer Start(int port, bool ignoreNicsWithOccupiedPorts = true); + SimpleTcpServer Start(int port, AddressFamily addressFamilyFilter); + SimpleTcpServer Start(IPAddress ipAddress, int port); + void Stop(); + int ConnectedClientsCount { get; } + } + + public class SimpleTcpServer : ISimpleTcpServer { public SimpleTcpServer() { From bdcf9d14c81d7e0a795211257e464b2f4f03ae32 Mon Sep 17 00:00:00 2001 From: "ESENDEX\\scott.lyons" Date: Wed, 31 May 2017 15:07:50 +0100 Subject: [PATCH 2/2] Moved IDisposable inheritance to ISimpleTcpClient --- SimpleTCP/SimpleTcpClient.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/SimpleTCP/SimpleTcpClient.cs b/SimpleTCP/SimpleTcpClient.cs index a20ff90..f432476 100644 --- a/SimpleTCP/SimpleTcpClient.cs +++ b/SimpleTCP/SimpleTcpClient.cs @@ -9,7 +9,7 @@ namespace SimpleTCP { - public interface ISimpleTcpClient + public interface ISimpleTcpClient : IDisposable { byte Delimiter { get; set; } Encoding StringEncoder { get; set; } @@ -22,7 +22,7 @@ public interface ISimpleTcpClient Message WriteLineAndGetReply(string data, TimeSpan timeout); } - public class SimpleTcpClient : ISimpleTcpClient, IDisposable + public class SimpleTcpClient : ISimpleTcpClient { public SimpleTcpClient() {