io.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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{nullptr};
  101. gearmand_io_st *next{nullptr};
  102. gearmand_io_st *prev{nullptr};
  103. gearmand_io_st *ready_next{nullptr};
  104. gearmand_io_st *ready_prev{nullptr};
  105. gearmand_con_st *context{nullptr};
  106. char *send_buffer_ptr{nullptr};
  107. gearmand_packet_st *recv_packet{nullptr};
  108. char *recv_buffer_ptr{nullptr};
  109. gearmand_packet_st packet{};
  110. gearman_server_con_st *root{nullptr};
  111. char send_buffer[GEARMAND_SEND_BUFFER_SIZE];
  112. char recv_buffer[GEARMAND_RECV_BUFFER_SIZE];
  113. gearmand_io_st() {
  114. this->options = {};
  115. this->_state = GEARMAND_CON_UNIVERSAL_INVALID;
  116. this->send_state = GEARMAND_CON_SEND_STATE_NONE;
  117. this->recv_state = GEARMAND_CON_RECV_UNIVERSAL_NONE;
  118. }
  119. const char* host() const;
  120. const char* port() const;
  121. #if 0
  122. void close_socket();
  123. #endif
  124. };
  125. namespace gearmand { namespace protocol {class Context; } }
  126. /*
  127. Free list for these are stored in gearman_server_thread_st[], otherwise they are owned by gearmand_con_st[]
  128. */
  129. struct gearman_server_con_st
  130. {
  131. gearmand_io_st con;
  132. bool is_sleeping{};
  133. bool is_exceptions{};
  134. bool is_dead{};
  135. bool is_noop_sent{};
  136. bool is_cleaned_up{};
  137. gearmand_error_t ret{};
  138. bool io_list{};
  139. bool proc_list{};
  140. bool proc_removed{};
  141. bool to_be_freed_list{};
  142. uint32_t io_packet_count{};
  143. uint32_t proc_packet_count{};
  144. uint32_t worker_count{};
  145. uint32_t client_count{};
  146. gearman_server_thread_st *thread{nullptr};
  147. gearman_server_con_st *next{nullptr};
  148. gearman_server_con_st *prev{nullptr};
  149. gearman_server_packet_st *packet{nullptr};
  150. gearman_server_packet_st *io_packet_list{nullptr};
  151. gearman_server_packet_st *io_packet_end{nullptr};
  152. gearman_server_packet_st *proc_packet_list{nullptr};
  153. gearman_server_packet_st *proc_packet_end{nullptr};
  154. gearman_server_con_st *io_next{nullptr};
  155. gearman_server_con_st *io_prev{nullptr};
  156. gearman_server_con_st *proc_next{nullptr};
  157. gearman_server_con_st *proc_prev{nullptr};
  158. gearman_server_con_st *to_be_freed_next{nullptr};
  159. gearman_server_con_st *to_be_freed_prev{nullptr};
  160. struct gearman_server_worker_st *worker_list{nullptr};
  161. struct gearman_server_client_st *client_list{nullptr};
  162. const char *_host{nullptr}; // client host
  163. const char *_port{nullptr}; // client port
  164. char id[GEARMAND_SERVER_CON_ID_SIZE];
  165. gearmand::protocol::Context* protocol{nullptr};
  166. struct event *timeout_event{nullptr};
  167. SSL* _ssl{nullptr};
  168. gearman_server_con_st()
  169. {
  170. }
  171. ~gearman_server_con_st()
  172. {
  173. }
  174. const char* host() const
  175. {
  176. if (_host)
  177. {
  178. return _host;
  179. }
  180. return "-";
  181. }
  182. const char* port() const
  183. {
  184. if (_port)
  185. {
  186. return _port;
  187. }
  188. return "-";
  189. }
  190. void set_protocol(gearmand::protocol::Context* arg)
  191. {
  192. protocol= arg;
  193. }
  194. void protocol_release()
  195. {
  196. if (protocol)
  197. {
  198. protocol->notify(this);
  199. if (protocol->is_owner())
  200. {
  201. delete protocol;
  202. }
  203. protocol= nullptr;
  204. }
  205. }
  206. };