代码拉取完成,页面将自动刷新
同步操作将从 王祥/im 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
#ifndef __M_UTIL_H__
#define __M_UTIL_H__
#include<iostream>
#include<string>
#include<memory>
#include<sstream>
#include<jsoncpp/json/json.h>
#include<mysql/mysql.h>
#include<assert.h>
#include<mutex>
#include<unordered_map>
namespace im
{
class JsonUtil
{
public:
static bool serialize(Json::Value &val,std::string *body)
{
Json::StreamWriterBuilder swb;
Json::StreamWriter* sw = swb.newStreamWriter();
std::stringstream ss;
int res = sw->write(val,&ss);
if(res != 0)
{
std::cout << "serialize error" << std::endl;
delete sw;
return false;
}
*body = ss.str();
delete sw;
return true;
}
static bool unserialize(std::string &body, Json::Value *val)
{
Json::CharReaderBuilder crb;
std::unique_ptr<Json::CharReader> cr(crb.newCharReader());
std::string err;
bool res = cr->parse(body.c_str(),body.c_str() + body.size(),val,&err);
if(res == false)
{
std::cout<<"error"<<std::endl;
return false;
}
return true;
}
};
class Mysql{
public:
static MYSQL* mysql_create()
{
MYSQL* mysql = mysql_init(NULL);
if(mysql == NULL)
{
std::cout << "mysql_init error"<< std::endl;
return NULL;
}
if(mysql_real_connect(mysql,"127.0.0.1","root","","dbim",0,NULL,0) == NULL)
{
std::cout<<"connect error"<<std::endl;
mysql_close(mysql);
return NULL;
}
mysql_set_character_set(mysql,"stf8");
return mysql;
}
static bool mysql_exec(MYSQL *mysql,const std::string &sql)
{
int ret = mysql_query(mysql, sql.c_str());
if(ret != 0)
{
std::cout<<"query error"<<"----- "<< mysql_error(mysql)<<std::endl;
return false;
}
return true;
}
static void mysql_destroy(MYSQL *mysql)
{
if(mysql == NULL)
{
return ;
}
mysql_destroy(mysql);
}
};
class Usertable{
private:
MYSQL *_mysql;
std::mutex _mutex;
public:
Usertable()
{
_mysql = Mysql::mysql_create();
// assert(_mysql != NULL);
if(_mysql == NULL)
{
std::cout<<"lianjieshibai"<<std::endl;
}
}
bool insert(const Json::Value &user)
{
#define INSERT_USER "insert into user value (null,'%s',MD5(%s),'%s',now());"
char sql[4096] = {0};
snprintf(sql,4095,INSERT_USER,user["username"].asCString(),
user["password"].asCString(),
user["nickname"].asCString());
return Mysql::mysql_exec(_mysql,sql);
}
bool check_login(const Json::Value &user)
{
#define CHECK_LOGIN "select id from user where username='%s' and password=MD5('%s');"
char sql[4096] = {0};
snprintf(sql,4095,CHECK_LOGIN,user["username"].asCString(),user["password"].asCString());
MYSQL_RES *res;
{
std::unique_lock<std::mutex> lock(_mutex);
bool ret = Mysql::mysql_exec(_mysql,sql);
if(ret == false)
{
return false;
}
res = mysql_store_result(_mysql);
}
if(res == NULL)
{
return false;
}
int num_row = mysql_num_rows(res);
if(num_row == 0)
{
return false;
}
mysql_free_result(res);
return true;
}
bool update_passward(const Json::Value &user)
{
#define UPDATE_PASSWORD "update user set password=MD5(%s) where username='%s' and password=MD5(%s);"
char sql[4096] = {0};
snprintf(sql,4095,UPDATE_PASSWORD,user["newpassword"].asCString(),
user["username"].asCString(),user["password"].asCString());
return Mysql::mysql_exec(_mysql,sql);
}
bool update_nickname(const Json::Value &user)
{
#define UPDATE_NICKNAME "update user set nickname='%s' where username='%s' and password=MD5(%s);"
char sql[4096] = {0};
snprintf(sql,4095,UPDATE_NICKNAME,user["nickname"].asCString(),
user["username"].asCString(),user["password"].asCString());
return Mysql::mysql_exec(_mysql,sql);
}
bool remove(const Json::Value user)
{
#define REMOVE "delete from user where username='%s' and password=MD5(%s);"
char sql[4096] = {0};
snprintf(sql,4095,REMOVE,
user["username"].asCString(),user["password"].asCString());
return Mysql::mysql_exec(_mysql,sql);
}
bool select_id(const std::string &name,Json::Value *user)
{
#define SELECT_ID "select id,nickname from user where username='%s';"
char sql[4096] = {0};
MYSQL_RES *res;
snprintf(sql,4095,SELECT_ID,name.c_str());
{
std::unique_lock<std::mutex> lock(_mutex);
bool ret = Mysql::mysql_exec(_mysql,sql);
if(ret == false)
{
return false;
}
res = mysql_store_result(_mysql);
}
if(res == NULL)
{
return false;
}
int num_row = mysql_num_rows(res);
if(num_row != 1)
{
return false;
}
MYSQL_ROW row = mysql_fetch_row(res);
(*user)["id"] = row[0];
(*user)["nickname"] = row[1];
mysql_free_result(res);
return true;
}
// bool select_name();
~Usertable()
{
Mysql::mysql_destroy(_mysql);
}
};
class Message
{
private:
MYSQL *_mysql;
std::mutex _mutex;
public:
Message()
{
_mysql = Mysql::mysql_create();
}
~Message()
{
Mysql::mysql_destroy(_mysql);
}
bool insert(const Json::Value &msg)
{
#define INSERTMSG "insert into message value(null,'%s','%s',now());"
char sql[4096] = {0};
snprintf(sql,4095,INSERTMSG,msg["uid"].asCString(),msg["msg"].asCString());
return Mysql::mysql_exec(_mysql,sql);
}
bool select(int time,Json::Value *msg)
{
#define SELECTMSG "select id,uid,msg,ctime from message where timestampdiff(second,ctime,now())<%d;"
char sql[4096] = {0};
snprintf(sql,4095,SELECTMSG,time);
MYSQL_RES *res;
{
std::unique_lock<std::mutex> lock(_mutex);
bool ret = Mysql::mysql_exec(_mysql,sql);
if(ret == false)
{
return false;
}
res = mysql_store_result(_mysql);
}
if(res == NULL)
{
return false;
}
int num_row = mysql_num_rows(res);
for(int i = 0;i < num_row;i++)
{
MYSQL_ROW row = mysql_fetch_row(res);
Json::Value m;
m["id"] = std::stoi(row[0]);
m["uid"] = std::stoi(row[1]);
m["msg"] = row[2];
m["ctime"] = row[3];
msg->append(m);
}
mysql_free_result(res);
return true;
}
};
class SessionManager{
private:
std::unordered_map<uint64_t,Json::Value> _ss;
std::mutex _mutex;
uint64_t _next_id;
public:
SessionManager()
{_next_id = 1;}
uint64_t insert(const Json::Value &user)
{
uint64_t id = _next_id;
std::unique_lock<std::mutex> lock(_mutex);
_ss.insert(std::make_pair(_next_id,user));
_next_id++;
return id;
}
bool get_session_id(uint64_t ssid,Json::Value *user)
{
auto it = _ss.find(ssid);
if(it == _ss.end())
{
return false;
}
*user = it->second;
return true;
}
bool exist(uint64_t ssid)
{
auto it = _ss.find(ssid);
if(it == _ss.end())
{
return false;
}
return true;
}
void remove(uint64_t ssid)
{
_ss.erase(ssid);
}
};
}
#endif
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。