Skip to content

Commit de8382d

Browse files
committed
- CS 104 slave: catch ASDUParserException and close connection when detecting an invalid ASDU
1 parent 1f38efa commit de8382d

File tree

1 file changed

+39
-28
lines changed

1 file changed

+39
-28
lines changed

lib60870/CS104/ClientConnection.cs

Lines changed: 39 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -119,17 +119,24 @@ private void ProcessASDUs()
119119

120120
while (callbackThreadRunning) {
121121

122-
while ((receivedASDUs.Count > 0) && (callbackThreadRunning) && (running)) {
123-
124-
ASDU asdu;
125-
126-
if (receivedASDUs.TryDequeue (out asdu)) {
127-
HandleASDU (asdu);
128-
}
129-
130-
}
122+
try {
123+
while ((receivedASDUs.Count > 0) && (callbackThreadRunning) && (running)) {
124+
125+
ASDU asdu;
126+
127+
if (receivedASDUs.TryDequeue (out asdu)) {
128+
HandleASDU (asdu);
129+
}
130+
131+
}
132+
133+
Thread.Sleep (50);
134+
}
135+
catch (ASDUParsingException) {
136+
DebugLog("Failed to parse ASDU --> close connection");
137+
running = false;
138+
}
131139

132-
Thread.Sleep (50);
133140
}
134141

135142
DebugLog ("ProcessASDUs exit thread");
@@ -341,32 +348,36 @@ private bool isSentBufferFull() {
341348
return false;
342349
}
343350

344-
private void PrintSendBuffer() {
345-
346-
if (oldestSentASDU != -1) {
351+
private void PrintSendBuffer()
352+
{
353+
if (debugOutput)
354+
{
355+
if (oldestSentASDU != -1)
356+
{
347357

348-
int currentIndex = oldestSentASDU;
358+
int currentIndex = oldestSentASDU;
349359

350-
int nextIndex = 0;
360+
int nextIndex = 0;
351361

352-
DebugLog ("------k-buffer------");
362+
DebugLog("------k-buffer------");
353363

354-
do {
355-
DebugLog (currentIndex + " : S " + sentASDUs[currentIndex].seqNo + " : time " +
356-
sentASDUs[currentIndex].sentTime + " : " + sentASDUs[currentIndex].queueIndex);
364+
do
365+
{
366+
DebugLog(currentIndex + " : S " + sentASDUs[currentIndex].seqNo + " : time " +
367+
sentASDUs[currentIndex].sentTime + " : " + sentASDUs[currentIndex].queueIndex);
357368

358-
if (currentIndex == newestSentASDU)
359-
nextIndex = -1;
360-
else
361-
currentIndex = (currentIndex + 1) % maxSentASDUs;
369+
if (currentIndex == newestSentASDU)
370+
nextIndex = -1;
371+
else
372+
currentIndex = (currentIndex + 1) % maxSentASDUs;
362373

363-
} while (nextIndex != -1);
374+
} while (nextIndex != -1);
364375

365-
DebugLog ("--------------------");
376+
DebugLog("--------------------");
366377

367-
}
368-
369-
}
378+
}
379+
}
380+
}
370381

371382
private void sendNextAvailableASDU()
372383
{

0 commit comments

Comments
 (0)