Ejemplo n.º 1
0
        /// <summary>
        /// Start listening to the server
        /// </summary>
        /// <param name="ipServer"></param>
        /// <param name="port"></param>
        /// <param name="reconnectInSeconds"></param>
        public override void StartClient(string ipServer, int port, int reconnectInSeconds = 5)
        {
            if (string.IsNullOrEmpty(ipServer))
            {
                throw new ArgumentNullException(nameof(ipServer));
            }
            if (port < 1 || port > 65535)
            {
                throw new ArgumentOutOfRangeException(nameof(port));
            }
            if (reconnectInSeconds < 3)
            {
                throw new ArgumentOutOfRangeException(nameof(reconnectInSeconds));
            }

            _sslCertificateCollection = new X509Certificate2Collection {
                _sslCertificate
            };

            Ip   = ipServer;
            Port = port;
            ReconnectInSeconds     = reconnectInSeconds;
            KeepAliveTimer.Enabled = false;

            Endpoint = new IPEndPoint(GetIp(ipServer), port);

            TokenSource = new CancellationTokenSource();
            Token       = TokenSource.Token;

            Task.Run(SendFromQueue, Token);

            Task.Run(() =>
            {
                try
                {
                    //Try and connect
                    Listener = new Socket(Endpoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
                    Listener.BeginConnect(Endpoint, this.OnConnectCallback, Listener);
                    ConnectedMre.WaitOne();

                    //If client is connected activate connected event
                    if (IsConnected())
                    {
                        InvokeConnected(this);
                    }
                    else
                    {
                        KeepAliveTimer.Enabled = false;
                        InvokeDisconnected(this);
                        Close();
                        ConnectedMre.Reset();
                        Listener.BeginConnect(Endpoint, this.OnConnectCallback, Listener);
                    }
                }
                catch (Exception ex)
                {
                    InvokeErrorThrown(ex);
                }
            }, Token);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Starts the client.
        /// <para>requires server ip, port number and how many seconds the client should wait to try to connect again. Default is 5 seconds</para>
        /// </summary>
        public override void StartClient(string ipServer, int port, int reconnectInSeconds = 5)
        {
            if (Disposed)
            {
                return;
            }

            if (string.IsNullOrEmpty(ipServer))
            {
                throw new ArgumentNullException(nameof(ipServer));
            }
            if (port < 1 || port > 65535)
            {
                throw new ArgumentOutOfRangeException(nameof(port));
            }
            if (reconnectInSeconds < 3)
            {
                throw new ArgumentOutOfRangeException(nameof(reconnectInSeconds));
            }


            Ip   = ipServer;
            Port = port;
            ReconnectInSeconds     = reconnectInSeconds;
            KeepAliveTimer.Enabled = false;

            if (EnableExtendedAuth)
            {
                SendAuthMessage();
            }
            else
            {
                SendBasicAuthMessage();
            }

            Endpoint = new IPEndPoint(GetIp(ipServer), port);

            TokenSource = new CancellationTokenSource();
            Token       = TokenSource.Token;

            Task.Run(SendFromQueue, Token);

            Task.Run(() =>
            {
                try
                {
                    if (Token.IsCancellationRequested || Disposed)
                    {
                        return;
                    }

                    //Try and connect
                    Listener = new Socket(Endpoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
                    Listener.BeginConnect(Endpoint, this.OnConnectCallback, Listener);
                    ConnectedMre.WaitOne();

                    //If client is connected activate connected event
                    if (IsConnected())
                    {
                        RaiseConnected();
                    }
                    else
                    {
                        KeepAliveTimer.Enabled = false;
                        RaiseDisconnected();
                        Close();
                        ConnectedMre.Reset();
                        Listener.BeginConnect(Endpoint, this.OnConnectCallback, Listener);
                    }
                }
                catch (Exception ex)
                {
                    throw new Exception(ex.Message, ex);
                }
            }, Token);
        }