QsHttpClient.h 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. #pragma once
  2. #include "QtCore/qobject.h"
  3. #include "QtCore/qtimer.h"
  4. #include <string>
  5. #include <Kaguya/kaguya.hpp>
  6. #include "yutil/queue.hpp"
  7. #include "ynet/http_client_plus.h"
  8. #include "QsBuffer.h"
  9. class QsHttpClient
  10. {
  11. public:
  12. enum MsgType
  13. {
  14. END,
  15. FAILED,
  16. DOWNING
  17. };
  18. struct MsgInfo
  19. {
  20. MsgType type;
  21. ylib::json data;
  22. };
  23. public:
  24. QsHttpClient();
  25. ~QsHttpClient();
  26. static void regist(kaguya::State& T);
  27. private:
  28. bool get(const std::string& url,bool wait);
  29. bool post(const std::string& url,const std::string& json_data);
  30. void close();
  31. bool head(const std::string& url);
  32. void setHeader(const std::string& name,const std::string& value);
  33. std::string responseHeader(const std::string& name);
  34. QsBuffer response();
  35. void onDownEnd(kaguya::LuaFunction);
  36. void onDownFailed(kaguya::LuaFunction);
  37. void onDowning(kaguya::LuaFunction);
  38. //signals:
  39. // void sigDowning(unsigned long long alldownsize,unsigned long long allsize);
  40. // void sigDownEnd();
  41. // void sigDownFailed();
  42. private:
  43. kaguya::LuaFunction m_onDownEnd;
  44. kaguya::LuaFunction m_onDownFailed;
  45. kaguya::LuaFunction m_onDowning;
  46. timestamp m_pre_onDowning_callback_msec = 0;
  47. ylib::buffer m_response;
  48. network::http::client_plus m_client;
  49. ylib::queue<MsgInfo> m_msg_queue;
  50. QTimer m_msg_timer;
  51. };