private void ConnectionProcessorThread() { while (threadCancellationSignal) { try { if (connectionState == ConnectionState.DISCONNECTED) { numberOfConnectionRetries = 0; connection.Connect(); while (numberOfConnectionRetries < 10) { if (connection.CheckState()) { this.connectionState = ConnectionState.CONNECTED; numberOfConnectionRetries = 0; break; } else { numberOfConnectionRetries++; if (numberOfConnectionRetries == 10) { connection.Disconect(); connectionState = ConnectionState.DISCONNECTED; } } } } else { processConnection.WaitOne(); while (commandQueue.TryDequeue(out currentCommand)) { lock (lockObj) { connection.Send(currentCommand.PackRequest()); byte[] message; byte[] header = connection.Recv(10); int recvLen = CalculateRecvLength(header[2]); byte[] dataChunks = connection.Recv(recvLen); message = new byte[header.Length + recvLen]; Buffer.BlockCopy(header, 0, message, 0, 10); Buffer.BlockCopy(dataChunks, 0, message, 10, recvLen); bool unsolicited = CheckIfUnsolicited(message[11]); if (unsolicited) { HandleReceivedBytes(message, unsolicited); DNP3ConfirmCommandParamters dnp3Param = new DNP3ConfirmCommandParamters(0xc0, (byte)DNP3FunctionCode.CONFIRM, 0xc0); //podesiti parametre IDNP3Function function = DNP3FunctionFactory.CreateConfirmFunction(dnp3Param); connection.Send(function.PackRequest()); } else { HandleReceivedBytes(message, unsolicited); } currentCommand = null; } } } } catch (SocketException se) { currentCommand = null; connectionState = ConnectionState.DISCONNECTED; Log().Log(new LogEventModel() { EventType = LogEventType.ERROR, Message = $"{se.Message}" }).GetAwaiter().GetResult(); } catch (Exception ex) { currentCommand = null; Log().Log(new LogEventModel() { EventType = LogEventType.WARN, Message = $"{ex.Message}" }).GetAwaiter().GetResult(); } } }