connection.hpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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-1.0/connection.h>
  40. #include "libgearman/interface/packet.hpp"
  41. #include "libgearman/interface/universal.hpp"
  42. #include "libgearman/ssl.h"
  43. struct gearman_connection_st
  44. {
  45. struct Options {
  46. bool server_options_sent;
  47. bool identifier_sent;
  48. bool ready;
  49. bool packet_in_use;
  50. Options() :
  51. server_options_sent(false),
  52. identifier_sent(false),
  53. ready(false),
  54. packet_in_use(false)
  55. { }
  56. } options;
  57. enum gearman_con_universal_t state;
  58. enum gearman_con_send_t send_state;
  59. enum gearman_con_recv_t recv_state;
  60. void set_pollfd(struct pollfd& pollfd_)
  61. {
  62. pollfd_.fd= socket_descriptor();
  63. pollfd_.events= _events;
  64. pollfd_.revents= 0;
  65. }
  66. bool events()
  67. {
  68. return _events;
  69. }
  70. bool is_events(int events_)
  71. {
  72. if (_events & events_)
  73. {
  74. return true;
  75. }
  76. return false;
  77. }
  78. bool is_revents(int revents_)
  79. {
  80. if (_revents & revents_)
  81. {
  82. return true;
  83. }
  84. return false;
  85. }
  86. private:
  87. short _events;
  88. short _revents;
  89. int fd;
  90. SSL* _ssl;
  91. int cached_errno;
  92. public:
  93. uint32_t created_id;
  94. uint32_t created_id_next;
  95. size_t send_buffer_size;
  96. size_t send_data_size;
  97. size_t send_data_offset;
  98. size_t recv_buffer_size;
  99. size_t recv_data_size;
  100. size_t recv_data_offset;
  101. private:
  102. gearman_universal_st &universal;
  103. gearman_connection_st *next;
  104. gearman_connection_st *prev;
  105. void *context;
  106. struct addrinfo *_addrinfo;
  107. struct addrinfo *addrinfo_next;
  108. public:
  109. const char *send_buffer_ptr;
  110. char *recv_buffer_ptr;
  111. gearman_packet_st _packet;
  112. char _host[GEARMAN_NI_MAXHOST];
  113. char _service[GEARMAN_NI_MAXSERV];
  114. char send_buffer[GEARMAN_SEND_BUFFER_SIZE];
  115. char recv_buffer[GEARMAN_RECV_BUFFER_SIZE];
  116. gearman_connection_st* next_connection(void)
  117. {
  118. return next;
  119. }
  120. int socket_descriptor() const
  121. {
  122. return fd;
  123. }
  124. int socket_descriptor_is_valid() const
  125. {
  126. return fd != INVALID_SOCKET;
  127. }
  128. void error(int errno_)
  129. {
  130. cached_errno= errno_;
  131. }
  132. void free_private_packet();
  133. gearman_connection_st(gearman_universal_st& universal_arg, const char*, const char*);
  134. ~gearman_connection_st();
  135. gearman_connection_st& operator=(const gearman_connection_st&) = delete;
  136. private:
  137. void set_host( const char *host, const char* service);
  138. void set_host( const char *host, const in_port_t port);
  139. public:
  140. const char* host(void) const
  141. {
  142. return _host;
  143. }
  144. const char* service(void) const
  145. {
  146. return _service;
  147. }
  148. gearman_return_t send_packet(const gearman_packet_st&, const bool flush_buffer);
  149. size_t send_and_flush(const void *data, size_t data_size, gearman_return_t *ret_ptr);
  150. gearman_return_t enable_ssl();
  151. private:
  152. friend void gearman_universal_st::flush();
  153. gearman_return_t flush();
  154. public:
  155. void close_socket();
  156. // Receive packet from a connection.
  157. gearman_packet_st *receiving(gearman_packet_st&,
  158. gearman_return_t& , const bool recv_data);
  159. // Receive packet data from a connection.
  160. size_t receive_data(void *data, size_t data_size, gearman_return_t&);
  161. // Set events to be watched for a connection.
  162. void set_events(short events);
  163. // Set events that are ready for a connection. This is used with the
  164. // external event callbacks.
  165. void set_revents(short revents);
  166. void reset_addrinfo();
  167. gearman_return_t lookup();
  168. void free_recv_packet();
  169. gearman_packet_st* recv_packet()
  170. {
  171. return _recv_packet;
  172. }
  173. void reset_recv_packet()
  174. {
  175. _recv_packet= NULL;
  176. }
  177. gearman_connection_st(const gearman_connection_st&);
  178. gearman_return_t send_identifier(void);
  179. private:
  180. gearman_return_t _send_packet(const gearman_packet_st&, const bool flush_buffer);
  181. gearman_return_t set_socket_options();
  182. size_t recv_socket(void *data, size_t data_size, gearman_return_t&);
  183. gearman_return_t connect_poll();
  184. gearman_packet_st *_recv_packet;
  185. };
  186. gearman_connection_st *gearman_connection_copy(gearman_universal_st& universal,
  187. const gearman_connection_st& from);
  188. /**
  189. * Create a connection structure with the given host and port.
  190. *
  191. * @param[in] gearman Structure previously initialized with gearman_create() or
  192. * gearman_clone().
  193. * @param[in] connection Caller allocated structure, or NULL to allocate one.
  194. * @param[in] host Host or IP address to connect to.
  195. * @param[in] port Port to connect to.
  196. * @return On success, a pointer to the (possibly allocated) structure. On
  197. * failure this will be NULL.
  198. */
  199. gearman_connection_st *gearman_connection_create(gearman_universal_st &universal,
  200. const char *host, const in_port_t&);
  201. gearman_connection_st *gearman_connection_create(gearman_universal_st &universal,
  202. const char* host, const char* service);