tbb libevent protobuf
This commit is contained in:
40
MS/CMakeLists.txt
Normal file
40
MS/CMakeLists.txt
Normal file
@@ -0,0 +1,40 @@
|
||||
project(MS)
|
||||
|
||||
aux_source_directory(pool/thread DIR_THREAD_POOL)
|
||||
aux_source_directory(pool/mem DIR_MEM_POOL)
|
||||
aux_source_directory(pool/object DIR_OBJECT_POOL)
|
||||
aux_source_directory(mmm DIR_MMM)
|
||||
aux_source_directory(tools DIR_TOOLS)
|
||||
|
||||
include_directories(${CMAKE_SOURCE_DIR}/include/libevent)
|
||||
include_directories(${CMAKE_SOURCE_DIR}/include/mp)
|
||||
include_directories(${CMAKE_SOURCE_DIR}/include/ini)
|
||||
include_directories(${CMAKE_SOURCE_DIR}/include/mysql++)
|
||||
include_directories(${CMAKE_SOURCE_DIR}/include/mysql++/mysql)
|
||||
include_directories(${CMAKE_SOURCE_DIR}/MDB/imm_mysqldb)
|
||||
include_directories(${CMAKE_SOURCE_DIR}/MS/works)
|
||||
message("CMAKE_SOURCE_DIR ${CMAKE_SOURCE_DIR}/include/libevent")
|
||||
|
||||
link_directories(${CMAKE_SOURCE_DIR}/lib/libevent)
|
||||
link_directories(${CMAKE_SOURCE_DIR}/lib/tbb)
|
||||
|
||||
add_subdirectory(works)
|
||||
|
||||
add_executable(MS
|
||||
main.cpp
|
||||
MS.cpp
|
||||
MS.cpp
|
||||
${DIR_THREAD_POOL}
|
||||
${DIR_MMM}
|
||||
${DIR_MEM_POOL}
|
||||
${DIR_OBJECT_POOL}
|
||||
${DIR_TOOLS}
|
||||
)
|
||||
|
||||
target_link_libraries(MS
|
||||
works
|
||||
event
|
||||
tbb
|
||||
imm_mysqldb
|
||||
MP
|
||||
)
|
||||
83
MS/MS.cpp
Normal file
83
MS/MS.cpp
Normal file
@@ -0,0 +1,83 @@
|
||||
//
|
||||
// Created by dongl on 23-4-8.
|
||||
//
|
||||
|
||||
#include "MS.h"
|
||||
#include <cstring>
|
||||
#include <netinet/in.h>
|
||||
#include "event2/listener.h"
|
||||
|
||||
|
||||
ev_pool* MS::pool = nullptr;
|
||||
class management* MS::mage = nullptr;
|
||||
MS::MS() {
|
||||
|
||||
pool = new ev_pool(4);
|
||||
pool->add_event_base(listener);
|
||||
pool->add_event_bases(4);
|
||||
pool->run();
|
||||
mage = new class management();
|
||||
}
|
||||
MS::~MS() {
|
||||
delete pool;
|
||||
delete mage;
|
||||
}
|
||||
|
||||
void MS::listener() {
|
||||
event_base *base = event_base_new();
|
||||
sockaddr_in sin = {0};
|
||||
memset(&sin, 0, sizeof(sin));
|
||||
sin = {
|
||||
AF_INET,
|
||||
htons(9999)
|
||||
};
|
||||
evconnlistener *listener =
|
||||
evconnlistener_new_bind(base,
|
||||
MS::link,
|
||||
base,
|
||||
LEV_OPT_CLOSE_ON_FREE | LEV_OPT_REUSEABLE | LEV_OPT_CLOSE_ON_EXEC |
|
||||
LEV_OPT_DEFERRED_ACCEPT,
|
||||
10,
|
||||
(sockaddr *) &sin,
|
||||
sizeof(sin)
|
||||
);
|
||||
event_base_dispatch(base);
|
||||
evconnlistener_free(listener);
|
||||
event_base_free(base);
|
||||
}
|
||||
|
||||
|
||||
void MS::link(struct evconnlistener *e, int fd, struct sockaddr *addr, int socklen, void *arg) {
|
||||
if (!pool) {
|
||||
perror("en: Prohibit manual static invocation MS::link()\n");
|
||||
return;
|
||||
}
|
||||
printf("listen_cb\n");
|
||||
pool->add_buffer_event(fd, read_cb, write_cb, event_cb, BEV_OPT_CLOSE_ON_FREE);
|
||||
fflush(stdout);
|
||||
}
|
||||
|
||||
void MS::read_cb(struct bufferevent *bev, void *ctx) {
|
||||
printf("[read]: %p, %p\n", ctx, bev);
|
||||
mage->read_packet(bev);
|
||||
fflush(stdout);
|
||||
}
|
||||
|
||||
void MS::write_cb(struct bufferevent *bev, void *ctx) {
|
||||
printf("[write]: %p\n", ctx);
|
||||
fflush(stdout);
|
||||
}
|
||||
|
||||
void MS::event_cb(struct bufferevent *bev, short what, void *ctx) {
|
||||
printf("[event]: %p\n", ctx);
|
||||
if (what == BEV_EVENT_EOF || BEV_EVENT_ERROR || BEV_EVENT_TIMEOUT) {
|
||||
printf("客户端退出\n");
|
||||
handler::remove_user(bev);
|
||||
bufferevent_free(bev);
|
||||
}
|
||||
|
||||
fflush(stdout);
|
||||
}
|
||||
|
||||
|
||||
|
||||
28
MS/MS.h
Normal file
28
MS/MS.h
Normal file
@@ -0,0 +1,28 @@
|
||||
//
|
||||
// Created by dongl on 23-4-8.
|
||||
//
|
||||
|
||||
#ifndef IM_SERVER_H
|
||||
#define IM_SERVER_H
|
||||
|
||||
#include "tbb/tbb.h"
|
||||
#include "event2/util.h"
|
||||
#include "pool/thread/ev_pool.h"
|
||||
#include "mmm/management.h"
|
||||
|
||||
class MS {
|
||||
public:
|
||||
MS();
|
||||
virtual ~MS();
|
||||
static void link(struct evconnlistener *e, evutil_socket_t s, struct sockaddr *addr, int socklen, void *arg);
|
||||
static void listener();
|
||||
static void read_cb(struct bufferevent *bev, void *ctx);
|
||||
static void write_cb(struct bufferevent *bev, void *ctx);
|
||||
static void event_cb(struct bufferevent *bev, short what, void *ctx);
|
||||
private:
|
||||
static ev_pool* pool;
|
||||
static class management* mage;
|
||||
};
|
||||
|
||||
|
||||
#endif //IM_SERVER_H
|
||||
10
MS/config/config.ini
Normal file
10
MS/config/config.ini
Normal file
@@ -0,0 +1,10 @@
|
||||
[server-mapping]
|
||||
0 = "controller/UserController"
|
||||
|
||||
|
||||
|
||||
|
||||
[mysql]
|
||||
|
||||
[redis]
|
||||
|
||||
10
MS/main.cpp
Normal file
10
MS/main.cpp
Normal file
@@ -0,0 +1,10 @@
|
||||
//
|
||||
// Created by dongl on 23-4-17.
|
||||
//
|
||||
|
||||
#include "MS.h"
|
||||
|
||||
|
||||
int main () {
|
||||
MS();
|
||||
}
|
||||
41
MS/mmm/agreement.cpp
Normal file
41
MS/mmm/agreement.cpp
Normal file
@@ -0,0 +1,41 @@
|
||||
//
|
||||
// Created by dongl on 23-4-20.
|
||||
//
|
||||
|
||||
#include "agreement.h"
|
||||
#include "Response.h"
|
||||
|
||||
|
||||
agreement_request::agreement_request() : agreement() {}
|
||||
agreement_request::~agreement_request() {}
|
||||
|
||||
void agreement_request::set (std::shared_ptr<mp::mph> &mph, std::shared_ptr<mp::request>& request, bufferevent* bev) {
|
||||
m_mph = mph;
|
||||
m_body = request->body();
|
||||
m_cqi = request->cqi();
|
||||
m_bev = bev;
|
||||
}
|
||||
|
||||
agreement_response::agreement_response() : agreement() {}
|
||||
agreement_response::~agreement_response() {}
|
||||
|
||||
|
||||
void agreement_response::set(mp::MP_TYPE type, mp::sri* sri, bufferevent* bev) {
|
||||
auto resp = std::make_shared<mp::response>(mp::response());
|
||||
|
||||
m_sri = resp->sri();
|
||||
m_sri.set_sri_username(sri->sri_username());
|
||||
m_sri.set_sri_msg(sri->sri_msg());
|
||||
m_sri.set_sri_code(sri->sri_code());
|
||||
m_sri.set_sri_token(sri->sri_token());
|
||||
|
||||
m_bev = bev;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
54
MS/mmm/agreement.h
Normal file
54
MS/mmm/agreement.h
Normal file
@@ -0,0 +1,54 @@
|
||||
//
|
||||
// Created by dongl on 23-4-20.
|
||||
//
|
||||
|
||||
#ifndef IM2_AGREEMENT_H
|
||||
#define IM2_AGREEMENT_H
|
||||
|
||||
|
||||
#include <cstdint>
|
||||
#include "Body.h"
|
||||
#include "proto/mp.mph.pb.h"
|
||||
#include "proto/mp.cqi.pb.h"
|
||||
#include "proto/mp.sri.pb.h"
|
||||
#include "proto/mp.response.pb.h"
|
||||
#include "proto/mp.request.pb.h"
|
||||
#include "event2/bufferevent.h"
|
||||
|
||||
class agreement {
|
||||
public:
|
||||
agreement(){}
|
||||
virtual ~agreement() {}
|
||||
};
|
||||
|
||||
|
||||
class agreement_request : public agreement{
|
||||
public:
|
||||
agreement_request();
|
||||
~agreement_request();
|
||||
|
||||
public:
|
||||
void set (std::shared_ptr<mp::mph> &mph, std::shared_ptr<mp::request>& request, bufferevent* bev);
|
||||
|
||||
public:
|
||||
std::shared_ptr<mp::mph> m_mph;
|
||||
mp::body m_body;
|
||||
mp::cqi m_cqi;
|
||||
bufferevent* m_bev;
|
||||
};
|
||||
|
||||
|
||||
class agreement_response : public agreement {
|
||||
public:
|
||||
agreement_response();
|
||||
~agreement_response() override;
|
||||
|
||||
public:
|
||||
void set (mp::MP_TYPE type, mp::sri* sri, bufferevent* bev);
|
||||
public:
|
||||
std::shared_ptr<mp::mph> m_mph;
|
||||
mp::sri m_sri;
|
||||
bufferevent* m_bev;
|
||||
};
|
||||
|
||||
#endif //IM2_AGREEMENT_H
|
||||
40
MS/mmm/analysis.h
Normal file
40
MS/mmm/analysis.h
Normal file
@@ -0,0 +1,40 @@
|
||||
//
|
||||
// Created by dongl on 23-4-21.
|
||||
//
|
||||
|
||||
#ifndef IM2_ANALYSIS_H
|
||||
#define IM2_ANALYSIS_H
|
||||
|
||||
#include <any>
|
||||
#include <variant>
|
||||
#include "proto/mp.request.pb.h"
|
||||
#include "agreement.h"
|
||||
|
||||
|
||||
class analysis {
|
||||
public:
|
||||
analysis(std::shared_ptr<mp::mph>& mph, char* data, bufferevent* bev) : m_mph(mph), m_data(data), m_bev(bev) {
|
||||
|
||||
}
|
||||
|
||||
std::shared_ptr<agreement_request> operator () () {
|
||||
// agreement_request
|
||||
auto type = m_mph->mp_type();
|
||||
auto agreementRequest = std::make_shared<agreement_request>(agreement_request());
|
||||
|
||||
// request
|
||||
auto request = std::make_shared<mp::request>(mp::request());
|
||||
request->ParseFromString(m_data);
|
||||
agreementRequest->set(m_mph, request, m_bev);
|
||||
|
||||
return agreementRequest;
|
||||
}
|
||||
private:
|
||||
std::shared_ptr<mp::mph> m_mph;
|
||||
char* m_data = nullptr;
|
||||
bufferevent* m_bev = nullptr;
|
||||
};
|
||||
|
||||
|
||||
|
||||
#endif //IM2_ANALYSIS_H
|
||||
54
MS/mmm/handler.cpp
Normal file
54
MS/mmm/handler.cpp
Normal file
@@ -0,0 +1,54 @@
|
||||
//
|
||||
// Created by dongl on 23-4-28.
|
||||
//
|
||||
|
||||
#include "handler.h"
|
||||
#include "Response.h"
|
||||
|
||||
std::map<uint64_t, userinfo*> handler::user_fd;
|
||||
|
||||
|
||||
void handler::resp(const std::shared_ptr<agreement_request>& request,
|
||||
const std::shared_ptr<agreement_response>& response) {
|
||||
// 用户操作逻辑包
|
||||
auto resp = new Response((mp::MP_TYPE) (request->m_mph->mp_type() + 20),
|
||||
response->m_sri.sri_code(), response->m_sri.sri_username(),
|
||||
response->m_sri.sri_msg(), response->m_sri.sri_token());
|
||||
auto ret = resp->packet();
|
||||
|
||||
bufferevent_write(request->m_bev, ret.c_str(), ret.size());
|
||||
delete resp;
|
||||
}
|
||||
|
||||
void
|
||||
handler::send(const std::shared_ptr<agreement_request> &request, const std::shared_ptr<agreement_response> &response) {
|
||||
// 聊天消息包
|
||||
userinfo *user = user_fd.find(request->m_body.target())->second;
|
||||
|
||||
}
|
||||
|
||||
|
||||
void handler::add_user(const std::shared_ptr<agreement_request>& request) {
|
||||
auto ele = new userinfo();
|
||||
ele->bev = request->m_bev;
|
||||
ele->ip = 0;
|
||||
user_fd.insert({request->m_body.account(), ele});
|
||||
}
|
||||
|
||||
void handler::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);
|
||||
}
|
||||
|
||||
void handler::remove_user(const std::shared_ptr<agreement_request>& request) {
|
||||
user_fd.erase(request->m_body.account());
|
||||
}
|
||||
|
||||
|
||||
35
MS/mmm/handler.h
Normal file
35
MS/mmm/handler.h
Normal file
@@ -0,0 +1,35 @@
|
||||
//
|
||||
// Created by dongl on 23-4-20.
|
||||
//
|
||||
|
||||
#ifndef IM2_HANDLER_H
|
||||
#define IM2_HANDLER_H
|
||||
|
||||
#include "agreement.h"
|
||||
|
||||
struct userinfo {
|
||||
bufferevent* bev;
|
||||
uint64_t ip;
|
||||
};
|
||||
|
||||
|
||||
class handler {
|
||||
public:
|
||||
virtual void run(std::shared_ptr<agreement_request> request, std::shared_ptr<agreement_response> response) = 0;
|
||||
|
||||
public:
|
||||
// 用户不在线时应 删除 user fd 映射
|
||||
static void add_user(const std::shared_ptr<agreement_request>& request);
|
||||
static void remove_user(const std::shared_ptr<agreement_request>& request);
|
||||
static void remove_user(bufferevent* bev);
|
||||
static void find_fd(uint64_t account);
|
||||
static void resp(const std::shared_ptr<agreement_request>& request, const std::shared_ptr<agreement_response>& response);
|
||||
static void send(const std::shared_ptr<agreement_request>& request, const std::shared_ptr<agreement_response>& response);
|
||||
|
||||
protected:
|
||||
static std::map<uint64_t, userinfo*> user_fd;
|
||||
};
|
||||
|
||||
|
||||
|
||||
#endif //IM2_HANDLER_H
|
||||
48
MS/mmm/management.cpp
Normal file
48
MS/mmm/management.cpp
Normal file
@@ -0,0 +1,48 @@
|
||||
//
|
||||
// Created by dongl on 23-4-21.
|
||||
//
|
||||
|
||||
#include "management.h"
|
||||
#include "Response.h"
|
||||
|
||||
|
||||
|
||||
management::management() {
|
||||
|
||||
}
|
||||
|
||||
management::~management() {
|
||||
|
||||
}
|
||||
|
||||
|
||||
void management::send_packet(bufferevent *bev) {
|
||||
|
||||
}
|
||||
|
||||
void management::read_packet(bufferevent *bev) {
|
||||
// read L 读包长度
|
||||
uint8_t packetLen;
|
||||
bufferevent_read(bev, &packetLen, 1);
|
||||
// read V 读包头
|
||||
char data_h[256] = {0};
|
||||
bufferevent_read(bev, data_h, packetLen);
|
||||
|
||||
auto mph = std::make_shared<mp::mph>(mp::mph());
|
||||
mph->ParseFromString(data_h);
|
||||
|
||||
// read V 读包体 包头内含有包体长度
|
||||
char data_b[256] = {0};
|
||||
bufferevent_read(bev, data_b, mph->mpb_size());
|
||||
|
||||
// 请求
|
||||
auto request = analysis(mph, data_b, bev)();
|
||||
// 响应
|
||||
auto response= std::make_shared<agreement_response>(agreement_response());
|
||||
// 执行逻辑 自定义
|
||||
mapping::run(mph->mp_type(), request, response);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
25
MS/mmm/management.h
Normal file
25
MS/mmm/management.h
Normal file
@@ -0,0 +1,25 @@
|
||||
//
|
||||
// Created by dongl on 23-4-21.
|
||||
//
|
||||
|
||||
#ifndef IM2_MANAGEMENT_H
|
||||
#define IM2_MANAGEMENT_H
|
||||
|
||||
|
||||
#include "event2/bufferevent.h"
|
||||
#include "mapping.h"
|
||||
#include "analysis.h"
|
||||
#include "Response.h"
|
||||
|
||||
|
||||
class management {
|
||||
public:
|
||||
management();
|
||||
virtual ~management();
|
||||
public:
|
||||
void read_packet(bufferevent* bev);
|
||||
void send_packet(bufferevent* bev);
|
||||
};
|
||||
|
||||
|
||||
#endif //IM2_MANAGEMENT_H
|
||||
38
MS/mmm/mapping.cpp
Normal file
38
MS/mmm/mapping.cpp
Normal file
@@ -0,0 +1,38 @@
|
||||
//
|
||||
// Created by dongl on 23-4-23.
|
||||
//
|
||||
#include "mapping.h"
|
||||
#include "SimpleIni.h"
|
||||
#include "EnumMapping.h"
|
||||
#include <fstream>
|
||||
|
||||
std::map<mp::MP_TYPE, handler*> mapping::map;
|
||||
|
||||
mapping::mapping() {
|
||||
if (map.empty()) {
|
||||
// CSimpleIniA ini;
|
||||
// ini.SetUnicode();
|
||||
// ini.LoadFile("/home/dongl/code/网络编程/IMS/config/db.ini");
|
||||
// auto key_value = ini.GetSection("server-mapping");
|
||||
|
||||
auto userController = new UserController();
|
||||
map.insert( std::pair<mp::MP_TYPE, handler*>(mp::MP_REQUEST_LOGIN, userController));
|
||||
map.insert( std::pair<mp::MP_TYPE, handler*>(mp::MP_REQUEST_REGISTER, userController));
|
||||
map.insert( std::pair<mp::MP_TYPE, handler*>(mp::MP_REQUEST_LOGOUT, userController));
|
||||
}
|
||||
}
|
||||
|
||||
void mapping::run(const mp::MP_TYPE mpTYpe, std::shared_ptr<agreement_request>& request, std::shared_ptr<agreement_response>& response) {
|
||||
mapping();
|
||||
printf("%s\n", myenumToString(mpTYpe));
|
||||
// 取出需要的执行对象
|
||||
auto fun = map.find(mpTYpe)->second;
|
||||
// 开始执行 请求
|
||||
fun->run(request, response);
|
||||
// 发送 响应
|
||||
handler::resp(request, response);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
24
MS/mmm/mapping.h
Normal file
24
MS/mmm/mapping.h
Normal file
@@ -0,0 +1,24 @@
|
||||
//
|
||||
// Created by dongl on 23-4-23.
|
||||
//
|
||||
|
||||
#ifndef IM2_MAPPING_H1
|
||||
#define IM2_MAPPING_H1
|
||||
|
||||
#include "agreement.h"
|
||||
#include "controller/works.h"
|
||||
|
||||
|
||||
class mapping {
|
||||
private:
|
||||
public:
|
||||
mapping();
|
||||
public:
|
||||
static void run(mp::MP_TYPE mpTYpe, std::shared_ptr<agreement_request>& request, std::shared_ptr<agreement_response>& response);
|
||||
private:
|
||||
static std::map<mp::MP_TYPE, handler*> map;
|
||||
};
|
||||
|
||||
|
||||
|
||||
#endif //IM2_MAPPING_H1
|
||||
5
MS/pool/mem/mem_pool.cpp
Normal file
5
MS/pool/mem/mem_pool.cpp
Normal file
@@ -0,0 +1,5 @@
|
||||
//
|
||||
// Created by dongl on 23-4-18.
|
||||
//
|
||||
|
||||
#include "mem_pool.h"
|
||||
33
MS/pool/mem/mem_pool.h
Normal file
33
MS/pool/mem/mem_pool.h
Normal file
@@ -0,0 +1,33 @@
|
||||
//
|
||||
// Created by dongl on 23-4-18.
|
||||
//
|
||||
|
||||
#ifndef IM2_MEM_POOL_H
|
||||
#define IM2_MEM_POOL_H
|
||||
|
||||
|
||||
#include <vector>
|
||||
#include <cstdint>
|
||||
|
||||
// mem 块
|
||||
struct mem_black {
|
||||
void* black_data;
|
||||
uint16_t black_id;
|
||||
uint16_t black_data_size;
|
||||
|
||||
};
|
||||
|
||||
// mem 桶
|
||||
struct mem_barrel {
|
||||
|
||||
};
|
||||
|
||||
// mem pool mgr
|
||||
class mem_pool {
|
||||
|
||||
private:
|
||||
;
|
||||
};
|
||||
|
||||
|
||||
#endif //IM2_MEM_POOL_H
|
||||
5
MS/pool/object/object_pool.cpp
Normal file
5
MS/pool/object/object_pool.cpp
Normal file
@@ -0,0 +1,5 @@
|
||||
//
|
||||
// Created by dongl on 23-4-18.
|
||||
//
|
||||
|
||||
#include "object_pool.h"
|
||||
15
MS/pool/object/object_pool.h
Normal file
15
MS/pool/object/object_pool.h
Normal file
@@ -0,0 +1,15 @@
|
||||
//
|
||||
// Created by dongl on 23-4-18.
|
||||
//
|
||||
|
||||
#ifndef IM2_OBJECT_POOL_H
|
||||
#define IM2_OBJECT_POOL_H
|
||||
|
||||
|
||||
class object_pool {
|
||||
|
||||
|
||||
};
|
||||
|
||||
|
||||
#endif //IM2_OBJECT_POOL_H
|
||||
29
MS/pool/thread/ev_base.h
Normal file
29
MS/pool/thread/ev_base.h
Normal file
@@ -0,0 +1,29 @@
|
||||
//
|
||||
// Created by dongl on 23-4-14.
|
||||
//
|
||||
|
||||
#ifndef IM_EV_BASE_H
|
||||
#define IM_EV_BASE_H
|
||||
|
||||
#include "event2/event.h"
|
||||
#include "event2/bufferevent.h"
|
||||
|
||||
class ev_base {
|
||||
public:
|
||||
explicit ev_base(event_base *base) : base(base) {}
|
||||
|
||||
void operator() (){
|
||||
event_base_dispatch(base);
|
||||
event_base_free(base);
|
||||
}
|
||||
private:
|
||||
event_base* base;
|
||||
int ev_base_id;
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
#endif //IM_EV_BASE_H
|
||||
125
MS/pool/thread/ev_pool.cpp
Normal file
125
MS/pool/thread/ev_pool.cpp
Normal file
@@ -0,0 +1,125 @@
|
||||
//
|
||||
// Created by dongl on 23-4-14.
|
||||
//
|
||||
|
||||
#include "ev_pool.h"
|
||||
|
||||
std::function<void()> ev_base(event_base* base) {
|
||||
return [=](){
|
||||
printf("ev_base ptr: %p, pthread: %ld\n",base, pthread_self());
|
||||
event_base_loop(base, EVLOOP_NO_EXIT_ON_EMPTY);
|
||||
};
|
||||
}
|
||||
|
||||
ev_pool::ev_pool(int size) : m_pool_max_size(size), m_pool_curr_size(0), m_poll(false) {
|
||||
|
||||
}
|
||||
|
||||
void ev_pool::add_event_base() {
|
||||
if (m_pool_curr_size >= m_pool_max_size && m_poll) {
|
||||
perror("add_event_base failed");
|
||||
return;
|
||||
}
|
||||
event_base* base = event_base_new();
|
||||
m_ev_bases.push_back(base);
|
||||
m_pool.run(ev_base(base));
|
||||
m_pool_curr_size += 1;
|
||||
}
|
||||
|
||||
void ev_pool::add_event_base(const std::function<void()>& function) {
|
||||
m_pool.run(function);
|
||||
}
|
||||
|
||||
void ev_pool::add_event_bases(int num) {
|
||||
if (num > m_pool_max_size && m_poll) {
|
||||
perror("add_event_bases failed");
|
||||
fflush(stdout);
|
||||
return;
|
||||
}
|
||||
for (int i = 0; i < num; ++i) {
|
||||
add_event_base();
|
||||
}
|
||||
}
|
||||
|
||||
bool ev_pool::polling(bool poll) {
|
||||
m_poll = poll;
|
||||
return m_poll;
|
||||
}
|
||||
|
||||
void read(evutil_socket_t, short, void *) {
|
||||
printf("read\n");
|
||||
fflush(stdout);
|
||||
}
|
||||
|
||||
void ev_pool::add_buffer_event(evutil_socket_t fd, bufferevent_data_cb readcb, bufferevent_data_cb writecb,
|
||||
bufferevent_event_cb eventcb, short events) {
|
||||
|
||||
event_base* base = dispatching();
|
||||
bufferevent* bev = bufferevent_socket_new(base, fd, events);
|
||||
bufferevent_setcb(bev, readcb, writecb, eventcb, base);
|
||||
bufferevent_enable(bev, EV_READ | EV_WRITE);
|
||||
m_bevs.insert(std::pair<event_base*, bufferevent*>(base, bev));
|
||||
printf("event_base: %p, fd: %d\n", base, fd);
|
||||
}
|
||||
|
||||
void ev_pool::add_event(evutil_socket_t fd, short events, event_callback_fn callback, void *callback_arg) {
|
||||
|
||||
event* ev = event_new(dispatching(), fd, events, callback, callback_arg);
|
||||
event_add(ev, nullptr);
|
||||
}
|
||||
|
||||
event_base *ev_pool::dispatching() {
|
||||
static auto base = m_ev_bases.begin();
|
||||
|
||||
if (base != m_ev_bases.end())
|
||||
base;
|
||||
else
|
||||
base = m_ev_bases.begin();
|
||||
return *base++;
|
||||
}
|
||||
|
||||
void ev_pool::ev_loop_exit(event_base* base) {
|
||||
free();
|
||||
}
|
||||
|
||||
void ev_pool::ev_base_exit(event_base* base) {
|
||||
auto b = std::remove(m_ev_bases.begin(), m_ev_bases.end(), base);
|
||||
bufferevent_free(m_bevs.find(*b)->second);
|
||||
event_base_loopexit(base, nullptr);
|
||||
event_base_free(base);
|
||||
|
||||
m_bevs.erase(m_bevs.find(*b));
|
||||
m_ev_bases.erase(b);
|
||||
}
|
||||
|
||||
void ev_pool::free() {
|
||||
for (const auto &item: m_bevs) {
|
||||
bufferevent_free(item.second);
|
||||
}
|
||||
for (const auto &item: m_ev_bases) {
|
||||
event_base_free(item);
|
||||
}
|
||||
m_bevs.clear();
|
||||
m_ev_bases.clear();
|
||||
}
|
||||
|
||||
void ev_pool::run() {
|
||||
m_pool.wait();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
38
MS/pool/thread/ev_pool.h
Normal file
38
MS/pool/thread/ev_pool.h
Normal file
@@ -0,0 +1,38 @@
|
||||
//
|
||||
// Created by dongl on 23-4-14.
|
||||
//
|
||||
|
||||
#ifndef IM_EV_POOL_H
|
||||
#define IM_EV_POOL_H
|
||||
|
||||
#include <tbb/tbb.h>
|
||||
#include <map>
|
||||
#include "event2/event.h"
|
||||
#include "event2/bufferevent.h"
|
||||
|
||||
class ev_pool {
|
||||
public:
|
||||
explicit ev_pool(int size = 4);
|
||||
void add_event_base();
|
||||
void add_event_base(const std::function<void()>& function);
|
||||
void add_event_bases(int num);
|
||||
bool polling(bool poll = false);
|
||||
void add_buffer_event(evutil_socket_t fd, bufferevent_data_cb readcb, bufferevent_data_cb writecb,
|
||||
bufferevent_event_cb eventcb, short events);
|
||||
void add_event(evutil_socket_t fd, short events, event_callback_fn callback, void *callback_arg);
|
||||
event_base* dispatching();
|
||||
void ev_loop_exit(event_base* base);
|
||||
void ev_base_exit(event_base* base);
|
||||
void run();
|
||||
void free();
|
||||
private:
|
||||
tbb::task_group m_pool;
|
||||
std::vector<event_base*> m_ev_bases;
|
||||
std::map<event_base*, bufferevent*> m_bevs;
|
||||
int m_pool_max_size;
|
||||
int m_pool_curr_size;
|
||||
bool m_poll;
|
||||
};
|
||||
|
||||
|
||||
#endif //IM_EV_POOL_H
|
||||
5
MS/tools/hash.cpp
Normal file
5
MS/tools/hash.cpp
Normal file
@@ -0,0 +1,5 @@
|
||||
//
|
||||
// Created by dongl on 23-4-26.
|
||||
//
|
||||
|
||||
#include "hash.h"
|
||||
14
MS/tools/hash.h
Normal file
14
MS/tools/hash.h
Normal file
@@ -0,0 +1,14 @@
|
||||
//
|
||||
// Created by dongl on 23-4-26.
|
||||
//
|
||||
|
||||
#ifndef IM2_HASH_H
|
||||
#define IM2_HASH_H
|
||||
|
||||
|
||||
class hash {
|
||||
|
||||
};
|
||||
|
||||
|
||||
#endif //IM2_HASH_H
|
||||
14
MS/works/CMakeLists.txt
Normal file
14
MS/works/CMakeLists.txt
Normal file
@@ -0,0 +1,14 @@
|
||||
project(works)
|
||||
|
||||
aux_source_directory(controller DIR_WORKS_CONTROLLER)
|
||||
aux_source_directory(service DIR_WORKS_SERVICE)
|
||||
aux_source_directory(db DIR_WORKS_DB)
|
||||
|
||||
include_directories(${CMAKE_SOURCE_DIR}/MS/mmm)
|
||||
include_directories(${CMAKE_SOURCE_DIR}/MDB/imm_mysqldb)
|
||||
|
||||
add_library(works
|
||||
${DIR_WORKS_CONTROLLER}
|
||||
${DIR_WORKS_SERVICE}
|
||||
${DIR_WORKS_DB}
|
||||
)
|
||||
24
MS/works/controller/UserController.cpp
Normal file
24
MS/works/controller/UserController.cpp
Normal file
@@ -0,0 +1,24 @@
|
||||
//
|
||||
// Created by dongl on 23-4-20.
|
||||
//
|
||||
|
||||
#include "UserController.h"
|
||||
|
||||
void UserController::run(std::shared_ptr<agreement_request> agreement_request, std::shared_ptr<agreement_response> agreement_response) {
|
||||
if (agreement_request->m_mph->mp_type() == mp::MP_REQUEST_LOGIN) {
|
||||
auto sri = service.login(agreement_request->m_body.account(), agreement_request->m_body.password());
|
||||
agreement_response->set(mp::MP_RESPONSE_LOGIN, sri, agreement_request->m_bev);
|
||||
handler::add_user(agreement_request);
|
||||
|
||||
} else if (agreement_request->m_mph->mp_type() == mp::MP_REQUEST_REGISTER) {
|
||||
auto sri = service.register_(agreement_request->m_body.account(), agreement_request->m_body.password());
|
||||
agreement_response->set(mp::MP_RESPONSE_REGISTER, sri, agreement_request->m_bev);
|
||||
|
||||
} else if (agreement_request->m_mph->mp_type() == mp::MP_REQUEST_LOGOUT) {
|
||||
auto sri = service.logout(agreement_request->m_body.account());
|
||||
agreement_response->set(mp::MP_RESPONSE_LOGOUT, sri, agreement_request->m_bev);
|
||||
handler::remove_user(agreement_request);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
18
MS/works/controller/UserController.h
Normal file
18
MS/works/controller/UserController.h
Normal file
@@ -0,0 +1,18 @@
|
||||
//
|
||||
// Created by dongl on 23-4-25.
|
||||
//
|
||||
|
||||
#ifndef IM2_USERCONTROLLER_H
|
||||
#define IM2_USERCONTROLLER_H
|
||||
|
||||
#include "../../mmm/handler.h"
|
||||
#include "service/UserService.h"
|
||||
|
||||
class UserController : public handler {
|
||||
public:
|
||||
void run(std::shared_ptr<agreement_request> agreement_request, std::shared_ptr<agreement_response> response) override;
|
||||
private:
|
||||
UserService service = UserService();
|
||||
};
|
||||
|
||||
#endif //IM2_USERCONTROLLER_H
|
||||
10
MS/works/controller/works.h
Normal file
10
MS/works/controller/works.h
Normal file
@@ -0,0 +1,10 @@
|
||||
//
|
||||
// Created by dongl on 23-4-26.
|
||||
//
|
||||
|
||||
#ifndef IM2_WORKS_H
|
||||
#define IM2_WORKS_H
|
||||
|
||||
#include "UserController.h"
|
||||
|
||||
#endif //IM2_WORKS_H
|
||||
104
MS/works/db/UserDB.cpp
Normal file
104
MS/works/db/UserDB.cpp
Normal file
@@ -0,0 +1,104 @@
|
||||
//
|
||||
// Created by dongl on 23-4-23.
|
||||
//
|
||||
|
||||
#include "UserDB.h"
|
||||
#include "template_table/im_user.h"
|
||||
#include "proto/mp.sri.pb.h"
|
||||
|
||||
UserDB::~UserDB() {
|
||||
delete sri;
|
||||
}
|
||||
|
||||
|
||||
mp::sri* UserDB::login(uint64_t account, const std::string& password) {
|
||||
auto query = conn->query("select * from im_user where account=%0:account;");
|
||||
query.parse();
|
||||
|
||||
std::vector<im_user> v;
|
||||
query.storein(v, account);
|
||||
printf("info: %s\n", query.info().c_str());
|
||||
|
||||
sri = new mp::sri();
|
||||
// 无账户
|
||||
if (v.empty()) {
|
||||
printf("无此用户\n");
|
||||
sri->set_sri_msg("account null!");
|
||||
sri->set_sri_code(mp::MP_LOGIN_ACCOUNT_NOT);
|
||||
return sri;
|
||||
}
|
||||
|
||||
// 判断密码
|
||||
im_user user = v[0];
|
||||
std::string source = std::to_string(account) + password + user.password_salt;
|
||||
size_t password_hash = std::hash<std::string>()(source);
|
||||
// hash_password(&password_hash, source.c_str(), source.size());
|
||||
|
||||
if (user.password == std::to_string(password_hash)) {
|
||||
printf("登陆成功\n");
|
||||
sri->set_sri_msg("登陆成功!");
|
||||
sri->set_sri_code(mp::MP_LOGIN_SUCCESS);
|
||||
sri->set_sri_token("token");
|
||||
sri->set_sri_username(user.username);
|
||||
} else {
|
||||
printf("登陆失败\n");
|
||||
sri->set_sri_msg("登陆失败!");
|
||||
sri->set_sri_code(mp::MP_LOGIN_FAIL);
|
||||
sri->set_sri_username("null");
|
||||
sri->set_sri_token("null");
|
||||
}
|
||||
|
||||
return sri;
|
||||
}
|
||||
|
||||
mp::sri *UserDB::register_(uint64_t account, const std::string &password, const std::string &password_salt,
|
||||
const std::string& client_info) {
|
||||
mysqlpp::String info(client_info);
|
||||
if (!sri) {
|
||||
sri = new mp::sri();
|
||||
}
|
||||
|
||||
// 检验重复账户
|
||||
auto query = conn->query("select account from im_user where account=%0:account;");
|
||||
query.parse();
|
||||
auto is_exist = query.store(account);
|
||||
printf("error: %s, row: %zu\n", conn->error(), is_exist.num_rows());
|
||||
|
||||
if (!is_exist.empty()) {
|
||||
sri->set_sri_msg("帐号已经注册!");
|
||||
sri->set_sri_code(mp::MP_REGISTER_EXIST);
|
||||
sri->set_sri_username("null");
|
||||
sri->set_sri_token("null");
|
||||
|
||||
printf("帐号已经注册!\n");
|
||||
return sri;
|
||||
}
|
||||
|
||||
// 添加账户
|
||||
query.insert(im_user(account, "用户"+ std::to_string(account), password, password_salt, info));
|
||||
auto ret = query.exec();
|
||||
if (ret) {
|
||||
sri->set_sri_msg("注册成功!");
|
||||
sri->set_sri_code(mp::MP_REGISTER_SUCCESS);
|
||||
sri->set_sri_username("null");
|
||||
sri->set_sri_token("null");
|
||||
printf("注册成功!\n");
|
||||
} else {
|
||||
sri->set_sri_msg("注册失败!");
|
||||
// sri->set_sri_code(mp::MP_REGISTER_EXIST);
|
||||
sri->set_sri_username("null");
|
||||
sri->set_sri_token("null");
|
||||
printf("注册失败!\n");
|
||||
}
|
||||
|
||||
return sri;
|
||||
}
|
||||
|
||||
mp::sri *UserDB::logout(uint64_t account) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
26
MS/works/db/UserDB.h
Normal file
26
MS/works/db/UserDB.h
Normal file
@@ -0,0 +1,26 @@
|
||||
//
|
||||
// Created by dongl on 23-4-23.
|
||||
//
|
||||
|
||||
#ifndef IM2_USERDB_H
|
||||
#define IM2_USERDB_H
|
||||
|
||||
#include "linkDB.h"
|
||||
#include "Sri.h"
|
||||
|
||||
class UserDB {
|
||||
public:
|
||||
virtual ~UserDB();
|
||||
|
||||
public:
|
||||
virtual mp::sri* login(uint64_t account, const std::string& password);
|
||||
virtual mp::sri* register_(uint64_t account, const std::string& password,
|
||||
const std::string& password_salt, const std::string& client_info);
|
||||
virtual mp::sri* logout(uint64_t account);
|
||||
private:
|
||||
mysqlpp::Connection* conn = DB::link();
|
||||
mp::sri* sri{};
|
||||
};
|
||||
|
||||
|
||||
#endif //IM2_USERDB_H
|
||||
20
MS/works/db/linkDB.cpp
Normal file
20
MS/works/db/linkDB.cpp
Normal file
@@ -0,0 +1,20 @@
|
||||
//
|
||||
// Created by dongl on 23-4-25.
|
||||
//
|
||||
#include "linkDB.h"
|
||||
|
||||
DB::DB() {}
|
||||
DB::~DB() {
|
||||
delete pool;
|
||||
}
|
||||
|
||||
ConnectionPool* DB::pool = nullptr;
|
||||
|
||||
mysqlpp::Connection* DB::link() {
|
||||
if (pool == nullptr) {
|
||||
pool = new ConnectionPool("124.221.152.192", "ims", "IMS", "WimTFC8N58kznx2k");
|
||||
}
|
||||
|
||||
return pool->safe_grab();
|
||||
}
|
||||
|
||||
22
MS/works/db/linkDB.h
Normal file
22
MS/works/db/linkDB.h
Normal file
@@ -0,0 +1,22 @@
|
||||
//
|
||||
// Created by dongl on 23-4-23.
|
||||
//
|
||||
|
||||
#ifndef IM2_LINKDB_H
|
||||
#define IM2_LINKDB_H
|
||||
|
||||
#include "ConnectionPool.h"
|
||||
|
||||
class DB {
|
||||
public:
|
||||
static mysqlpp::Connection* link();
|
||||
private:
|
||||
DB();
|
||||
|
||||
virtual ~DB();
|
||||
private:
|
||||
static ConnectionPool* pool;
|
||||
};
|
||||
|
||||
|
||||
#endif //IM2_LINKDB_H
|
||||
31
MS/works/service/UserService.cpp
Normal file
31
MS/works/service/UserService.cpp
Normal file
@@ -0,0 +1,31 @@
|
||||
//
|
||||
// Created by dongl on 23-4-23.
|
||||
//
|
||||
|
||||
#include "UserService.h"
|
||||
#include <experimental/random>
|
||||
|
||||
mp::sri* UserService::login(uint64_t account, const std::string& password) {
|
||||
return userDb.login(account, password);
|
||||
}
|
||||
|
||||
mp::sri* UserService::register_(uint64_t account, const std::string &password) {
|
||||
// 生成salt
|
||||
std::string password_salt;
|
||||
for (int i = 0; i < std::experimental::randint(5, 8); ++i) {
|
||||
password_salt.push_back((char )std::experimental::randint(33, 126));
|
||||
}
|
||||
|
||||
// 加密密码 生成hash
|
||||
std::string source = std::to_string(account) + password + password_salt;
|
||||
size_t password_hash = std::hash<std::string>()(source);
|
||||
// hash_password(&password_hash, source.c_str(), source.size());
|
||||
|
||||
printf("password_hash: %zu, password_salt: %s\n", password_hash, password_salt.c_str());
|
||||
|
||||
return userDb.register_(account, std::to_string(password_hash), password_salt, " ");
|
||||
}
|
||||
|
||||
mp::sri* UserService::logout(uint64_t account) {
|
||||
return nullptr;
|
||||
}
|
||||
19
MS/works/service/UserService.h
Normal file
19
MS/works/service/UserService.h
Normal file
@@ -0,0 +1,19 @@
|
||||
//
|
||||
// Created by dongl on 23-4-23.
|
||||
//
|
||||
|
||||
#ifndef IM2_USERSERVICE_H
|
||||
#define IM2_USERSERVICE_H
|
||||
#include "db/UserDB.h"
|
||||
|
||||
class UserService {
|
||||
|
||||
public:
|
||||
virtual mp::sri* login(uint64_t account, const std::string& password);
|
||||
virtual mp::sri* register_(uint64_t account, const std::string& password);
|
||||
virtual mp::sri* logout(uint64_t account);
|
||||
private:
|
||||
UserDB userDb = UserDB();
|
||||
};
|
||||
|
||||
#endif //IM2_USERSERVICE_H
|
||||
Reference in New Issue
Block a user