Bugfixes in host connection params setup

This commit is contained in:
Felix Weiß
2022-07-19 17:37:10 +02:00
parent 38f0f9f523
commit 6c7c368b55
2 changed files with 26 additions and 15 deletions

View File

@@ -97,14 +97,14 @@ class Program {
Task.Factory.StartNew(async () => { 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(); await interf.ConnectAsync();
if(interf.IsConnected) { if (interf.IsConnected) {
var plcInf = await interf.GetPLCInfoAsync(); await Task.Delay(5000);
Console.WriteLine(plcInf);
} }
@@ -112,15 +112,16 @@ class Program {
} }
//manual endpoint
using (var interf = new MewtocolInterface("10.237.191.3")) { 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(); await interf.ConnectAsync();
if (interf.IsConnected) { if(interf.IsConnected) {
var plcInf = await interf.GetPLCInfoAsync(); await Task.Delay(5000);
Console.WriteLine(plcInf);
} }

View File

@@ -43,7 +43,7 @@ namespace MewtocolNet {
/// </summary> /// </summary>
public event PropertyChangedEventHandler PropertyChanged; public event PropertyChangedEventHandler PropertyChanged;
private int connectTimeout = 1000; private int connectTimeout = 3000;
/// <summary> /// <summary>
/// The initial connection timeout in milliseconds /// The initial connection timeout in milliseconds
/// </summary> /// </summary>
@@ -277,31 +277,40 @@ namespace MewtocolNet {
try { try {
if(HostEndpoint != null) { if(HostEndpoint != null) {
client = new TcpClient(HostEndpoint) { client = new TcpClient(HostEndpoint) {
ReceiveBufferSize = RecBufferSize, ReceiveBufferSize = RecBufferSize,
NoDelay = false, NoDelay = false,
ExclusiveAddressUse = true,
}; };
var ep = (IPEndPoint)client.Client.LocalEndPoint;
Logger.Log($"Connecting [MAN] endpoint: {ep.Address}:{ep.Port}", LogLevel.Verbose, this);
} else { } else {
client = new TcpClient() { client = new TcpClient() {
ReceiveBufferSize = RecBufferSize, ReceiveBufferSize = RecBufferSize,
NoDelay = false, NoDelay = false,
ExclusiveAddressUse = true, ExclusiveAddressUse = true,
}; };
} }
var result = client.BeginConnect(targetIP, port, null, null); var result = client.BeginConnect(targetIP, port, null, null);
var success = result.AsyncWaitHandle.WaitOne(TimeSpan.FromMilliseconds(ConnectTimeout)); var success = result.AsyncWaitHandle.WaitOne(TimeSpan.FromMilliseconds(ConnectTimeout));
if(!success) { if(!success || !client.Connected) {
OnMajorSocketExceptionWhileConnecting(); OnMajorSocketExceptionWhileConnecting();
return; 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 = client.GetStream();
stream.ReadTimeout = 1000; stream.ReadTimeout = 1000;
Console.WriteLine($"Connected {client.Connected}");
await Task.CompletedTask; await Task.CompletedTask;
} catch (SocketException) { } catch (SocketException) {
@@ -720,10 +729,11 @@ namespace MewtocolNet {
if (client == null || !client.Connected ) { if (client == null || !client.Connected ) {
await ConnectTCP(); await ConnectTCP();
if (!client.Connected)
return null;
} }
if (client == null || !client.Connected)
return null;
var message = _blockString.ToHexASCIIBytes(); var message = _blockString.ToHexASCIIBytes();
//send request //send request
@@ -759,7 +769,7 @@ namespace MewtocolNet {
} }
} catch (IOException) { } catch (IOException) {
Logger.Log($"Critical IO exception on receive", LogLevel.Critical, this); OnMajorSocketExceptionWhileConnected();
return null; return null;
} catch (SocketException) { } catch (SocketException) {
OnMajorSocketExceptionWhileConnected(); OnMajorSocketExceptionWhileConnected();