コード例 #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 <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);
                }
            });
        }