Skip to content

Commit

Permalink
Bugfixes in host connection params setup
Browse files Browse the repository at this point in the history
  • Loading branch information
Sandoun committed Jul 19, 2022
1 parent 38f0f9f commit 6c7c368
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 15 deletions.
17 changes: 9 additions & 8 deletions Examples/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,30 +97,31 @@ static void Scenario2 () {

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);
}
interf.Disconnect();
}
//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);
}
Expand Down
24 changes: 17 additions & 7 deletions MewtocolNet/Mewtocol/MewtocolInterface.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public partial class MewtocolInterface : INotifyPropertyChanged, IDisposable {
/// </summary>
public event PropertyChangedEventHandler PropertyChanged;

private int connectTimeout = 1000;
private int connectTimeout = 3000;
/// <summary>
/// The initial connection timeout in milliseconds
/// </summary>
Expand Down Expand Up @@ -277,31 +277,40 @@ private async Task ConnectTCP () {
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) {
Expand Down Expand Up @@ -720,10 +729,11 @@ private async Task<string> SendSingleBlock (string _blockString) {

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
Expand Down Expand Up @@ -759,7 +769,7 @@ private async Task<string> SendSingleBlock (string _blockString) {
}

} catch (IOException) {
Logger.Log($"Critical IO exception on receive", LogLevel.Critical, this);
OnMajorSocketExceptionWhileConnected();
return null;
} catch (SocketException) {
OnMajorSocketExceptionWhileConnected();
Expand Down

0 comments on commit 6c7c368

Please sign in to comment.