// // Created by dongl on 23-5-10. // #ifndef IM2_SESSION_H #define IM2_SESSION_H #include "event2/bufferevent.h" #include "agreement.h" #include #include #include #include #include struct userinfo { bufferevent* bev; char ip[15]; std::map session; // 用户的 seesion; }; struct bev_key { bufferevent* bev; std::string key; std::string value; }; enum session_build_type { SESSION_SUPPORT_USER = 0, SESSION_SUPPORT_SESSION = 1, SESSION_SUPPORT_ALL = 2, }; class session { public: /** * 空构造是 全部支持 */ session(); /** * 选择支持 */ session(session_build_type type); public: // 添加用户 void add_user(mp::sri* sri, std::shared_ptr& request); // 查找用户 std::optional find_user(uint64_t account); // 查找用户 std::optional> find_user_fd(uint64_t account); // 删除用户 void remove_user(const std::shared_ptr &request); // 删除用户 void remove_user(bufferevent* bev); // 是否有这个用户 bool is_user(const std::string& account); // 是否有这个用户 bool is_user(uint64_t account); // 初始化 session void init_session(bufferevent* bev); // 设置 session void set_session(bufferevent* bev, const std::string& session_key, const std::string& session_value); // 获取 session std::optional get_session(bufferevent* bev, const std::string& session_key); // 删除 session void remove_session(bufferevent* bev, const std::string& session_key); void remove_session(userinfo* user); // 时间轮思路的 定时器 void timing(); protected: tbb::task_group time_wheel; // 时间轮 线程池 std::map user_fd; // 用户的链接 暂时是一直链接 交给libevent 管理 std::map> session_map; // 当前链接的 存在的 seesion; std::map> session_time_wheel; // session 有效期 超时轮 映射 }; #endif //IM2_SESSION_H