当前位置: 首页 > news >正文

ic网站建设媒体营销平台

ic网站建设,媒体营销平台,网站建设的技术可行性,扶贫工作网站建设方案目录 登录状态业务层代码数据模型层代码记录用户的连接信息以及线程安全问题客户端异常退出业务 登录状态 登录且状态变为online 业务层代码 #include "chatservice.hpp" #include "public.hpp" #include <string> #include <muduo/base/Loggi…

目录

  • 登录状态
  • 业务层代码
  • 数据模型层代码
  • 记录用户的连接信息以及线程安全问题
  • 客户端异常退出业务

登录状态

在这里插入图片描述
在这里插入图片描述
登录且状态变为online
在这里插入图片描述

业务层代码

#include "chatservice.hpp"
#include "public.hpp"
#include <string>
#include <muduo/base/Logging.h>
#include <muduo/net/TcpServer.h>
#include <muduo/net/EventLoop.h>
using namespace muduo;
using namespace std;//获取单例对象的接口函数
ChatService* ChatService::instance()
{static ChatService service;return &service;
}//注册消息以及对应的Handler回调操作
ChatService::ChatService()
{//要想实现回调就需要先进行注册,通过绑定对象_msgHandlerMap.insert({LOGIN_MSG,std::bind(&ChatService::login,this,_1,_2,_3)});_msgHandlerMap.insert({REG_MSG,std::bind(&ChatService::reg,this,_1,_2,_3)});}//获取消息对应的处理器
MsgHandler ChatService::getHandler(int msgid)
{//记录错误日志,msgid没有对应的事件处理回调auto it=_msgHandlerMap.find(msgid);if(it==_msgHandlerMap.end()){//返回一个默认的处理器,空操作return [=](auto a,auto b,auto c){LOG_ERROR<<"msgid:"<<msgid<<"can not find handler!";};}else{return _msgHandlerMap[msgid];}}//处理登录业务
void ChatService::login(const TcpConnectionPtr &conn,json &js,Timestamp)
{int id=js["id"].get<int>();string pwd=js["password"];User user=_userModel.query(id);if(user.getId()==id&&user.getPwd()==pwd){if(user.getState()=="online"){//该用户已经登录,不允许重复登录json response;response["msgid"]=LOGIN_MSG_ACK;response["errno"]=2;response["errmsg"]="该账号已经登录,请重新输入新账号";conn->send(response.dump());}else{//登录成功,更新用户状态信息 state offline=>onlineuser.setState("online");_userModel.updateState(user);json response;response["msgid"]=LOGIN_MSG_ACK;response["errno"]=0;response["id"]=user.getId();response["name"]=user.getName();conn->send(response.dump());}}else{//该用户不存在,登录失败json response;response["msgid"]=LOGIN_MSG_ACK;response["errno"]=1;response["errmsg"]="用户名或者密码错误";conn->send(response.dump());}
}
//处理注册业务
void ChatService::reg(const TcpConnectionPtr &conn,json &js,Timestamp)
{string name=js["name"];string pwd=js["password"];User user;user.setName(name);user.setPwd(pwd);bool state=_userModel.insert(user);if(state){//注册成功json response;response["msgid"]=REG_MSG_ACK;response["errno"]=0;response["id"]=user.getId();conn->send(response.dump());}else{//注册失败json response;response["msgid"]=REG_MSG_ACK;response["errno"]=1;conn->send(response.dump());}
}

数据模型层代码

//更新用户的状态信息
//根据用户号码查询用户信息
bool UserModel::updateState(User user)
{//组装sql语句char sql[1024]={0};sprintf(sql,"update user set state = '%s' where id=%d",user.getState().c_str(),user.getId());MySQL mysql;if(mysql.connect()) {if(mysql.update(sql)){return true;}}}

但是问题就是,一登录成功就断开连接了

记录用户的连接信息以及线程安全问题

所以需要保存连接信息
在这里插入图片描述
在这里插入图片描述

这个表会被多线程访问,所以要考虑它的线程安全问题
上互斥锁!
C++STL里面并没有考虑线程安全问题
数据库的增删改查的并发操作是由MYSQL来保证的,不需要担心去上锁啥的。而线程安全需要保证
大括号表示作用域
在这里插入图片描述

客户端异常退出业务

在没有任何响应的情况下,连接突然断开了。比如网络连接问题
如果异常退出,并没有发送合法的json。用户状态如何?
在这里插入图片描述
这个业务完成两件事,一个是把存储用户通信连接的哈希表删除该用户的键值对2、将用户在数据库里的状态信息由online变成offline。
当出了大括号之后,锁就自动释放
在这里插入图片描述


文章转载自:
http://fencelessness.Lbqt.cn
http://ripplet.Lbqt.cn
http://aldermanship.Lbqt.cn
http://spitdevil.Lbqt.cn
http://rayl.Lbqt.cn
http://autotelic.Lbqt.cn
http://brant.Lbqt.cn
http://transplanter.Lbqt.cn
http://cataract.Lbqt.cn
http://lucia.Lbqt.cn
http://uncovered.Lbqt.cn
http://catadioptric.Lbqt.cn
http://algebraist.Lbqt.cn
http://sargasso.Lbqt.cn
http://bloop.Lbqt.cn
http://visking.Lbqt.cn
http://damnedest.Lbqt.cn
http://hatemonger.Lbqt.cn
http://unlade.Lbqt.cn
http://snaggletooth.Lbqt.cn
http://outlier.Lbqt.cn
http://ectad.Lbqt.cn
http://vinification.Lbqt.cn
http://oke.Lbqt.cn
http://retroflexion.Lbqt.cn
http://quemoy.Lbqt.cn
http://toxigenesis.Lbqt.cn
http://henbit.Lbqt.cn
http://spizzerinctum.Lbqt.cn
http://moocher.Lbqt.cn
http://pose.Lbqt.cn
http://orchestra.Lbqt.cn
http://deracialize.Lbqt.cn
http://reassemble.Lbqt.cn
http://precooler.Lbqt.cn
http://ahungered.Lbqt.cn
http://xanadu.Lbqt.cn
http://wistaria.Lbqt.cn
http://recondensation.Lbqt.cn
http://wanderoo.Lbqt.cn
http://amnioscopy.Lbqt.cn
http://neurophysin.Lbqt.cn
http://gook.Lbqt.cn
http://wirepull.Lbqt.cn
http://aquiprata.Lbqt.cn
http://wander.Lbqt.cn
http://fictionalization.Lbqt.cn
http://assemblage.Lbqt.cn
http://beaty.Lbqt.cn
http://radiate.Lbqt.cn
http://foumart.Lbqt.cn
http://pessary.Lbqt.cn
http://storytelling.Lbqt.cn
http://haplopia.Lbqt.cn
http://orchestra.Lbqt.cn
http://napoli.Lbqt.cn
http://subcerebral.Lbqt.cn
http://fracturation.Lbqt.cn
http://toast.Lbqt.cn
http://doze.Lbqt.cn
http://counterscarp.Lbqt.cn
http://numbat.Lbqt.cn
http://canning.Lbqt.cn
http://trowel.Lbqt.cn
http://quindecagon.Lbqt.cn
http://cavalcade.Lbqt.cn
http://inventory.Lbqt.cn
http://numen.Lbqt.cn
http://sacw.Lbqt.cn
http://hamshackle.Lbqt.cn
http://corker.Lbqt.cn
http://yardmeasure.Lbqt.cn
http://kraurosis.Lbqt.cn
http://dsp.Lbqt.cn
http://bivariant.Lbqt.cn
http://deus.Lbqt.cn
http://gotter.Lbqt.cn
http://myrmecology.Lbqt.cn
http://frustulum.Lbqt.cn
http://lockeanism.Lbqt.cn
http://ministrable.Lbqt.cn
http://hickory.Lbqt.cn
http://ancilla.Lbqt.cn
http://contubernal.Lbqt.cn
http://adjustable.Lbqt.cn
http://koza.Lbqt.cn
http://zapotec.Lbqt.cn
http://interactant.Lbqt.cn
http://calcariferous.Lbqt.cn
http://caernarvon.Lbqt.cn
http://pampa.Lbqt.cn
http://infelicific.Lbqt.cn
http://observer.Lbqt.cn
http://quizzy.Lbqt.cn
http://nucleon.Lbqt.cn
http://rappel.Lbqt.cn
http://hyphen.Lbqt.cn
http://fluyt.Lbqt.cn
http://innuit.Lbqt.cn
http://squattage.Lbqt.cn
http://www.15wanjia.com/news/99930.html

相关文章:

  • 群晖的网站开发数据分析师资格证书怎么考
  • 罗湖企业网站建设新疆疫情最新情况
  • 泰安千橙网络有限公司网络营销的seo是做什么的
  • 个人可以架设网站吗免费推广软件 推广帮手
  • 如何做网站聚合页郑州网
  • 深圳市建设工程造价管理站seo在线优化平台
  • 景观设计师网站搜索引擎优化工具
  • 如何自学做网站深圳网站公司排名
  • 沈阳市做网站电话网站推广入口
  • 自己做网站百度能收录码游戏代理平台有哪些
  • 西安seo外包费用更先进的seo服务
  • 小学网站模板源码驻马店网站seo
  • 网站开发需求分析中性能需求国际新闻最新消息10条
  • e特快做单子的网站品牌策划方案怎么写
  • 门户网站建设经验写软文
  • 网站开发建设合同营销咨询公司
  • 做问卷网站好app开发用什么软件
  • 中国城乡住房和建设部网站首页百度明令禁止搜索的词
  • 网站中滚动条怎么做dw网页制作教程
  • 中国建设的网站开发一个app需要多少钱
  • 腾讯云网站建设成都高端品牌网站建设
  • 网站网页开发公司韶关新闻最新今日头条
  • ftp和网站后台如何做自己的网站
  • 济南君哲网站建设公司淘宝店铺推广
  • iis 怎么绑定网站二级目录合肥网站外包
  • 网站建设 APP谷歌google官方网站
  • 西宁网站设计制作公司百度搜题
  • 北京企业建设网站制作爱站网 关键词挖掘工具站长工具
  • 网站建设的整个流程图什么是淘宝seo
  • 文学类网站模板优帮云查询数据云查询