加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
server.hpp 8.77 KB
一键复制 编辑 原始数据 按行查看 历史
王祥 提交于 2023-04-06 20:57 . 网页聊天室
#include"mongoose.h"
#include<iostream>
#include<string>
#include"util.hpp"
namespace im{
class Server{
private:
SessionManager *ss_manager;
Usertable *user_table;
Message *message_table;
struct mg_mgr _mgr;
std::unordered_map<struct mg_connection*,uint64_t> conn_ss;
std::string _root = "./www.root";
private:
static void user_add(struct mg_connection *c,struct mg_http_message *hm,Server *ser)
{
std::string body(hm->body.ptr,hm->body.len);
Json::Value user_json;
bool ret = JsonUtil::unserialize(body, &user_json);
if(ret == false)
{
Json::Value err;
err["result"] = "false";
err["reason"] = "解析出错";
std::string body1;
JsonUtil::serialize(err,&body1);
mg_http_reply(c,400,NULL,"%s",body1.c_str());
return ;
}
Usertable *table = ser->get_usertable();
ret = table->insert(user_json);
if(ret == false)
{
Json::Value err;
err["result"] = "false";
err["reason"] = "插入失败";
std::string body1;
JsonUtil::serialize(err,&body1);
mg_http_reply(c,500,NULL,"%s",body1.c_str());
return ;
}
Json::Value res;
res["result"] = "true";
res["reason"] = "插入成功";
std::string body1;
JsonUtil::serialize(res,&body1);
mg_http_reply(c,200,NULL,"%s",body1.c_str());
}
static void user_login(struct mg_connection *c,struct mg_http_message *hm,Server *ser)
{
std::string body(hm->body.ptr,hm->body.len);
Json::Value user_json;
bool ret = JsonUtil::unserialize(body, &user_json);
if(ret == false)
{
Json::Value err;
err["result"] = "false";
err["reason"] = "解析出错";
std::string body1;
JsonUtil::serialize(err,&body1);
mg_http_reply(c,400,NULL,"%s",body1.c_str());
return ;
}
Usertable *table = ser->get_usertable();
ret = table->check_login(user_json);
if(ret == false)
{
Json::Value err;
err["result"] = "false";
err["reason"] = "登录失败";
std::string body1;
JsonUtil::serialize(err,&body1);
mg_http_reply(c,401,NULL,"%s",body1.c_str());
return ;
}
SessionManager *ssm = ser->get_session();
std::unordered_map<struct mg_connection*,uint64_t> *connss = ser->get_conn_ss();
uint64_t id = ssm->insert(user_json);
// connss->insert(std::make_pair<c,id>);
(*connss)[c] = id;
std::string cookie = "Set-Cookie: SSID=" + std::to_string(id)+"\r\n";
Json::Value res;
res["result"] = "true";
res["reason"] = "登录成功";
std::string body1;
JsonUtil::serialize(res,&body1);
mg_http_reply(c,200,cookie.c_str(),"%s",body1.c_str());
}
static void get_hostroy_message(struct mg_connection *c,struct mg_http_message *hm,Server *ser)
{
Json::Value user_json;
Message *msg = ser->get_messagetable();
msg->select(3000,&user_json);
std::string body;
JsonUtil::serialize(user_json,&body);
mg_http_reply(c,200,"Content-Type:application/json\r\n","%s",body.c_str());
}
static void boradcast_msg(struct mg_mgr *mgr,const std::string &msg)
{
struct mg_connection *conn = mgr->conns;
for(;conn != NULL;conn = conn->next)
{
if(conn->is_websocket)
{
mg_ws_send(conn,msg.c_str(),msg.size(),WEBSOCKET_OP_TEXT);
}
}
}
static void handler(struct mg_connection *c,int ev,void *ev_date ,void *data)
{
Server *ser = (Server*)data;
if(ev == MG_EV_HTTP_MSG)
{
struct mg_http_message *hm = (struct mg_http_message *)ev_date;
std::string method(hm->method.ptr,hm->method.len);
std::string uri(hm->uri.ptr,hm->uri.len);
if(method == "POST" && uri == "/user")//xin zeng
{
// std::cout<<"--------";
user_add(c,hm,ser);
}
else if(method == "POST" && uri == "/login")//deng lu
{
user_login(c,hm,ser);
}
else if(method == "PUT" && uri == "/user/password")//xiu gai mi ma
{
}
else if(method == "PUT" && uri == "/user/nickname")//ciu gai ni cheng
{
}
else if(method == "DELDTE" && uri == "/user")//zhu xiao yong hu
{
}
else if(method == "GET" && uri == "/user")//huo qu yong hu
{
struct mg_str *cookie = mg_http_get_header(hm,"Cookie");
if(cookie == NULL)
{
std::cout<<"--------";
mg_http_reply(c,401,"","%s","cookie-------401");
return ;
}
std::string cookie_str(cookie->ptr,cookie->len);
std::cout<<cookie_str<<std::endl;
size_t pos = cookie_str.find("=");
std::string key = cookie_str.substr(pos+1);
uint64_t ssid = std::atoi(key.c_str());
std::cout<<ssid<<std::endl;
SessionManager *ssm = ser->get_session();
if(ssm->exist(ssid) == false)
{
std::cout<<"-------------------";
mg_http_reply(c,401,"","%s","ssid----------401");
return ;
}
Json::Value user;
ssm->get_session_id(ssid,&user);
Usertable *table = ser->get_usertable();
Json::Value new_user;
table->select_id(user["username"].asString(),&new_user);
std::string user_str;
JsonUtil::serialize(new_user,&user_str);
mg_http_reply(c,200,"Content-Type: application/json\r\n","%s",user_str.c_str());
}
else if(method == "GET" && uri == "/message")
{
get_hostroy_message(c,hm,ser);
}
else if(method == "GET"&& uri == "/ws")//xie yi qie huan
{
std::cout<<"ws1"<<std::endl;
struct mg_str *cookie = mg_http_get_header(hm,"Cookie");
if(cookie == NULL)
{
mg_http_reply(c,401,"","%s","401");
return ;
}
std::string cookie_str(cookie->ptr,cookie->len);
size_t pos = cookie_str.find("=");
std::string key = cookie_str.substr(pos+1);
uint64_t ssid = std::atoi(key.c_str());
SessionManager *ssm = ser->get_session();
if(ssm->exist(ssid)== false)
{
mg_http_reply(c,401,"","%s","401");
return ;
}
std::cout<<"ws"<<std::endl;
mg_ws_upgrade(c,hm,NULL);
}
else{
struct mg_http_serve_opts opt = {.root_dir = ser->get_root().c_str()};
mg_http_serve_dir(c,hm,&opt);
}
}
else if(ev == MG_EV_WS_OPEN )
{
//wo shou cheng gong
}
else if(ev == MG_EV_WS_MSG)
{
//ke hu duo fa song xiao xi
struct mg_ws_message *wsmsg = (struct mg_ws_message*)ev_date;
std::string msg(wsmsg->data.ptr,wsmsg->data.len);
std::cout<<msg<<std::endl;
boradcast_msg(ser->get_mgr(),msg);
Json::Value json;
JsonUtil::unserialize(msg,&json);
ser->get_messagetable()->insert(json);
}
else if(ev == MG_EV_CLOSE)
{
//lian jie duo kai
}
}
public:
Server()
{
mg_mgr_init(&_mgr);
user_table = new Usertable();
message_table = new Message();
ss_manager = new SessionManager();
}
void start(int port)
{
std::string address = "0.0.0.0:" + std::to_string(port);
mg_http_listen(&_mgr,address.c_str(),handler,this);
for(;;)mg_mgr_poll(&_mgr,1000);
}
struct mg_mgr* get_mgr(){return &_mgr;}
std::string& get_root(){return _root;}
Usertable* get_usertable(){return user_table;}
Message* get_messagetable(){return message_table;}
SessionManager* get_session(){return ss_manager;}
std::unordered_map<struct mg_connection*,uint64_t>* get_conn_ss(){return &conn_ss;}
};
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化