http_session.h 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #pragma once
  2. #include "http_define.h"
  3. #if USE_NET_HTTP_WEBSITE
  4. #include <map>
  5. #include <string>
  6. #include <vector>
  7. #include "http_interface.h"
  8. #include "yutil/counter.hpp"
  9. #include "yutil/cache.h"
  10. namespace ylib
  11. {
  12. namespace network
  13. {
  14. namespace http
  15. {
  16. class server;
  17. struct session_config
  18. {
  19. session_config()
  20. {
  21. update_sec = 0;
  22. timeout = 0;
  23. }
  24. // 更新时间
  25. uint64 update_sec;
  26. // 过期时间
  27. uint32 timeout;
  28. // 数据
  29. ylib::json data;
  30. };
  31. class session_mgr;
  32. class session :public ylib::json
  33. {
  34. public:
  35. session();
  36. ~session();
  37. void init(const std::string& session_name);
  38. void destory();
  39. std::string session_id();
  40. void update();
  41. friend class session_mgr;
  42. friend class request;
  43. private:
  44. std::string m_session_name;
  45. session_mgr* m_mgr;
  46. };
  47. class session_mgr :public ylib::error_base, public network::http::interface_
  48. {
  49. public:
  50. session_mgr();
  51. ~session_mgr();
  52. bool start(const std::string& cache_path, uint32 timeout_sec);
  53. void close();
  54. std::string create(int32 timeout_sec);
  55. bool add(const std::string& session_name, int32 timeout_sec);
  56. bool exist(const std::string& session_name);
  57. void set(const std::string& session_name, http::session& session);
  58. void del(const std::string& session_name);
  59. bool get(const std::string& session_name, http::session& session);
  60. void update(const std::string& session_name, int32 timeout_sec);
  61. void clear();
  62. ylib::cache* cache();
  63. friend class session;
  64. private:
  65. ylib::counter<size_t> m_index;
  66. ylib::cache* m_cache;
  67. uint32 m_timeout_sec;
  68. };
  69. }
  70. }
  71. }
  72. #endif