universal.hpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. * Copyright (C) 2008 Brian Aker, Eric Day
  7. * All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions are
  11. * met:
  12. *
  13. * * Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. *
  16. * * Redistributions in binary form must reproduce the above
  17. * copyright notice, this list of conditions and the following disclaimer
  18. * in the documentation and/or other materials provided with the
  19. * distribution.
  20. *
  21. * * The names of its contributors may not be used to endorse or
  22. * promote products derived from this software without specific prior
  23. * written permission.
  24. *
  25. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  26. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  27. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  28. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  29. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  30. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  31. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  32. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  33. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  34. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  35. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  36. *
  37. */
  38. #pragma once
  39. #include "libgearman/allocator.hpp"
  40. #include "libgearman/server_options.hpp"
  41. #include "libgearman/interface/packet.hpp"
  42. #include "libgearman/vector.h"
  43. #include "libgearman/assert.hpp"
  44. enum universal_options_t
  45. {
  46. GEARMAN_UNIVERSAL_NON_BLOCKING,
  47. GEARMAN_UNIVERSAL_DONT_TRACK_PACKETS,
  48. GEARMAN_UNIVERSAL_IDENTIFY,
  49. GEARMAN_UNIVERSAL_MAX
  50. };
  51. /**
  52. @todo this is only used by the server and should be made private.
  53. */
  54. typedef struct gearman_connection_st gearman_connection_st;
  55. typedef gearman_return_t (gearman_event_watch_fn)(gearman_connection_st *con,
  56. short events, void *context);
  57. struct gearman_universal_st
  58. {
  59. struct Options {
  60. bool dont_track_packets;
  61. bool non_blocking;
  62. Options() :
  63. dont_track_packets(false),
  64. non_blocking(false)
  65. { }
  66. } options;
  67. gearman_verbose_t verbose;
  68. uint32_t con_count;
  69. uint32_t packet_count;
  70. uint32_t pfds_size;
  71. uint32_t sending;
  72. int timeout; // Connection timeout.
  73. gearman_connection_st *con_list;
  74. gearman_server_options_st *server_options_list;
  75. gearman_packet_st *packet_list;
  76. struct pollfd *pfds;
  77. gearman_log_fn *log_fn;
  78. void *log_context;
  79. gearman_allocator_t allocator;
  80. struct gearman_vector_st *_identifier;
  81. struct gearman_vector_st *_namespace;
  82. struct CYASSL_CTX* _ctx_ssl;
  83. struct error_st {
  84. gearman_return_t rc;
  85. int last_errno;
  86. char last_error[GEARMAN_MAX_ERROR_SIZE];
  87. error_st():
  88. rc(GEARMAN_SUCCESS),
  89. last_errno(0)
  90. {
  91. last_error[0]= 0;
  92. }
  93. } _error;
  94. int wakeup_fd[2];
  95. bool is_non_blocking() const
  96. {
  97. return options.non_blocking;
  98. }
  99. void non_blocking(bool arg_)
  100. {
  101. options.non_blocking= arg_;
  102. }
  103. const char *error() const
  104. {
  105. if (_error.last_error[0] == 0)
  106. {
  107. return NULL;
  108. }
  109. return static_cast<const char *>(_error.last_error);
  110. }
  111. gearman_return_t error_code() const
  112. {
  113. return _error.rc;
  114. }
  115. void error_code(gearman_return_t rc)
  116. {
  117. _error.rc= rc;
  118. }
  119. int last_errno() const
  120. {
  121. return _error.last_errno;
  122. }
  123. void last_errno(int last_errno_)
  124. {
  125. _error.last_errno= last_errno_;
  126. }
  127. bool has_connections() const
  128. {
  129. return con_count;
  130. }
  131. void reset_error()
  132. {
  133. _error.rc= GEARMAN_SUCCESS;
  134. _error.last_errno= 0;
  135. _error.last_error[0]= 0;
  136. }
  137. gearman_return_t option(const universal_options_t& option_, bool value);
  138. gearman_universal_st(const universal_options_t *options_= NULL) :
  139. verbose(GEARMAN_VERBOSE_NEVER),
  140. con_count(0),
  141. packet_count(0),
  142. pfds_size(0),
  143. sending(0),
  144. timeout(-1),
  145. con_list(NULL),
  146. server_options_list(NULL),
  147. packet_list(NULL),
  148. pfds(NULL),
  149. log_fn(NULL),
  150. log_context(NULL),
  151. allocator(gearman_default_allocator()),
  152. _identifier(NULL),
  153. _namespace(NULL),
  154. _ctx_ssl(NULL)
  155. {
  156. wakeup_fd[0]= INVALID_SOCKET;
  157. wakeup_fd[1]= INVALID_SOCKET;
  158. if (options_)
  159. {
  160. while (*options_ != GEARMAN_UNIVERSAL_MAX)
  161. {
  162. /**
  163. @note Check for bad value, refactor gearman_add_options().
  164. */
  165. (void)option(*options_, true);
  166. options_++;
  167. }
  168. }
  169. // Only does something if SSL has been enabled.
  170. bool ret= init_ssl();
  171. if (ret == false)
  172. {
  173. abort();
  174. }
  175. }
  176. bool init_ssl();
  177. struct CYASSL_CTX* ctx_ssl()
  178. {
  179. return _ctx_ssl;
  180. }
  181. ~gearman_universal_st();
  182. void identifier(const char *identifier_, const size_t identifier_size_);
  183. };
  184. static inline bool gearman_universal_is_non_blocking(gearman_universal_st &self)
  185. {
  186. return self.is_non_blocking();
  187. }
  188. static inline const char *gearman_universal_error(const gearman_universal_st &self)
  189. {
  190. return self.error();
  191. }
  192. static inline gearman_return_t gearman_universal_error_code(const gearman_universal_st &self)
  193. {
  194. return self.error_code();
  195. }
  196. static inline int gearman_universal_errno(const gearman_universal_st &self)
  197. {
  198. return self.last_errno();
  199. }
  200. static inline void universal_reset_error(gearman_universal_st &self)
  201. {
  202. self.reset_error();
  203. }