示例#1
0
        public bool Send_FileDown(string remotePath, out Response response)
        {
            // open a file for writing
            const string downloadDir = "Downloads";

            if (!Directory.Exists(downloadDir))
            {
                try
                {
                    Directory.CreateDirectory(downloadDir);
                }
                catch (Exception e)
                {
                    response = new Response(false, String.Format("Unable to create base directory '{0}': '{1}'", downloadDir, e.Message));
                    return(true);
                }
            }
            string filename   = Path.GetFileName(remotePath);
            string localPath  = Path.Combine(downloadDir, filename);
            string errMessage = "";

            using (Stream fileOut = FtFileManager.OpenForWrite(localPath, out errMessage))
            {
                if (fileOut == null)
                {
                    response = new Response(false, errMessage);
                    return(true);
                }

                // Open a new connection to the server
                using (SslStream sslServer = connectFt())
                {
                    if (sslServer == null)
                    {
                        response = new Response(false, "Could not connect to server for file transfer.");
                        return(false);
                    }

                    FtClient ftClient = new FtClient(sslServer);

                    // write the command string
                    bool stillConnected = ftClient.Serializer.Serialize(String.Format("file_down:{0}", remotePath));
                    if (!stillConnected)
                    {
                        response = null;
                        return(false);
                    }

                    // expect ack
                    if (!ReadAck(ftClient.Deserializer, StringDelimiter, out response))
                    {
                        return(false);  // server disconnected unexpectedly
                    }
                    // server can't send file
                    if (!response.Success)
                    {
                        return(true);
                    }

                    // receive the file
                    var task = FtFileManager.FileReceiveAsync(ftClient.Client, fileOut);
                    task.Wait();
                    if (!task.Result.Item1)
                    {
                        return(false);
                    }

                    // expect ack
                    if (!ReadAck(ftClient.Deserializer, StringDelimiter, out response))
                    {
                        return(false);
                    }

                    return(true);
                }
            }
        }
示例#2
0
        public bool Send_FileUp(string localPath, out Response response)
        {
            // open the file for reading
            string errMessage = "";

            using (Stream fileIn = FtFileManager.OpenForRead(localPath, out errMessage))
            {
                if (fileIn == null)
                {
                    response = new Response(false, errMessage);
                    return(true);
                }

                // Open a new connection to the server
                using (SslStream sslServer = connectFt())
                {
                    if (sslServer == null)
                    {
                        response = new Response(false, "Could not connect to server for file transfer.");
                        return(false);
                    }

                    FtClient ftClient = new FtClient(sslServer);

                    string filename = Path.GetFileName(localPath);

                    // write the command string
                    if (!ftClient.Serializer.Serialize(String.Format("file_up:{0}", filename)))
                    {  // server disconnected unexpectedly
                        response = null;
                        return(false);
                    }

                    // expect ack
                    if (!ReadAck(ftClient.Deserializer, StringDelimiter, out response))
                    {
                        return(false);  // server disconnected unexpectedly
                    }
                    // server can't receive file
                    if (!response.Success)
                    {
                        return(true);
                    }

                    // send the file
                    var task = FtFileManager.FileSendAsync(sslServer, fileIn);
                    task.Wait();
                    if (!task.Result)
                    {
                        return(false);
                    }

                    // expect ack
                    // currently doesn't handle failed transfers DEV: fix
                    if (!ReadAck(ftClient.Deserializer, StringDelimiter, out response))
                    {
                        return(false);
                    }

                    return(true);
                }
            }
        }