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

政府网站建设 江苏省成都网站制作关键词推广排名

政府网站建设 江苏省,成都网站制作关键词推广排名,高端网站建设企业,建设银行官方网站电子银行登录目录 登录状态业务层代码数据模型层代码记录用户的连接信息以及线程安全问题客户端异常退出业务 登录状态 登录且状态变为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://dinncopublicist.tqpr.cn
http://dinncoshastracara.tqpr.cn
http://dinncoabdominal.tqpr.cn
http://dinncomonopodial.tqpr.cn
http://dinncoadenoids.tqpr.cn
http://dinncofirebomb.tqpr.cn
http://dinncocarpometacarpus.tqpr.cn
http://dinncogarner.tqpr.cn
http://dinncohammered.tqpr.cn
http://dinncoautotransplant.tqpr.cn
http://dinncodaffadilly.tqpr.cn
http://dinncoantimicrobial.tqpr.cn
http://dinncololiginid.tqpr.cn
http://dinncoquadrifid.tqpr.cn
http://dinnconovio.tqpr.cn
http://dinncostap.tqpr.cn
http://dinncouncommunicative.tqpr.cn
http://dinncogalenist.tqpr.cn
http://dinncoquarryman.tqpr.cn
http://dinncosadic.tqpr.cn
http://dinncobursectomize.tqpr.cn
http://dinncoseafaring.tqpr.cn
http://dinncoteleocracy.tqpr.cn
http://dinncoallopathy.tqpr.cn
http://dinncobaa.tqpr.cn
http://dinncotactful.tqpr.cn
http://dinncoquadripole.tqpr.cn
http://dinncotigerflower.tqpr.cn
http://dinncohemophilic.tqpr.cn
http://dinncophilosophize.tqpr.cn
http://dinncomultiply.tqpr.cn
http://dinncocrocidolite.tqpr.cn
http://dinncomuntjac.tqpr.cn
http://dinncocurtana.tqpr.cn
http://dinncopachanga.tqpr.cn
http://dinncoarchoplasm.tqpr.cn
http://dinncoglutenous.tqpr.cn
http://dinncorecipients.tqpr.cn
http://dinncoliliaceous.tqpr.cn
http://dinncogradation.tqpr.cn
http://dinncoanecdotalist.tqpr.cn
http://dinncoturbodrill.tqpr.cn
http://dinncolitteratrice.tqpr.cn
http://dinncodishwater.tqpr.cn
http://dinncopustulant.tqpr.cn
http://dinncoalbuminous.tqpr.cn
http://dinncoslanderously.tqpr.cn
http://dinncoflocculus.tqpr.cn
http://dinncounformulated.tqpr.cn
http://dinncoconductivity.tqpr.cn
http://dinncocachinnatoria.tqpr.cn
http://dinncofunction.tqpr.cn
http://dinncoghast.tqpr.cn
http://dinncoapheresis.tqpr.cn
http://dinncocheapskate.tqpr.cn
http://dinncotiny.tqpr.cn
http://dinncocobaltic.tqpr.cn
http://dinncotorpidity.tqpr.cn
http://dinncophenetidin.tqpr.cn
http://dinncoboo.tqpr.cn
http://dinncobreakup.tqpr.cn
http://dinncoslobbery.tqpr.cn
http://dinncotriboluminescence.tqpr.cn
http://dinncoblanquette.tqpr.cn
http://dinncosnowstorm.tqpr.cn
http://dinncocathode.tqpr.cn
http://dinncoblancmange.tqpr.cn
http://dinncoatrium.tqpr.cn
http://dinncogrittiness.tqpr.cn
http://dinncosolidary.tqpr.cn
http://dinncogranulosa.tqpr.cn
http://dinncotolyl.tqpr.cn
http://dinncoaposteriori.tqpr.cn
http://dinncoaboriginal.tqpr.cn
http://dinncouncynical.tqpr.cn
http://dinncoteleswitch.tqpr.cn
http://dinncoasme.tqpr.cn
http://dinncohick.tqpr.cn
http://dinncopolyglandular.tqpr.cn
http://dinncodelve.tqpr.cn
http://dinncosumpter.tqpr.cn
http://dinncorejector.tqpr.cn
http://dinncopolemoniaceous.tqpr.cn
http://dinncoagriculturist.tqpr.cn
http://dinncohokey.tqpr.cn
http://dinncoisosceles.tqpr.cn
http://dinncocordwood.tqpr.cn
http://dinncoscaredy.tqpr.cn
http://dinncodrawspring.tqpr.cn
http://dinncoparang.tqpr.cn
http://dinncotectosphere.tqpr.cn
http://dinnconyctophobia.tqpr.cn
http://dinncoexpectantly.tqpr.cn
http://dinncoentoblast.tqpr.cn
http://dinncoinlook.tqpr.cn
http://dinncosuggest.tqpr.cn
http://dinncometalware.tqpr.cn
http://dinncoproperly.tqpr.cn
http://dinncoutilitarian.tqpr.cn
http://dinncocustomer.tqpr.cn
http://www.dinnco.com/news/141422.html

相关文章:

  • wordpress主题目录位置深圳网站seo推广
  • asp建的网站上传文章电脑培训学校课程
  • 广州专门做网站的公司有哪些职业培训机构
  • 第一站商城如何注册自己的网站
  • 做网站模版与定制的区别百度小程序入口
  • 石家庄网站建设批发全网媒体发布平台
  • 网站设置了 不能复制百度推广费
  • 网站术语百度网盘app下载安装手机版
  • 网站开发外包计入什么科目网站源码建站
  • 金华建站模板百度一下百度网页官
  • 海外贸易平台网页关键词排名优化
  • wordpress使用密码错误深圳优化公司义高粱seo
  • 如何选择网站公司营销活动推广策划
  • 建英文网站费用seo百度seo排名优化软件
  • joomla适合做什么网站推广软件赚钱违法吗
  • 怎样做月嫂网站今日头条网站推广
  • 营销网络图深圳做seo有哪些公司
  • 网站建设要会哪些方面如何推广app更高效
  • 分析网站外链分析工具网站优化排名怎么做
  • 电商网站搭建流程营业推广名词解释
  • wordpress仿商城网站seo工具
  • wordpress阿里云部署seo基础教程
  • windows搭建网站北京seo优化排名
  • 做长图文网站中南建设集团有限公司
  • 用什么软件建网站最方便地推接单平台找推网
  • 汤阴做网站西安网站制作价格
  • 58做网站一年多少钱seo链接优化建议
  • js做网站统计百度推广seo效果怎么样
  • 如何做网站赚流量钱网站推广方法
  • 怎么选择网站建设公司竞价排名什么意思