RedisServer解析(一)

文章目录


前言

之前解析了server对buttonrpc的使用,现在分析server是如何接受client发送到信息完成对应的redis操作

RedisServer

getInstance

cpp 复制代码
RedisServer* RedisServer::getInstance()
{
	static RedisServer redis;
	return &redis;
}

单例模式中的懒汉模式。因为本项目是单线程项目,不存在多个线程同时调用getinstance的情况,因此不需要额外处理。这里提供在多线程中实现单例模式懒汉模式的方法。

cpp 复制代码
#include <mutex>
class RedisServer {
public:
    static RedisServer* getInstance();
private:
    RedisServer();
    static std::once_flag initFlag; // 用于控制初始化的全局变量
};
std::once_flag RedisServer::initFlag;
RedisServer* RedisServer::getInstance() {
    static RedisServer* instance = nullptr;
    std::call_once(initFlag, []() {
        instance = new RedisServer();
    });
    return instance;
}

start

cpp 复制代码
void RedisServer::start() {
    signal(SIGINT, signalHandler);  
    printLogo();
    printStartMessage();
}

注册sigint信号处理函数,sighandler的作用是在使用sigint时将缓冲区刷新一遍。

client

接下来查看server绑定的函数server.bind("redis_command", &RedisServer::handleClient, RedisServer::getInstance());

getinstance是实例,redis_command是map的key值。handleClient则是命令处理函数,client通过此函数 string res = client.call<string>("redis_command", message).val();向server请求。

因此我们接下来查看client的请求过程。

client.call

cpp 复制代码
inline buttonrpc::value_t<R> buttonrpc::call(std::string name, P1 p1)
{
	Serializer ds;
	ds << name << p1;
	return net_call<R>(ds);
}

Serializer对<<进行了重载,在这里就是将name和参数1对内容写入ds.m_iodevice。然后调用net_call。

net_call

cpp 复制代码
template<typename R>
inline buttonrpc::value_t<R> buttonrpc::net_call(Serializer& ds)
{
	zmq::message_t request(ds.size() + 1);
	memcpy(request.data(), ds.data(), ds.size());
	if (m_error_code != RPC_ERR_RECV_TIMEOUT) {
		send(request);
	}
	zmq::message_t reply;
	recv(reply);
	value_t<R> val;
	if (reply.size() == 0) {
		// timeout
		m_error_code = RPC_ERR_RECV_TIMEOUT;
		val.set_code(RPC_ERR_RECV_TIMEOUT);
		val.set_msg("recv timeout");
		return val;
	}
	m_error_code = RPC_ERR_SUCCESS;
	ds.clear();
	ds.write_raw_data((char*)reply.data(), reply.size());
	ds.reset();
	ds >> val;
	return val;
}

代码比较简答,send(request)将信息发送给服务端,服务端对信息进行处理,其处理逻辑在上一遍博客中已经讲过了。在send和recv之后就是一次信息的处理。在call_函数中有代码段

cpp 复制代码
 auto fun = m_handlers[name]; //获取函数
 fun(ds, data, len); 

我们知道这其实就是调用了一次handleClient函数。

handleClient

cpp 复制代码
if (!startMulti) {
   std::shared_ptr<CommandParser> commandParser = flyweightFactory->getParser(command);
    if (commandParser == nullptr) {
        responseMessage = "Error: Command '" + command + "' not recognized.";
    }
    else {
        try {
            responseMessage = commandParser->parse(tokens);
        }
        catch (const std::exception& e) {
            responseMessage = "Error processing command '" + command + "': " + e.what();
        }
    }

    // 发送响应消息回客户端
    return responseMessage;
}

代码逻辑比较清晰,就是根据输入的command进行不同的操作。一些特殊的命令进行单独处理,而对于常规的命令则采用统一的入口flyweightFactory->getParser(command)。并对返回的信息执行responseMessage = commandParser->parse(tokens);

参考

zeromq

相关推荐
sukalot36 分钟前
windows C++-Lambda表达式(五)
开发语言·c++
jwensh2 小时前
【Linux】如何快速查看 linux 服务器有几个cpu
java·linux·服务器
shyuu_2 小时前
配置PXE预启动执行环境:使用PXE装机服务器网络引导装机
linux·运维·服务器·网络·云计算·运维开发
叫我龙翔5 小时前
【项目日记】高并发内存池---实现线程缓存
c++·缓存·哈希
hmbbPdx_5 小时前
RK3588 MPP使用Live555 进行推流
c++
让开,我要吃人了6 小时前
HarmonyOS NEXT 实战开发:实现日常提醒应用
linux·前端·华为·移动开发·harmonyos·鸿蒙·鸿蒙系统
深情不及里子6 小时前
CI/CD之Jenkins用于Linux系统的部署方式汇总
linux·ci/cd·容器·kubernetes·jenkins
linuxMinx6 小时前
Linux~Docker容器入门学习
linux·运维·服务器·网络
驱动起爆大师x_x6 小时前
一文了解肖特基二极管、稳压二极管和TVS管
linux·笔记·学习·机器人·自动驾驶
星眺北海6 小时前
【后端】使用uWSGI部署django项目
linux·python·django·uwsgi