欢迎来到冰点文库! | 帮助中心 分享价值,成长自我!
冰点文库
全部分类
  • 临时分类>
  • IT计算机>
  • 经管营销>
  • 医药卫生>
  • 自然科学>
  • 农林牧渔>
  • 人文社科>
  • 工程科技>
  • PPT模板>
  • 求职职场>
  • 解决方案>
  • 总结汇报>
  • ImageVerifierCode 换一换
    首页 冰点文库 > 资源分类 > DOCX文档下载
    分享到微信 分享到微博 分享到QQ空间

    linux下使用hiredis异步API实现subpub消息订阅和发布的功能Word文档格式.docx

    • 资源ID:877180       资源大小:18.60KB        全文页数:9页
    • 资源格式: DOCX        下载积分:1金币
    快捷下载 游客一键下载
    账号登录下载
    微信登录下载
    三方登录下载: 微信开放平台登录 QQ登录
    二维码
    微信扫一扫登录
    下载资源需要1金币
    邮箱/手机:
    温馨提示:
    快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。
    如填写123,账号就是123,密码也是123。
    支付方式: 支付宝    微信支付   
    验证码:   换一换

    加入VIP,免费下载
     
    账号:
    密码:
    验证码:   换一换
      忘记密码?
        
    友情提示
    2、PDF文件下载后,可能会被浏览器默认打开,此种情况可以点击浏览器菜单,保存网页到桌面,就可以正常下载了。
    3、本站不支持迅雷下载,请使用电脑自带的IE浏览器,或者360浏览器、谷歌浏览器下载即可。
    4、本站资源下载后的文档和图纸-无水印,预览文档经过压缩,下载后原文更清晰。
    5、试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。

    linux下使用hiredis异步API实现subpub消息订阅和发布的功能Word文档格式.docx

    1、 bool disconnect(); bool publish(const std:string &channel_name, const std:message); private: / 下面三个回调函数供redis服务调用 / 连接回调 static void connect_callback(const redisAsyncContext *redis_context, int status); / 断开连接的回调 static void disconnect_callback(const redisAsyncContext *redis_context, int status

    2、); / 执行命令回调 static void command_callback(redisAsyncContext *redis_context, void *reply, void *privdata); / 事件分发线程函数 static void *event_thread(void *data); void *event_proc(); / libevent事件对象 event_base *_event_base; / 事件线程ID pthread_t _event_thread; / 事件线程的信号量 sem_t _event_sem; / hiredis异步对象 redisAsy

    3、ncContext *_redis_context; ; #endif redis_publisher.cpp redis_publisher.cpp & */ #include #include #include #include redis_publisher.h CRedisPublisher:CRedisPublisher():_event_base(0), _event_thread(0), _redis_context(0) CRedisPublisher:CRedisPublisher() bool CRedisPublisher:init() / initialize the

    4、event _event_base = event_base_new(); / 创建libevent对象 if (NULL = _event_base) printf( Create redis event failed.n); return false; memset(&_event_sem, 0, sizeof(_event_sem); int ret = sem_init(&_event_sem, 0, 0); if (ret != 0) printf( Init sem failed.n return true; bool CRedisPublisher:uninit() _event

    5、_base = NULL; sem_destroy(&_event_sem); return true;connect() / connect redis _redis_context = redisAsyncConnect(127.0.0.1, 6379); / 异步连接到redis服务器上,使用默认端口 if (NULL = _redis_context) printf( Connect redis failed.n if (_redis_context-&err) printf( Connect redis error: %d, %sn, _redis_context-&err, _re

    6、dis_context-&errstr); / 输出错误信息 return false; / attach the event redisLibeventAttach(_redis_context, _event_base); / 将事件绑定到redis context上,使设置给redis的回调跟事件关联 / 创建事件处理线程 int ret = pthread_create(&_event_thread, 0, &CRedisPublisher:event_thread, this); create event thread failed.n disconnect(); / 设置连接回调,

    7、当异步调用连接后,服务器处理连接请求结束后调用,通知调用者连接的状态 redisAsyncSetConnectCallback(_redis_context, &connect_callback); / 设置断开连接回调,当服务器断开连接后,通知调用者连接断开,调用者可以利用这个函数实现重连 redisAsyncSetDisconnectCallback(_redis_context, &disconnect_callback); / 启动事件线程 sem_post(&disconnect() if (_redis_context) redisAsyncDisconnect(_redis_co

    8、ntext); redisAsyncFree(_redis_context); _redis_context = NULL;publish(const std:message) int ret = redisAsyncCommand(_redis_context, &command_callback, this, PUBLISH %s %s, channel_name.c_str(), message.c_str(); if (REDIS_ERR = ret) printf(Publish command failed: %dn, ret); void CRedisPublisher:conn

    9、ect_callback(const redisAsyncContext *redis_context, int status) if (status != REDIS_OK) printf( Error: %sn, redis_context-& else printf( Redis connected!n void CRedisPublisher:disconnect_callback( const redisAsyncContext *redis_context, int status) if (status != REDIS_OK) / 这里异常退出,可以尝试重连 printf( /

    10、消息接收回调函数 void CRedisPublisher:command_callback(redisAsyncContext *redis_context, void *reply, void *privdata) printf(command callback.n / 这里不执行任何操作 void *CRedisPublisher:event_thread(void *data) if (NULL = data) printf( Error! assert(false); return NULL; CRedisPublisher *self_this = reinterpret_cast

    11、(data); return self_this-&event_proc(); void *CRedisPublisher:event_proc() sem_wait(& / 开启事件分发,event_base_dispatch会阻塞 event_base_dispatch(_event_base); redis_subscriber.h redis_subscriber.h & 封装hiredis,实现消息订阅redis功能 */ #ifndef REDIS_SUBSCRIBER_H #define REDIS_SUBSCRIBER_H #include #include #include

    12、#include #include #include #include #include #include class CRedisSubscriber public: typedef std:tr1:function NotifyMessageFn; / 回调函数对象类型,当接收到消息后调用回调把消息发送出去 CRedisSubscriber(); CRedisSubscriber(); bool init(const NotifyMessageFn &fn); / 传入回调对象 bool uninit(); / 可以多次调用,订阅多个频道 bool subscribe(const std:

    13、channel_name); / 通知外层的回调函数对象 NotifyMessageFn _notify_message_fn; #endif redis_subscriber.cpp: /* & redis_subscriber.cpp &redis_subscriber.h CRedisSubscriber:CRedisSubscriber():_event_base(0), _event_thread(0), _redis_context(0) CRedisSubscriber:CRedisSubscriber() bool CRedisSubscriber:init(const Not

    14、ifyMessageFn &fn) / initialize the event _notify_message_fn = fn; _event_base = event_base_new(); bool CRedisSubscriber:CRedisSubscriber: bool CRedisSubscriber:subscribe(const std:channel_name) int ret = redisAsyncCommand(_redis_context, &SUBSCRIBE %s, channel_name.c_str();Subscribe command failed:

    15、printf( Subscribe success: void CRedisSubscriber: void CRedisSubscriber: / 消息接收回调函数 void CRedisSubscriber:command_callback(redisAsyncContext *redis_context, void *reply, void *privdata) if (NULL = reply | NULL = privdata) return ; / 静态函数中,要使用类的成员变量,把当前的this指针传进来,用this指针间接访问 CRedisSubscriber *self_th

    16、is = reinterpret_cast(privdata); redisReply *redis_reply = reinterpret_cast(reply); / 订阅接收到的消息是一个带三元素的数组 if (redis_reply-&type = REDIS_REPLY_ARRAY & redis_reply-&elements = 3) printf( Recieve message:%s:%d:%dn, redis_reply-&element0-&str, redis_reply-&len, redis_reply-&element1-&element2-&len); / 调用函数对象把消息通知给外层 self_this-&_notify_message_fn(redis_reply-& void *CRedisSubscriber:event_thread


    注意事项

    本文(linux下使用hiredis异步API实现subpub消息订阅和发布的功能Word文档格式.docx)为本站会员主动上传,冰点文库仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰点文库(点击联系客服),我们立即给予删除!

    温馨提示:如果因为网速或其他原因下载失败请重新下载,重复下载不扣分。




    关于我们 - 网站声明 - 网站地图 - 资源地图 - 友情链接 - 网站客服 - 联系我们

    copyright@ 2008-2023 冰点文库 网站版权所有

    经营许可证编号:鄂ICP备19020893号-2


    收起
    展开