XCRC.cs 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Net.FtpClient;
  4. namespace System.Net.FtpClient.Extensions {
  5. /// <summary>
  6. /// Implementation of the non-standard XCRC command
  7. /// </summary>
  8. public static class XCRC {
  9. delegate string AsyncGetXCRC(string path);
  10. static Dictionary<IAsyncResult, AsyncGetXCRC> m_asyncmethods = new Dictionary<IAsyncResult, AsyncGetXCRC>();
  11. /// <summary>
  12. /// Get the CRC value of the specified file. This is a non-standard extension of the protocol
  13. /// and may throw a FtpCommandException if the server does not support it.
  14. /// </summary>
  15. /// <param name="client">FtpClient object</param>
  16. /// <param name="path">The path of the file you'd like the server to compute the CRC value for.</param>
  17. /// <returns>The response from the server, typically the CRC value. FtpCommandException thrown on error</returns>
  18. public static string GetXCRC(this FtpClient client, string path) {
  19. FtpReply reply;
  20. if (!(reply = client.Execute("XCRC {0}", path)).Success)
  21. throw new FtpCommandException(reply);
  22. return reply.Message;
  23. }
  24. /// <summary>
  25. /// Asynchronusly retrieve a CRC hash. The XCRC command is non-standard
  26. /// and not guaranteed to work.
  27. /// </summary>
  28. /// <param name="client">FtpClient Object</param>
  29. /// <param name="path">Full or relative path to remote file</param>
  30. /// <param name="callback">AsyncCallback</param>
  31. /// <param name="state">State Object</param>
  32. /// <returns>IAsyncResult</returns>
  33. public static IAsyncResult BeginGetXCRC(this FtpClient client, string path, AsyncCallback callback, object state) {
  34. AsyncGetXCRC func = new AsyncGetXCRC(client.GetXCRC);
  35. IAsyncResult ar = func.BeginInvoke(path, callback, state); ;
  36. lock (m_asyncmethods) {
  37. m_asyncmethods.Add(ar, func);
  38. }
  39. return ar;
  40. }
  41. /// <summary>
  42. /// Ends an asynchronous call to BeginGetXCRC()
  43. /// </summary>
  44. /// <param name="ar">IAsyncResult returned from BeginGetXCRC()</param>
  45. /// <returns>The CRC hash of the specified file.</returns>
  46. public static string EndGetXCRC(IAsyncResult ar) {
  47. AsyncGetXCRC func = null;
  48. lock (m_asyncmethods) {
  49. if (!m_asyncmethods.ContainsKey(ar))
  50. throw new InvalidOperationException("The specified IAsyncResult was not found in the collection.");
  51. func = m_asyncmethods[ar];
  52. m_asyncmethods.Remove(ar);
  53. }
  54. return func.EndInvoke(ar);
  55. }
  56. }
  57. }