using System;
using System.Collections.Generic;
using FluentFTP;
namespace FluentFTP.Extensions {
///
/// Implementation of the non-standard XCRC command
///
public static class XCRC {
delegate string AsyncGetXCRC(string path);
static Dictionary m_asyncmethods = new Dictionary();
///
/// Get the CRC value of the specified file. This is a non-standard extension of the protocol
/// and may throw a FtpCommandException if the server does not support it.
///
/// FtpClient object
/// The path of the file you'd like the server to compute the CRC value for.
/// The response from the server, typically the CRC value. FtpCommandException thrown on error
public static string GetXCRC(this FtpClient client, string path) {
FtpReply reply;
if (!(reply = client.Execute("XCRC {0}", path)).Success)
throw new FtpCommandException(reply);
return reply.Message;
}
///
/// Asynchronusly retrieve a CRC hash. The XCRC command is non-standard
/// and not guaranteed to work.
///
/// FtpClient Object
/// Full or relative path to remote file
/// AsyncCallback
/// State Object
/// IAsyncResult
public static IAsyncResult BeginGetXCRC(this FtpClient client, string path, AsyncCallback callback, object state) {
AsyncGetXCRC func = new AsyncGetXCRC(client.GetXCRC);
IAsyncResult ar = func.BeginInvoke(path, callback, state); ;
lock (m_asyncmethods) {
m_asyncmethods.Add(ar, func);
}
return ar;
}
///
/// Ends an asynchronous call to BeginGetXCRC()
///
/// IAsyncResult returned from BeginGetXCRC()
/// The CRC hash of the specified file.
public static string EndGetXCRC(IAsyncResult ar) {
AsyncGetXCRC func = null;
lock (m_asyncmethods) {
if (!m_asyncmethods.ContainsKey(ar))
throw new InvalidOperationException("The specified IAsyncResult was not found in the collection.");
func = m_asyncmethods[ar];
m_asyncmethods.Remove(ar);
}
return func.EndInvoke(ar);
}
}
}