internal void UnmarkAllASDUs()
 {
     if (asduQueue != null)
     {
         asduQueue.UnmarkAllASDUs();
     }
 }
示例#2
0
        private void HandleConnection()
        {
            byte[] bytes = new byte[300];

            try
            {
                try
                {
                    running = true;

                    if (tlsSecInfo != null)
                    {
                        DebugLog("Setup TLS");

                        RemoteCertificateValidationCallback validationCallback = CertificateValidationCallback;

                        if (tlsSecInfo.CertificateValidationCallback != null)
                        {
                            validationCallback = tlsSecInfo.CertificateValidationCallback;
                        }

                        SslStream sslStream = new SslStream(socketStream, true, validationCallback);

                        bool authenticationSuccess = false;

                        try
                        {
                            sslStream.AuthenticateAsServer(tlsSecInfo.OwnCertificate, true, System.Security.Authentication.SslProtocols.Tls, false);

                            if (sslStream.IsAuthenticated == true)
                            {
                                socketStream          = sslStream;
                                authenticationSuccess = true;
                            }
                        }
                        catch (IOException e)
                        {
                            if (e.GetBaseException() != null)
                            {
                                DebugLog("TLS authentication error: " + e.GetBaseException().Message);
                            }
                            else
                            {
                                DebugLog("TLS authentication error: " + e.Message);
                            }
                        }

                        if (authenticationSuccess == true)
                        {
                            socketStream = sslStream;
                        }
                        else
                        {
                            DebugLog("TLS authentication failed");
                            running = false;
                        }
                    }

                    if (running)
                    {
                        socketStream.ReadTimeout = 50;

                        callbackThread = new Thread(ProcessASDUs);
                        callbackThread.Start();

                        ResetT3Timeout();
                    }

                    while (running)
                    {
                        try
                        {
                            // Receive the response from the remote device.
                            int bytesRec = receiveMessage(bytes);

                            if (bytesRec > 0)
                            {
                                DebugLog("RCVD: " + BitConverter.ToString(bytes, 0, bytesRec));

                                if (HandleMessage(bytes, bytesRec) == false)
                                {
                                    /* close connection on error */
                                    running = false;
                                }

                                if (unconfirmedReceivedIMessages >= apciParameters.W)
                                {
                                    lastConfirmationTime         = SystemUtils.currentTimeMillis();
                                    unconfirmedReceivedIMessages = 0;
                                    timeoutT2Triggered           = false;
                                    SendSMessage();
                                }
                            }
                            else if (bytesRec == -1)
                            {
                                running = false;
                            }
                        }
                        catch (System.IO.IOException)
                        {
                            running = false;
                        }

                        if (fileServer != null)
                        {
                            fileServer.HandleFileTransmission();
                        }

                        if (handleTimeouts() == false)
                        {
                            running = false;
                        }

                        if (running)
                        {
                            if (isActive)
                            {
                                SendWaitingASDUs();
                            }

                            Thread.Sleep(1);
                        }
                    }

                    isActive = false;

                    DebugLog("CLOSE CONNECTION!");

                    // Release the socket.

                    socket.Shutdown(SocketShutdown.Both);
                    socket.Close();

                    socketStream.Dispose();
                    socket.Dispose();

                    DebugLog("CONNECTION CLOSED!");
                }
                catch (ArgumentNullException ane)
                {
                    DebugLog("ArgumentNullException : " + ane.ToString());
                }
                catch (SocketException se)
                {
                    DebugLog("SocketException : " + se.ToString());
                }
                catch (Exception e)
                {
                    DebugLog("Unexpected exception : " + e.ToString());
                }
            }
            catch (Exception e)
            {
                DebugLog(e.ToString());
            }

            // unmark unconfirmed messages in queue if k-buffer not empty
            if (oldestSentASDU != -1)
            {
                asduQueue.UnmarkAllASDUs();
            }

            server.Remove(this);

            if (callbackThreadRunning)
            {
                callbackThreadRunning = false;
                callbackThread.Join();
            }

            DebugLog("Connection thread finished");
        }