Beispiel #1
0
 private void MeshClientAuthenticationSucceeded(object sender, EventArgs args)
 {
     Logger?.Invoke("[MeshClient] Server " + PeerNode.IpPort + " authentication succeeded");
     AuthenticationSucceeded?.Invoke(this, EventArgs.Empty);
 }
 internal void HandleAuthenticationSucceeded(object sender, AuthenticationSucceededEventArgs args)
 {
     AuthenticationSucceeded?.Invoke(sender, args);
 }
Beispiel #3
0
 internal void HandleAuthenticationSucceeded(object sender, EventArgs args)
 {
     CatchAndReport(() => AuthenticationSucceeded?.Invoke(sender, args), "AuthenticationSucceeded", sender);
 }
Beispiel #4
0
 internal void HandleAuthenticationSucceeded(object sender, EventArgs args)
 {
     WrappedEventHandler(() => AuthenticationSucceeded?.Invoke(sender, args), "ServerConnected", sender);
 }
        private async Task DataReceiver()
        {
            while (true)
            {
                bool readLocked = false;

                try
                {
                    _Token.ThrowIfCancellationRequested();

                    if (_Client == null ||
                        !_Client.Connected ||
                        _Token.IsCancellationRequested)
                    {
                        Logger?.Invoke("[WatsonTcpClient] Disconnect detected");
                        break;
                    }

                    WatsonMessage msg = null;
                    readLocked = await _ReadLock.WaitAsync(1);

                    bool buildSuccess = false;

                    if (_SslStream != null)
                    {
                        msg = new WatsonMessage(_SslStream, Debug);
                    }
                    else
                    {
                        msg = new WatsonMessage(_TcpStream, Debug);
                    }

                    if (_MessageReceived != null &&
                        _MessageReceived.GetInvocationList().Length > 0)
                    {
                        buildSuccess = await msg.Build();
                    }
                    else if (_StreamReceived != null &&
                             _StreamReceived.GetInvocationList().Length > 0)
                    {
                        buildSuccess = await msg.BuildStream();
                    }
                    else
                    {
                        break;
                    }

                    if (!buildSuccess)
                    {
                        Logger?.Invoke("[WatsonTcpClient] Message build failed due to disconnect");
                        break;
                    }

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

                        continue;
                    }

                    if (msg.Status == MessageStatus.Removed)
                    {
                        Logger?.Invoke("[WatsonTcpClient] Disconnect due to server-side removal");
                        break;
                    }
                    else if (msg.Status == MessageStatus.Disconnecting)
                    {
                        Logger?.Invoke("[WatsonTcpClient] Disconnect due to server shutdown");
                        break;
                    }
                    else if (msg.Status == MessageStatus.AuthSuccess)
                    {
                        Logger?.Invoke("[WatsonTcpClient] Authentication successful");
                        AuthenticationSucceeded?.Invoke(this, EventArgs.Empty);
                        continue;
                    }
                    else if (msg.Status == MessageStatus.AuthFailure)
                    {
                        Logger?.Invoke("[WatsonTcpClient] Authentication failed");
                        AuthenticationFailure?.Invoke(this, EventArgs.Empty);
                        continue;
                    }

                    if (msg.Status == MessageStatus.AuthRequired)
                    {
                        Logger?.Invoke("[WatsonTcpClient] Authentication required by server; please authenticate using pre-shared key");
                        if (AuthenticationRequested != null)
                        {
                            string psk = AuthenticationRequested();
                            if (!String.IsNullOrEmpty(psk))
                            {
                                Authenticate(psk);
                            }
                        }
                        continue;
                    }

                    if (_MessageReceived != null &&
                        _MessageReceived.GetInvocationList().Length > 0)
                    {
                        MessageReceivedFromServerEventArgs args = new MessageReceivedFromServerEventArgs(msg.Metadata, msg.Data);
                        _MessageReceived?.Invoke(this, args);
                    }
                    else if (_StreamReceived != null &&
                             _StreamReceived.GetInvocationList().Length > 0)
                    {
                        StreamReceivedFromServerEventArgs args = new StreamReceivedFromServerEventArgs(msg.Metadata, msg.ContentLength, msg.DataStream);
                        _StreamReceived?.Invoke(this, args);
                    }
                    else
                    {
                        break;
                    }

                    _Stats.ReceivedMessages = _Stats.ReceivedMessages + 1;
                    _Stats.ReceivedBytes   += msg.ContentLength;
                }
                catch (Exception e)
                {
                    Logger?.Invoke(
                        "[WatsonTcpClient] Data receiver exception: " +
                        Environment.NewLine +
                        e.ToString() +
                        Environment.NewLine);
                    break;
                }
                finally
                {
                    if (readLocked)
                    {
                        _ReadLock.Release();
                    }
                }
            }

            Logger?.Invoke("[WatsonTcpClient] Data receiver terminated");
            Connected = false;
            ServerDisconnected?.Invoke(this, EventArgs.Empty);
            Dispose();
        }