qt.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*-
  2. * Copyright (C) 2014 Pietro Cerutti <gahr@gahr.ch>
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. * 1. Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * 2. Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. *
  13. * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  14. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  15. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  16. * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
  17. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  18. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  19. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  20. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  21. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  22. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  23. * SUCH DAMAGE.
  24. */
  25. #ifndef __HIREDIS_QT_H__
  26. #define __HIREDIS_QT_H__
  27. #include <QSocketNotifier>
  28. #include "../async.h"
  29. static void RedisQtAddRead(void *);
  30. static void RedisQtDelRead(void *);
  31. static void RedisQtAddWrite(void *);
  32. static void RedisQtDelWrite(void *);
  33. static void RedisQtCleanup(void *);
  34. class RedisQtAdapter : public QObject {
  35. Q_OBJECT
  36. friend
  37. void RedisQtAddRead(void * adapter) {
  38. RedisQtAdapter * a = static_cast<RedisQtAdapter *>(adapter);
  39. a->addRead();
  40. }
  41. friend
  42. void RedisQtDelRead(void * adapter) {
  43. RedisQtAdapter * a = static_cast<RedisQtAdapter *>(adapter);
  44. a->delRead();
  45. }
  46. friend
  47. void RedisQtAddWrite(void * adapter) {
  48. RedisQtAdapter * a = static_cast<RedisQtAdapter *>(adapter);
  49. a->addWrite();
  50. }
  51. friend
  52. void RedisQtDelWrite(void * adapter) {
  53. RedisQtAdapter * a = static_cast<RedisQtAdapter *>(adapter);
  54. a->delWrite();
  55. }
  56. friend
  57. void RedisQtCleanup(void * adapter) {
  58. RedisQtAdapter * a = static_cast<RedisQtAdapter *>(adapter);
  59. a->cleanup();
  60. }
  61. public:
  62. RedisQtAdapter(QObject * parent = 0)
  63. : QObject(parent), m_ctx(0), m_read(0), m_write(0) { }
  64. ~RedisQtAdapter() {
  65. if (m_ctx != 0) {
  66. m_ctx->ev.data = NULL;
  67. }
  68. }
  69. int setContext(redisAsyncContext * ac) {
  70. if (ac->ev.data != NULL) {
  71. return REDIS_ERR;
  72. }
  73. m_ctx = ac;
  74. m_ctx->ev.data = this;
  75. m_ctx->ev.addRead = RedisQtAddRead;
  76. m_ctx->ev.delRead = RedisQtDelRead;
  77. m_ctx->ev.addWrite = RedisQtAddWrite;
  78. m_ctx->ev.delWrite = RedisQtDelWrite;
  79. m_ctx->ev.cleanup = RedisQtCleanup;
  80. return REDIS_OK;
  81. }
  82. private:
  83. void addRead() {
  84. if (m_read) return;
  85. m_read = new QSocketNotifier(m_ctx->c.fd, QSocketNotifier::Read, 0);
  86. connect(m_read, SIGNAL(activated(int)), this, SLOT(read()));
  87. }
  88. void delRead() {
  89. if (!m_read) return;
  90. delete m_read;
  91. m_read = 0;
  92. }
  93. void addWrite() {
  94. if (m_write) return;
  95. m_write = new QSocketNotifier(m_ctx->c.fd, QSocketNotifier::Write, 0);
  96. connect(m_write, SIGNAL(activated(int)), this, SLOT(write()));
  97. }
  98. void delWrite() {
  99. if (!m_write) return;
  100. delete m_write;
  101. m_write = 0;
  102. }
  103. void cleanup() {
  104. delRead();
  105. delWrite();
  106. }
  107. private slots:
  108. void read() { redisAsyncHandleRead(m_ctx); }
  109. void write() { redisAsyncHandleWrite(m_ctx); }
  110. private:
  111. redisAsyncContext * m_ctx;
  112. QSocketNotifier * m_read;
  113. QSocketNotifier * m_write;
  114. };
  115. #endif /* !__HIREDIS_QT_H__ */