Skip to content

Commit 6f8f891

Browse files
committed
Added poller delay
- added downstream and upstream speeds - refactored SerialQueue - counted version up to 0.5.5
1 parent 45a9fa0 commit 6f8f891

File tree

5 files changed

+88
-42
lines changed

5 files changed

+88
-42
lines changed

Examples/Program.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,14 @@ await interf.ConnectAsync(
7979
//set the current second to the PLCs TIME register
8080
interf.SetRegister(nameof(registers.TestTime), TimeSpan.FromSeconds(DateTime.Now.Second));
8181

82+
while(true) {
83+
84+
Console.WriteLine($"Speed UP: {interf.BytesPerSecondUpstream} B/s");
85+
Console.WriteLine($"Speed DOWN: {interf.BytesPerSecondDownstream} B/s");
86+
87+
await Task.Delay(1000);
88+
}
89+
8290
});
8391

8492
}

MewtocolNet/Mewtocol/DynamicInterface.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,8 @@ internal void AttachPoller () {
117117

118118
it++;
119119

120+
await Task.Delay(PollerDelayMs);
121+
120122
}
121123

122124
});

MewtocolNet/Mewtocol/MewtocolInterface.cs

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,15 @@ public int ConnectTimeout {
5252
set { connectTimeout = value; }
5353
}
5454

55+
private int pollerDelayMs = 0;
56+
/// <summary>
57+
/// Delay for each poller cycle in milliseconds, default = 0
58+
/// </summary>
59+
public int PollerDelayMs {
60+
get { return pollerDelayMs; }
61+
set { pollerDelayMs = value; }
62+
}
63+
5564
/// <summary>
5665
/// The host ip endpoint, leave it null to use an automatic interface
5766
/// </summary>
@@ -101,6 +110,9 @@ private set {
101110
private int stationNumber;
102111
private int cycleTimeMs = 25;
103112

113+
private int bytesTotalCountedUpstream = 0;
114+
private int bytesTotalCountedDownstream = 0;
115+
104116
/// <summary>
105117
/// The current IP of the PLC connection
106118
/// </summary>
@@ -125,13 +137,40 @@ private set {
125137
}
126138
}
127139

140+
private int bytesPerSecondUpstream = 0;
141+
/// <summary>
142+
/// The current transmission speed in bytes per second
143+
/// </summary>
144+
public int BytesPerSecondUpstream {
145+
get { return bytesPerSecondUpstream; }
146+
private set {
147+
bytesPerSecondUpstream = value;
148+
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(BytesPerSecondUpstream)));
149+
}
150+
}
151+
152+
private int bytesPerSecondDownstream = 0;
153+
/// <summary>
154+
/// The current transmission speed in bytes per second
155+
/// </summary>
156+
public int BytesPerSecondDownstream {
157+
get { return bytesPerSecondDownstream; }
158+
private set {
159+
bytesPerSecondDownstream = value;
160+
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(BytesPerSecondDownstream)));
161+
}
162+
}
163+
128164
internal NetworkStream stream;
129165
internal TcpClient client;
130166
internal readonly SerialQueue queue = new SerialQueue();
131167
private int RecBufferSize = 128;
132168
internal int SendExceptionsInRow = 0;
133169
internal bool ImportantTaskRunning = false;
134170

171+
private Stopwatch speedStopwatchUpstr;
172+
private Stopwatch speedStopwatchDownstr;
173+
135174
#region Initialization
136175

