/// <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); }
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); } }
/// <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); }