io.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
  2. *
  3. * Gearmand client and server library.
  4. *
  5. * Copyright (C) 2011 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/plugins/base.h"
  39. #include "libgearman/ssl.h"
  40. struct gearmand_io_st
  41. {
  42. struct {
  43. bool ready;
  44. bool packet_in_use;
  45. bool external_fd;
  46. bool ignore_lost_connection;
  47. bool close_after_flush;
  48. } options;
  49. enum {
  50. GEARMAND_CON_UNIVERSAL_INVALID,
  51. GEARMAND_CON_UNIVERSAL_CONNECTED
  52. } _state;
  53. enum {
  54. GEARMAND_CON_SEND_STATE_NONE,
  55. GEARMAND_CON_SEND_UNIVERSAL_PRE_FLUSH,
  56. GEARMAND_CON_SEND_UNIVERSAL_FORCE_FLUSH,
  57. GEARMAND_CON_SEND_UNIVERSAL_FLUSH,
  58. GEARMAND_CON_SEND_UNIVERSAL_FLUSH_DATA
  59. } send_state;
  60. enum {
  61. GEARMAND_CON_RECV_UNIVERSAL_NONE,
  62. GEARMAND_CON_RECV_UNIVERSAL_READ,
  63. GEARMAND_CON_RECV_STATE_READ_DATA
  64. } recv_state;
  65. short events;
  66. short revents;
  67. private:
  68. int _fd;
  69. public:
  70. gearmand_error_t set_fd(const int fd_)
  71. {
  72. options.external_fd= true;
  73. _fd= fd_;
  74. _state= gearmand_io_st::GEARMAND_CON_UNIVERSAL_CONNECTED;
  75. return _io_setsockopt();
  76. }
  77. gearmand_error_t _io_setsockopt();
  78. int fd() const
  79. {
  80. return _fd;
  81. }
  82. bool has_fd() const
  83. {
  84. return _fd != INVALID_SOCKET;
  85. }
  86. void clear()
  87. {
  88. _fd= INVALID_SOCKET;
  89. events= 0;
  90. revents= 0;
  91. }
  92. uint32_t created_id;
  93. uint32_t created_id_next;
  94. size_t send_buffer_size;
  95. size_t send_data_size;
  96. size_t send_data_offset;
  97. size_t recv_buffer_size;
  98. size_t recv_data_size;
  99. size_t recv_data_offset;
  100. gearmand_connection_list_st *universal;
  101. gearmand_io_st *next;
  102. gearmand_io_st *prev;
  103. gearmand_io_st *ready_next;
  104. gearmand_io_st *ready_prev;
  105. gearmand_con_st *context;
  106. char *send_buffer_ptr;
  107. gearmand_packet_st *recv_packet;
  108. char *recv_buffer_ptr;
  109. gearmand_packet_st packet;
  110. gearman_server_con_st *root;
  111. char send_buffer[GEARMAND_SEND_BUFFER_SIZE];
  112. char recv_buffer[GEARMAND_RECV_BUFFER_SIZE];
  113. gearmand_io_st() { }
  114. const char* host() const;
  115. const char* port() const;
  116. #if 0
  117. void close_socket();
  118. #endif
  119. };
  120. namespace gearmand { namespace protocol {class Context; } }
  121. /*
  122. Free list for these are stored in gearman_server_thread_st[], otherwise they are owned by gearmand_con_st[]
  123. */
  124. struct gearman_server_con_st
  125. {
  126. gearmand_io_st con;
  127. bool is_sleeping;
  128. bool is_exceptions;
  129. bool is_dead;
  130. bool is_noop_sent;
  131. bool is_cleaned_up;
  132. gearmand_error_t ret;
  133. bool io_list;
  134. bool proc_list;
  135. bool proc_removed;
  136. bool to_be_freed_list;
  137. uint32_t io_packet_count;
  138. uint32_t proc_packet_count;
  139. uint32_t worker_count;
  140. uint32_t client_count;
  141. gearman_server_thread_st *thread;
  142. gearman_server_con_st *next;
  143. gearman_server_con_st *prev;
  144. gearman_server_packet_st *packet;
  145. gearman_server_packet_st *io_packet_list;
  146. gearman_server_packet_st *io_packet_end;
  147. gearman_server_packet_st *proc_packet_list;
  148. gearman_server_packet_st *proc_packet_end;
  149. gearman_server_con_st *io_next;
  150. gearman_server_con_st *io_prev;
  151. gearman_server_con_st *proc_next;
  152. gearman_server_con_st *proc_prev;
  153. gearman_server_con_st *to_be_freed_next;
  154. gearman_server_con_st *to_be_freed_prev;
  155. struct gearman_server_worker_st *worker_list;
  156. struct gearman_server_client_st *client_list;
  157. const char *_host; // client host
  158. const char *_port; // client port
  159. char id[GEARMAND_SERVER_CON_ID_SIZE];
  160. gearmand::protocol::Context* protocol;
  161. struct event *timeout_event;
  162. SSL* _ssl;
  163. gearman_server_con_st()
  164. {
  165. }
  166. ~gearman_server_con_st()
  167. {
  168. }
  169. const char* host() const
  170. {
  171. if (_host)
  172. {
  173. return _host;
  174. }
  175. return "-";
  176. }
  177. const char* port() const
  178. {
  179. if (_port)
  180. {
  181. return _port;
  182. }
  183. return "-";
  184. }
  185. void set_protocol(gearmand::protocol::Context* arg)
  186. {
  187. protocol= arg;
  188. }
  189. void protocol_release()
  190. {
  191. if (protocol)
  192. {
  193. protocol->notify(this);
  194. if (protocol->is_owner())
  195. {
  196. delete protocol;
  197. protocol= NULL;
  198. }
  199. protocol= NULL;
  200. }
  201. }
  202. };