137176
/// <summary>
@@ -736,13 +775,30 @@ private async Task<string> SendSingleBlock (string _blockString) {
736775

737776
var message = _blockString.ToHexASCIIBytes();
738777

778+
//time measuring
779+
if(speedStopwatchUpstr == null) {
780+
speedStopwatchUpstr = Stopwatch.StartNew();
781+
}
782+
783+
if(speedStopwatchUpstr.Elapsed.TotalSeconds >= 1) {
784+
speedStopwatchUpstr.Restart();
785+
bytesTotalCountedUpstream = 0;
786+
}
787+
739788
//send request
740789
using (var sendStream = new MemoryStream(message)) {
741790
await sendStream.CopyToAsync(stream);
742791
Logger.Log($"[--------------------------------]", LogLevel.Critical, this);
743792
Logger.Log($"--> OUT MSG: {_blockString}", LogLevel.Critical, this);
744793
}
745794

795+
//calc upstream speed
796+
bytesTotalCountedUpstream += message.Length;
797+
798+
var perSecUpstream = (double)((bytesTotalCountedUpstream / speedStopwatchUpstr.Elapsed.TotalMilliseconds) * 1000);
799+
if (perSecUpstream <= 10000)
800+
BytesPerSecondUpstream = (int)Math.Round(perSecUpstream, MidpointRounding.AwayFromZero);
801+
746802
//await result
747803
StringBuilder response = new StringBuilder();
748804
try {
@@ -755,6 +811,17 @@ private async Task<string> SendSingleBlock (string _blockString) {
755811
while (!endLineCode && !startMsgCode) {
756812

757813
do {
814+
815+
//time measuring
816+
if (speedStopwatchDownstr == null) {
817+
speedStopwatchDownstr = Stopwatch.StartNew();
818+
}
819+
820+
if (speedStopwatchDownstr.Elapsed.TotalSeconds >= 1) {
821+
speedStopwatchDownstr.Restart();
822+
bytesTotalCountedDownstream = 0;
823+
}
824+
758825
int bytes = await stream.ReadAsync(responseBuffer, 0, responseBuffer.Length);
759826

760827
endLineCode = responseBuffer.Any(x => x == 0x0D);
@@ -777,8 +844,18 @@ private async Task<string> SendSingleBlock (string _blockString) {
777844
}
778845

779846
if(!string.IsNullOrEmpty(response.ToString())) {
847+
780848
Logger.Log($"<-- IN MSG: {response}", LogLevel.Critical, this);
849+
850+
bytesTotalCountedDownstream += Encoding.ASCII.GetByteCount(response.ToString());
851+
852+
var perSecDownstream = (double)((bytesTotalCountedDownstream / speedStopwatchDownstr.Elapsed.TotalMilliseconds) * 1000);
853+
854+
if(perSecUpstream <= 10000)
855+
BytesPerSecondDownstream = (int)Math.Round(perSecUpstream, MidpointRounding.AwayFromZero);
856+
781857
return response.ToString();
858+
782859
} else {
783860
return null;
784861
}

MewtocolNet/MewtocolNet.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<PropertyGroup>
33
<TargetFramework>netstandard2.0</TargetFramework>
44
<PackageId>MewtocolNet</PackageId>
5-
<Version>0.5.2</Version>
5+
<Version>0.5.5</Version>
66
<Authors>Felix Weiss</Authors>
77
<Company>Womed</Company>
88
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>

MewtocolNet/Queue/SerialQueue.cs

Lines changed: 0 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -8,47 +8,6 @@ internal class SerialQueue {
88
readonly object _locker = new object();
99
readonly WeakReference<Task> _lastTask = new WeakReference<Task>(null);
1010

11-
internal Task Enqueue (Action action) {
12-
return Enqueue<bool>(() => {
13-
action();
14-
return true;
15-
});
16-
}
17-
18-
internal Task<T> Enqueue<T> (Func<T> function) {
19-
lock (_locker) {
20-
Task lastTask;
21-
Task<T> resultTask;
22-
23-
if (_lastTask.TryGetTarget(out lastTask)) {
24-
resultTask = lastTask.ContinueWith(_ => function(), TaskContinuationOptions.ExecuteSynchronously);
25-
} else {
26-
resultTask = Task.Run(function);
27-
}
28-
29-
_lastTask.SetTarget(resultTask);
30-
31-
return resultTask;
32-
}
33-
}
34-
35-
internal Task Enqueue (Func<Task> asyncAction) {
36-
lock (_locker) {
37-
Task lastTask;
38-
Task resultTask;
39-
40-
if (_lastTask.TryGetTarget(out lastTask)) {
41-
resultTask = lastTask.ContinueWith(_ => asyncAction(), TaskContinuationOptions.ExecuteSynchronously).Unwrap();
42-
} else {
43-
resultTask = Task.Run(asyncAction);
44-
}
45-
46-
_lastTask.SetTarget(resultTask);
47-
48-
return resultTask;
49-
}
50-
}
51-
5211
internal Task<T> Enqueue<T> (Func<Task<T>> asyncFunction) {
5312
lock (_locker) {
5413
Task lastTask;

0 commit comments

Comments
 (0)