gearmand.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
  2. *
  3. * Gearmand client and server library.
  4. *
  5. * Copyright (C) 2011-2013 Data Differential, http://datadifferential.com/
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions are
  10. * met:
  11. *
  12. * * Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * * Redistributions in binary form must reproduce the above
  16. * copyright notice, this list of conditions and the following disclaimer
  17. * in the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * * The names of its contributors may not be used to endorse or
  21. * promote products derived from this software without specific prior
  22. * written permission.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  25. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  26. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  27. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  28. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  29. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  30. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  31. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  32. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  33. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  34. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  35. *
  36. */
  37. #pragma once
  38. #include "libgearman-server/struct/server.h"
  39. #include "libgearman/ssl.h"
  40. #include "libgearman-server/struct/port.h"
  41. #include <vector>
  42. #include <cstring>
  43. struct gearmand_st
  44. {
  45. class SocketOpt {
  46. public:
  47. SocketOpt():
  48. _keepalive(false),
  49. _keepalive_idle(-1),
  50. _keepalive_interval(-1),
  51. _keepalive_count(-1)
  52. {
  53. }
  54. bool keepalive()
  55. {
  56. return _keepalive;
  57. }
  58. void keepalive(int keepalive_)
  59. {
  60. _keepalive= keepalive_;
  61. }
  62. int keepalive_idle()
  63. {
  64. return _keepalive_idle;
  65. }
  66. void keepalive_idle(int keepalive_idle_)
  67. {
  68. _keepalive= true;
  69. _keepalive_idle= keepalive_idle_;
  70. }
  71. int keepalive_interval()
  72. {
  73. return _keepalive_interval;
  74. }
  75. void keepalive_interval(int keepalive_interval_)
  76. {
  77. _keepalive= true;
  78. _keepalive_interval= keepalive_interval_;
  79. }
  80. int keepalive_count()
  81. {
  82. return _keepalive_count;
  83. }
  84. void keepalive_count(int keepalive_count_)
  85. {
  86. _keepalive= true;
  87. _keepalive_count= keepalive_count_;
  88. }
  89. private:
  90. bool _keepalive;
  91. int _keepalive_idle;
  92. int _keepalive_interval;
  93. int _keepalive_count;
  94. } _socketopt;
  95. SocketOpt& socketopt()
  96. {
  97. return _socketopt;
  98. }
  99. gearmand_verbose_t verbose;
  100. gearmand_error_t ret;
  101. int backlog; // Set socket backlog for listening connection
  102. bool is_listen_event;
  103. bool is_wakeup_event;
  104. bool _exceptions;
  105. int timeout;
  106. uint32_t threads;
  107. uint32_t thread_count;
  108. uint32_t free_dcon_count;
  109. uint32_t max_thread_free_dcon_count;
  110. int wakeup_fd[2];
  111. char *host;
  112. gearmand_log_fn *log_fn;
  113. void *log_context;
  114. struct event_base *base;
  115. gearmand_thread_st *thread_list;
  116. gearmand_thread_st *thread_add_next;
  117. gearmand_con_st *free_dcon_list;
  118. gearman_server_st server;
  119. struct event wakeup_event;
  120. std::vector<gearmand_port_st> _port_list;
  121. private:
  122. SSL_CTX* _ctx_ssl;
  123. public:
  124. gearmand_st(const char *host_,
  125. uint32_t threads_,
  126. int backlog_,
  127. const gearmand_verbose_t verbose_,
  128. bool exceptions_) :
  129. verbose(verbose_),
  130. ret(GEARMAND_SUCCESS),
  131. backlog(backlog_),
  132. is_listen_event(false),
  133. is_wakeup_event(false),
  134. _exceptions(exceptions_),
  135. timeout(-1),
  136. threads(threads_),
  137. thread_count(0),
  138. free_dcon_count(0),
  139. max_thread_free_dcon_count(0),
  140. host(NULL),
  141. log_fn(NULL),
  142. log_context(NULL),
  143. base(NULL),
  144. thread_list(NULL),
  145. thread_add_next(NULL),
  146. free_dcon_list(NULL),
  147. _ctx_ssl(NULL)
  148. {
  149. if (host_)
  150. {
  151. host= strdup(host_);
  152. }
  153. wakeup_fd[0]= -1;
  154. wakeup_fd[1]= -1;
  155. }
  156. void init_ssl()
  157. {
  158. #if defined(HAVE_SSL) && HAVE_SSL
  159. SSL_load_error_strings();
  160. SSL_library_init();
  161. _ctx_ssl= SSL_CTX_new(SSLv23_server_method());
  162. #endif
  163. }
  164. SSL_CTX* ctx_ssl()
  165. {
  166. return _ctx_ssl;
  167. }
  168. ~gearmand_st()
  169. {
  170. if (host)
  171. {
  172. free(host);
  173. }
  174. #if defined(HAVE_SSL) && HAVE_SSL
  175. SSL_CTX_free(_ctx_ssl);
  176. #endif
  177. }
  178. bool exceptions() const
  179. {
  180. return _exceptions;
  181. }
  182. };