/// <summary> /// Close stream /// </summary> /// <returns></returns> public Task CloseAsync() { Task pumps; lock (this) { // Set close state _open.Cancel(); _receive.Complete(); _send.Complete(); // Remove ourselves from the listener... Close(); // Fail any in progress open if (!Tcs.Task.IsCompleted) { Tcs.TrySetException(new SocketException(SocketError.Closed)); } if (_pumps == null) { return(TaskEx.Completed); } pumps = _pumps; _pumps = null; return(pumps); } }
public override void onRequestFinish(MegaSDK api, MRequest request, MError e) { base.onRequestFinish(api, request, e); if (Tcs.Task.IsFaulted) { return; } if (request.getType() == MRequestType.TYPE_LOGIN) { switch (e.getErrorCode()) { case MErrorType.API_OK: // Login was successful Tcs?.TrySetResult(true); return; case MErrorType.API_ESID: // Bad session ID Tcs?.TrySetException(new BadSessionIdException()); Tcs?.TrySetResult(false); return; default: // Unknown result, but not successful Tcs?.TrySetResult(false); return; } } }
public void SignalException(Exception exception) { Trace2($"Signal {exception} (enqueue)"); _tcs.TrySetException(exception); // Ensure continuations are run asynchronously //_tcs.TrySetExceptionAsync(exception); }
public override async Task Initialize() { try { await AsyncConnection.CreateTableAsync <Host>(); await AsyncConnection.CreateTableAsync <CommandSsh>(); await Migrate(); Tcs.TrySetResult(null); } catch (Exception e) { Tcs.TrySetException(e); throw; } }
/// <summary> /// Close stream /// </summary> /// <returns></returns> public async Task CloseAsync() { // Remove ourselves from the listener... RelayStream stream; _relay._streamMap.TryRemove(StreamId, out stream); // If not remotely closed, close streaming now if (!_open.IsCancellationRequested) { try { // Set close state _open.Cancel(); _consumerQueue.CompleteAdding(); // Fail any in progress open Tcs.TrySetException(new SocketException(SocketError.Closed)); ProxyEventSource.Log.StreamClosing(this, _codec.Stream); if (_producerTask != null) { // Wait until stream etc. is closed and gone _streaming.Cancel(); await _producerTask.ConfigureAwait(false); } } catch (Exception ex) { ProxyEventSource.Log.HandledExceptionAsWarning(this, ex); } _producerTask = null; } // Mark all waiting readers as cancelled TaskCompletionSource <bool> tcs; while (_consumerQueue.TryTake(out tcs, 0)) { // Cancel all waiting readers due to closing tcs.TrySetException(new SocketException(SocketError.Closed)); } }