ftp_client.h 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. #pragma once
  2. #include "define.h"
  3. #include "util/file.h"
  4. #include "util/thread.h"
  5. #include "base/error.h"
  6. #if USE_NET_FTP
  7. namespace ylib
  8. {
  9. namespace network
  10. {
  11. namespace ftp
  12. {
  13. class client;
  14. typedef void(*CALLBACK_FTPCLIENT_DOWNLOADING)(class ylib::network::ftp::client* client, uint32 all_size, uint32 down_size, uint64 param);
  15. typedef void(*CALLBACK_FTPCLIENT_DOWNLOADED)(class ylib::network::ftp::client* client, bool result, uint64 param);
  16. struct DownInfo
  17. {
  18. ~DownInfo()
  19. {
  20. if (file != nullptr)
  21. {
  22. file->close();
  23. delete file;
  24. }
  25. }
  26. ylib::file_io* file = nullptr;
  27. uint32 size = 0;
  28. void* fileptr = nullptr;
  29. std::string local_path;
  30. std::string remote_path;
  31. uint64 param = 0;
  32. CALLBACK_FTPCLIENT_DOWNLOADING downloading = nullptr;
  33. CALLBACK_FTPCLIENT_DOWNLOADED downloaded = nullptr;
  34. bool ok = false;
  35. };
  36. class client :public ylib::error_base, private ylib::ithread
  37. {
  38. public:
  39. client();
  40. ~client();
  41. bool connect(const std::string& ipaddress, const std::string& username, const std::string& password, ushort port);
  42. void close();
  43. bool upload(const std::string& local_filepath, const std::string& remote_filepath);
  44. bool download(const std::string& local_filepath, const std::string& remote_filepath, size_t param, CALLBACK_FTPCLIENT_DOWNLOADING downloading, CALLBACK_FTPCLIENT_DOWNLOADED downloaded);
  45. void wait();
  46. bool download(const std::string& local_filepath, const std::string& remote_filepath);
  47. bool create_dir(const std::string& remote_path);
  48. bool delete_dir(const std::string& remote_path);
  49. bool delete_file(const std::string& remote_path);
  50. private:
  51. void* m_ptr = nullptr;
  52. DownInfo* m_download = nullptr;
  53. CALLBACK_FTPCLIENT_DOWNLOADING m_cb_downloading = nullptr;
  54. CALLBACK_FTPCLIENT_DOWNLOADED m_cb_downloaded = nullptr;
  55. virtual bool run() override;
  56. };
  57. }
  58. }
  59. };
  60. #endif