IM/MS/mmm/session.h
2023-07-04 14:10:13 +08:00

84 lines
2.3 KiB
C++

//
// Created by dongl on 23-5-10.
//
#ifndef IM2_SESSION_H
#define IM2_SESSION_H
#include "event2/bufferevent.h"
#include "agreement.h"
#include <string>
#include <map>
#include <optional>
#include <oneapi/tbb/task_group.h>
#include <list>
struct userinfo {
bufferevent* bev;
char ip[15];
std::map<std::string, std::string> 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<agreement_request>& request);
// 查找用户
std::optional<userinfo*> find_user(uint64_t account);
// 查找用户
std::optional<std::pair<uint64_t, userinfo*>> find_user_fd(uint64_t account);
// 删除用户
void remove_user(const std::shared_ptr<agreement_request> &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<std::string> 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<uint64_t, userinfo*> user_fd; // 用户的链接 暂时是一直链接 交给libevent 管理
std::map<bufferevent*, std::map<std::string, std::string>> session_map; // 当前链接的 存在的 seesion;
std::map<time_t, std::list<bev_key*>> session_time_wheel; // session 有效期 超时轮 映射
};
#endif //IM2_SESSION_H