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
        /// <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)
                {
                    _completionPromise.TrySetResult(IOResult.Failed(_bytesWritten, ex));
                    Cancel();
                }
                return(true);

            case OnError error:
                _log.Error(error.Cause, $"Tearing down FileSink({_f.FullName}) due to upstream error");
                _completionPromise.TrySetResult(IOResult.Failed(_bytesWritten, error.Cause));
                Context.Stop(Self);
                return(true);

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

            case var msg when _flushCommand != null && ReferenceEquals(_flushCommand, msg):
                _chan.Flush();
                return(true);
            }

            return(false);
        }
Beispiel #6
0
 /// <summary>
 /// TBD
 /// </summary>
 protected override void PreStart()
 {
     try
     {
         _chan = _f.Open(_fileMode, FileAccess.Write);
         if (_startPosition > 0)
         {
             _chan.Position = _startPosition;
         }
         base.PreStart();
     }
     catch (Exception ex)
     {
         _completionPromise.TrySetResult(IOResult.Failed(_bytesWritten, ex));
         Cancel();
     }
 }
Beispiel #7
0
        /// <summary>
        /// TBD
        /// </summary>
        protected override void PreStart()
        {
            try
            {
                _chan = _f.Open(FileMode.Open, FileAccess.Read);
                if (_startPosition > 0)
                {
                    _chan.Position = _startPosition;
                }
            }
            catch (Exception ex)
            {
                _completionPromise.TrySetResult(IOResult.Failed(0, ex));
                OnErrorThenStop(ex);
            }

            base.PreStart();
        }
Beispiel #8
0
        /// <summary>
        /// TBD
        /// </summary>
        protected override void PreStart()
        {
            try
            {
                // Allow opening the same file for reading multiple times
                _chan = _f.Open(FileMode.Open, FileAccess.Read, FileShare.Read);
                if (_startPosition > 0)
                {
                    _chan.Position = _startPosition;
                }
            }
            catch (Exception ex)
            {
                _completionPromise.TrySetResult(IOResult.Failed(0, ex));
                OnErrorThenStop(ex);
            }

            base.PreStart();
        }
Beispiel #9
0
 /// <summary>
 /// TBD
 /// </summary>
 /// <param name="message">TBD</param>
 /// <returns>TBD</returns>
 protected override bool Receive(object message)
 {
     return(message.Match()
            .With <OnNext>(next =>
     {
         try
         {
             var byteString = (ByteString)next.Element;
             var bytes = byteString.ToArray();
             _chan.Write(bytes, 0, bytes.Length);
             _bytesWritten += bytes.Length;
         }
         catch (Exception ex)
         {
             _completionPromise.TrySetResult(IOResult.Failed(_bytesWritten, ex));
             Cancel();
         }
     })
            .With <OnError>(error =>
     {
         _log.Error(error.Cause, $"Tearing down FileSink({_f.FullName}) due to upstream error");
         _completionPromise.TrySetResult(IOResult.Failed(_bytesWritten, error.Cause));
         Context.Stop(Self);
     })
            .With <OnComplete>(() =>
     {
         try
         {
             _chan.Flush(true);
         }
         catch (Exception ex)
         {
             _completionPromise.TrySetResult(IOResult.Failed(_bytesWritten, ex));
         }
         Context.Stop(Self);
     })
            .WasHandled);
 }
        /// <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 #11
0
 /// <summary>
 /// TBD
 /// </summary>
 /// <param name="message">TBD</param>
 /// <returns>TBD</returns>
 protected override bool Receive(object message)
 {
     return(message.Match()
            .With <OnNext>(next =>
     {
         try
         {
             var bytes = next.Element as ByteString;
             //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();
         }
     })
            .With <OnError>(error =>
     {
         _log.Error(error.Cause,
                    $"Tearing down OutputStreamSink due to upstream error, wrote bytes: {_bytesWritten}");
         _completionPromise.TrySetResult(IOResult.Failed(_bytesWritten, error.Cause));
         Context.Stop(Self);
     })
            .With <OnComplete>(() =>
     {
         Context.Stop(Self);
         _outputStream.Flush();
     })
            .WasHandled);
 }