Esempio n. 1
0
        private async Task <WatsonMessage> MessageReadAsync(ClientMetadata client)
        {
            /*
             *
             * Do not catch exceptions, let them get caught by the data reader
             * to destroy the connection
             *
             */

            WatsonMessage msg = null;

            if (_Mode == Mode.Ssl)
            {
                msg = new WatsonMessage(client.SslStream, Debug);
                await msg.Build();
            }
            else if (_Mode == Mode.Tcp)
            {
                msg = new WatsonMessage(client.NetworkStream, Debug);
                await msg.Build();
            }
            else
            {
                throw new ArgumentException("Unknown mode: " + _Mode.ToString());
            }

            return(msg);
        }
Esempio n. 2
0
        internal async Task <WatsonMessage> MessageReadAsync(bool readDataStream)
        {
            WatsonMessage msg = new WatsonMessage(TrafficStream);
            await msg.Build(readDataStream);

            return(msg);
        }
Esempio n. 3
0
        private async Task DataReceiver(CancellationToken?cancelToken = null)
        {
            try
            {
                #region Wait-for-Data

                while (true)
                {
                    cancelToken?.ThrowIfCancellationRequested();

                    #region Check-Connection

                    if (_Client == null)
                    {
                        Log("*** DataReceiver null TCP interface detected, disconnection or close assumed");
                        break;
                    }

                    if (!_Client.Connected)
                    {
                        Log("*** DataReceiver server disconnected");
                        break;
                    }

                    if (_Ssl != null && !_Ssl.CanRead)
                    {
                        Log("*** DataReceiver cannot read from SSL stream");
                        break;
                    }

                    #endregion

                    #region Read-Message-and-Handle

                    WatsonMessage msg = null;

                    if (_Ssl != null)
                    {
                        msg = new WatsonMessage(_Ssl, Debug);

                        if (ReadDataStream)
                        {
                            await msg.Build();
                        }
                        else
                        {
                            await msg.BuildStream();
                        }
                    }
                    else
                    {
                        msg = new WatsonMessage(_Client.GetStream(), Debug);

                        if (ReadDataStream)
                        {
                            await msg.Build();
                        }
                        else
                        {
                            await msg.BuildStream();
                        }
                    }

                    if (msg == null)
                    {
                        await Task.Delay(30);

                        continue;
                    }

                    if (msg.Status == MessageStatus.AuthSuccess)
                    {
                        Log("DataReceiver successfully authenticated");
                        AuthenticationSucceeded?.Invoke();
                        continue;
                    }
                    else if (msg.Status == MessageStatus.AuthFailure)
                    {
                        Log("DataReceiver authentication failed, please authenticate using pre-shared key");
                        AuthenticationFailure?.Invoke();
                        continue;
                    }

                    if (msg.Status == MessageStatus.AuthRequired)
                    {
                        Log("DataReceiver authentication required, please authenticate using pre-shared key");
                        if (AuthenticationRequested != null)
                        {
                            string psk = AuthenticationRequested();
                            if (!String.IsNullOrEmpty(psk))
                            {
                                Authenticate(psk);
                            }
                        }
                        continue;
                    }

                    if (ReadDataStream)
                    {
                        if (MessageReceived != null)
                        {
                            Task <bool> unawaited = Task.Run(() => MessageReceived(msg.Data));
                        }
                    }
                    else
                    {
                        StreamReceived?.Invoke(msg.ContentLength, msg.DataStream);
                    }

                    #endregion
                }

                #endregion
            }
            catch (OperationCanceledException)
            {
            }
            catch (ObjectDisposedException)
            {
            }
            catch (Exception e)
            {
                if (Debug)
                {
                    Log("*** DataReceiver server disconnected");
                    Log(Common.SerializeJson(e));
                }
            }
            finally
            {
                Connected = false;
                ServerDisconnected?.Invoke();
            }
        }
Esempio n. 4
0
        private async Task DataReceiver(CancellationToken?cancelToken = null)
        {
            try
            {
                #region Wait-for-Data

                while (true)
                {
                    cancelToken?.ThrowIfCancellationRequested();

                    #region Check-Connection

                    if (_Client == null)
                    {
                        Log("*** DataReceiver null TCP interface detected, disconnection or close assumed");
                        break;
                    }

                    if (!_Client.Connected)
                    {
                        Log("*** DataReceiver server disconnected");
                        break;
                    }

                    if (_Ssl != null && !_Ssl.CanRead)
                    {
                        Log("*** DataReceiver cannot read from SSL stream");
                        break;
                    }

                    #endregion

                    #region Read-Message-and-Handle

                    WatsonMessage msg = null;

                    if (_Ssl != null)
                    {
                        msg = new WatsonMessage(_Ssl, Debug);
                        await msg.Build();
                    }
                    else
                    {
                        msg = new WatsonMessage(_Client.GetStream(), Debug);
                        await msg.Build();
                    }

                    if (msg == null)
                    {
                        await Task.Delay(30);

                        continue;
                    }

                    if (msg.Status == MessageStatus.AuthSuccess)
                    {
                        Log("DataReceiver successfully authenticated");
                    }

                    if (msg.Status == MessageStatus.AuthRequired)
                    {
                        Log("DataReceiver received authentication request, please authenticate using pre-shared key");
                    }

                    Task <bool> unawaited = Task.Run(() => MessageReceived(msg.Data));

                    #endregion
                }

                #endregion
            }
            catch (OperationCanceledException)
            {
                throw; // normal cancellation
            }
            catch (Exception)
            {
                Log("*** DataReceiver server disconnected");
            }
            finally
            {
                Connected = false;
                ServerDisconnected?.Invoke();
            }
        }