http_router.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. #pragma once
  2. #include "http_define.h"
  3. #if USE_NET_HTTP_WEBSITE
  4. #include "http_interface.h"
  5. #include <functional>
  6. #include <map>
  7. #include <regex>
  8. #include "util/array.hpp"
  9. #include "util/queue.hpp"
  10. #include "util/thread.h"
  11. class IHPThreadPool;
  12. namespace ylib
  13. {
  14. namespace network
  15. {
  16. namespace http
  17. {
  18. class reqpack;
  19. class http_server_lst;
  20. class request;
  21. class response;
  22. class interceptor;
  23. class subscribe;
  24. /*************************************************************************
  25. * class:路由中专服务
  26. *************************************************************************/
  27. class router :public ylib::error_base, public network::http::interface_, private ylib::ithread
  28. {
  29. public:
  30. /*线程参数信息*/
  31. struct thread_param_info
  32. {
  33. void clear() {}
  34. network::http::router* router;
  35. network::http::reqpack* reqpack;
  36. };
  37. public:
  38. router();
  39. ~router();
  40. /******************************************************************
  41. * function:启动
  42. * param
  43. * config : 配置项
  44. * return:
  45. * 失败可通过 last_error() 返回错误信息。
  46. ******************************************************************/
  47. bool start(const router_config& config);
  48. /******************************************************************
  49. * function:关闭
  50. ******************************************************************/
  51. void close();
  52. /******************************************************************
  53. * function:拦截器
  54. ******************************************************************/
  55. network::http::interceptor* interceptor();
  56. /******************************************************************
  57. * function:订阅器
  58. ******************************************************************/
  59. network::http::subscribe* subscribe();
  60. /******************************************************************
  61. * function:其它
  62. * desc:未订阅请求触发该回调
  63. * param
  64. * callback : 触发回调
  65. ******************************************************************/
  66. void other(std::function<void(network::http::request*, network::http::response*,network::http::websocket_message*)> callback);
  67. /******************************************************************
  68. * function:数据接收后回调
  69. ******************************************************************/
  70. void on_recved(std::function<void(const ylib::buffer& begin, ylib::buffer* end)> callback);
  71. /******************************************************************
  72. * function:关闭回调
  73. ******************************************************************/
  74. void on_close(std::function<void(uint64 connid,network::http::websocket_message* ws_msg)> callback);
  75. /******************************************************************
  76. * function:数据发送前回调
  77. * desc:不支持大文件断点传输方式
  78. ******************************************************************/
  79. void on_sendbefore(std::function<void(const ylib::buffer& begin, ylib::buffer* end)> callback);
  80. /******************************************************************
  81. * function:线程回调[禁止调用]
  82. * desc:put提交后投递到线程池,线程池开始执行调用该回调。
  83. * param
  84. * param : 线程参数
  85. ******************************************************************/
  86. void __thread_callback(reqpack* rq);
  87. friend class http_server_lst;
  88. friend class response;
  89. /******************************************************************
  90. * function:队列数
  91. ******************************************************************/
  92. size_t queue_size();
  93. private:
  94. // 添加任务
  95. void push(reqpack* rp);
  96. // 添加连接断开任务
  97. void push_close(uint64 connid,std::shared_ptr<network::http::websocket_message> ws_msg);
  98. // 是否为代理任务
  99. bool is_proxy(reqpack* rp);
  100. #if HTTP_LUA_ENGINE == 1
  101. // LUA执行
  102. void lua_engine(reqpack *rp,const network::http::subscribe_info& info);
  103. #endif
  104. private:
  105. // 线程池
  106. IHPThreadPool* m_threadpool;
  107. // [回调] 未订阅请求
  108. std::function<void(network::http::request*, network::http::response*,network::http::websocket_message*)> m_callback_other;
  109. // [回调] 接收后
  110. std::function<void(const ylib::buffer& begin, ylib::buffer* end)> m_callback_recved;
  111. // [回调] 发送前
  112. std::function<void(const ylib::buffer& begin, ylib::buffer* end)> m_callback_sendbefore;
  113. // [回调] 关闭
  114. std::function<void(uint64 connid,network::http::websocket_message* ws_msg)> m_callback_close;
  115. // 拦截器
  116. std::unique_ptr<network::http::interceptor> m_interceptor;
  117. // 订阅器
  118. std::unique_ptr<network::http::subscribe> m_subscribe;
  119. private:
  120. ylib::queue<network::http::reqpack*> m_handle_queue;
  121. router_config m_config;
  122. // 通过 ithread 继承
  123. virtual bool run() override;
  124. };
  125. }
  126. }
  127. }
  128. #endif