protected override void OnConnectCallback(IAsyncResult result)
        {
            if (Disposed)
            {
                return;
            }

            var server = (Socket)result.AsyncState;

            try
            {
                //Client is connected to server and set connected variable
                server.EndConnect(result);
                ConnectedMre.Set();
                KeepAliveTimer.Enabled = true;
                var state = new ClientMetadata(Listener);
                Receive(state);
            }
            catch (ObjectDisposedException ex)
            {
                RaiseErrorThrown(ex);
            }
            catch (SocketException)
            {
                Thread.Sleep(ReconnectInSeconds * 1000);
                if (!Token.IsCancellationRequested && !Disposed)
                {
                    Listener.BeginConnect(Endpoint, OnConnectCallback, Listener);
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message, ex);
            }
        }
Example #2
0
        protected override async void OnConnectCallback(IAsyncResult result)
        {
            var client = (Socket)result.AsyncState;

            try
            {
                //Client is connected to server and set connected variable
                client.EndConnect(result);

                var stream = new NetworkStream(Listener);
                _sslStream = new SslStream(stream, false, ValidateCertificate, null);

                var success = await Authenticate(_sslStream);

                if (success)
                {
                    ConnectedMre.Set();
                    KeepAliveTimer.Enabled = true;
                    var state = new ClientMetadata(Listener);
                    Receive(state);
                }
                else
                {
                    throw new AuthenticationException("Client cannot be authenticated.");
                }
            }
            catch (SocketException)
            {
                ConnectedMre.Reset();
                DisposeSslStream();

                Thread.Sleep(ReconnectInSeconds * 1000);
                if (Listener != null && !Disposed)
                {
                    Listener.BeginConnect(Endpoint, OnConnectCallback, Listener);
                }
                else if (Listener == null && !Disposed)
                {
                    StartClient(Ip, Port, ReconnectInSeconds);
                }
            }
            catch (Exception ex)
            {
                RaiseErrorThrown(ex);
                // throw new Exception(ex.Message, ex);
            }
        }
        protected override void OnConnectCallback(IAsyncResult result)
        {
            if (Disposed)
            {
                return;
            }

            var server = (Socket)result.AsyncState;

            try
            {
                //Client is connected to server and set connected variable
                server.EndConnect(result);
                ConnectedMre.Set();
                KeepAliveTimer.Enabled = true;
                var state = new ClientMetadata(Listener);
                Receive(state);
            }
            catch (ObjectDisposedException ex)
            {
                RaiseErrorThrown(ex);
            }
            catch (SocketException)
            {
                if (Disposed)
                {
                    return;
                }

                ConnectedMre.Reset();

                Thread.Sleep(ReconnectInSeconds * 1000);
                if (Listener != null && !Disposed)
                {
                    Listener.BeginConnect(Endpoint, OnConnectCallback, Listener);
                }
                else if (Listener == null && !Disposed)
                {
                    StartClient(Ip, Port, ReconnectInSeconds);
                }
            }
            catch (Exception ex)
            {
                RaiseErrorThrown(ex);
            }
        }
Example #4
0
        //Called when connection starts
        protected override void OnConnectCallback(IAsyncResult result)
        {
            var client = (Socket)result.AsyncState;

            try
            {
                //Client is connected to server and set connected variable
                client.EndConnect(result);

                var stream = new NetworkStream(Listener);
                _sslStream = new SslStream(stream, false, new RemoteCertificateValidationCallback(ValidateCertificate), null);

                Task.Run(SendFromQueue, Token);

                Task.Run(() =>
                {
                    bool success = Authenticate(_sslStream).Result;

                    if (success)
                    {
                        ConnectedMre.Set();
                        KeepAliveTimer.Enabled = true;
                        Receive();
                    }
                    else
                    {
                        throw new AuthenticationException("Client cannot be authenticated.");
                    }
                }, new CancellationTokenSource(10000).Token);
            }
            catch (SocketException)
            {
                DisposeSslStream();

                Thread.Sleep(ReconnectInSeconds * 1000);
                Listener.BeginConnect(Endpoint, this.OnConnectCallback, Listener);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message, ex);
            }
        }