Skip to content

Commit

Permalink
- CS104: fixed timeout T2 problem (S message was sent before timeout …
Browse files Browse the repository at this point in the history
…elapsed)
  • Loading branch information
mzillgith committed Apr 14, 2018
1 parent a563f36 commit 54083d8
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 15 deletions.
2 changes: 1 addition & 1 deletion examples/cs104-server3/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ public static void Main (string[] args)

server.EnqueueASDU (newAsdu);

waitTime = 1000;
waitTime = 5000;
}
}

Expand Down
15 changes: 9 additions & 6 deletions lib60870/CS104/ClientConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,15 @@ private void DebugLog(string msg)
private int receiveCount = 0;

private int unconfirmedReceivedIMessages = 0; /* number of unconfirmed messages received */
private Int64 lastConfirmationTime = System.Int64.MaxValue; /* timestamp when the last confirmation message was sent */

/* T3 parameter handling */
private UInt64 nextT3Timeout;
private int outStandingTestFRConMessages = 0;

/* T2 parameter handling */
private bool timeoutT2Triggered = false;
private Int64 lastConfirmationTime = System.Int64.MaxValue; /* timestamp when the last confirmation message was sent */

private TlsSecurityInformation tlsSecInfo = null;

private APCIParameters apciParameters;
Expand All @@ -85,8 +88,6 @@ private void DebugLog(string msg)

private Queue<BufferFrame> waitingASDUsHighPrio = null;

private bool firstIMessageReceived = false;

/* data structure for k-size sent ASDU buffer */
private struct SentASDU
{
Expand Down Expand Up @@ -317,6 +318,7 @@ private int SendIMessage(BufferFrame asdu)
DebugLog("SEND I (size = " + msgSize + ") : " + BitConverter.ToString(buffer, 0, msgSize));
sendCount = (sendCount + 1) % 32768;
unconfirmedReceivedIMessages = 0;
timeoutT2Triggered = false;
}
}
catch (System.IO.IOException) {
Expand Down Expand Up @@ -768,8 +770,8 @@ private bool HandleMessage(byte[] buffer, int msgSize)
return false;
}

if (firstIMessageReceived == false) {
firstIMessageReceived = true;
if (timeoutT2Triggered == false) {
timeoutT2Triggered = true;
lastConfirmationTime = currentTime; /* start timeout T2 */
}

Expand Down Expand Up @@ -904,6 +906,7 @@ private bool handleTimeouts() {

lastConfirmationTime = (long) currentTime;
unconfirmedReceivedIMessages = 0;
timeoutT2Triggered = false;
SendSMessage ();
}
}
Expand Down Expand Up @@ -1056,8 +1059,8 @@ private void HandleConnection()

if (unconfirmedReceivedIMessages >= apciParameters.W) {
lastConfirmationTime = SystemUtils.currentTimeMillis();

unconfirmedReceivedIMessages = 0;
timeoutT2Triggered = false;
SendSMessage ();
}
}
Expand Down
21 changes: 13 additions & 8 deletions lib60870/CS104/Connection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,10 @@ private struct SentASDU
private Thread workerThread = null;

private int unconfirmedReceivedIMessages; /* number of unconfirmed messages received */

/* T2 timeout handling */
private long lastConfirmationTime; /* timestamp when the last confirmation message was sent */
private bool timeoutT2Triggered = false;

private Socket socket = null;
private Stream netStream = null;
Expand All @@ -225,7 +228,6 @@ private struct SentASDU
private bool running = false;
private bool connecting = false;
private bool socketError;
private bool firstIMessageReceived = false;
private SocketException lastException;

private static int connectionCounter = 0;
Expand Down Expand Up @@ -334,7 +336,7 @@ private void ResetConnection()
receiveSequenceNumber = 0;
unconfirmedReceivedIMessages = 0;
lastConfirmationTime = System.Int64.MaxValue;
firstIMessageReceived = false;
timeoutT2Triggered = false;
outStandingTestFRConMessages = 0;

uMessageTimeout = 0;
Expand Down Expand Up @@ -519,13 +521,13 @@ private int SendIMessage(ASDU asdu)

if (running)
{
//socket.Send (buffer, msgSize, SocketFlags.None);

netStream.Write(buffer, 0, msgSize);

sendSequenceNumber = (sendSequenceNumber + 1) % 32768;
statistics.SentMsgCounter++;

unconfirmedReceivedIMessages = 0;
timeoutT2Triggered = false;

if (sentMessageHandler != null)
{
Expand Down Expand Up @@ -1157,9 +1159,9 @@ private bool checkMessage(byte[] buffer, int msgSize)
if ((buffer[2] & 1) == 0)
{ /* I format frame */

if (firstIMessageReceived == false)
if (timeoutT2Triggered == false)
{
firstIMessageReceived = true;
timeoutT2Triggered = true;
lastConfirmationTime = currentTime; /* start timeout T2 */
}

Expand Down Expand Up @@ -1393,10 +1395,11 @@ private bool handleTimeouts()
if (unconfirmedReceivedIMessages > 0)
{
if (checkConfirmTimeout((long)currentTime))
{

{
lastConfirmationTime = (long)currentTime;

unconfirmedReceivedIMessages = 0;
timeoutT2Triggered = false;

SendSMessage(); /* send confirmation message */
}
Expand Down Expand Up @@ -1635,6 +1638,8 @@ private void HandleConnection()
lastConfirmationTime = SystemUtils.currentTimeMillis();

unconfirmedReceivedIMessages = 0;
timeoutT2Triggered = false;

SendSMessage();
}

Expand Down
1 change: 1 addition & 0 deletions lib60870/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
[assembly: NeutralResourcesLanguage("")]
[assembly: InternalsVisibleTo("lib60870-tests")]
[assembly: InternalsVisibleTo("lib60870.NET")]
[assembly: InternalsVisibleTo("lib60870.NET.CS103")]
// The following attributes are used to specify the signing key for the assembly,
// if desired. See the Mono documentation for more information about signing.
//[assembly: AssemblyDelaySign(false)]
Expand Down

0 comments on commit 54083d8

Please sign in to comment.