128 lines
4.1 KiB
C++
128 lines
4.1 KiB
C++
//
|
|
// Created by dongl on 23-5-10.
|
|
//
|
|
|
|
#include <arpa/inet.h>
|
|
#include <thread>
|
|
#include "session.h"
|
|
|
|
//std::map<uint64_t, userinfo*> session::user_fd;
|
|
//std::map<bufferevent*, std::map<std::string, std::string>> session::session_map;
|
|
|
|
/// curr mem user curd user session
|
|
void session::add_user(mp::sri* sri, std::shared_ptr<agreement_request>& request) {
|
|
if (sri->sri_code() == mp::MP_LOGIN_SUCCESS) {
|
|
auto ele = new userinfo();
|
|
ele->bev = request->m_bev;
|
|
sprintf(ele->ip, "%s", inet_ntoa(request->m_addr->sin_addr));
|
|
|
|
user_fd.insert({strtol(request->m_body.account().c_str(), nullptr, 0), ele});
|
|
}
|
|
}
|
|
|
|
void session::remove_user(bufferevent *bev) {
|
|
uint64_t target_ele;
|
|
for (const auto &item: user_fd) {
|
|
if (bev == item.second->bev) {
|
|
target_ele = item.first;
|
|
break;
|
|
}
|
|
}
|
|
|
|
user_fd.erase(target_ele);
|
|
bufferevent_free(bev);
|
|
}
|
|
|
|
void session::remove_user(const std::shared_ptr<agreement_request>& request) {
|
|
user_fd.erase(strtol(request->m_body.account().c_str(), nullptr, 0));
|
|
bufferevent_free(request->m_bev);
|
|
}
|
|
|
|
bool session::is_user(const std::string& account) {
|
|
if( user_fd.find(strtol(account.c_str(), nullptr, 0)) == user_fd.cend() ){
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
std::optional<std::pair<uint64_t, userinfo*>> session::find_user_fd(uint64_t account) {
|
|
auto ret = user_fd.find(account);
|
|
if (ret != user_fd.cend()) {
|
|
// return std::make_optional<std::pair<uint64_t, userinfo*>>(ret->first, ret->second);
|
|
return {{ret->first, ret->second} };
|
|
} else {
|
|
return std::nullopt;
|
|
}
|
|
}
|
|
|
|
/// user bev fd lik session
|
|
// 给用户 添加会话信息
|
|
void session::set_session(bufferevent* bev, const std::string &session_key, const std::string &session_value) {
|
|
auto ret = session_map.find(bev);
|
|
// 先看有没有 bev session 没有初始化并且添加一个 有取出来sub session 在添加
|
|
if (ret != session_map.cend()) {
|
|
std::map<std::string, std::string> sub_session;
|
|
sub_session.insert({session_key, session_value});
|
|
session_map.insert({bev, sub_session});
|
|
} else {
|
|
ret->second.insert({session_key, session_value});
|
|
}
|
|
|
|
/// map 内部自动排序 添加 时间 要删除信息的链表
|
|
// 添加时间轮 定时器
|
|
time_t t = time(nullptr);
|
|
auto retwheel = session_time_wheel.find(t);
|
|
if (retwheel != session_time_wheel.cend()) {
|
|
auto list = session_time_wheel.find(t)->second;
|
|
list.emplace_back(bev, session_key);
|
|
session_time_wheel.insert({t, list});
|
|
} else {
|
|
std::list<std::pair<bufferevent*/*bev*/, std::string/*key*/>> list;
|
|
list.emplace_back(bev, session_key);
|
|
session_time_wheel.insert({t, list});
|
|
}
|
|
}
|
|
|
|
// 给用户 查询会话信息
|
|
std::optional<std::string> session::get_session(bufferevent* bev, const std::string &session_key) {
|
|
auto sess = session_map.find(bev);
|
|
std::map<std::string, std::string> sub_sess;
|
|
if (sess != session_map.cend()) {
|
|
auto ret = sess->second.find("session_key");
|
|
if (ret != sub_sess.cend()) {
|
|
return ret->second;
|
|
}
|
|
}
|
|
return std::nullopt;
|
|
}
|
|
|
|
void session::remove_session(bufferevent *bev, const std::string &session_key) {
|
|
auto ret = session_map.find(bev);
|
|
std::map<std::string, std::string> sub_sess;
|
|
if (ret != session_map.cend()) {
|
|
auto sub = ret->second.find(session_key);
|
|
if (sub != sub_sess.cend()) {
|
|
ret->second.erase(sub);
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
// 时间轮思路的 定时器
|
|
void session::timing() {
|
|
time_wheel.run([&]() {
|
|
while (true) {
|
|
if (!session_time_wheel.empty()) {
|
|
auto wheel = session_time_wheel.begin();
|
|
if (wheel->first > time(nullptr) + 300000) { // 5分钟
|
|
// 超时5分钟就删除
|
|
for (const auto &item: wheel->second) {
|
|
remove_session(item.first, item.second);
|
|
}
|
|
}
|
|
}
|
|
std::this_thread::sleep_for(std::chrono::seconds(1));
|
|
}
|
|
});
|
|
}
|