#include <iostream>#include <json/json.h>#include <json/value.h>#include <json/reader.h>#include "CServer.h"#include "ConfigMgr.h"#include "RedisMgr.h"void TestRedis() {//连接redis 需要启动才可以进行连接//redis默认监听端口为6387 可以再配置文件中修改redisContext* c = redisConnect("127.0.0.1", 6379);if (c->err){printf("Connect to redisServer faile:%s\n", c->errstr);redisFree(c); return;}printf("Connect to redisServer Success\n");std::string redis_password = "123";redisReply* r = (redisReply*)redisCommand(c, "AUTH %s", redis_password.c_str());if (r->type == REDIS_REPLY_ERROR) {printf("Redis认证失败!\n");}else {printf("Redis认证成功!\n");}//为redis设置keyconst char* command1 = "set stest1 value1";//执行redis命令行r = (redisReply*)redisCommand(c, command1);//如果返回NULL则说明执行失败if (NULL == r){printf("Execut command1 failure\n");redisFree(c); return;}//如果执行失败则释放连接if (!(r->type == REDIS_REPLY_STATUS && (strcmp(r->str, "OK") == 0 || strcmp(r->str, "ok") == 0))){printf("Failed to execute command[%s]\n", command1);freeReplyObject(r);redisFree(c); return;}//执行成功 释放redisCommand执行后返回的redisReply所占用的内存freeReplyObject(r);printf("Succeed to execute command[%s]\n", command1);const char* command2 = "strlen stest1";r = (redisReply*)redisCommand(c, command2);//如果返回类型不是整形 则释放连接if (r->type != REDIS_REPLY_INTEGER){printf("Failed to execute command[%s]\n", command2);freeReplyObject(r);redisFree(c); return;}//获取字符串长度int length = r->integer;freeReplyObject(r);printf("The length of 'stest1' is %d.\n", length);printf("Succeed to execute command[%s]\n", command2);//获取redis键值对信息const char* command3 = "get stest1";r = (redisReply*)redisCommand(c, command3);if (r->type != REDIS_REPLY_STRING){printf("Failed to execute command[%s]\n", command3);freeReplyObject(r);redisFree(c); return;}printf("The value of 'stest1' is %s\n", r->str);freeReplyObject(r);printf("Succeed to execute command[%s]\n", command3);const char* command4 = "get stest2";r = (redisReply*)redisCommand(c, command4);if (r->type != REDIS_REPLY_NIL){printf("Failed to execute command[%s]\n", command4);freeReplyObject(r);redisFree(c); return;}freeReplyObject(r);printf("Succeed to execute command[%s]\n", command4);//释放连接资源redisFree(c);}void TestRedis1() {/*assert(RedisMgr::GetInstance()->Connect("127.0.0.1", 6379));assert(RedisMgr::GetInstance()->Auth("123"));*/assert(RedisMgr::GetInstance()->Set("blogwebsite", "llfc.club"));std::string value = "";assert(RedisMgr::GetInstance()->Get("blogwebsite", value));assert(RedisMgr::GetInstance()->Get("nonekey", value) == false);assert(RedisMgr::GetInstance()->HSet("bloginfo", "blogwebsite", "llfc.club"));assert(RedisMgr::GetInstance()->HGet("bloginfo", "blogwebsite") != "");assert(RedisMgr::GetInstance()->ExistsKey("bloginfo"));assert(RedisMgr::GetInstance()->Del("bloginfo"));assert(RedisMgr::GetInstance()->Del("bloginfo"));assert(RedisMgr::GetInstance()->ExistsKey("bloginfo") == false);assert(RedisMgr::GetInstance()->LPush("lpushkey1", "lpushvalue1"));assert(RedisMgr::GetInstance()->LPush("lpushkey1", "lpushvalue2"));assert(RedisMgr::GetInstance()->LPush("lpushkey1", "lpushvalue3"));assert(RedisMgr::GetInstance()->RPop("lpushkey1", value));assert(RedisMgr::GetInstance()->RPop("lpushkey1", value));assert(RedisMgr::GetInstance()->LPop("lpushkey1", value));assert(RedisMgr::GetInstance()->LPop("lpushkey2", value) == false);RedisMgr::GetInstance()->Close();}int main(){//TestRedis1();//读取配置auto & gCfgMgr = ConfigMgr::Inst();std::string gate_port_str = gCfgMgr["GateServer"]["Port"];unsigned short gate_port = atoi(gate_port_str.c_str());try{unsigned short port = gate_port;//初始化上下文,默认1个线程net::io_context ioc{ 1 };//生成一个信号集boost::asio::signal_set signals(ioc, SIGINT, SIGTERM);//型号集异步等待,例如类似受到键盘上ctrl+c 这样的停止命令后,做出什么反应signals.async_wait([&ioc](const boost::system::error_code& err,int signal_number) {if (err) {return;}// 关闭iocioc.stop();});std::make_shared<CServer>(ioc, port)->Start();std::cout << "Server start at : " << port << std::endl;// 开始轮询ioc.run();}catch (std::exception const& e) {std::cerr << "Error" << e.what() << std::endl;return EXIT_FAILURE;}}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。