From 6c7c368b55914f5e670297e0c56b870668a331d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felix=20Wei=C3=9F?= <72068105+Sandoun@users.noreply.github.com> Date: Tue, 19 Jul 2022 17:37:10 +0200 Subject: [PATCH] Bugfixes in host connection params setup --- Examples/Program.cs | 17 ++++++++-------- MewtocolNet/Mewtocol/MewtocolInterface.cs | 24 ++++++++++++++++------- 2 files changed, 26 insertions(+), 15 deletions(-) diff --git a/Examples/Program.cs b/Examples/Program.cs index 0ecfb80..c442672 100644 --- a/Examples/Program.cs +++ b/Examples/Program.cs @@ -97,14 +97,14 @@ class Program { Task.Factory.StartNew(async () => { - using(var interf = new MewtocolInterface("10.237.191.3")) { + //automatic endpoint + using (var interf = new MewtocolInterface("10.237.191.3")) { await interf.ConnectAsync(); - if(interf.IsConnected) { + if (interf.IsConnected) { - var plcInf = await interf.GetPLCInfoAsync(); - Console.WriteLine(plcInf); + await Task.Delay(5000); } @@ -112,15 +112,16 @@ class Program { } - + //manual endpoint using (var interf = new MewtocolInterface("10.237.191.3")) { + interf.HostEndpoint = new System.Net.IPEndPoint(System.Net.IPAddress.Parse("10.237.191.77"), 0); + await interf.ConnectAsync(); - if (interf.IsConnected) { + if(interf.IsConnected) { - var plcInf = await interf.GetPLCInfoAsync(); - Console.WriteLine(plcInf); + await Task.Delay(5000); } diff --git a/MewtocolNet/Mewtocol/MewtocolInterface.cs b/MewtocolNet/Mewtocol/MewtocolInterface.cs index eb0c63a..4c54900 100644 --- a/MewtocolNet/Mewtocol/MewtocolInterface.cs +++ b/MewtocolNet/Mewtocol/MewtocolInterface.cs @@ -43,7 +43,7 @@ namespace MewtocolNet { /// public event PropertyChangedEventHandler PropertyChanged; - private int connectTimeout = 1000; + private int connectTimeout = 3000; /// /// The initial connection timeout in milliseconds /// @@ -277,31 +277,40 @@ namespace MewtocolNet { try { if(HostEndpoint != null) { + client = new TcpClient(HostEndpoint) { ReceiveBufferSize = RecBufferSize, NoDelay = false, - ExclusiveAddressUse = true, }; + var ep = (IPEndPoint)client.Client.LocalEndPoint; + Logger.Log($"Connecting [MAN] endpoint: {ep.Address}:{ep.Port}", LogLevel.Verbose, this); + } else { + client = new TcpClient() { ReceiveBufferSize = RecBufferSize, NoDelay = false, ExclusiveAddressUse = true, }; + } var result = client.BeginConnect(targetIP, port, null, null); var success = result.AsyncWaitHandle.WaitOne(TimeSpan.FromMilliseconds(ConnectTimeout)); - if(!success) { + if(!success || !client.Connected) { OnMajorSocketExceptionWhileConnecting(); return; } + if(HostEndpoint == null) { + var ep = (IPEndPoint)client.Client.LocalEndPoint; + Logger.Log($"Connecting [AUTO] endpoint: {ep.Address.MapToIPv4()}:{ep.Port}", LogLevel.Verbose, this); + } + stream = client.GetStream(); stream.ReadTimeout = 1000; - Console.WriteLine($"Connected {client.Connected}"); await Task.CompletedTask; } catch (SocketException) { @@ -720,10 +729,11 @@ namespace MewtocolNet { if (client == null || !client.Connected ) { await ConnectTCP(); - if (!client.Connected) - return null; } + if (client == null || !client.Connected) + return null; + var message = _blockString.ToHexASCIIBytes(); //send request @@ -759,7 +769,7 @@ namespace MewtocolNet { } } catch (IOException) { - Logger.Log($"Critical IO exception on receive", LogLevel.Critical, this); + OnMajorSocketExceptionWhileConnected(); return null; } catch (SocketException) { OnMajorSocketExceptionWhileConnected();