From aa18c535030eafced51c24f48b3c4ba7a8c1d4d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?FactorySI=20Solu=C3=A7=C3=B5es=20Integradas?= Date: Sat, 13 Jun 2020 14:44:15 -0300 Subject: [PATCH 01/10] added to the udp protocol control and management project --- SimpleTCP/MessagemUdp.cs | 53 ++++++++++++ SimpleTCP/SimpleTCP.csproj | 2 + SimpleTCP/SimpleUdpClient.cs | 151 +++++++++++++++++++++++++++++++++++ 3 files changed, 206 insertions(+) create mode 100644 SimpleTCP/MessagemUdp.cs create mode 100644 SimpleTCP/SimpleUdpClient.cs diff --git a/SimpleTCP/MessagemUdp.cs b/SimpleTCP/MessagemUdp.cs new file mode 100644 index 0000000..fb1841a --- /dev/null +++ b/SimpleTCP/MessagemUdp.cs @@ -0,0 +1,53 @@ +using System.Linq; +using System.Net.Sockets; +using System.Text; + +namespace SimpleTCP +{ + public class MessagemUdp + { + private UdpClient _udpClient; + private Encoding _encoder; + private byte _writeLineDelimiter; + private bool _autoTrim; + + public byte[] Data { get; } + + public UdpClient TcpClient => _udpClient; + + internal MessagemUdp(byte[] data, UdpClient udpClient, Encoding stringEncoder, byte lineDelimiter, bool autoTrim) + { + Data = data; + _udpClient = udpClient; + _encoder = stringEncoder; + _writeLineDelimiter = lineDelimiter; + _autoTrim = autoTrim; + } + + public string MessageString => _autoTrim ? _encoder.GetString(Data).Trim() : _encoder.GetString(Data); + + public void Reply(byte[] data) + { + _udpClient.Send(data, data.Length); + } + + public void Reply(string data) + { + if (string.IsNullOrEmpty(data)) { return; } + Reply(_encoder.GetBytes(data)); + } + + public void ReplyLine(string data) + { + if (string.IsNullOrEmpty(data)) { return; } + if (data.LastOrDefault() != _writeLineDelimiter) + { + Reply(data + _encoder.GetString(new[] { _writeLineDelimiter })); + } + else + { + Reply(data); + } + } + } +} \ No newline at end of file diff --git a/SimpleTCP/SimpleTCP.csproj b/SimpleTCP/SimpleTCP.csproj index bc5ae86..76ab4b3 100644 --- a/SimpleTCP/SimpleTCP.csproj +++ b/SimpleTCP/SimpleTCP.csproj @@ -42,12 +42,14 @@ + + diff --git a/SimpleTCP/SimpleUdpClient.cs b/SimpleTCP/SimpleUdpClient.cs new file mode 100644 index 0000000..0dd87f6 --- /dev/null +++ b/SimpleTCP/SimpleUdpClient.cs @@ -0,0 +1,151 @@ +using System; +using System.Collections.Generic; +using System.Net; +using System.Net.Sockets; +using System.Text; +using System.Threading; + +namespace SimpleTCP +{ + public class SimpleUdpClient : IDisposable + { + private Thread _thread; + + internal bool QueueStop { get; set; } + + public bool Connected { get; private set; } + + public byte Delimiter { get; set; } + + public UdpClient UdpClient { get; private set; } + + public IPEndPoint EndPoint { get; set; } + + public SimpleUdpClient() + { + Connected = false; + Delimiter = 0x13; + } + + public event EventHandler DataReceived; + + public void Connect(string hostName, int port) + { + try + { + EndPoint = new IPEndPoint(IPAddress.Parse(hostName), port); + + UdpClient = new UdpClient(hostName, port); + UdpClient.Connect(hostName, port); + + Connected = UdpClient.Client.Connected; + + if (!Connected) return; + if (_thread != null) return; + + _thread = new Thread(ListLoop) { IsBackground = true }; + _thread.Start(); + + } + catch (Exception erro) + { + throw new Exception(erro.Message); + } + } + + public void Write(byte[] dados) + { + try + { + if (UdpClient?.Client == null) return; + if (!Connected) return; + if (!UdpClient.Client.Connected) return; + UdpClient?.Send(dados, dados.Length); + } + catch (Exception erro) + { + throw new Exception(erro.Message); + } + } + + public void Disconnect() + { + try + { + QueueStop = false; + Connected = false; + UdpClient?.Close(); + } + catch (Exception erro) + { + throw new Exception(erro.Message); + } + } + + private void ListLoop() + { + while (!QueueStop) + { + try + { + RunLoopStep(); + } + catch + { + // + } + + Thread.Sleep(10); + } + + _thread = null; + } + + private void RunLoopStep() + { + if (UdpClient == null) { return; } + if (UdpClient.Client.Connected == false) { return; } + var c = UdpClient; + + var bytesAvailable = c.Available; + if (bytesAvailable == 0) + { + Thread.Sleep(10); + return; + } + + var bytesReceived = new List(); + + var rec = EndPoint; + + while (c.Available > 0 && UdpClient.Client.Connected) + { + bytesReceived.AddRange(c.Receive(ref rec)); + } + + if (bytesReceived.Count > 0) + { + NotifyEndTransmissionRx(c, bytesReceived.ToArray()); + } + } + + private void NotifyEndTransmissionRx(UdpClient client, byte[] msg) + { + if (DataReceived == null) return; + var m = new MessagemUdp(msg, client, Encoding.ASCII, Delimiter, false); + DataReceived(this, m); + } + + public void Dispose() + { + QueueStop = false; + UdpClient.Close(); + ((IDisposable)UdpClient)?.Dispose(); + } + + protected virtual void OnDataReceived(MessagemUdp e) + { + DataReceived?.Invoke(this, e); + } + } +} From ef995e0d46fcb7f9b666cff9872638967d71c35d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?FactorySI=20Solu=C3=A7=C3=B5es=20Integradas?= Date: Tue, 16 Jun 2020 15:16:03 -0300 Subject: [PATCH 02/10] =?UTF-8?q?Implementando=20controle=20de=20conex?= =?UTF-8?q?=C3=A3o=20na=20thread=20RunLoopStep=20para=20Udp?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- SimpleTCP/SimpleUdpClient.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/SimpleTCP/SimpleUdpClient.cs b/SimpleTCP/SimpleUdpClient.cs index 0dd87f6..cbbe27a 100644 --- a/SimpleTCP/SimpleUdpClient.cs +++ b/SimpleTCP/SimpleUdpClient.cs @@ -104,6 +104,7 @@ private void ListLoop() private void RunLoopStep() { if (UdpClient == null) { return; } + if (UdpClient.Client == null) { return; } if (UdpClient.Client.Connected == false) { return; } var c = UdpClient; From 96c8b8140be3f0a35941a798a5b618a38d74d178 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?FactorySI=20Solu=C3=A7=C3=B5es=20Integradas?= Date: Thu, 18 Jun 2020 13:43:54 -0300 Subject: [PATCH 03/10] Adicionado um novo procedimento para envio de dados em forma de byte[] no metodo WriteLineAndGetReply --- SimpleTCP/SimpleTcpClient.cs | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/SimpleTCP/SimpleTcpClient.cs b/SimpleTCP/SimpleTcpClient.cs index 5c66a93..9df1939 100644 --- a/SimpleTCP/SimpleTcpClient.cs +++ b/SimpleTCP/SimpleTcpClient.cs @@ -3,9 +3,7 @@ using System.Diagnostics; using System.Linq; using System.Net.Sockets; -using System.Text; using System.Threading; -using System.Threading.Tasks; namespace SimpleTCP { @@ -184,6 +182,24 @@ public Message WriteLineAndGetReply(string data, TimeSpan timeout) return mReply; } + public Message WriteLineAndGetReply(byte[] data, TimeSpan timeout) + { + Message mReply = null; + this.DataReceived += (s, e) => { mReply = e; }; + Write(data); + + Stopwatch sw = new Stopwatch(); + sw.Start(); + + while (mReply == null && sw.Elapsed < timeout) + { + System.Threading.Thread.Sleep(10); + } + + return mReply; + } + + #region IDisposable Support private bool disposedValue = false; // To detect redundant calls From 074622fc9384e7d3e0d80fd1be167ea35e707906 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?FactorySI=20Solu=C3=A7=C3=B5es=20Integradas?= Date: Tue, 23 Jun 2020 13:25:39 -0300 Subject: [PATCH 04/10] =?UTF-8?q?Implementando=20Heran=C3=A7a=20de=20Class?= =?UTF-8?q?=20do=20TcpClient=20para=20TcpClienteX,=20para=20dar=20suporte?= =?UTF-8?q?=20a=20nome=20no=20socket?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- SimpleTCP/SimpleTCP.csproj | 1 + SimpleTCP/SimpleTcpClient.cs | 12 ++++++------ SimpleTCP/TcpClientX.cs | 9 +++++++++ 3 files changed, 16 insertions(+), 6 deletions(-) create mode 100644 SimpleTCP/TcpClientX.cs diff --git a/SimpleTCP/SimpleTCP.csproj b/SimpleTCP/SimpleTCP.csproj index 76ab4b3..21237a7 100644 --- a/SimpleTCP/SimpleTCP.csproj +++ b/SimpleTCP/SimpleTCP.csproj @@ -50,6 +50,7 @@ + diff --git a/SimpleTCP/SimpleTcpClient.cs b/SimpleTCP/SimpleTcpClient.cs index 9df1939..e712705 100644 --- a/SimpleTCP/SimpleTcpClient.cs +++ b/SimpleTCP/SimpleTcpClient.cs @@ -7,7 +7,7 @@ namespace SimpleTCP { - public class SimpleTcpClient : IDisposable + public class SimpleTcpClient : IDisposable { public SimpleTcpClient() { @@ -20,7 +20,7 @@ public SimpleTcpClient() private List _queuedMsg = new List(); public byte Delimiter { get; set; } public System.Text.Encoding StringEncoder { get; set; } - private TcpClient _client = null; + private TcpClientX _client = null; public event EventHandler DelimiterDataReceived; public event EventHandler DataReceived; @@ -36,8 +36,8 @@ public SimpleTcpClient Connect(string hostNameOrIpAddress, int port) throw new ArgumentNullException("hostNameOrIpAddress"); } - _client = new TcpClient(); - _client.Connect(hostNameOrIpAddress, port); + _client = new TcpClientX(); + _client.Connect(hostNameOrIpAddress, port); StartRxThread(); @@ -61,7 +61,7 @@ public SimpleTcpClient Disconnect() return this; } - public TcpClient TcpClient { get { return _client; } } + public TcpClientX TcpClient { get { return _client; } } private void ListenerLoop(object state) { @@ -143,7 +143,7 @@ private void NotifyEndTransmissionRx(TcpClient client, byte[] msg) public void Write(byte[] data) { if (_client == null) { throw new Exception("Cannot send data to a null TcpClient (check to see if Connect was called)"); } - _client.GetStream().Write(data, 0, data.Length); + _client.GetStream().Write(data, 0, data.Length); } public void Write(string data) diff --git a/SimpleTCP/TcpClientX.cs b/SimpleTCP/TcpClientX.cs new file mode 100644 index 0000000..8e2db15 --- /dev/null +++ b/SimpleTCP/TcpClientX.cs @@ -0,0 +1,9 @@ +using System.Net.Sockets; + +namespace SimpleTCP +{ + public class TcpClientX : TcpClient + { + public string Name { get; set; } + } +} \ No newline at end of file From 42005ab98777bb626ff147b6df2efa29503eb3f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?FactorySI=20Solu=C3=A7=C3=B5es=20Integradas?= Date: Tue, 23 Jun 2020 13:30:33 -0300 Subject: [PATCH 05/10] Adicionado suporte ao Tcp Servidor TcpClientX por sobre carga --- SimpleTCP/SimpleTcpServer.cs | 130 ++++++++++++++++++++++------------- 1 file changed, 82 insertions(+), 48 deletions(-) diff --git a/SimpleTCP/SimpleTcpServer.cs b/SimpleTCP/SimpleTcpServer.cs index bdafc3e..30edd6b 100644 --- a/SimpleTCP/SimpleTcpServer.cs +++ b/SimpleTCP/SimpleTcpServer.cs @@ -1,13 +1,10 @@ using System; -using System.Collections.Concurrent; using System.Collections.Generic; using System.Linq; using System.Net; using System.Net.NetworkInformation; using System.Net.Sockets; -using System.Text; using System.Threading; -using System.Threading.Tasks; namespace SimpleTCP { @@ -33,9 +30,9 @@ public IEnumerable GetIPAddresses() { List ipAddresses = new List(); - IEnumerable enabledNetInterfaces = NetworkInterface.GetAllNetworkInterfaces() - .Where(nic => nic.OperationalStatus == OperationalStatus.Up); - foreach (NetworkInterface netInterface in enabledNetInterfaces) + IEnumerable enabledNetInterfaces = NetworkInterface.GetAllNetworkInterfaces() + .Where(nic => nic.OperationalStatus == OperationalStatus.Up); + foreach (NetworkInterface netInterface in enabledNetInterfaces) { IPInterfaceProperties ipProps = netInterface.GetIPProperties(); foreach (UnicastIPAddressInformation addr in ipProps.UnicastAddresses) @@ -64,10 +61,10 @@ public List GetListeningIPs() return listenIps.OrderByDescending(ip => RankIpAddress(ip)).ToList(); } - + public void Broadcast(byte[] data) { - foreach(var client in _listeners.SelectMany(x => x.ConnectedClients)) + foreach (var client in _listeners.SelectMany(x => x.ConnectedClients)) { client.GetStream().Write(data, 0, data.Length); } @@ -150,28 +147,28 @@ private static IEnumerable TryGetCurrentNetworkInterfaces() public SimpleTcpServer Start(int port, bool ignoreNicsWithOccupiedPorts = true) { var ipSorted = GetIPAddresses(); - bool anyNicFailed = false; + bool anyNicFailed = false; foreach (var ipAddr in ipSorted) { - try - { - Start(ipAddr, port); - } - catch (SocketException ex) - { - DebugInfo(ex.ToString()); - anyNicFailed = true; - } + try + { + Start(ipAddr, port); + } + catch (SocketException ex) + { + DebugInfo(ex.ToString()); + anyNicFailed = true; + } } - if (!IsStarted) - throw new InvalidOperationException("Port was already occupied for all network interfaces"); + if (!IsStarted) + throw new InvalidOperationException("Port was already occupied for all network interfaces"); - if (anyNicFailed && !ignoreNicsWithOccupiedPorts) - { - Stop(); - throw new InvalidOperationException("Port was already occupied for one or more network interfaces."); - } + if (anyNicFailed && !ignoreNicsWithOccupiedPorts) + { + Stop(); + throw new InvalidOperationException("Port was already occupied for one or more network interfaces."); + } return this; } @@ -191,9 +188,9 @@ public SimpleTcpServer Start(int port, AddressFamily addressFamilyFilter) return this; } - public bool IsStarted { get { return _listeners.Any(l => l.Listener.Active); } } + public bool IsStarted { get { return _listeners.Any(l => l.Listener.Active); } } - public SimpleTcpServer Start(IPAddress ipAddress, int port) + public SimpleTcpServer Start(IPAddress ipAddress, int port) { Server.ServerListener listener = new Server.ServerListener(this, ipAddress, port); _listeners.Add(listener); @@ -203,20 +200,31 @@ public SimpleTcpServer Start(IPAddress ipAddress, int port) public void Stop() { - _listeners.All(l => l.QueueStop = true); - while (_listeners.Any(l => l.Listener.Active)){ - Thread.Sleep(100); - }; + _listeners.All(l => l.QueueStop = true); + while (_listeners.Any(l => l.Listener.Active)) + { + Thread.Sleep(100); + }; _listeners.Clear(); } public int ConnectedClientsCount { - get { + get + { return _listeners.Sum(l => l.ConnectedClientsCount); } } + internal void NotifyDelimiterMessageRx(Server.ServerListener listener, TcpClientX client, byte[] msg) + { + if (DelimiterDataReceived != null) + { + Message m = new Message(msg, client, StringEncoder, Delimiter, AutoTrimStrings); + DelimiterDataReceived(this, m); + } + } + internal void NotifyDelimiterMessageRx(Server.ServerListener listener, TcpClient client, byte[] msg) { if (DelimiterDataReceived != null) @@ -226,6 +234,15 @@ internal void NotifyDelimiterMessageRx(Server.ServerListener listener, TcpClient } } + internal void NotifyEndTransmissionRx(Server.ServerListener listener, TcpClientX client, byte[] msg) + { + if (DataReceived != null) + { + Message m = new Message(msg, client, StringEncoder, Delimiter, AutoTrimStrings); + DataReceived(this, m); + } + } + internal void NotifyEndTransmissionRx(Server.ServerListener listener, TcpClient client, byte[] msg) { if (DataReceived != null) @@ -235,6 +252,14 @@ internal void NotifyEndTransmissionRx(Server.ServerListener listener, TcpClient } } + internal void NotifyClientConnected(Server.ServerListener listener, TcpClientX newClient) + { + if (ClientConnected != null) + { + ClientConnected(this, newClient); + } + } + internal void NotifyClientConnected(Server.ServerListener listener, TcpClient newClient) { if (ClientConnected != null) @@ -243,6 +268,14 @@ internal void NotifyClientConnected(Server.ServerListener listener, TcpClient ne } } + internal void NotifyClientDisconnected(Server.ServerListener listener, TcpClientX disconnectedClient) + { + if (ClientDisconnected != null) + { + ClientDisconnected(this, disconnectedClient); + } + } + internal void NotifyClientDisconnected(Server.ServerListener listener, TcpClient disconnectedClient) { if (ClientDisconnected != null) @@ -251,20 +284,21 @@ internal void NotifyClientDisconnected(Server.ServerListener listener, TcpClient } } - #region Debug logging - - [System.Diagnostics.Conditional("DEBUG")] - void DebugInfo(string format, params object[] args) - { - if (_debugInfoTime == null) - { - _debugInfoTime = new System.Diagnostics.Stopwatch(); - _debugInfoTime.Start(); - } - System.Diagnostics.Debug.WriteLine(_debugInfoTime.ElapsedMilliseconds + ": " + format, args); - } - System.Diagnostics.Stopwatch _debugInfoTime; - - #endregion Debug logging - } + + #region Debug logging + + [System.Diagnostics.Conditional("DEBUG")] + void DebugInfo(string format, params object[] args) + { + if (_debugInfoTime == null) + { + _debugInfoTime = new System.Diagnostics.Stopwatch(); + _debugInfoTime.Start(); + } + System.Diagnostics.Debug.WriteLine(_debugInfoTime.ElapsedMilliseconds + ": " + format, args); + } + System.Diagnostics.Stopwatch _debugInfoTime; + + #endregion Debug logging + } } From 848e7a1adcfbb7062db2dc780213cc7859a7e87d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?FactorySI=20Solu=C3=A7=C3=B5es=20Integradas?= Date: Tue, 23 Jun 2020 13:38:50 -0300 Subject: [PATCH 06/10] Corrigido Carregamdo de Class TcpClientX --- SimpleTCP.Tests/ServerTests.cs | 5 ++-- SimpleTCP/Server/ServerListener.cs | 8 +++---- SimpleTCP/SimpleTcpServer.cs | 37 +++--------------------------- 3 files changed, 10 insertions(+), 40 deletions(-) diff --git a/SimpleTCP.Tests/ServerTests.cs b/SimpleTCP.Tests/ServerTests.cs index feea838..9e6619c 100644 --- a/SimpleTCP.Tests/ServerTests.cs +++ b/SimpleTCP.Tests/ServerTests.cs @@ -14,9 +14,10 @@ public class ServerTests : IDisposable public ServerTests() { _server = new SimpleTcpServer().Start(_serverPort); - } + } + - public void Dispose() + public void Dispose() { if (_server.IsStarted) _server.Stop(); diff --git a/SimpleTCP/Server/ServerListener.cs b/SimpleTCP/Server/ServerListener.cs index 7f39d97..af6d7d1 100644 --- a/SimpleTCP/Server/ServerListener.cs +++ b/SimpleTCP/Server/ServerListener.cs @@ -12,8 +12,8 @@ namespace SimpleTCP.Server internal class ServerListener { private TcpListenerEx _listener = null; - private List _connectedClients = new List(); - private List _disconnectedClients = new List(); + private List _connectedClients = new List(); + private List _disconnectedClients = new List(); private SimpleTcpServer _parent = null; private List _queuedMsg = new List(); private byte _delimiter = 0x13; @@ -24,7 +24,7 @@ public int ConnectedClientsCount get { return _connectedClients.Count; } } - public IEnumerable ConnectedClients { get { return _connectedClients; } } + public IEnumerable ConnectedClients { get { return _connectedClients; } } internal ServerListener(SimpleTcpServer parentServer, IPAddress ipAddress, int port) { @@ -104,7 +104,7 @@ private void RunLoopStep() if (_listener.Pending()) { - var newClient = _listener.AcceptTcpClient(); + var newClient = _listener.AcceptTcpClient() as TcpClientX; _connectedClients.Add(newClient); _parent.NotifyClientConnected(this, newClient); } diff --git a/SimpleTCP/SimpleTcpServer.cs b/SimpleTCP/SimpleTcpServer.cs index 30edd6b..fb5f814 100644 --- a/SimpleTCP/SimpleTcpServer.cs +++ b/SimpleTCP/SimpleTcpServer.cs @@ -21,8 +21,9 @@ public SimpleTcpServer() public System.Text.Encoding StringEncoder { get; set; } public bool AutoTrimStrings { get; set; } - public event EventHandler ClientConnected; - public event EventHandler ClientDisconnected; + public event EventHandler ClientConnected; + public event EventHandler ClientDisconnected; + public event EventHandler DelimiterDataReceived; public event EventHandler DataReceived; @@ -225,14 +226,6 @@ internal void NotifyDelimiterMessageRx(Server.ServerListener listener, TcpClient } } - internal void NotifyDelimiterMessageRx(Server.ServerListener listener, TcpClient client, byte[] msg) - { - if (DelimiterDataReceived != null) - { - Message m = new Message(msg, client, StringEncoder, Delimiter, AutoTrimStrings); - DelimiterDataReceived(this, m); - } - } internal void NotifyEndTransmissionRx(Server.ServerListener listener, TcpClientX client, byte[] msg) { @@ -243,15 +236,6 @@ internal void NotifyEndTransmissionRx(Server.ServerListener listener, TcpClientX } } - internal void NotifyEndTransmissionRx(Server.ServerListener listener, TcpClient client, byte[] msg) - { - if (DataReceived != null) - { - Message m = new Message(msg, client, StringEncoder, Delimiter, AutoTrimStrings); - DataReceived(this, m); - } - } - internal void NotifyClientConnected(Server.ServerListener listener, TcpClientX newClient) { if (ClientConnected != null) @@ -260,14 +244,6 @@ internal void NotifyClientConnected(Server.ServerListener listener, TcpClientX n } } - internal void NotifyClientConnected(Server.ServerListener listener, TcpClient newClient) - { - if (ClientConnected != null) - { - ClientConnected(this, newClient); - } - } - internal void NotifyClientDisconnected(Server.ServerListener listener, TcpClientX disconnectedClient) { if (ClientDisconnected != null) @@ -276,13 +252,6 @@ internal void NotifyClientDisconnected(Server.ServerListener listener, TcpClient } } - internal void NotifyClientDisconnected(Server.ServerListener listener, TcpClient disconnectedClient) - { - if (ClientDisconnected != null) - { - ClientDisconnected(this, disconnectedClient); - } - } #region Debug logging From 0c183946c7d2e0de473802949a12dd48e7374dc6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?FactorySI=20Solu=C3=A7=C3=B5es=20Integradas?= Date: Tue, 23 Jun 2020 13:39:29 -0300 Subject: [PATCH 07/10] =?UTF-8?q?Limpeza=20de=20c=C3=B3digo?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- SimpleTCP/Server/ServerListener.cs | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/SimpleTCP/Server/ServerListener.cs b/SimpleTCP/Server/ServerListener.cs index af6d7d1..73f313b 100644 --- a/SimpleTCP/Server/ServerListener.cs +++ b/SimpleTCP/Server/ServerListener.cs @@ -1,11 +1,7 @@ -using System; -using System.Collections.Generic; -using System.Linq; +using System.Collections.Generic; using System.Net; using System.Net.Sockets; -using System.Text; using System.Threading; -using System.Threading.Tasks; namespace SimpleTCP.Server { From 1d8aef5a5684608f233ef4d8639aade4e2dc30ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?FactorySI=20Solu=C3=A7=C3=B5es=20Integradas?= Date: Tue, 23 Jun 2020 13:49:05 -0300 Subject: [PATCH 08/10] Implementando Param para o Contructor SimpleTcpClient --- SimpleTCP/SimpleTCP.csproj | 1 + SimpleTCP/SimpleTcpClient.cs | 14 ++++++++++++-- SimpleTCP/SimpleTcpParam.cs | 7 +++++++ 3 files changed, 20 insertions(+), 2 deletions(-) create mode 100644 SimpleTCP/SimpleTcpParam.cs diff --git a/SimpleTCP/SimpleTCP.csproj b/SimpleTCP/SimpleTCP.csproj index 21237a7..20fc765 100644 --- a/SimpleTCP/SimpleTCP.csproj +++ b/SimpleTCP/SimpleTCP.csproj @@ -48,6 +48,7 @@ + diff --git a/SimpleTCP/SimpleTcpClient.cs b/SimpleTCP/SimpleTcpClient.cs index e712705..939bc1c 100644 --- a/SimpleTCP/SimpleTcpClient.cs +++ b/SimpleTCP/SimpleTcpClient.cs @@ -9,13 +9,23 @@ namespace SimpleTCP { public class SimpleTcpClient : IDisposable { - public SimpleTcpClient() + private readonly SimpleTcpParam _param; + + public SimpleTcpClient() { StringEncoder = System.Text.Encoding.UTF8; ReadLoopIntervalMs = 10; Delimiter = 0x13; } + public SimpleTcpClient(SimpleTcpParam param) + { + _param = param; + StringEncoder = System.Text.Encoding.UTF8; + ReadLoopIntervalMs = 10; + Delimiter = 0x13; + } + private Thread _rxThread = null; private List _queuedMsg = new List(); public byte Delimiter { get; set; } @@ -36,7 +46,7 @@ public SimpleTcpClient Connect(string hostNameOrIpAddress, int port) throw new ArgumentNullException("hostNameOrIpAddress"); } - _client = new TcpClientX(); + _client = new TcpClientX {Name = _param?.Name}; _client.Connect(hostNameOrIpAddress, port); StartRxThread(); diff --git a/SimpleTCP/SimpleTcpParam.cs b/SimpleTCP/SimpleTcpParam.cs new file mode 100644 index 0000000..bb2333d --- /dev/null +++ b/SimpleTCP/SimpleTcpParam.cs @@ -0,0 +1,7 @@ +namespace SimpleTCP +{ + public class SimpleTcpParam + { + public string Name { get; set; } + } +} \ No newline at end of file From c9afaa11bc9650ae30a436da68bf39ebb5cf1474 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?FactorySI=20Solu=C3=A7=C3=B5es=20Integradas?= Date: Tue, 23 Jun 2020 14:50:32 -0300 Subject: [PATCH 09/10] =?UTF-8?q?Alterand=20todos=20os=20carregamentos=20p?= =?UTF-8?q?ara=20a=20vers=C3=A3o=20padr=C3=A3o?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- SimpleTCP.Tests/CommTest.cs | 16 ++++++++++++---- SimpleTCP/Server/ConnectedClient.cs | 7 +------ SimpleTCP/Server/ServerListener.cs | 9 +++++---- SimpleTCP/Server/TcpListenerEx.cs | 4 ++-- SimpleTCP/SimpleTCP.csproj | 1 - SimpleTCP/SimpleTcpClient.cs | 6 +++--- SimpleTCP/SimpleTcpServer.cs | 12 ++++++------ SimpleTCP/TcpClientX.cs | 9 --------- 8 files changed, 29 insertions(+), 35 deletions(-) delete mode 100644 SimpleTCP/TcpClientX.cs diff --git a/SimpleTCP.Tests/CommTest.cs b/SimpleTCP.Tests/CommTest.cs index 37040a3..08eb022 100644 --- a/SimpleTCP.Tests/CommTest.cs +++ b/SimpleTCP.Tests/CommTest.cs @@ -18,16 +18,24 @@ public class CommTest public void SimpleCommTest() { SimpleTcpServer server = new SimpleTcpServer().Start(8910); - SimpleTcpClient client = new SimpleTcpClient().Connect(server.GetListeningIPs().FirstOrDefault(ip => ip.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork).ToString(), 8910); + SimpleTcpClient client = new SimpleTcpClient(new SimpleTcpParam{Name = "Alex"}).Connect(server.GetListeningIPs().FirstOrDefault(ip => ip.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork).ToString(), 8910); - server.DelimiterDataReceived += (sender, msg) => { + server.ClientConnected += (sender, x) => + { + var name = x.Name; + }; + + + server.DelimiterDataReceived += (sender, msg) => + { _serverRx.Add(msg.MessageString); string serverReply = Guid.NewGuid().ToString(); msg.ReplyLine(serverReply); _serverTx.Add(serverReply); }; - client.DelimiterDataReceived += (sender, msg) => { + client.DelimiterDataReceived += (sender, msg) => + { _clientRx.Add(msg.MessageString); }; @@ -69,7 +77,7 @@ public void SimpleCommTest() Assert.IsTrue(true); - + } } } diff --git a/SimpleTCP/Server/ConnectedClient.cs b/SimpleTCP/Server/ConnectedClient.cs index c1704a2..65179f4 100644 --- a/SimpleTCP/Server/ConnectedClient.cs +++ b/SimpleTCP/Server/ConnectedClient.cs @@ -1,10 +1,5 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Net; +using System.Net; using System.Net.Sockets; -using System.Text; -using System.Threading.Tasks; namespace SimpleTCP { diff --git a/SimpleTCP/Server/ServerListener.cs b/SimpleTCP/Server/ServerListener.cs index 73f313b..f0ad15b 100644 --- a/SimpleTCP/Server/ServerListener.cs +++ b/SimpleTCP/Server/ServerListener.cs @@ -5,11 +5,12 @@ namespace SimpleTCP.Server { + internal class ServerListener { private TcpListenerEx _listener = null; - private List _connectedClients = new List(); - private List _disconnectedClients = new List(); + private List _connectedClients = new List(); + private List _disconnectedClients = new List(); private SimpleTcpServer _parent = null; private List _queuedMsg = new List(); private byte _delimiter = 0x13; @@ -20,7 +21,7 @@ public int ConnectedClientsCount get { return _connectedClients.Count; } } - public IEnumerable ConnectedClients { get { return _connectedClients; } } + public IEnumerable ConnectedClients { get { return _connectedClients; } } internal ServerListener(SimpleTcpServer parentServer, IPAddress ipAddress, int port) { @@ -100,7 +101,7 @@ private void RunLoopStep() if (_listener.Pending()) { - var newClient = _listener.AcceptTcpClient() as TcpClientX; + var newClient = _listener.AcceptTcpClient(); _connectedClients.Add(newClient); _parent.NotifyClientConnected(this, newClient); } diff --git a/SimpleTCP/Server/TcpListenerEx.cs b/SimpleTCP/Server/TcpListenerEx.cs index 1811c48..0a8a2c9 100644 --- a/SimpleTCP/Server/TcpListenerEx.cs +++ b/SimpleTCP/Server/TcpListenerEx.cs @@ -28,9 +28,9 @@ public TcpListenerEx(IPEndPoint localEP) : base(localEP) /// An that represents the local IP address. The port on which to listen for incoming connection attempts. is null. is not between and . public TcpListenerEx(IPAddress localaddr, int port) : base(localaddr, port) { - } + } - public new bool Active + public new bool Active { get { return base.Active; } } diff --git a/SimpleTCP/SimpleTCP.csproj b/SimpleTCP/SimpleTCP.csproj index 20fc765..9fb5462 100644 --- a/SimpleTCP/SimpleTCP.csproj +++ b/SimpleTCP/SimpleTCP.csproj @@ -51,7 +51,6 @@ - diff --git a/SimpleTCP/SimpleTcpClient.cs b/SimpleTCP/SimpleTcpClient.cs index 939bc1c..36fd36c 100644 --- a/SimpleTCP/SimpleTcpClient.cs +++ b/SimpleTCP/SimpleTcpClient.cs @@ -30,7 +30,7 @@ public SimpleTcpClient(SimpleTcpParam param) private List _queuedMsg = new List(); public byte Delimiter { get; set; } public System.Text.Encoding StringEncoder { get; set; } - private TcpClientX _client = null; + private TcpClient _client = null; public event EventHandler DelimiterDataReceived; public event EventHandler DataReceived; @@ -46,7 +46,7 @@ public SimpleTcpClient Connect(string hostNameOrIpAddress, int port) throw new ArgumentNullException("hostNameOrIpAddress"); } - _client = new TcpClientX {Name = _param?.Name}; + _client = new TcpClient(); _client.Connect(hostNameOrIpAddress, port); StartRxThread(); @@ -71,7 +71,7 @@ public SimpleTcpClient Disconnect() return this; } - public TcpClientX TcpClient { get { return _client; } } + public TcpClient TcpClient { get { return _client; } } private void ListenerLoop(object state) { diff --git a/SimpleTCP/SimpleTcpServer.cs b/SimpleTCP/SimpleTcpServer.cs index fb5f814..4f21057 100644 --- a/SimpleTCP/SimpleTcpServer.cs +++ b/SimpleTCP/SimpleTcpServer.cs @@ -21,8 +21,8 @@ public SimpleTcpServer() public System.Text.Encoding StringEncoder { get; set; } public bool AutoTrimStrings { get; set; } - public event EventHandler ClientConnected; - public event EventHandler ClientDisconnected; + public event EventHandler ClientConnected; + public event EventHandler ClientDisconnected; public event EventHandler DelimiterDataReceived; public event EventHandler DataReceived; @@ -217,7 +217,7 @@ public int ConnectedClientsCount } } - internal void NotifyDelimiterMessageRx(Server.ServerListener listener, TcpClientX client, byte[] msg) + internal void NotifyDelimiterMessageRx(Server.ServerListener listener, TcpClient client, byte[] msg) { if (DelimiterDataReceived != null) { @@ -227,7 +227,7 @@ internal void NotifyDelimiterMessageRx(Server.ServerListener listener, TcpClient } - internal void NotifyEndTransmissionRx(Server.ServerListener listener, TcpClientX client, byte[] msg) + internal void NotifyEndTransmissionRx(Server.ServerListener listener, TcpClient client, byte[] msg) { if (DataReceived != null) { @@ -236,7 +236,7 @@ internal void NotifyEndTransmissionRx(Server.ServerListener listener, TcpClientX } } - internal void NotifyClientConnected(Server.ServerListener listener, TcpClientX newClient) + internal void NotifyClientConnected(Server.ServerListener listener, TcpClient newClient) { if (ClientConnected != null) { @@ -244,7 +244,7 @@ internal void NotifyClientConnected(Server.ServerListener listener, TcpClientX n } } - internal void NotifyClientDisconnected(Server.ServerListener listener, TcpClientX disconnectedClient) + internal void NotifyClientDisconnected(Server.ServerListener listener, TcpClient disconnectedClient) { if (ClientDisconnected != null) { diff --git a/SimpleTCP/TcpClientX.cs b/SimpleTCP/TcpClientX.cs deleted file mode 100644 index 8e2db15..0000000 --- a/SimpleTCP/TcpClientX.cs +++ /dev/null @@ -1,9 +0,0 @@ -using System.Net.Sockets; - -namespace SimpleTCP -{ - public class TcpClientX : TcpClient - { - public string Name { get; set; } - } -} \ No newline at end of file From 9c00f27dbe5765aa52faebb8a4693ed0176bf882 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?FactorySI=20Solu=C3=A7=C3=B5es=20Integradas?= Date: Tue, 23 Jun 2020 15:13:39 -0300 Subject: [PATCH 10/10] =?UTF-8?q?Corre=C3=A7=C3=B5es?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- SimpleTCP.Tests/CommTest.cs | 6 ------ SimpleTCP/Server/TcpListenerEx.cs | 15 +++++++++++++-- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/SimpleTCP.Tests/CommTest.cs b/SimpleTCP.Tests/CommTest.cs index 08eb022..357ab81 100644 --- a/SimpleTCP.Tests/CommTest.cs +++ b/SimpleTCP.Tests/CommTest.cs @@ -20,12 +20,6 @@ public void SimpleCommTest() SimpleTcpServer server = new SimpleTcpServer().Start(8910); SimpleTcpClient client = new SimpleTcpClient(new SimpleTcpParam{Name = "Alex"}).Connect(server.GetListeningIPs().FirstOrDefault(ip => ip.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork).ToString(), 8910); - server.ClientConnected += (sender, x) => - { - var name = x.Name; - }; - - server.DelimiterDataReceived += (sender, msg) => { _serverRx.Add(msg.MessageString); diff --git a/SimpleTCP/Server/TcpListenerEx.cs b/SimpleTCP/Server/TcpListenerEx.cs index 0a8a2c9..83d4f89 100644 --- a/SimpleTCP/Server/TcpListenerEx.cs +++ b/SimpleTCP/Server/TcpListenerEx.cs @@ -19,8 +19,19 @@ public class TcpListenerEx : TcpListener /// /// An that represents the local endpoint to which to bind the listener . is null. public TcpListenerEx(IPEndPoint localEP) : base(localEP) - { - } + { + AcceptTcpClient(); + + + } + + + public TcpClient AcceptTcpClientX() + { + var a = this.AcceptTcpClient(); + + return a; + } /// /// Initializes a new instance of the class that listens for incoming connection attempts on the specified local IP address and port number.