Esempio n. 1
0
        /// <summary>
        /// Reads and decodes message from the stream from LOC server.
        /// </summary>
        /// <param name="CancellationToken">Cancallation token for async calls.</param>
        /// <param name="CheckProtocolViolation">If set to true, the function checks whether a protocol violation occurred and if so, it sends protocol violation error to the peer.</param>
        /// <returns>Received message of null if the function fails.</returns>
        public async Task <LocProtocolMessage> ReceiveMessageAsync(CancellationToken CancellationToken, bool CheckProtocolViolation = false)
        {
            log.Trace("()");

            LocProtocolMessage res = null;

            RawMessageResult rawMessage = await messageReader.ReceiveMessageAsync(CancellationToken);

            if (rawMessage.Data != null)
            {
                res = (LocProtocolMessage)LocMessageBuilder.CreateMessageFromRawData(rawMessage.Data);
            }
            else
            {
                log.Debug("Connection to LOC server has been terminated.");
            }

            if (CheckProtocolViolation)
            {
                if ((res == null) || rawMessage.ProtocolViolation)
                {
                    await messageProcessor.SendProtocolViolation(this);
                }
            }

            log.Trace("(-):{0}", res != null ? "LocProtocolMessage" : "null");
            return(res);
        }
        /// <summary>
        /// Reads messages from the client stream and processes them in a loop until the client disconnects
        /// or until an action (such as a protocol violation) that leads to disconnecting of the client occurs.
        /// </summary>
        public async Task ReceiveMessageLoop()
        {
            log.Trace("()");

            try
            {
                if (UseTls)
                {
                    SslStream  sslStream = (SslStream)Stream;
                    ConfigBase config    = (ConfigBase)Base.ComponentDictionary[ConfigBase.ComponentName];
                    await sslStream.AuthenticateAsServerAsync((X509Certificate)config.Settings["TcpServerTlsCertificate"], false, SslProtocols.Tls12, false);
                }

                RawMessageReader messageReader = new RawMessageReader(Stream);
                while (!shutdownSignaling.IsShutdown)
                {
                    RawMessageResult rawMessage = await messageReader.ReceiveMessageAsync(shutdownSignaling.ShutdownCancellationTokenSource.Token);

                    bool disconnect        = rawMessage.Data == null;
                    bool protocolViolation = rawMessage.ProtocolViolation;
                    if (rawMessage.Data != null)
                    {
                        IProtocolMessage message = CreateMessageFromRawData(rawMessage.Data);
                        if (message != null)
                        {
                            disconnect = !await messageProcessor.ProcessMessageAsync(this, message);
                        }
                        else
                        {
                            protocolViolation = true;
                        }
                    }

                    if (protocolViolation)
                    {
                        await messageProcessor.SendProtocolViolation(this);

                        break;
                    }

                    if (disconnect)
                    {
                        break;
                    }
                }
            }
            catch (Exception e)
            {
                log.Error("Exception occurred: {0}", e.ToString());
            }

            log.Trace("(-)");
        }