Beispiel #1
0
        /// <summary>
        /// TBD
        /// </summary>
        /// <param name="message">TBD</param>
        /// <returns>TBD</returns>
        protected override bool Receive(object message)
        {
            switch (message)
            {
            case OnNext next:
                try
                {
                    var byteString = (ByteString)next.Element;
                    var bytes      = byteString.ToArray();
                    _chan.Write(bytes, 0, bytes.Length);
                    _bytesWritten += bytes.Length;
                    if (_autoFlush)
                    {
                        _chan.Flush(true);
                    }
                }
                catch (Exception ex)
                {
                    CloseAndComplete(IOResult.Failed(_bytesWritten, ex));
                    Cancel();
                }
                return(true);

            case OnError error:
                _log.Error(error.Cause, "Tearing down FileSink({0}) due to upstream error", _f.FullName);
                CloseAndComplete(new Try <IOResult>(new AbruptIOTerminationException(IOResult.Success(_bytesWritten), error.Cause)));
                Context.Stop(Self);
                return(true);

            case OnComplete _:
                try
                {
                    _chan.Flush(true);
                }
                catch (Exception ex)
                {
                    CloseAndComplete(IOResult.Failed(_bytesWritten, ex));
                }
                Context.Stop(Self);
                return(true);

            case FlushSignal _:
                try
                {
                    _chan.Flush();
                }
                catch (Exception ex)
                {
                    _log.Error(ex, "Tearing down FileSink({0}). File flush failed.", _f.FullName);
                    CloseAndComplete(IOResult.Failed(_bytesWritten, ex));
                    Context.Stop(Self);
                }
                return(true);
            }

            return(false);
        }
Beispiel #2
0
 /// <summary>
 /// TBD
 /// </summary>
 protected override void PostStop()
 {
     base.PostStop();
     try
     {
         _inputstream?.Dispose();
     }
     catch (Exception ex)
     {
         _completionSource.SetResult(IOResult.Failed(_readBytesTotal, ex));
     }
     _completionSource.SetResult(IOResult.Success(_readBytesTotal));
 }
Beispiel #3
0
        /// <summary>
        /// TBD
        /// </summary>
        protected override void PostStop()
        {
            try
            {
                _chan?.Dispose();
            }
            catch (Exception ex)
            {
                _completionPromise.TrySetResult(IOResult.Failed(_bytesWritten, ex));
            }

            _completionPromise.TrySetResult(IOResult.Success(_bytesWritten));
            base.PostStop();
        }
Beispiel #4
0
        /// <summary>
        /// TBD
        /// </summary>
        protected override void PostStop()
        {
            base.PostStop();

            try
            {
                _chan?.Dispose();
            }
            catch (Exception ex)
            {
                _completionPromise.TrySetResult(IOResult.Failed(_readBytesTotal, ex));
            }

            _completionPromise.TrySetResult(IOResult.Success(_readBytesTotal));
        }
Beispiel #5
0
        public void SynchronousFileSink_should_write_single_line_to_a_file_from_lazy_sink()
        {
            this.AssertAllStagesStopped(() =>
            {
                TargetFile(f =>
                {
                    var lazySink = Sink.LazySink(
                        (ByteString _) => Task.FromResult(FileIO.ToFile(f)),
                        () => Task.FromResult(IOResult.Success(0)))
                                   .MapMaterializedValue(t => t.AwaitResult());

                    var completion = Source.From(new [] { _testByteStrings.Head() })
                                     .RunWith(lazySink, _materializer);

                    completion.AwaitResult();
                    CheckFileContent(f, _testLines.Head());
                });
            }, _materializer);
        }
        /// <summary>
        /// TBD
        /// </summary>
        /// <param name="message">TBD</param>
        /// <returns>TBD</returns>
        protected override bool Receive(object message)
        {
            switch (message)
            {
            case OnNext next:
                try
                {
                    var bytes = (ByteString)next.Element;
                    //blocking write
                    _outputStream.Write(bytes.ToArray(), 0, bytes.Count);
                    _bytesWritten += bytes.Count;
                    if (_autoFlush)
                    {
                        _outputStream.Flush();
                    }
                }
                catch (Exception ex)
                {
                    _completionPromise.TrySetResult(IOResult.Failed(_bytesWritten, ex));
                    Cancel();
                }
                return(true);

            case OnError error:
                _log.Error(error.Cause, "Tearing down OutputStreamSink due to upstream error, wrote bytes: {0}", _bytesWritten);
                _completionPromise.TrySetException(new AbruptIOTerminationException(IOResult.Success(_bytesWritten), error.Cause));
                Context.Stop(Self);
                return(true);

            case OnComplete _:
                Context.Stop(Self);
                _outputStream.Flush();
                return(true);
            }

            return(false);
        }
Beispiel #7
0
 /// <summary>
 /// TBD
 /// </summary>
 protected override void PostStop()
 {
     CloseAndComplete(IOResult.Success(_bytesWritten));
     base.PostStop();
 }