~brianaker/gearmand/apache-config

« back to all changes in this revision

Viewing changes to libgearman/connection.cc

Merge lp:~brianaker/gearmand/connection-less-error-prone Build: jenkins-Gearmand-827

Show diffs side-by-side

added added

removed removed

Lines of Context:
88
88
 * @{
89
89
 */
90
90
 
91
 
gearman_connection_st::gearman_connection_st(gearman_universal_st &universal_arg) :
 
91
gearman_connection_st::gearman_connection_st(gearman_universal_st &universal_arg, const char* host_, const char* service_):
92
92
  state(GEARMAN_CON_UNIVERSAL_ADDRINFO),
93
93
  send_state(GEARMAN_CON_SEND_STATE_NONE),
94
94
  recv_state(GEARMAN_CON_RECV_UNIVERSAL_NONE),
114
114
  send_buffer_ptr(NULL),
115
115
  _recv_packet(NULL)
116
116
{
 
117
  set_host(host_, service_);
 
118
 
117
119
  if (universal.con_list)
118
120
  {
119
121
    universal.con_list->prev= this;
124
126
 
125
127
  send_buffer_ptr= send_buffer;
126
128
  recv_buffer_ptr= recv_buffer;
127
 
  _host[0]= 0;
128
 
  _service[0]= 0;
129
129
}
130
130
 
131
 
/**
132
 
 * Initialize a connection structure. Always check the return value even if
133
 
 * passing in a pre-allocated structure. Some other initialization may have
134
 
 * failed.
135
 
 */
136
 
static gearman_connection_st *__connection_create(gearman_universal_st &universal)
 
131
gearman_connection_st *gearman_connection_create(gearman_universal_st& universal,
 
132
                                                 const char *host_, const char* service_)
137
133
{
138
 
  gearman_connection_st *connection= new (std::nothrow) gearman_connection_st(universal);
139
 
  if (connection == NULL)
 
134
  gearman_connection_st *connection= new (std::nothrow) gearman_connection_st(universal, host_, service_);
 
135
  if (connection)
 
136
  {
 
137
    if (gearman_failed(connection->lookup()))
 
138
    {
 
139
      delete connection;
 
140
      return NULL;
 
141
    }
 
142
  }
 
143
  else
140
144
  {
141
145
    gearman_perror(universal, errno, "Failed at new() gearman_connection_st new");
142
146
  }
145
149
}
146
150
 
147
151
gearman_connection_st *gearman_connection_create(gearman_universal_st& universal,
148
 
                                                 const char *host, const char* service_)
149
 
{
150
 
  gearman_connection_st *connection= __connection_create(universal);
151
 
  if (connection)
152
 
  {
153
 
    connection->set_host(host, (const char*)service_);
154
 
 
155
 
    if (gearman_failed(connection->lookup()))
156
 
    {
157
 
      delete connection;
158
 
      return NULL;
159
 
    }
160
 
  }
161
 
 
162
 
  return connection;
163
 
}
164
 
 
165
 
gearman_connection_st *gearman_connection_create(gearman_universal_st& universal,
166
 
                                                 const char *host, const in_port_t& port)
167
 
{
168
 
  gearman_connection_st *connection= __connection_create(universal);
169
 
  if (connection)
170
 
  {
171
 
    connection->set_host(host, port);
172
 
 
173
 
    if (gearman_failed(connection->lookup()))
174
 
    {
175
 
      delete connection;
176
 
      return NULL;
177
 
    }
178
 
  }
179
 
 
180
 
  return connection;
 
152
                                                 const char *host, const in_port_t& port_)
 
153
{
 
154
  const char *service_ptr= NULL;
 
155
  char service[GEARMAN_NI_MAXSERV];
 
156
 
 
157
  if (port_ < 1)
 
158
  {
 
159
    service_ptr= GEARMAN_DEFAULT_TCP_PORT_STRING;
 
160
  }
 
161
  else
 
162
  {
 
163
    snprintf(service, sizeof(service), "%hu", uint16_t(port_));
 
164
    service[GEARMAN_NI_MAXSERV -1]= 0;
 
165
 
 
166
    service_ptr= service;
 
167
  }
 
168
 
 
169
  return gearman_connection_create(universal, host, service_ptr);
181
170
}
182
171
 
183
172
gearman_connection_st *gearman_connection_copy(gearman_universal_st& universal,
184
173
                                               const gearman_connection_st& from)
185
174
{
186
 
  gearman_connection_st *connection= __connection_create(universal);
187
 
 
 
175
  gearman_connection_st *connection= new (std::nothrow) gearman_connection_st(universal, from.host(), from.service());
188
176
  if (connection)
189
177
  {
190
178
    connection->options.ready= from.options.ready;
194
182
    strcpy(connection->_host, from._host);
195
183
    strcpy(connection->_service, from._service);
196
184
  }
 
185
  else
 
186
  {
 
187
    gearman_perror(universal, errno, "Failed at new() gearman_connection_st new");
 
188
  }
197
189
 
198
190
  return connection;
199
191
}
250
242
  else
251
243
  {
252
244
    snprintf(_service, sizeof(_service), "%hu", uint16_t(port_));
253
 
    _service[GEARMAN_NI_MAXSERV - 1]= 0;
 
245
    _service[GEARMAN_NI_MAXSERV -1]= 0;
254
246
 
255
247
    set_host(host_, _service);
256
248
  }
267
259
    host_= GEARMAN_DEFAULT_TCP_HOST;
268
260
  }
269
261
  strncpy(_host, host_, GEARMAN_NI_MAXHOST);
270
 
  _host[GEARMAN_NI_MAXHOST - 1]= 0;
 
262
  _host[GEARMAN_NI_MAXHOST -1]= 0;
271
263
 
272
 
  if (service_ != _service)
 
264
  if (service_ and service_[0])
 
265
  { }
 
266
  else
273
267
  {
274
 
    if (service_)
275
 
    {
276
 
      size_t string_len= strlen(service_);
277
 
      if (string_len == 0)
278
 
      {
279
 
        service_= GEARMAN_DEFAULT_TCP_PORT_STRING;
280
 
      }
281
 
    }
282
 
    else
283
 
    {
284
 
      service_= GEARMAN_DEFAULT_TCP_PORT_STRING;
285
 
    }
286
 
    strncpy(_service, service_, GEARMAN_NI_MAXSERV);
287
 
    _service[GEARMAN_NI_MAXSERV - 1]= 0;
 
268
    service_= GEARMAN_DEFAULT_TCP_PORT_STRING;
288
269
  }
 
270
  strncpy(_service, service_, GEARMAN_NI_MAXSERV);
 
271
  _service[GEARMAN_NI_MAXSERV -1]= 0;
289
272
}
290
273
 
291
274
/*