网络编程
单例模式
- 各变量随进程结束而结束
变为单例类
template <typename T>
class Singleton
{
protected:
Singleton() = default;
Singleton(const Singleton<T>&) = delete;
}
- 这样使得子类和父类没有了拷贝构造和默认构造,成为了一个单例
GetInstance()
- 利用call_once实现调用一次
static shared_ptr<T>GetInstance()
{
static once_flag s_flag;
call_once(s_flag, [&]() {
_instance = shared_ptr<T>(new T);
})
return _instance;
}
Singelton
#pragma once
#include "Singleton.h"
#include <queue>
#include <thread>
#include "Session.h"
#include <map>
#include <functional>
#include <json/json.h>
#include <json/value.h>
#include <json/reader.h>
typedef function<void(shared_ptr<Session>, const short& msg_id, const string& msg_data)> FunCallBack;
class LogicSystem:public Singleton<LogicSystem>
{
friend class Singleton<LogicSystem>;
private:
LogicSystem();
void RegisterCallBack();
void HelloWordCallBack(shared_ptr<Session>, const short& msg_id, const string& msg_data);
void DealMsg();
queue<shared_ptr<LogicNode>>_msg_que;
mutex _mutex;
condition_variable _consnume;
thread _worker_thread;
bool _b_stop;
map<short, FunCallBack> _fun_callback;
public:
~LogicSystem();
void PostMsgToQue(shared_ptr<LogicNode>msg);
};
LogicNode
头文件
class LogicNode
{
friend class LogicSystem;
public:
LogicNode(shared_ptr<Session>, shared_ptr<RecvNode>);
private:
shared_ptr<Session> _session;
shared_ptr<RecvNode> _recvnode;
};
实现
LogicNode::LogicNode(shared_ptr<Session> session, shared_ptr<RecvNode> recvnode)
:_session(session),_recvnode(recvnode)
{
}
LogicSystem
头文件
#pragma once
#include "Singleton.h"
#include <queue>
#include <thread>
#include "Session.h"
#include <map>
#include <functional>
#include <json/json.h>
#include <json/value.h>
#include <json/reader.h>
typedef function<void(shared_ptr<Session>, const short& msg_id, const string& msg_data)> FunCallBack;
class LogicSystem:public Singleton<LogicSystem>
{
friend class Singleton<LogicSystem>;
private:
LogicSystem();
void RegisterCallBack();
void HelloWordCallBack(shared_ptr<Session>, const short& msg_id, const string& msg_data);
void DealMsg();
queue<shared_ptr<LogicNode>>_msg_que;
mutex _mutex;
condition_variable _consnume;
thread _worker_thread;
bool _b_stop;
map<short, FunCallBack> _fun_callback;
public:
~LogicSystem();
void PostMsgToQue(shared_ptr<LogicNode>msg);
};
实现
#include "LogicSystem.h"
#include "LogicSystem.h"
using namespace std;
LogicSystem::LogicSystem()
:_b_stop(false)
{
RegisterCallBack();
_worker_thread = thread(&LogicSystem::DealMsg, this);
}
void LogicSystem::RegisterCallBack()
{
_fun_callback[MSG_HELLO_WORLD] = bind(&LogicSystem::HelloWordCallBack, this,
std::placeholders::_1, std::placeholders::_2, std::placeholders::_3);
}
void HelloWordCallBack(shared_ptr<Session>session, const short& msg_id, const string& msg_data)
{
Json::Reader reader;
Json::Value root;
reader.parse(msg_data, root);
cout << "receive msg id is " << root["id"].asInt() << "msg data is " << root["data"].asString() << endl;
root["data"] = "server has receive msg,msg data is" << root["data"].asString();
string return_str = root.toStyledString();
session->Send(return_str, root["id"].asInt());
}
void LogicSystem::DealMsg()
{
while (1)
{
unique_lock<mutex> unique_lk(_mutex);
while (_msg_que.empty() && !_b_stop)
{
_consnume.wait(unique_lk);
}
if (_b_stop)
{
while (!_msg_que.empty())
{
auto msg_node = _msg_que.front();
cout << "recv msg id is " << msg_node->_recvnode->_msg_id << endl;
auto call_back_iter = _fun_callback.find(msg_node->_recvnode->_msg_id);
if (call_back_iter == _fun_callback.end())
{
_msg_que.pop();
continue;
}
call_back_iter->second(msg_node->_session, msg_node->_recvnode->_msg_id,
string(msg_node->_recvnode->_data, msg_node->_recvnode->_cur_len));
_msg_que.pop();
}
break;
}
auto msg_node = _msg_que.front();
cout << "recv msg id is " << msg_node->_recvnode->_msg_id << endl;
auto call_back_iter = _fun_callback.find(msg_node->_recvnode->_msg_id);
auto call_back_iter = _fun_callback.find(msg_node->_recvnode->_msg_id);
if (call_back_iter == _fun_callback.end())
{
_msg_que.pop();
continue;
}
call_back_iter->second(msg_node->_session, msg_node->_recvnode->_msg_id,
string(msg_node->_recvnode->_data, msg_node->_recvnode->_cur_len));
_msg_que.pop();
}
}
void LogicSystem::PostMsgToQue(shared_ptr<LogicNode>msg)
{
unique_lock<mutex> unique_lk(_mutex);
_msg_que.push(msg);
if (_msg_que.size() == 1)
{
_consnume.notify_one();
}
}
LogicSystem::~LogicSystem()
{
_b_stop = 1;
_consnume.notify_one();
_worker_thread.join();
}
MySQL
索引4
回表
- 具有联合索引的无需回表
- 在无二级索引的需要回表
前缀索引
- 将字符串的一部分建立索引,可以节省空间,提高索引效率
create index idx_name on table_nbame(column(n));
- 通过前缀长度求最好性能的长度
vue3
条件渲染
v-if/v-else
- 惰性的,只有ture才会被渲染
- 判断条件是否为真
- 为真渲染,为假不渲染
<template>
<h3>条件渲染</h3>
<div v-if="flag">you can in true look my?</div>
<div v-else>you can in flase look me?</div>
</template>
<script>
export default {
data() {
return{
flag:!true
}
}
}
</script>
v-eles-if
- 与c/c++的eles if用法一致
<template>
<h3>条件渲染</h3>
<div v-if="type=='A'">A</div>
<div v-else-if="type=='B'">B</div>
<div v-else-if="type=='C'">C</div>
</template>
<script>
export default {
data() {
return{
type:'A'
}
}
}
</script>
v-show
- 始终渲染,被css影藏,基于display的
- 只判定自身能否显示
- 需要频繁切换需要
<template>
<h3>条件渲染</h3>
<div v-show="flag">you can in v-show look me?</div>
</template>
<script>
export default {
data() {
return{
flag:true,
}
}
}
</script>
列表渲染
- 利用v-for循环实现
- in/of都可以的
<template>
<h3>列表渲染</h3>
<p v-for="item in names">{{ item }}</p>
</template>
<script>
export default {
data() {
return{
names:["xzt","jst","tfs"]
}
}
}
</script>
v-for
- 类比Python的循环,{{}}中是迭代器
- 遍历对象的所有属性
获取索引
<template>
<h3>列表渲染</h3>
<p v-for="(item,index) in names">{{ item }} and {{ index }}</p>
</template>
<script>
export default {
data() {
return{
names:["xzt","jst","tfs"]
}
}
}
</script>
