继续 那个错误分析
文章目录
- redis 部分的错误
- oberver_channel_message测试
- 测试代码
- 问题
- 订阅命令错误--分析原因
- 为什么客户端还会报空json呢
- 问题分析
- 但是!但是!
- 改进
redis 部分的错误
oberver_channel_message测试
测试代码
// 在独立线程中接受订阅频道的消息--存在的意义 看订阅那里
void Redis::oberver_channel_message()
{redisReply *reply = nullptr;while (REDIS_OK == redisGetReply(this->_subscribe_context, (void **)&reply)){cout<<"jajjaja"<<endl;cout<<reply->dval<<endl;cout<<reply->element<<endl;cout<<1<<endl;cout<<reply->element[0]->str<<endl;cout<<2<<endl;cout<<reply->element[1]->str<<endl;cout<<3<<endl;cout.flush();// cout<<reply->element[2]->str<<endl;cout.flush();cout<<4<<endl;cout.flush();cout<<reply->elements<<endl;cout.flush();cout<<5<<endl;cout.flush();cout<<reply->integer<<endl;cout.flush();cout<<6<<endl;cout.flush();cout<<reply->len<<endl;cout<<7<<endl;// cout<<reply->str<<endl;cout<<8<<endl;cout<<reply->type<<endl;cout<<9<<endl;cout<<reply->vtype<<endl;cout<<10<<endl;// 订阅收到的消息 是一个带三个元素的数组if (reply != nullptr && reply->element[2] != nullptr && reply->element[2]->str != nullptr){// 给业务层上报通道上发生的消息_notify_message_handler(atoi(reply->element[1]->str), reply->element[2]->str);/*数组的下标1, 2对应 redis 回应的 (2)(3)1) "message"2) "13"3) "hello"*/}freeReplyObject(reply);}cerr << ">>>>>>>>>>>>>>>>>observer_channel_message quit<<<<<<<<<<<<<<<<<<<<" << endl;
}
问题
仅是个人理解, 记录一下,
cout<<reply->element[2]->str<<endl;
和
cout<<reply->str<<endl;
这两行 不注释, 服务器那边 会 的 打印 会停到 这两句的上一句
源码 还没看, 推测是, reply 里面的 str, , 做了 特殊处理, 应该是为了安全吧, 不让看,不让访问, 怎么输入, 都看不出来, 注释掉后, 会每通信一次, 就会打印一轮
订阅命令错误–分析原因
实际就是 昨天总结的那样, 就是访问 非法内存导致 段错误, 段错误触发的 SIGSEGV 信号 会导致 整个进程 崩溃
为什么客户端还会报空json呢
问题分析
char buffer[1024] = {0}; // 接收服务器返回的数据
len = recv(clientfd, buffer, 1024, 0); // 接收数据
cout<<"recv"<<endl;
cout<<buffer<<endl;
if (len < 0)
{cerr << "recv error" << endl;
}
这部分代码, 按理说, 服务器那边段错误, 也算是 异常退出
代码 应该终止在 recv函数这里,
但是!但是!
段错误崩溃 recv() 在本地会返回 0
,就跟对方正常关闭是一样的!!!
这是最关键的, 也因为这个, buffer 本身就是空, 往后走, json反序列化 报 空json, 也就报 json空 的 错误了
改进
char buffer[1024] = {0}; // 接收服务器返回的数据
len = recv(clientfd, buffer, 1024, 0); // 接收数据if (len < 0 || len == 0)
{if(len == 0){cout<<"server quit!"<<endl;return 0;}else{cerr << "recv error" << endl;}}