~brianaker/gearmand/osx9-fixes

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/*  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
 * 
 *  Gearmand client and server library.
 *
 *  Copyright (C) 2011-2013 Data Differential, http://datadifferential.com/
 *  Copyright (C) 2008 Brian Aker, Eric Day
 *  All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are
 *  met:
 *
 *      * Redistributions of source code must retain the above copyright
 *  notice, this list of conditions and the following disclaimer.
 *
 *      * Redistributions in binary form must reproduce the above
 *  copyright notice, this list of conditions and the following disclaimer
 *  in the documentation and/or other materials provided with the
 *  distribution.
 *
 *      * The names of its contributors may not be used to endorse or
 *  promote products derived from this software without specific prior
 *  written permission.
 *
 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 *  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 *  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 *  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 *  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 */

#pragma once

#include <libgearman-1.0/connection.h>
#include "libgearman/interface/packet.hpp"
#include "libgearman/interface/universal.hpp"

#include "libgearman/ssl.h"

struct gearman_connection_st
{
  struct Options {
    bool server_options_sent;
    bool identifier_sent;
    bool ready;
    bool packet_in_use;

    Options() :
      server_options_sent(false),
      identifier_sent(false),
      ready(false),
      packet_in_use(false)
    { }
  } options;
  enum gearman_con_universal_t state;
  enum gearman_con_send_t send_state;
  enum gearman_con_recv_t recv_state;

  void set_pollfd(struct pollfd& pollfd_)
  {
    pollfd_.fd= socket_descriptor();
    pollfd_.events= _events;
    pollfd_.revents= 0;
  }

  bool events()
  {
    return _events;
  }

  bool is_events(int events_)
  {
    if (_events & events_)
    {
      return true;
    }

    return false;
  }

  bool is_revents(int revents_)
  {
    if (_revents & revents_)
    {
      return true;
    }

    return false;
  }

private:
  short _events;
  short _revents;
  int fd;
  SSL* _ssl;
  int cached_errno;

public:
  uint32_t created_id;
  uint32_t created_id_next;
  size_t send_buffer_size;
  size_t send_data_size;
  size_t send_data_offset;
  size_t recv_buffer_size;
  size_t recv_data_size;
  size_t recv_data_offset;

private:
  gearman_universal_st &universal;
  gearman_connection_st *next;
  gearman_connection_st *prev;
  void *context;
  struct addrinfo *_addrinfo;
  struct addrinfo *addrinfo_next;

public:
  const char *send_buffer_ptr;
  char *recv_buffer_ptr;
  gearman_packet_st _packet;
  char _host[GEARMAN_NI_MAXHOST];
  char _service[GEARMAN_NI_MAXSERV];
  char send_buffer[GEARMAN_SEND_BUFFER_SIZE];
  char recv_buffer[GEARMAN_RECV_BUFFER_SIZE];

  gearman_connection_st* next_connection(void)
  {
    return next;
  }

  int socket_descriptor() const
  {
    return fd;
  }

  int socket_descriptor_is_valid() const
  {
    return fd != INVALID_SOCKET;
  }

  void error(int errno_)
  {
    cached_errno= errno_;
  }

  void free_private_packet();

  gearman_connection_st(gearman_universal_st& universal_arg, const char*, const char*);

  ~gearman_connection_st();

private:
  void set_host( const char *host, const char* service);
  void set_host( const char *host, const in_port_t port);

public:

  const char* host(void) const
  {
    return _host;
  }

  const char* service(void) const
  {
    return _service;
  }

  gearman_return_t send_packet(const gearman_packet_st&, const bool flush_buffer);
  size_t send_and_flush(const void *data, size_t data_size, gearman_return_t *ret_ptr);

  gearman_return_t enable_ssl();
private:
  friend void gearman_universal_st::flush();
  gearman_return_t flush();

public:
  void close_socket();

  // Receive packet from a connection.
  gearman_packet_st *receiving(gearman_packet_st&,
                               gearman_return_t& , const bool recv_data);

  // Receive packet data from a connection.
  size_t receive_data(void *data, size_t data_size, gearman_return_t&);

  // Set events to be watched for a connection.
  void set_events(short events);

 // Set events that are ready for a connection. This is used with the
 // external event callbacks.
  void set_revents(short revents);

  void reset_addrinfo();

  gearman_return_t lookup();

  void free_recv_packet();
  gearman_packet_st* recv_packet()
  {
    return _recv_packet;
  }

  void reset_recv_packet()
  {
    _recv_packet= NULL;
  }

  gearman_connection_st(const gearman_connection_st&);

  gearman_return_t send_identifier(void);

private:
  gearman_return_t _send_packet(const gearman_packet_st&, const bool flush_buffer);
  gearman_return_t set_socket_options();
  size_t recv_socket(void *data, size_t data_size, gearman_return_t&);
  gearman_return_t connect_poll();

  gearman_packet_st *_recv_packet;
};

gearman_connection_st *gearman_connection_copy(gearman_universal_st& universal,
                                               const gearman_connection_st& from);

/**
 * Create a connection structure with the given host and port.
 *
 * @param[in] gearman Structure previously initialized with gearman_create() or
 *  gearman_clone().
 * @param[in] connection Caller allocated structure, or NULL to allocate one.
 * @param[in] host Host or IP address to connect to.
 * @param[in] port Port to connect to.
 * @return On success, a pointer to the (possibly allocated) structure. On
 *  failure this will be NULL.
 */
gearman_connection_st *gearman_connection_create(gearman_universal_st &universal,
                                                 const char *host, const in_port_t&);

gearman_connection_st *gearman_connection_create(gearman_universal_st &universal,
                                                 const char* host, const char* service);