Skip to content

Commit 6c7c368

Browse files
committed
Bugfixes in host connection params setup
1 parent 38f0f9f commit 6c7c368

File tree

2 files changed

+26
-15
lines changed

2 files changed

+26
-15
lines changed

Examples/Program.cs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -97,30 +97,31 @@ static void Scenario2 () {
9797

9898
Task.Factory.StartNew(async () => {
9999

100-
using(var interf = new MewtocolInterface("10.237.191.3")) {
100+
//automatic endpoint
101+
using (var interf = new MewtocolInterface("10.237.191.3")) {
101102

102103
await interf.ConnectAsync();
103104

104-
if(interf.IsConnected) {
105+
if (interf.IsConnected) {
105106

106-
var plcInf = await interf.GetPLCInfoAsync();
107-
Console.WriteLine(plcInf);
107+
await Task.Delay(5000);
108108

109109
}
110110

111111
interf.Disconnect();
112112

113113
}
114114

115-
115+
//manual endpoint
116116
using (var interf = new MewtocolInterface("10.237.191.3")) {
117117

118+
interf.HostEndpoint = new System.Net.IPEndPoint(System.Net.IPAddress.Parse("10.237.191.77"), 0);
119+
118120
await interf.ConnectAsync();
119121

120-
if (interf.IsConnected) {
122+
if(interf.IsConnected) {
121123

122-
var plcInf = await interf.GetPLCInfoAsync();
123-
Console.WriteLine(plcInf);
124+
await Task.Delay(5000);
124125

125126
}
126127

MewtocolNet/Mewtocol/MewtocolInterface.cs

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public partial class MewtocolInterface : INotifyPropertyChanged, IDisposable {
4343
/// </summary>
4444
public event PropertyChangedEventHandler PropertyChanged;
4545

46-
private int connectTimeout = 1000;
46+
private int connectTimeout = 3000;
4747
/// <summary>
4848
/// The initial connection timeout in milliseconds
4949
/// </summary>
@@ -277,31 +277,40 @@ private async Task ConnectTCP () {
277277
try {
278278

279279
if(HostEndpoint != null) {
280+
280281
client = new TcpClient(HostEndpoint) {
281282
ReceiveBufferSize = RecBufferSize,
282283
NoDelay = false,
283-
ExclusiveAddressUse = true,
284284
};
285+
var ep = (IPEndPoint)client.Client.LocalEndPoint;
286+
Logger.Log($"Connecting [MAN] endpoint: {ep.Address}:{ep.Port}", LogLevel.Verbose, this);
287+
285288
} else {
289+
286290
client = new TcpClient() {
287291
ReceiveBufferSize = RecBufferSize,
288292
NoDelay = false,
289293
ExclusiveAddressUse = true,
290294
};
295+
291296
}
292297

293298
var result = client.BeginConnect(targetIP, port, null, null);
294299
var success = result.AsyncWaitHandle.WaitOne(TimeSpan.FromMilliseconds(ConnectTimeout));
295300

296-
if(!success) {
301+
if(!success || !client.Connected) {
297302
OnMajorSocketExceptionWhileConnecting();
298303
return;
299304
}
300305

306+
if(HostEndpoint == null) {
307+
var ep = (IPEndPoint)client.Client.LocalEndPoint;
308+
Logger.Log($"Connecting [AUTO] endpoint: {ep.Address.MapToIPv4()}:{ep.Port}", LogLevel.Verbose, this);
309+
}
310+
301311
stream = client.GetStream();
302312
stream.ReadTimeout = 1000;
303313

304-
Console.WriteLine($"Connected {client.Connected}");
305314
await Task.CompletedTask;
306315

307316
} catch (SocketException) {
@@ -720,10 +729,11 @@ private async Task<string> SendSingleBlock (string _blockString) {
720729

721730
if (client == null || !client.Connected ) {
722731
await ConnectTCP();
723-
if (!client.Connected)
724-
return null;
725732
}
726733

734+
if (client == null || !client.Connected)
735+
return null;
736+
727737
var message = _blockString.ToHexASCIIBytes();
728738

729739
//send request
@@ -759,7 +769,7 @@ private async Task<string> SendSingleBlock (string _blockString) {
759769
}
760770

761771
} catch (IOException) {
762-
Logger.Log($"Critical IO exception on receive", LogLevel.Critical, this);
772+
OnMajorSocketExceptionWhileConnected();
763773
return null;
764774
} catch (SocketException) {
765775
OnMajorSocketExceptionWhileConnected();

0 commit comments

Comments
 (0)