gearmand.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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= true;
  61. _keepalive= keepalive_;
  62. }
  63. int keepalive_idle()
  64. {
  65. return _keepalive_idle;
  66. }
  67. void keepalive_idle(int keepalive_idle_)
  68. {
  69. _keepalive= true;
  70. _keepalive_idle= keepalive_idle_;
  71. }
  72. int keepalive_interval()
  73. {
  74. return _keepalive_interval;
  75. }
  76. void keepalive_interval(int keepalive_interval_)
  77. {
  78. _keepalive= true;
  79. _keepalive_interval= keepalive_interval_;
  80. }
  81. int keepalive_count()
  82. {
  83. return _keepalive_count;
  84. }
  85. void keepalive_count(int keepalive_count_)
  86. {
  87. _keepalive= true;
  88. _keepalive_count= keepalive_count_;
  89. }
  90. private:
  91. bool _keepalive;
  92. int _keepalive_idle;
  93. int _keepalive_interval;
  94. int _keepalive_count;
  95. } _socketopt;
  96. SocketOpt& socketopt()
  97. {
  98. return _socketopt;
  99. }
  100. gearmand_verbose_t verbose;
  101. gearmand_error_t ret;
  102. int backlog; // Set socket backlog for listening connection
  103. bool is_listen_event;
  104. bool is_wakeup_event;
  105. bool _exceptions;
  106. int timeout;
  107. uint32_t threads;
  108. uint32_t thread_count;
  109. uint32_t free_dcon_count;
  110. uint32_t max_thread_free_dcon_count;
  111. int wakeup_fd[2];
  112. char *host;
  113. gearmand_log_fn *log_fn;
  114. void *log_context;
  115. struct event_base *base;
  116. gearmand_thread_st *thread_list;
  117. gearmand_thread_st *thread_add_next;
  118. gearmand_con_st *free_dcon_list;
  119. gearman_server_st server;
  120. struct event wakeup_event;
  121. std::vector<gearmand_port_st> _port_list;
  122. private:
  123. SSL_CTX* _ctx_ssl;
  124. public:
  125. gearmand_st(const char *host_,
  126. uint32_t threads_,
  127. int backlog_,
  128. const gearmand_verbose_t verbose_,
  129. bool exceptions_) :
  130. verbose(verbose_),
  131. ret(GEARMAND_SUCCESS),
  132. backlog(backlog_),
  133. is_listen_event(false),
  134. is_wakeup_event(false),
  135. _exceptions(exceptions_),
  136. timeout(-1),
  137. threads(threads_),
  138. thread_count(0),
  139. free_dcon_count(0),
  140. max_thread_free_dcon_count(0),
  141. host(NULL),
  142. log_fn(NULL),
  143. log_context(NULL),
  144. base(NULL),
  145. thread_list(NULL),
  146. thread_add_next(NULL),
  147. free_dcon_list(NULL),
  148. _ctx_ssl(NULL)
  149. {
  150. if (host_)
  151. {
  152. host= strdup(host_);
  153. }
  154. wakeup_fd[0]= -1;
  155. wakeup_fd[1]= -1;
  156. }
  157. void init_ssl()
  158. {
  159. #if defined(HAVE_SSL) && HAVE_SSL
  160. SSL_load_error_strings();
  161. SSL_library_init();
  162. _ctx_ssl= SSL_CTX_new(SSLv23_server_method());
  163. #endif
  164. }
  165. SSL_CTX* ctx_ssl()
  166. {
  167. return _ctx_ssl;
  168. }
  169. ~gearmand_st()
  170. {
  171. if (host)
  172. {
  173. free(host);
  174. }
  175. #if defined(HAVE_SSL) && HAVE_SSL
  176. SSL_CTX_free(_ctx_ssl);
  177. #endif
  178. }
  179. bool exceptions() const
  180. {
  181. return _exceptions;
  182. }
  183. };