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 
38 #pragma once
39 
40 #include "libgearman-server/plugins/base.h"
41 
42 struct gearmand_io_st
43 {
44   struct {
45     bool ready;
46     bool packet_in_use;
47     bool external_fd;
48     bool ignore_lost_connection;
49     bool close_after_flush;
50   } options;
51   enum {
52     GEARMAND_CON_UNIVERSAL_INVALID,
53     GEARMAND_CON_UNIVERSAL_CONNECTED
54   } _state;
55   enum {
56     GEARMAND_CON_SEND_STATE_NONE,
57     GEARMAND_CON_SEND_UNIVERSAL_PRE_FLUSH,
58     GEARMAND_CON_SEND_UNIVERSAL_FORCE_FLUSH,
59     GEARMAND_CON_SEND_UNIVERSAL_FLUSH,
60     GEARMAND_CON_SEND_UNIVERSAL_FLUSH_DATA
61   } send_state;
62   enum {
63     GEARMAND_CON_RECV_UNIVERSAL_NONE,
64     GEARMAND_CON_RECV_UNIVERSAL_READ,
65     GEARMAND_CON_RECV_STATE_READ_DATA
66   } recv_state;
67   short events;
68   short revents;
69   int fd;
70   uint32_t created_id;
71   uint32_t created_id_next;
72   size_t send_buffer_size;
73   size_t send_data_size;
74   size_t send_data_offset;
75   size_t recv_buffer_size;
76   size_t recv_data_size;
77   size_t recv_data_offset;
78   gearmand_connection_list_st *universal;
79   gearmand_io_st *next;
80   gearmand_io_st *prev;
81   gearmand_io_st *ready_next;
82   gearmand_io_st *ready_prev;
83   gearmand_con_st *context;
84   char *send_buffer_ptr;
85   gearmand_packet_st *recv_packet;
86   char *recv_buffer_ptr;
87   gearmand_packet_st packet;
88   gearman_server_con_st *root;
89   char send_buffer[GEARMAN_SEND_BUFFER_SIZE];
90   char recv_buffer[GEARMAN_RECV_BUFFER_SIZE];
91 };
92 
93 namespace gearmand { namespace protocol {class Context; } }
94 
95 /*
96   Free list for these are stored in gearman_server_thread_st[], otherwise they are owned by gearmand_con_st[]
97   */
98 struct gearman_server_con_st
99 {
100   gearmand_io_st con;
101   bool is_sleeping;
102   bool is_exceptions;
103   bool is_dead;
104   bool is_noop_sent;
105   bool is_cleaned_up;
106   gearmand_error_t ret;
107   bool io_list;
108   bool proc_list;
109   bool proc_removed;
110   bool to_be_freed_list;
111   uint32_t io_packet_count;
112   uint32_t proc_packet_count;
113   uint32_t worker_count;
114   uint32_t client_count;
115   gearman_server_thread_st *thread;
116   gearman_server_con_st *next;
117   gearman_server_con_st *prev;
118   gearman_server_packet_st *packet;
119   gearman_server_packet_st *io_packet_list;
120   gearman_server_packet_st *io_packet_end;
121   gearman_server_packet_st *proc_packet_list;
122   gearman_server_packet_st *proc_packet_end;
123   gearman_server_con_st *io_next;
124   gearman_server_con_st *io_prev;
125   gearman_server_con_st *proc_next;
126   gearman_server_con_st *proc_prev;
127   gearman_server_con_st *to_be_freed_next;
128   gearman_server_con_st *to_be_freed_prev;
129   struct gearman_server_worker_st *worker_list;
130   struct gearman_server_client_st *client_list;
131   const char *_host; // client host
132   const char *_port; // client port
133   char id[GEARMAN_SERVER_CON_ID_SIZE];
134   gearmand::protocol::Context* protocol;
135   struct event *timeout_event;
136 
gearman_server_con_stgearman_server_con_st137   gearman_server_con_st()
138   {
139   }
140 
~gearman_server_con_stgearman_server_con_st141   ~gearman_server_con_st()
142   {
143   }
144 
set_protocolgearman_server_con_st145   void set_protocol(gearmand::protocol::Context* arg)
146   {
147     protocol= arg;
148   }
149 
protocol_releasegearman_server_con_st150   void protocol_release()
151   {
152     if (protocol)
153     {
154       protocol->notify(this);
155       if (protocol->is_owner())
156       {
157         delete protocol;
158         protocol= NULL;
159       }
160       protocol= NULL;
161     }
162   }
163 };
164