コード例 #1
0
        private void Connected()
        {
            Receive <ListDirectory>((cmd) =>
            {
                StopIdlePeriod();

                IEnumerable <SftpFileInfo> result = null;
                try
                {
                    result = _connection.ListDirectory(cmd.RemotePath, null);
                }
                catch (Exception)
                {
                    result = new SftpFileInfo[] { };
                }
                this.Sender.Tell(result, Self);

                StartIdlePeriod();
            });

            Receive <ReceiveTimeout>((cmd) =>
            {
                if (DateTimeOffset.Now - _idleFromTime > TimeSpan.FromSeconds(ConnectionTimeoutInSeconds))
                {
                    StopIdlePeriod();

                    _connection.Disconnect();
                    _connection.Dispose();

                    Become(Disconnected);
                }
            });
        }
コード例 #2
0
        private void Connected()
        {
            Receive <ListDirectory>((cmd) =>
            {
                StopIdlePeriod();

                IEnumerable <SftpFileInfo> result = null;
                try
                {
                    result = _connection.ListDirectory(cmd.RemotePath, null);
                }
                catch (Exception)
                {
                    result = new SftpFileInfo[] { };
                }
                this.Sender.Tell(result, Self);

                StartIdlePeriod();
            });

            Receive <UploadFile>((cmd) =>
            {
                StopIdlePeriod();

                Utils.EnsureParentDirectoryExists(_connection, cmd.RemotePath);
                using (var stream = _fileStreamProvider.OpenRead(cmd.LocalPath))
                {
                    _connection.UploadFile(stream, cmd.RemotePath, null);
                }

                StartIdlePeriod();
            });

            Receive <DownloadFile>((cmd) =>
            {
                StopIdlePeriod();

                using (var stream = _fileStreamProvider.OpenWrite(cmd.LocalPath))
                {
                    _connection.DownloadFile(cmd.RemotePath, stream, null);
                }

                StartIdlePeriod();
            });

            Receive <ReceiveTimeout>((cmd) =>
            {
                if (DateTimeOffset.Now - _idleFromTime > TimeSpan.FromSeconds(ConnectionTimeoutInSeconds))
                {
                    StopIdlePeriod();

                    _connection.Disconnect();
                    _connection.Dispose();

                    Become(Disconnected);
                }
            });
        }
コード例 #3
0
        private void Connected()
        {
            Receive <Disconnect>((cmd) =>
            {
                _connection.Disconnect();
                _connection.Dispose();

                Become(Disconnected);
            });
        }
コード例 #4
0
        protected virtual void Dispose(bool disposing)
        {
            // Do not dispose this object multiple times
            if (this.disposed)
            {
                return;
            }

            // If the method has been called by user code then it is safe to access objects
            if (disposing)
            {
                _sftpClient?.Dispose();
            }

            // Mark this object as disposed (so it does not happen twice)
            this.disposed = true;
        }
コード例 #5
0
        private void Connected()
        {
            Receive <ListDirectory>((cmd) =>
            {
                IEnumerable <SftpFileInfo> result = null;
                try
                {
                    result = _connection.ListDirectory(cmd.RemotePath, null);
                }
                catch (Exception)
                {
                    result = new SftpFileInfo[] { };
                }
                this.Sender.Tell(result);
            });
            Receive <Disconnect>((cmd) =>
            {
                _connection.Disconnect();
                _connection.Dispose();

                Become(Disconnected);
            });
        }
コード例 #6
0
 public void Dispose()
 {
     _sftpClient?.Dispose();
 }
コード例 #7
0
        private void Connected()
        {
            Receive <ListDirectory>((cmd) =>
            {
                StopIdlePeriod();

                IEnumerable <SftpFileInfo> result = null;
                try
                {
                    result = _connection.ListDirectory(cmd.RemotePath, null);
                }
                catch (Exception)
                {
                    result = new SftpFileInfo[] { };
                }
                this.Sender.Tell(result, Self);

                StartIdlePeriod();
            });

            Receive <UploadFile>((cmd) =>
            {
                StopIdlePeriod();

                AsyncCallback callback = ar =>
                {
                    try
                    {
                        _connection.EndUploadFile(ar);
                        var result = _clientFactory.CreateSftpAsyncResult(ar);
                        if (result.IsCanceled)
                        {
                            this.Self.Tell(new Cancelled());
                        }
                        else
                        {
                            this.Self.Tell(new Completed());
                        }
                    }
                    catch (Exception ex)
                    {
                        this.Self.Tell(new Error(ex.Message));
                    }
                };
                Utils.EnsureParentDirectoryExists(_connection, cmd.RemotePath);
                _stream      = _fileStreamProvider.OpenRead(cmd.LocalPath);
                _asyncResult = _connection.BeginUploadFile(_stream, cmd.RemotePath, callback, null);

                Become(Transferring);
            });

            Receive <DownloadFile>((cmd) =>
            {
                StopIdlePeriod();

                AsyncCallback callback = ar =>
                {
                    try
                    {
                        _connection.EndDownloadFile(ar);
                        var result = _clientFactory.CreateSftpAsyncResult(ar);
                        if (result.IsCanceled)
                        {
                            this.Self.Tell(new Cancelled());
                        }
                        else
                        {
                            this.Self.Tell(new Completed());
                        }
                    }
                    catch (Exception ex)
                    {
                        this.Self.Tell(new Error(ex.Message));
                    }
                };
                _stream      = _fileStreamProvider.OpenWrite(cmd.LocalPath);
                _asyncResult = _connection.BeginDownloadFile(cmd.RemotePath, _stream, callback, null);

                Become(Transferring);
            });

            Receive <ReceiveTimeout>((cmd) =>
            {
                if (DateTimeOffset.Now - _idleFromTime > TimeSpan.FromSeconds(ConnectionTimeoutInSeconds))
                {
                    StopIdlePeriod();

                    _connection.Disconnect();
                    _connection.Dispose();

                    Become(Disconnected);
                }
            });
        }