public Task StartReconnection() { reconnectCancellationSource = new CancellationTokenSource(); return(Task.Factory.StartNew(async() => { reconnectCancellationSource.Token.ThrowIfCancellationRequested(); while (true) { if (reconnectCancellationSource.Token.IsCancellationRequested) { reconnectCancellationSource.Token.ThrowIfCancellationRequested(); } if (ConnectionChallengeFunction == null) { throw new ConfigurationException("Unsetted challenge function for reconnection."); } if (!await ConnectionChallengeFunction(this)) { StoptListener(); ConnectionFailure?.Invoke(this, DateTime.UtcNow); await ProtocolStream.Reconnect(); startListener(); } await Task.Delay(ConnectionDelay); } }, reconnectCancellationSource.Token)); }
/// <summary> /// Desconecta del servidor /// </summary> internal void Disconnect() { if (ProtocolStream != null) { // Libera el stream de datos try { ProtocolStream.Dispose(); } catch (System.Net.Sockets.SocketException) { } catch (System.IO.IOException) { } // Libera el objeto ProtocolStream = null; } }
/// <summary> /// Disposes the transport. /// </summary> internal void Disconnect() { if (ProtocolStream != null) { try { ProtocolStream.Dispose(); } catch (SocketException) { } catch (IOException) { } ProtocolStream = null; } }