| 1 |
/* |
| 2 |
* |
| 3 |
* Licensed to the Apache Software Foundation (ASF) under one |
| 4 |
* or more contributor license agreements. See the NOTICE file |
| 5 |
* distributed with this work for additional information |
| 6 |
* regarding copyright ownership. The ASF licenses this file |
| 7 |
* to you under the Apache License, Version 2.0 (the |
| 8 |
* "License"); you may not use this file except in compliance |
| 9 |
* with the License. You may obtain a copy of the License at |
| 10 |
* |
| 11 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 12 |
* |
| 13 |
* Unless required by applicable law or agreed to in writing, |
| 14 |
* software distributed under the License is distributed on an |
| 15 |
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 16 |
* KIND, either express or implied. See the License for the |
| 17 |
* specific language governing permissions and limitations |
| 18 |
* under the License. |
| 19 |
* |
| 20 |
*/ |
| 21 |
|
| 22 |
/* Enable POSIX features for uv.h */ |
| 23 |
#ifndef _POSIX_C_SOURCE |
| 24 |
#define _POSIX_C_SOURCE 200809L |
| 25 |
#endif |
| 26 |
|
| 27 |
#include "core/logger_private.h" |
| 28 |
#include "proactor-internal.h" |
| 29 |
|
| 30 |
#include <proton/condition.h> |
| 31 |
#include <proton/connection_driver.h> |
| 32 |
#include <proton/engine.h> |
| 33 |
#include <proton/listener.h> |
| 34 |
#include <proton/message.h> |
| 35 |
#include <proton/netaddr.h> |
| 36 |
#include <proton/proactor.h> |
| 37 |
#include <proton/raw_connection.h> |
| 38 |
#include <proton/transport.h> |
| 39 |
|
| 40 |
#include <uv.h> |
| 41 |
#include "netaddr-internal.h" /* Include after socket headers via uv.h */ |
| 42 |
|
| 43 |
/* All asserts are cheap and should remain in a release build for debuggability */ |
| 44 |
#undef NDEBUG |
| 45 |
#include <assert.h> |
| 46 |
|
| 47 |
#include <stddef.h> |
| 48 |
#include <stdio.h> |
| 49 |
#include <stdlib.h> |
| 50 |
#include <string.h> |
| 51 |
|
| 52 |
/* |
| 53 |
libuv functions are thread unsafe, we use a"leader-worker-follower" model as follows: |
| 54 |
|
| 55 |
- At most one thread at a time is the "leader". The leader runs the UV loop till there |
| 56 |
are events to process and then becomes a "worker" |
| 57 |
|
| 58 |
- Concurrent "worker" threads process events for separate connections or listeners. |
| 59 |
When they run out of work they become "followers" |
| 60 |
|
| 61 |
- A "follower" is idle, waiting for work. When the leader becomes a worker, one follower |
| 62 |
takes over as the new leader. |
| 63 |
|
| 64 |
Any thread that calls pn_proactor_wait() or pn_proactor_get() can take on any of the |
| 65 |
roles as required at run-time. Monitored sockets (connections or listeners) are passed |
| 66 |
between threads on thread-safe queues. |
| 67 |
|
| 68 |
Function naming: |
| 69 |
- on_*() - libuv callbacks, called in leader thread via uv_run(). |
| 70 |
- leader_* - only called in leader thread from |
| 71 |
- *_lh - called with the relevant lock held |
| 72 |
*/ |
| 73 |
|
| 74 |
const char *AMQP_PORT = "5672"; |
| 75 |
const char *AMQP_PORT_NAME = "amqp"; |
| 76 |
|
| 77 |
/* pn_proactor_t and pn_listener_t are plain C structs with normal memory management. |
| 78 |
CLASSDEF is for identification when used as a pn_event_t context. |
| 79 |
*/ |
| 80 |
PN_STRUCT_CLASSDEF(pn_proactor) |
| 81 |
PN_STRUCT_CLASSDEF(pn_listener) |
| 82 |
|
| 83 |
|
| 84 |
/* ================ Queues ================ */ |
| 85 |
static int unqueued; /* Provide invalid address for _unqueued pointers */ |
| 86 |
|
| 87 |
#define QUEUE_DECL(T) \ |
| 88 |
typedef struct T##_queue_t { T##_t *front, *back; } T##_queue_t; \ |
| 89 |
\ |
| 90 |
static T##_t *T##_unqueued = (T##_t*)&unqueued; \ |
| 91 |
\ |
| 92 |
static void T##_push(T##_queue_t *q, T##_t *x) { \ |
| 93 |
assert(x->next == T##_unqueued); \ |
| 94 |
x->next = NULL; \ |
| 95 |
if (!q->front) { \ |
| 96 |
q->front = q->back = x; \ |
| 97 |
} else { \ |
| 98 |
q->back->next = x; \ |
| 99 |
q->back = x; \ |
| 100 |
} \ |
| 101 |
} \ |
| 102 |
\ |
| 103 |
static T##_t* T##_pop(T##_queue_t *q) { \ |
| 104 |
T##_t *x = q->front; \ |
| 105 |
if (x) { \ |
| 106 |
q->front = x->next; \ |
| 107 |
x->next = T##_unqueued; \ |
| 108 |
} \ |
| 109 |
return x; \ |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
/* All work structs and UV callback data structs start with a struct_type member */ |
| 114 |
typedef enum { T_CONNECTION, T_LISTENER, T_LSOCKET } struct_type; |
| 115 |
|
| 116 |
/* A stream of serialized work for the proactor */ |
| 117 |
typedef struct work_t { |
| 118 |
/* Immutable */ |
| 119 |
struct_type type; |
| 120 |
pn_proactor_t *proactor; |
| 121 |
|
| 122 |
/* Protected by proactor.lock */ |
| 123 |
struct work_t* next; |
| 124 |
bool working; /* Owned by a worker thread */ |
| 125 |
} work_t; |
| 126 |
|
| 127 |
QUEUE_DECL(work) |
| 128 |
|
| 129 |
static void work_init(work_t* w, pn_proactor_t* p, struct_type type) { |
| 130 |
w->proactor = p; |
| 131 |
w->next = work_unqueued; |
| 132 |
w->type = type; |
| 133 |
w->working = true; |
| 134 |
} |
| 135 |
|
| 136 |
/* ================ IO ================ */ |
| 137 |
|
| 138 |
/* A resolvable address */ |
| 139 |
typedef struct addr_t { |
| 140 |
char addr_buf[PN_MAX_ADDR]; |
| 141 |
const char *host, *port; |
| 142 |
uv_getaddrinfo_t getaddrinfo; /* UV getaddrinfo request, contains list of addrinfo */ |
| 143 |
struct addrinfo* addrinfo; /* The current addrinfo being tried */ |
| 144 |
} addr_t; |
| 145 |
|
| 146 |
/* A single listening socket, a listener can have more than one */ |
| 147 |
typedef struct lsocket_t { |
| 148 |
struct_type type; /* Always T_LSOCKET */ |
| 149 |
pn_listener_t *parent; |
| 150 |
uv_tcp_t tcp; |
| 151 |
struct lsocket_t *next; |
| 152 |
} lsocket_t; |
| 153 |
|
| 154 |
PN_STRUCT_CLASSDEF(pn_listener_socket) |
| 155 |
|
| 156 |
typedef enum { W_NONE, W_PENDING, W_CLOSED } wake_state; |
| 157 |
|
| 158 |
/* An incoming or outgoing connection. */ |
| 159 |
typedef struct pconnection_t { |
| 160 |
work_t work; /* Must be first to allow casting */ |
| 161 |
struct pconnection_t *next; /* For listener list */ |
| 162 |
|
| 163 |
/* Only used by owner thread */ |
| 164 |
pn_connection_driver_t driver; |
| 165 |
pn_event_batch_t batch; |
| 166 |
|
| 167 |
/* Only used by leader */ |
| 168 |
uv_tcp_t tcp; |
| 169 |
addr_t addr; |
| 170 |
|
| 171 |
uv_connect_t connect; /* Outgoing connection only */ |
| 172 |
int connected; /* 0: not connected, <0: connecting after error, 1 = connected ok */ |
| 173 |
|
| 174 |
lsocket_t *lsocket; /* Incoming connection only */ |
| 175 |
|
| 176 |
struct pn_netaddr_t local, remote; /* Actual addresses */ |
| 177 |
uv_timer_t timer; |
| 178 |
uv_write_t write; |
| 179 |
size_t writing; /* size of pending write request, 0 if none pending */ |
| 180 |
bool read_started; |
| 181 |
uv_shutdown_t shutdown; |
| 182 |
|
| 183 |
/* Locked for thread-safe access */ |
| 184 |
uv_mutex_t lock; |
| 185 |
wake_state wake; |
| 186 |
} pconnection_t; |
| 187 |
|
| 188 |
QUEUE_DECL(pconnection) |
| 189 |
|
| 190 |
typedef enum { |
| 191 |
L_UNINIT, /**<< Not yet listening */ |
| 192 |
L_LISTENING, /**<< Listening */ |
| 193 |
L_CLOSE, /**<< Close requested */ |
| 194 |
L_CLOSING, /**<< Socket close initiated, wait for all to close */ |
| 195 |
L_CLOSED /**<< User saw PN_LISTENER_CLOSED, all done */ |
| 196 |
} listener_state; |
| 197 |
|
| 198 |
/* A listener */ |
| 199 |
struct pn_listener_t { |
| 200 |
work_t work; /* Must be first to allow casting */ |
| 201 |
|
| 202 |
/* Only used by owner thread */ |
| 203 |
pn_event_batch_t batch; |
| 204 |
pn_record_t *attachments; |
| 205 |
void *context; |
| 206 |
size_t backlog; |
| 207 |
|
| 208 |
/* Only used by leader */ |
| 209 |
addr_t addr; |
| 210 |
lsocket_t *lsockets; |
| 211 |
int dynamic_port; /* Record dynamic port from first bind(0) */ |
| 212 |
|
| 213 |
/* Invariant listening addresses allocated during leader_listen_lh() */ |
| 214 |
struct pn_netaddr_t *addrs; |
| 215 |
int addrs_len; |
| 216 |
|
| 217 |
/* Locked for thread-safe access. uv_listen can't be stopped or cancelled so we can't |
| 218 |
* detach a listener from the UV loop to prevent concurrent access. |
| 219 |
*/ |
| 220 |
uv_mutex_t lock; |
| 221 |
pn_condition_t *condition; |
| 222 |
pn_collector_t *collector; |
| 223 |
pconnection_queue_t accept; /* pconnection_t list for accepting */ |
| 224 |
listener_state state; |
| 225 |
}; |
| 226 |
|
| 227 |
typedef enum { TM_NONE, TM_REQUEST, TM_PENDING, TM_FIRED } timeout_state_t; |
| 228 |
|
| 229 |
struct pn_proactor_t { |
| 230 |
/* Notification */ |
| 231 |
uv_async_t notify; |
| 232 |
uv_async_t interrupt; |
| 233 |
|
| 234 |
/* Leader thread */ |
| 235 |
uv_cond_t cond; |
| 236 |
uv_loop_t loop; |
| 237 |
uv_timer_t timer; |
| 238 |
|
| 239 |
/* Owner thread: proactor collector and batch can belong to leader or a worker */ |
| 240 |
pn_collector_t *collector; |
| 241 |
pn_event_batch_t batch; |
| 242 |
|
| 243 |
/* Protected by lock */ |
| 244 |
uv_mutex_t lock; |
| 245 |
work_queue_t worker_q; /* ready for work, to be returned via pn_proactor_wait() */ |
| 246 |
work_queue_t leader_q; /* waiting for attention by the leader thread */ |
| 247 |
timeout_state_t timeout_state; |
| 248 |
pn_millis_t timeout; |
| 249 |
size_t active; /* connection/listener count for INACTIVE events */ |
| 250 |
pn_condition_t *disconnect_cond; /* disconnect condition */ |
| 251 |
|
| 252 |
bool has_leader; /* A thread is working as leader */ |
| 253 |
bool disconnect; /* disconnect requested */ |
| 254 |
bool batch_working; /* batch is being processed in a worker thread */ |
| 255 |
bool need_interrupt; /* Need a PN_PROACTOR_INTERRUPT event */ |
| 256 |
bool need_inactive; /* need INACTIVE event */ |
| 257 |
bool timeout_processed; |
| 258 |
}; |
| 259 |
|
| 260 |
|
| 261 |
/* Notify the leader thread that there is something to do outside of uv_run() */ |
| 262 |
static inline void notify(pn_proactor_t* p) { |
| 263 |
uv_async_send(&p->notify); |
| 264 |
} |
| 265 |
|
| 266 |
/* Set the interrupt flag in the leader thread to avoid race conditions. */ |
| 267 |
void on_interrupt(uv_async_t *async) { |
| 268 |
if (async->data) { |
| 269 |
pn_proactor_t *p = (pn_proactor_t*)async->data; |
| 270 |
p->need_interrupt = true; |
| 271 |
} |
| 272 |
} |
| 273 |
|
| 274 |
/* Notify that this work item needs attention from the leader at the next opportunity */ |
| 275 |
static void work_notify(work_t *w) { |
| 276 |
uv_mutex_lock(&w->proactor->lock); |
| 277 |
/* If the socket is in use by a worker or is already queued then leave it where it is. |
| 278 |
It will be processed in pn_proactor_done() or when the queue it is on is processed. |
| 279 |
*/ |
| 280 |
if (!w->working && w->next == work_unqueued) { |
| 281 |
work_push(&w->proactor->leader_q, w); |
| 282 |
notify(w->proactor); |
| 283 |
} |
| 284 |
uv_mutex_unlock(&w->proactor->lock); |
| 285 |
} |
| 286 |
|
| 287 |
/* Notify the leader of a newly-created work item */ |
| 288 |
static void work_start(work_t *w) { |
| 289 |
uv_mutex_lock(&w->proactor->lock); |
| 290 |
if (w->next == work_unqueued) { /* No-op if already queued */ |
| 291 |
w->working = false; |
| 292 |
work_push(&w->proactor->leader_q, w); |
| 293 |
notify(w->proactor); |
| 294 |
uv_mutex_unlock(&w->proactor->lock); |
| 295 |
} |
| 296 |
} |
| 297 |
|
| 298 |
static void parse_addr(addr_t *addr, const char *str) { |
| 299 |
pni_parse_addr(str, addr->addr_buf, sizeof(addr->addr_buf), &addr->host, &addr->port); |
| 300 |
} |
| 301 |
|
| 302 |
/* Protect read/update of pn_connnection_t pointer to it's pconnection_t |
| 303 |
* |
| 304 |
* Global because pn_connection_wake()/pn_connection_proactor() navigate from |
| 305 |
* the pn_connection_t before we know the proactor or driver. Critical sections |
| 306 |
* are small: only get/set of the pn_connection_t driver pointer. |
| 307 |
* |
| 308 |
* TODO: replace mutex with atomic load/store |
| 309 |
*/ |
| 310 |
static pthread_mutex_t driver_ptr_mutex; |
| 311 |
|
| 312 |
static uv_once_t global_init_once = UV_ONCE_INIT; |
| 313 |
static void global_init_fn(void) { /* Call via uv_once(&global_init_once, global_init_fn) */ |
| 314 |
uv_mutex_init(&driver_ptr_mutex); |
| 315 |
} |
| 316 |
|
| 317 |
static pconnection_t *get_pconnection(pn_connection_t* c) { |
| 318 |
if (!c) return NULL; |
| 319 |
uv_mutex_lock(&driver_ptr_mutex); |
| 320 |
pn_connection_driver_t *d = *pn_connection_driver_ptr(c); |
| 321 |
uv_mutex_unlock(&driver_ptr_mutex); |
| 322 |
if (!d) return NULL; |
| 323 |
return containerof(d, pconnection_t, driver); |
| 324 |
} |
| 325 |
|
| 326 |
static void set_pconnection(pn_connection_t* c, pconnection_t *pc) { |
| 327 |
uv_mutex_lock(&driver_ptr_mutex); |
| 328 |
*pn_connection_driver_ptr(c) = pc ? &pc->driver : NULL; |
| 329 |
uv_mutex_unlock(&driver_ptr_mutex); |
| 330 |
} |
| 331 |
|
| 332 |
static pn_event_t *pconnection_batch_next(pn_event_batch_t *batch); |
| 333 |
|
| 334 |
static pconnection_t *pconnection(pn_proactor_t *p, pn_connection_t *c, pn_transport_t *t, bool server) { |
| 335 |
pconnection_t *pc = (pconnection_t*)calloc(1, sizeof(*pc)); |
| 336 |
if (!pc || pn_connection_driver_init(&pc->driver, c, t) != 0) { |
| 337 |
return NULL; |
| 338 |
} |
| 339 |
work_init(&pc->work, p, T_CONNECTION); |
| 340 |
pc->batch.next_event = pconnection_batch_next; |
| 341 |
pc->next = pconnection_unqueued; |
| 342 |
pc->write.data = &pc->work; |
| 343 |
uv_mutex_init(&pc->lock); |
| 344 |
if (server) { |
| 345 |
pn_transport_set_server(pc->driver.transport); |
| 346 |
} |
| 347 |
set_pconnection(pc->driver.connection, pc); |
| 348 |
return pc; |
| 349 |
} |
| 350 |
|
| 351 |
static void pconnection_free(pconnection_t *pc) { |
| 352 |
pn_connection_t *c = pc->driver.connection; |
| 353 |
if (c) set_pconnection(c, NULL); |
| 354 |
pn_connection_driver_destroy(&pc->driver); |
| 355 |
if (pc->addr.getaddrinfo.addrinfo) { |
| 356 |
uv_freeaddrinfo(pc->addr.getaddrinfo.addrinfo); /* Interrupted after resolve */ |
| 357 |
} |
| 358 |
uv_mutex_destroy(&pc->lock); |
| 359 |
free(pc); |
| 360 |
} |
| 361 |
|
| 362 |
static pn_event_t *listener_batch_next(pn_event_batch_t *batch); |
| 363 |
static pn_event_t *proactor_batch_next(pn_event_batch_t *batch); |
| 364 |
|
| 365 |
static inline pn_proactor_t *batch_proactor(pn_event_batch_t *batch) { |
| 366 |
return (batch->next_event == proactor_batch_next) ? |
| 367 |
containerof(batch, pn_proactor_t, batch) : NULL; |
| 368 |
} |
| 369 |
|
| 370 |
static inline pn_listener_t *batch_listener(pn_event_batch_t *batch) { |
| 371 |
return (batch->next_event == listener_batch_next) ? |
| 372 |
containerof(batch, pn_listener_t, batch) : NULL; |
| 373 |
} |
| 374 |
|
| 375 |
static inline pconnection_t *batch_pconnection(pn_event_batch_t *batch) { |
| 376 |
return (batch->next_event == pconnection_batch_next) ? |
| 377 |
containerof(batch, pconnection_t, batch) : NULL; |
| 378 |
} |
| 379 |
|
| 380 |
static inline work_t *batch_work(pn_event_batch_t *batch) { |
| 381 |
pconnection_t *pc = batch_pconnection(batch); |
| 382 |
if (pc) return &pc->work; |
| 383 |
pn_listener_t *l = batch_listener(batch); |
| 384 |
if (l) return &l->work; |
| 385 |
return NULL; |
| 386 |
} |
| 387 |
|
| 388 |
static void check_for_inactive(pn_proactor_t *p) { |
| 389 |
/* No future events: no active socket io, no pending timer, no |
| 390 |
current event processing. */ |
| 391 |
if (!p->batch_working && !p->active && !p->need_interrupt && p->timeout_state == TM_NONE) |
| 392 |
p->need_inactive = true; |
| 393 |
} |
| 394 |
|
| 395 |
/* Total count of listener and connections for PN_PROACTOR_INACTIVE */ |
| 396 |
static void add_active(pn_proactor_t *p) { |
| 397 |
uv_mutex_lock(&p->lock); |
| 398 |
++p->active; |
| 399 |
uv_mutex_unlock(&p->lock); |
| 400 |
} |
| 401 |
|
| 402 |
static void remove_active_lh(pn_proactor_t *p) { |
| 403 |
assert(p->active > 0); |
| 404 |
if (--p->active == 0) { |
| 405 |
check_for_inactive(p); |
| 406 |
} |
| 407 |
} |
| 408 |
|
| 409 |
static void remove_active(pn_proactor_t *p) { |
| 410 |
uv_mutex_lock(&p->lock); |
| 411 |
remove_active_lh(p); |
| 412 |
uv_mutex_unlock(&p->lock); |
| 413 |
} |
| 414 |
|
| 415 |
/* Final close event for for a pconnection_t, disconnects from proactor */ |
| 416 |
static void on_close_pconnection_final(uv_handle_t *h) { |
| 417 |
/* Free resources associated with a pconnection_t. |
| 418 |
If the life of the pn_connection_t has been extended with reference counts |
| 419 |
we want the pconnection_t to have the same lifespan so calls to pn_connection_wake() |
| 420 |
will be valid, but no-ops. |
| 421 |
*/ |
| 422 |
pconnection_t *pc = (pconnection_t*)h->data; |
| 423 |
remove_active(pc->work.proactor); |
| 424 |
pconnection_free(pc); |
| 425 |
} |
| 426 |
|
| 427 |
static void uv_safe_close(uv_handle_t *h, uv_close_cb cb) { |
| 428 |
/* Only close if h has been initialized and is not already closing */ |
| 429 |
if (h->type && !uv_is_closing(h)) { |
| 430 |
uv_close(h, cb); |
| 431 |
} |
| 432 |
} |
| 433 |
|
| 434 |
static void on_close_pconnection(uv_handle_t *h) { |
| 435 |
pconnection_t *pc = (pconnection_t*)h->data; |
| 436 |
/* Delay the free till the timer handle is also closed */ |
| 437 |
uv_timer_stop(&pc->timer); |
| 438 |
uv_safe_close((uv_handle_t*)&pc->timer, on_close_pconnection_final); |
| 439 |
} |
| 440 |
|
| 441 |
static void listener_close_lh(pn_listener_t* l) { |
| 442 |
if (l->state < L_CLOSE) { |
| 443 |
l->state = L_CLOSE; |
| 444 |
} |
| 445 |
work_notify(&l->work); |
| 446 |
} |
| 447 |
|
| 448 |
static void on_close_lsocket(uv_handle_t *h) { |
| 449 |
lsocket_t* ls = (lsocket_t*)h->data; |
| 450 |
pn_listener_t *l = ls->parent; |
| 451 |
if (l) { |
| 452 |
/* Remove from list */ |
| 453 |
lsocket_t **pp = &l->lsockets; |
| 454 |
for (; *pp != ls; pp = &(*pp)->next) |
| 455 |
; |
| 456 |
*pp = ls->next; |
| 457 |
work_notify(&l->work); |
| 458 |
} |
| 459 |
free(ls); |
| 460 |
} |
| 461 |
|
| 462 |
/* Remember the first error code from a bad connect attempt. |
| 463 |
* This is not yet a full-blown error as we might succeed connecting |
| 464 |
* to a different address if there are several. |
| 465 |
*/ |
| 466 |
static inline void pconnection_bad_connect(pconnection_t *pc, int err) { |
| 467 |
if (!pc->connected) { |
| 468 |
pc->connected = err; /* Remember first connect error in case they all fail */ |
| 469 |
} |
| 470 |
} |
| 471 |
|
| 472 |
/* Set the error condition, but don't close the driver. */ |
| 473 |
static void pconnection_set_error(pconnection_t *pc, int err, const char* what) { |
| 474 |
pn_connection_driver_t *driver = &pc->driver; |
| 475 |
pn_connection_driver_bind(driver); /* Make sure we are bound so errors will be reported */ |
| 476 |
pni_proactor_set_cond(pn_transport_condition(driver->transport), |
| 477 |
what, pc->addr.host , pc->addr.port, uv_strerror(err)); |
| 478 |
} |
| 479 |
|
| 480 |
/* Set the error condition and close the driver. */ |
| 481 |
static void pconnection_error(pconnection_t *pc, int err, const char* what) { |
| 482 |
assert(err); |
| 483 |
pconnection_bad_connect(pc, err); |
| 484 |
pconnection_set_error(pc, err, what); |
| 485 |
pn_connection_driver_close(&pc->driver); |
| 486 |
} |
| 487 |
|
| 488 |
static void listener_error_lh(pn_listener_t *l, int err, const char* what) { |
| 489 |
assert(err); |
| 490 |
if (!pn_condition_is_set(l->condition)) { |
| 491 |
pni_proactor_set_cond(l->condition, what, l->addr.host, l->addr.port, uv_strerror(err)); |
| 492 |
} |
| 493 |
listener_close_lh(l); |
| 494 |
} |
| 495 |
|
| 496 |
static void listener_error(pn_listener_t *l, int err, const char* what) { |
| 497 |
uv_mutex_lock(&l->lock); |
| 498 |
listener_error_lh(l, err, what); |
| 499 |
uv_mutex_unlock(&l->lock); |
| 500 |
} |
| 501 |
|
| 502 |
static int pconnection_init(pconnection_t *pc) { |
| 503 |
int err = 0; |
| 504 |
err = uv_tcp_init(&pc->work.proactor->loop, &pc->tcp); |
| 505 |
if (!err) { |
| 506 |
pc->tcp.data = pc; |
| 507 |
pc->connect.data = pc; |
| 508 |
err = uv_timer_init(&pc->work.proactor->loop, &pc->timer); |
| 509 |
if (!err) { |
| 510 |
pc->timer.data = pc; |
| 511 |
} else { |
| 512 |
uv_close((uv_handle_t*)&pc->tcp, NULL); |
| 513 |
} |
| 514 |
} |
| 515 |
if (err) { |
| 516 |
pconnection_error(pc, err, "initialization"); |
| 517 |
} |
| 518 |
return err; |
| 519 |
} |
| 520 |
|
| 521 |
static void try_connect(pconnection_t *pc); |
| 522 |
|
| 523 |
static void on_connect_fail(uv_handle_t *handle) { |
| 524 |
pconnection_t *pc = (pconnection_t*)handle->data; |
| 525 |
/* Create a new TCP socket, the current one is closed */ |
| 526 |
int err = uv_tcp_init(&pc->work.proactor->loop, &pc->tcp); |
| 527 |
if (err) { |
| 528 |
pc->connected = err; |
| 529 |
pc->addr.addrinfo = NULL; /* No point in trying anymore, we can't create a socket */ |
| 530 |
} else { |
| 531 |
try_connect(pc); |
| 532 |
} |
| 533 |
} |
| 534 |
|
| 535 |
static void pconnection_addresses(pconnection_t *pc) { |
| 536 |
int len; |
| 537 |
len = sizeof(pc->local.ss); |
| 538 |
uv_tcp_getsockname(&pc->tcp, (struct sockaddr*)&pc->local.ss, &len); |
| 539 |
len = sizeof(pc->remote.ss); |
| 540 |
uv_tcp_getpeername(&pc->tcp, (struct sockaddr*)&pc->remote.ss, &len); |
| 541 |
} |
| 542 |
|
| 543 |
/* Outgoing connection */ |
| 544 |
static void on_connect(uv_connect_t *connect, int err) { |
| 545 |
pconnection_t *pc = (pconnection_t*)connect->data; |
| 546 |
if (!err) { |
| 547 |
pc->connected = 1; |
| 548 |
pconnection_addresses(pc); |
| 549 |
work_notify(&pc->work); |
| 550 |
uv_freeaddrinfo(pc->addr.getaddrinfo.addrinfo); /* Done with address info */ |
| 551 |
pc->addr.getaddrinfo.addrinfo = NULL; |
| 552 |
} else { |
| 553 |
pconnection_bad_connect(pc, err); |
| 554 |
uv_safe_close((uv_handle_t*)&pc->tcp, on_connect_fail); /* Try the next addr if there is one */ |
| 555 |
} |
| 556 |
} |
| 557 |
|
| 558 |
/* Incoming connection ready to be accepted */ |
| 559 |
static void on_connection(uv_stream_t* server, int err) { |
| 560 |
/* Unlike most on_* functions, this can be called by the leader thread when the listener |
| 561 |
* is ON_WORKER or ON_LEADER, because |
| 562 |
* |
| 563 |
* 1. There's no way to stop libuv from calling on_connection(). |
| 564 |
* 2. There can be multiple lsockets per listener. |
| 565 |
* |
| 566 |
* Update the state of the listener and queue it for leader attention. |
| 567 |
*/ |
| 568 |
lsocket_t *ls = (lsocket_t*)server->data; |
| 569 |
pn_listener_t *l = ls->parent; |
| 570 |
if (err) { |
| 571 |
listener_error(l, err, "on incoming connection"); |
| 572 |
} else { |
| 573 |
uv_mutex_lock(&l->lock); |
| 574 |
pn_collector_put(l->collector, PN_CLASSCLASS(pn_listener_socket), ls, PN_LISTENER_ACCEPT); |
| 575 |
uv_mutex_unlock(&l->lock); |
| 576 |
} |
| 577 |
work_notify(&l->work); |
| 578 |
} |
| 579 |
|
| 580 |
/* Common address resolution for leader_listen and leader_connect */ |
| 581 |
static int leader_resolve(pn_proactor_t *p, addr_t *addr, bool listen) { |
| 582 |
struct addrinfo hints = { 0 }; |
| 583 |
hints.ai_family = AF_UNSPEC; |
| 584 |
hints.ai_socktype = SOCK_STREAM; |
| 585 |
hints.ai_flags = AI_ADDRCONFIG; |
| 586 |
if (listen) { |
| 587 |
hints.ai_flags |= AI_PASSIVE | AI_ALL; |
| 588 |
} |
| 589 |
int err = uv_getaddrinfo(&p->loop, &addr->getaddrinfo, NULL, addr->host, addr->port, &hints); |
| 590 |
addr->addrinfo = addr->getaddrinfo.addrinfo; /* Start with the first addrinfo */ |
| 591 |
return err; |
| 592 |
} |
| 593 |
|
| 594 |
/* Try to connect to the current addrinfo. Called by leader and via callbacks for retry.*/ |
| 595 |
static void try_connect(pconnection_t *pc) { |
| 596 |
struct addrinfo *ai = pc->addr.addrinfo; |
| 597 |
if (!ai) { /* End of list, connect fails */ |
| 598 |
uv_freeaddrinfo(pc->addr.getaddrinfo.addrinfo); |
| 599 |
pc->addr.getaddrinfo.addrinfo = NULL; |
| 600 |
pconnection_bad_connect(pc, UV_EAI_NODATA); |
| 601 |
pconnection_error(pc, pc->connected, "connecting to"); |
| 602 |
work_notify(&pc->work); |
| 603 |
} else { |
| 604 |
pc->addr.addrinfo = ai->ai_next; /* Advance for next attempt */ |
| 605 |
int err = uv_tcp_connect(&pc->connect, &pc->tcp, ai->ai_addr, on_connect); |
| 606 |
if (err) { |
| 607 |
pconnection_bad_connect(pc, err); |
| 608 |
uv_close((uv_handle_t*)&pc->tcp, on_connect_fail); /* Queue up next attempt */ |
| 609 |
} |
| 610 |
} |
| 611 |
} |
| 612 |
|
| 613 |
static bool leader_connect(pconnection_t *pc) { |
| 614 |
int err = pconnection_init(pc); |
| 615 |
if (!err) err = leader_resolve(pc->work.proactor, &pc->addr, false); |
| 616 |
if (err) { |
| 617 |
pconnection_error(pc, err, "on connect resolving"); |
| 618 |
return true; |
| 619 |
} else { |
| 620 |
try_connect(pc); |
| 621 |
return false; |
| 622 |
} |
| 623 |
} |
| 624 |
|
| 625 |
static int lsocket(pn_listener_t *l, struct addrinfo *ai) { |
| 626 |
lsocket_t *ls = (lsocket_t*)calloc(1, sizeof(lsocket_t)); |
| 627 |
ls->type = T_LSOCKET; |
| 628 |
ls->tcp.data = ls; |
| 629 |
ls->parent = NULL; |
| 630 |
ls->next = NULL; |
| 631 |
int err = uv_tcp_init(&l->work.proactor->loop, &ls->tcp); |
| 632 |
if (err) { |
| 633 |
free(ls); /* Will never be closed */ |
| 634 |
} else { |
| 635 |
if (l->dynamic_port) set_port(ai->ai_addr, l->dynamic_port); |
| 636 |
int flags = (ai->ai_family == AF_INET6) ? UV_TCP_IPV6ONLY : 0; |
| 637 |
err = uv_tcp_bind(&ls->tcp, ai->ai_addr, flags); |
| 638 |
if (!err) err = uv_listen((uv_stream_t*)&ls->tcp, l->backlog, on_connection); |
| 639 |
if (!err) { |
| 640 |
/* Get actual listening address */ |
| 641 |
pn_netaddr_t *na = &l->addrs[l->addrs_len++]; |
| 642 |
int len = sizeof(na->ss); |
| 643 |
uv_tcp_getsockname(&ls->tcp, (struct sockaddr*)(&na->ss), &len); |
| 644 |
if (na == l->addrs) { /* First socket, check for dynamic port bind */ |
| 645 |
l->dynamic_port = check_dynamic_port(ai->ai_addr, pn_netaddr_sockaddr(na)); |
| 646 |
} else { |
| 647 |
(na-1)->next = na; /* Link into list */ |
| 648 |
} |
| 649 |
/* Add to l->lsockets list */ |
| 650 |
ls->parent = l; |
| 651 |
ls->next = l->lsockets; |
| 652 |
l->lsockets = ls; |
| 653 |
} else { |
| 654 |
uv_close((uv_handle_t*)&ls->tcp, on_close_lsocket); /* Freed by on_close_lsocket */ |
| 655 |
} |
| 656 |
} |
| 657 |
return err; |
| 658 |
} |
| 659 |
|
| 660 |
#define ARRAY_LEN(A) (sizeof(A)/sizeof(*(A))) |
| 661 |
|
| 662 |
/* Listen on all available addresses */ |
| 663 |
static void leader_listen_lh(pn_listener_t *l) { |
| 664 |
int err = leader_resolve(l->work.proactor, &l->addr, true); |
| 665 |
if (!err) { |
| 666 |
/* Allocate enough space for the pn_netaddr_t addresses */ |
| 667 |
size_t len = 0; |
| 668 |
for (struct addrinfo *ai = l->addr.getaddrinfo.addrinfo; ai; ai = ai->ai_next) { |
| 669 |
++len; |
| 670 |
} |
| 671 |
l->addrs = (pn_netaddr_t*)calloc(len, sizeof(pn_netaddr_t)); |
| 672 |
|
| 673 |
/* Find the working addresses */ |
| 674 |
for (struct addrinfo *ai = l->addr.getaddrinfo.addrinfo; ai; ai = ai->ai_next) { |
| 675 |
int err2 = lsocket(l, ai); |
| 676 |
if (err2) { |
| 677 |
err = err2; |
| 678 |
} |
| 679 |
} |
| 680 |
uv_freeaddrinfo(l->addr.getaddrinfo.addrinfo); |
| 681 |
l->addr.getaddrinfo.addrinfo = NULL; |
| 682 |
if (l->lsockets) { /* Ignore errors if we got at least one good listening socket */ |
| 683 |
err = 0; |
| 684 |
} |
| 685 |
} |
| 686 |
if (err) { |
| 687 |
listener_error_lh(l, err, "listening on"); |
| 688 |
} else { |
| 689 |
pn_collector_put(l->collector, PN_CLASSCLASS(pn_listener), l, PN_LISTENER_OPEN); |
| 690 |
} |
| 691 |
} |
| 692 |
|
| 693 |
void pn_listener_free(pn_listener_t *l) { |
| 694 |
if (l) { |
| 695 |
if (l->addrs) free(l->addrs); |
| 696 |
if (l->addr.getaddrinfo.addrinfo) { /* Interrupted after resolve */ |
| 697 |
uv_freeaddrinfo(l->addr.getaddrinfo.addrinfo); |
| 698 |
} |
| 699 |
if (l->collector) pn_collector_free(l->collector); |
| 700 |
if (l->condition) pn_condition_free(l->condition); |
| 701 |
if (l->attachments) pn_free(l->attachments); |
| 702 |
while (l->lsockets) { |
| 703 |
lsocket_t *ls = l->lsockets; |
| 704 |
l->lsockets = ls->next; |
| 705 |
free(ls); |
| 706 |
} |
| 707 |
assert(!l->accept.front); |
| 708 |
uv_mutex_destroy(&l->lock); |
| 709 |
free(l); |
| 710 |
} |
| 711 |
} |
| 712 |
|
| 713 |
/* Process a listener, return true if it has events for a worker thread */ |
| 714 |
static bool leader_process_listener(pn_listener_t *l) { |
| 715 |
/* NOTE: l may be concurrently accessed by on_connection() */ |
| 716 |
bool closed = false; |
| 717 |
uv_mutex_lock(&l->lock); |
| 718 |
|
| 719 |
/* Process accepted connections */ |
| 720 |
for (pconnection_t *pc = pconnection_pop(&l->accept); pc; pc = pconnection_pop(&l->accept)) { |
| 721 |
int err = pconnection_init(pc); |
| 722 |
if (!err) err = uv_accept((uv_stream_t*)&pc->lsocket->tcp, (uv_stream_t*)&pc->tcp); |
| 723 |
if (!err) { |
| 724 |
pconnection_addresses(pc); |
| 725 |
} else { |
| 726 |
listener_error(l, err, "accepting from"); |
| 727 |
pconnection_error(pc, err, "accepting from"); |
| 728 |
} |
| 729 |
work_start(&pc->work); /* Process events for the accepted/failed connection */ |
| 730 |
} |
| 731 |
|
| 732 |
switch (l->state) { |
| 733 |
|
| 734 |
case L_UNINIT: |
| 735 |
l->state = L_LISTENING; |
| 736 |
leader_listen_lh(l); |
| 737 |
break; |
| 738 |
|
| 739 |
case L_LISTENING: |
| 740 |
break; |
| 741 |
|
| 742 |
case L_CLOSE: /* Close requested, start closing lsockets */ |
| 743 |
l->state = L_CLOSING; |
| 744 |
for (lsocket_t *ls = l->lsockets; ls; ls = ls->next) { |
| 745 |
uv_safe_close((uv_handle_t*)&ls->tcp, on_close_lsocket); |
| 746 |
} |
| 747 |
/* NOTE: Fall through in case we have 0 sockets - e.g. resolver error */ |
| 748 |
|
| 749 |
case L_CLOSING: /* Closing - can we send PN_LISTENER_CLOSE? */ |
| 750 |
if (!l->lsockets) { |
| 751 |
l->state = L_CLOSED; |
| 752 |
pn_collector_put(l->collector, PN_CLASSCLASS(pn_listener), l, PN_LISTENER_CLOSE); |
| 753 |
} |
| 754 |
break; |
| 755 |
|
| 756 |
case L_CLOSED: /* Closed, has LISTENER_CLOSE has been processed? */ |
| 757 |
if (!pn_collector_peek(l->collector)) { |
| 758 |
remove_active(l->work.proactor); |
| 759 |
closed = true; |
| 760 |
} |
| 761 |
} |
| 762 |
bool has_work = !closed && pn_collector_peek(l->collector); |
| 763 |
uv_mutex_unlock(&l->lock); |
| 764 |
|
| 765 |
if (closed) { |
| 766 |
pn_listener_free(l); |
| 767 |
} |
| 768 |
return has_work; |
| 769 |
} |
| 770 |
|
| 771 |
/* Generate tick events and return millis till next tick or 0 if no tick is required */ |
| 772 |
static pn_millis_t leader_tick(pconnection_t *pc) { |
| 773 |
uint64_t now = uv_now(pc->timer.loop); |
| 774 |
uint64_t next = pn_transport_tick(pc->driver.transport, now); |
| 775 |
return next ? next - now : 0; |
| 776 |
} |
| 777 |
|
| 778 |
static void on_tick(uv_timer_t *timer) { |
| 779 |
pconnection_t *pc = (pconnection_t*)timer->data; |
| 780 |
leader_tick(pc); |
| 781 |
work_notify(&pc->work); |
| 782 |
} |
| 783 |
|
| 784 |
static void on_read(uv_stream_t* stream, ssize_t nread, const uv_buf_t* buf) { |
| 785 |
pconnection_t *pc = (pconnection_t*)stream->data; |
| 786 |
if (nread > 0) { |
| 787 |
pn_connection_driver_read_done(&pc->driver, nread); |
| 788 |
} else if (nread < 0) { |
| 789 |
if (nread != UV_EOF) { /* hangup */ |
| 790 |
pconnection_set_error(pc, nread, "on read from"); |
| 791 |
} |
| 792 |
pn_connection_driver_close(&pc->driver); |
| 793 |
} |
| 794 |
work_notify(&pc->work); |
| 795 |
} |
| 796 |
|
| 797 |
static void on_write(uv_write_t* write, int err) { |
| 798 |
pconnection_t *pc = (pconnection_t*)write->data; |
| 799 |
size_t size = pc->writing; |
| 800 |
pc->writing = 0; |
| 801 |
if (err) { |
| 802 |
pconnection_set_error(pc, err, "on write to"); |
| 803 |
pn_connection_driver_write_close(&pc->driver); |
| 804 |
} else if (!pn_connection_driver_write_closed(&pc->driver)) { |
| 805 |
pn_connection_driver_write_done(&pc->driver, size); |
| 806 |
} |
| 807 |
work_notify(&pc->work); |
| 808 |
} |
| 809 |
|
| 810 |
static void on_timeout(uv_timer_t *timer) { |
| 811 |
pn_proactor_t *p = (pn_proactor_t*)timer->data; |
| 812 |
uv_mutex_lock(&p->lock); |
| 813 |
if (p->timeout_state == TM_PENDING) { /* Only fire if still pending */ |
| 814 |
p->timeout_state = TM_FIRED; |
| 815 |
} |
| 816 |
uv_stop(&p->loop); /* UV does not always stop after on_timeout without this */ |
| 817 |
uv_mutex_unlock(&p->lock); |
| 818 |
} |
| 819 |
|
| 820 |
/* Read buffer allocation function for uv, just returns the transports read buffer. */ |
| 821 |
static void alloc_read_buffer(uv_handle_t* stream, size_t size, uv_buf_t* buf) { |
| 822 |
pconnection_t *pc = (pconnection_t*)stream->data; |
| 823 |
pn_rwbytes_t rbuf = pn_connection_driver_read_buffer(&pc->driver); |
| 824 |
*buf = uv_buf_init(rbuf.start, rbuf.size); |
| 825 |
} |
| 826 |
|
| 827 |
/* Set the event in the proactor's batch */ |
| 828 |
static pn_event_batch_t *proactor_batch_lh(pn_proactor_t *p, pn_event_type_t t) { |
| 829 |
pn_collector_put(p->collector, PN_CLASSCLASS(pn_proactor), p, t); |
| 830 |
p->batch_working = true; |
| 831 |
return &p->batch; |
| 832 |
} |
| 833 |
|
| 834 |
static pn_event_t *listener_batch_next(pn_event_batch_t *batch) { |
| 835 |
pn_listener_t *l = batch_listener(batch); |
| 836 |
uv_mutex_lock(&l->lock); |
| 837 |
pn_event_t *e = pn_collector_next(l->collector); |
| 838 |
uv_mutex_unlock(&l->lock); |
| 839 |
return pni_log_event(l, e); |
| 840 |
} |
| 841 |
|
| 842 |
static pn_event_t *proactor_batch_next(pn_event_batch_t *batch) { |
| 843 |
pn_proactor_t *p = batch_proactor(batch); |
| 844 |
assert(p->batch_working); |
| 845 |
return pni_log_event(p, pn_collector_next(p->collector)); |
| 846 |
} |
| 847 |
|
| 848 |
static pn_event_t *pconnection_batch_next(pn_event_batch_t *batch) { |
| 849 |
pconnection_t *pc = batch_pconnection(batch); |
| 850 |
return pn_connection_driver_next_event(&pc->driver); |
| 851 |
} |
| 852 |
|
| 853 |
/* Return the next event batch or NULL if no events are available */ |
| 854 |
static pn_event_batch_t *get_batch_lh(pn_proactor_t *p) { |
| 855 |
if (!p->batch_working) { /* Can generate proactor events */ |
| 856 |
if (p->need_inactive) { |
| 857 |
p->need_inactive = false; |
| 858 |
return proactor_batch_lh(p, PN_PROACTOR_INACTIVE); |
| 859 |
} |
| 860 |
if (p->need_interrupt) { |
| 861 |
p->need_interrupt = false; |
| 862 |
return proactor_batch_lh(p, PN_PROACTOR_INTERRUPT); |
| 863 |
} |
| 864 |
if (p->timeout_state == TM_FIRED) { |
| 865 |
p->timeout_state = TM_NONE; |
| 866 |
p->timeout_processed = true; |
| 867 |
return proactor_batch_lh(p, PN_PROACTOR_TIMEOUT); |
| 868 |
} |
| 869 |
} |
| 870 |
for (work_t *w = work_pop(&p->worker_q); w; w = work_pop(&p->worker_q)) { |
| 871 |
assert(w->working); |
| 872 |
switch (w->type) { |
| 873 |
case T_CONNECTION: |
| 874 |
return &((pconnection_t*)w)->batch; |
| 875 |
case T_LISTENER: |
| 876 |
return &((pn_listener_t*)w)->batch; |
| 877 |
default: |
| 878 |
break; |
| 879 |
} |
| 880 |
} |
| 881 |
return NULL; |
| 882 |
} |
| 883 |
|
| 884 |
/* Check wake state and generate WAKE event if needed */ |
| 885 |
static void check_wake(pconnection_t *pc) { |
| 886 |
uv_mutex_lock(&pc->lock); |
| 887 |
if (pc->wake == W_PENDING) { |
| 888 |
pn_connection_t *c = pc->driver.connection; |
| 889 |
pn_collector_put(pn_connection_collector(c), PN_OBJECT, c, PN_CONNECTION_WAKE); |
| 890 |
pc->wake = W_NONE; |
| 891 |
} |
| 892 |
uv_mutex_unlock(&pc->lock); |
| 893 |
} |
| 894 |
|
| 895 |
/* Process a pconnection, return true if it has events for a worker thread */ |
| 896 |
static bool leader_process_pconnection(pconnection_t *pc) { |
| 897 |
/* Important to do the following steps in order */ |
| 898 |
if (!pc->connected) { |
| 899 |
return leader_connect(pc); |
| 900 |
} |
| 901 |
if (pc->writing) { |
| 902 |
/* We can't do anything while a write request is pending */ |
| 903 |
return false; |
| 904 |
} |
| 905 |
/* Must process INIT and BOUND events before we do any IO-related stuff */ |
| 906 |
if (pn_connection_driver_has_event(&pc->driver)) { |
| 907 |
return true; |
| 908 |
} |
| 909 |
if (pn_connection_driver_finished(&pc->driver)) { |
| 910 |
uv_mutex_lock(&pc->lock); |
| 911 |
pc->wake = W_CLOSED; /* wake() is a no-op from now on */ |
| 912 |
uv_mutex_unlock(&pc->lock); |
| 913 |
uv_safe_close((uv_handle_t*)&pc->tcp, on_close_pconnection); |
| 914 |
} else { |
| 915 |
/* Check for events that can be generated without blocking for IO */ |
| 916 |
check_wake(pc); |
| 917 |
pn_millis_t next_tick = leader_tick(pc); |
| 918 |
pn_rwbytes_t rbuf = pn_connection_driver_read_buffer(&pc->driver); |
| 919 |
pn_bytes_t wbuf = pn_connection_driver_write_buffer(&pc->driver); |
| 920 |
/* If we still have no events, make async UV requests */ |
| 921 |
if (!pn_connection_driver_has_event(&pc->driver)) { |
| 922 |
int err = 0; |
| 923 |
const char *what = NULL; |
| 924 |
if (!err && next_tick) { |
| 925 |
what = "connection timer start"; |
| 926 |
err = uv_timer_start(&pc->timer, on_tick, next_tick, 0); |
| 927 |
} |
| 928 |
if (!err) { |
| 929 |
what = "write"; |
| 930 |
if (wbuf.size > 0) { |
| 931 |
uv_buf_t buf = uv_buf_init((char*)wbuf.start, wbuf.size); |
| 932 |
err = uv_write(&pc->write, (uv_stream_t*)&pc->tcp, &buf, 1, on_write); |
| 933 |
if (!err) { |
| 934 |
pc->writing = wbuf.size; |
| 935 |
} |
| 936 |
} else if (pn_connection_driver_write_closed(&pc->driver)) { |
| 937 |
uv_shutdown(&pc->shutdown, (uv_stream_t*)&pc->tcp, NULL); |
| 938 |
} |
| 939 |
} |
| 940 |
if (!err && !pc->read_started && rbuf.size > 0) { |
| 941 |
what = "read"; |
| 942 |
err = uv_read_start((uv_stream_t*)&pc->tcp, alloc_read_buffer, on_read); |
| 943 |
if (!err) { |
| 944 |
pc->read_started = true; |
| 945 |
} |
| 946 |
} |
| 947 |
if (err) { |
| 948 |
/* Some IO requests failed, generate the error events */ |
| 949 |
pconnection_error(pc, err, what); |
| 950 |
} |
| 951 |
} |
| 952 |
} |
| 953 |
return pn_connection_driver_has_event(&pc->driver); |
| 954 |
} |
| 955 |
|
| 956 |
/* Detach a connection from the UV loop so it can be used safely by a worker */ |
| 957 |
void pconnection_detach(pconnection_t *pc) { |
| 958 |
if (pc->connected && !pc->writing) { /* Can't detach while a write is pending */ |
| 959 |
if (pc->read_started) { |
| 960 |
uv_read_stop((uv_stream_t*)&pc->tcp); |
| 961 |
pc->read_started = false; |
| 962 |
} |
| 963 |
uv_timer_stop(&pc->timer); |
| 964 |
} |
| 965 |
} |
| 966 |
|
| 967 |
static void on_proactor_disconnect(uv_handle_t* h, void* v) { |
| 968 |
if (h->type == UV_TCP) { |
| 969 |
switch (*(struct_type*)h->data) { |
| 970 |
case T_CONNECTION: { |
| 971 |
pconnection_t *pc = (pconnection_t*)h->data; |
| 972 |
pn_condition_t *cond = pc->work.proactor->disconnect_cond; |
| 973 |
if (cond) { |
| 974 |
pn_condition_copy(pn_transport_condition(pc->driver.transport), cond); |
| 975 |
} |
| 976 |
pn_connection_driver_close(&pc->driver); |
| 977 |
work_notify(&pc->work); |
| 978 |
break; |
| 979 |
} |
| 980 |
case T_LSOCKET: { |
| 981 |
pn_listener_t *l = ((lsocket_t*)h->data)->parent; |
| 982 |
if (l) { |
| 983 |
pn_condition_t *cond = l->work.proactor->disconnect_cond; |
| 984 |
if (cond) { |
| 985 |
pn_condition_copy(pn_listener_condition(l), cond); |
| 986 |
} |
| 987 |
pn_listener_close(l); |
| 988 |
} |
| 989 |
break; |
| 990 |
} |
| 991 |
default: |
| 992 |
break; |
| 993 |
} |
| 994 |
} |
| 995 |
} |
| 996 |
|
| 997 |
/* Process the leader_q and the UV loop, in the leader thread */ |
| 998 |
static pn_event_batch_t *leader_lead_lh(pn_proactor_t *p, uv_run_mode mode) { |
| 999 |
/* Set timeout timer if there was a request, let it count down while we process work */ |
| 1000 |
if (p->timeout_state == TM_REQUEST) { |
| 1001 |
p->timeout_state = TM_PENDING; |
| 1002 |
uv_timer_stop(&p->timer); |
| 1003 |
uv_timer_start(&p->timer, on_timeout, p->timeout, 0); |
| 1004 |
} |
| 1005 |
/* If disconnect was requested, walk the socket list */ |
| 1006 |
if (p->disconnect) { |
| 1007 |
p->disconnect = false; |
| 1008 |
if (p->active) { |
| 1009 |
uv_mutex_unlock(&p->lock); |
| 1010 |
uv_walk(&p->loop, on_proactor_disconnect, NULL); |
| 1011 |
uv_mutex_lock(&p->lock); |
| 1012 |
} else { |
| 1013 |
p->need_inactive = true; /* Send INACTIVE right away, nothing to do. */ |
| 1014 |
} |
| 1015 |
} |
| 1016 |
pn_event_batch_t *batch = NULL; |
| 1017 |
for (work_t *w = work_pop(&p->leader_q); w; w = work_pop(&p->leader_q)) { |
| 1018 |
assert(!w->working); |
| 1019 |
|
| 1020 |
uv_mutex_unlock(&p->lock); /* Unlock to process each item, may add more items to leader_q */ |
| 1021 |
bool has_work = false; |
| 1022 |
switch (w->type) { |
| 1023 |
case T_CONNECTION: |
| 1024 |
has_work = leader_process_pconnection((pconnection_t*)w); |
| 1025 |
break; |
| 1026 |
case T_LISTENER: |
| 1027 |
has_work = leader_process_listener((pn_listener_t*)w); |
| 1028 |
break; |
| 1029 |
default: |
| 1030 |
break; |
| 1031 |
} |
| 1032 |
uv_mutex_lock(&p->lock); |
| 1033 |
|
| 1034 |
if (has_work && !w->working && w->next == work_unqueued) { |
| 1035 |
if (w->type == T_CONNECTION) { |
| 1036 |
pconnection_detach((pconnection_t*)w); |
| 1037 |
} |
| 1038 |
w->working = true; |
| 1039 |
work_push(&p->worker_q, w); |
| 1040 |
} |
| 1041 |
} |
| 1042 |
batch = get_batch_lh(p); /* Check for work */ |
| 1043 |
if (!batch) { /* No work, run the UV loop */ |
| 1044 |
uv_mutex_unlock(&p->lock); /* Unlock to run UV loop */ |
| 1045 |
uv_run(&p->loop, mode); |
| 1046 |
uv_mutex_lock(&p->lock); |
| 1047 |
batch = get_batch_lh(p); |
| 1048 |
} |
| 1049 |
return batch; |
| 1050 |
} |
| 1051 |
|
| 1052 |
/**** public API ****/ |
| 1053 |
|
| 1054 |
pn_event_batch_t *pn_proactor_get(struct pn_proactor_t* p) { |
| 1055 |
uv_mutex_lock(&p->lock); |
| 1056 |
pn_event_batch_t *batch = get_batch_lh(p); |
| 1057 |
if (batch == NULL && !p->has_leader) { |
| 1058 |
/* Try a non-blocking lead to generate some work */ |
| 1059 |
p->has_leader = true; |
| 1060 |
batch = leader_lead_lh(p, UV_RUN_NOWAIT); |
| 1061 |
p->has_leader = false; |
| 1062 |
uv_cond_broadcast(&p->cond); /* Signal followers for possible work */ |
| 1063 |
} |
| 1064 |
uv_mutex_unlock(&p->lock); |
| 1065 |
return batch; |
| 1066 |
} |
| 1067 |
|
| 1068 |
pn_event_batch_t *pn_proactor_wait(struct pn_proactor_t* p) { |
| 1069 |
uv_mutex_lock(&p->lock); |
| 1070 |
pn_event_batch_t *batch = get_batch_lh(p); |
| 1071 |
while (!batch && p->has_leader) { |
| 1072 |
uv_cond_wait(&p->cond, &p->lock); /* Follow the leader */ |
| 1073 |
batch = get_batch_lh(p); |
| 1074 |
} |
| 1075 |
if (!batch) { /* Become leader */ |
| 1076 |
p->has_leader = true; |
| 1077 |
do { |
| 1078 |
batch = leader_lead_lh(p, UV_RUN_ONCE); |
| 1079 |
} while (!batch); |
| 1080 |
p->has_leader = false; |
| 1081 |
uv_cond_broadcast(&p->cond); /* Signal a followers. One takes over, many can work. */ |
| 1082 |
} |
| 1083 |
uv_mutex_unlock(&p->lock); |
| 1084 |
return batch; |
| 1085 |
} |
| 1086 |
|
| 1087 |
void pn_proactor_done(pn_proactor_t *p, pn_event_batch_t *batch) { |
| 1088 |
if (!batch) return; |
| 1089 |
uv_mutex_lock(&p->lock); |
| 1090 |
work_t *w = batch_work(batch); |
| 1091 |
if (w) { |
| 1092 |
assert(w->working); |
| 1093 |
assert(w->next == work_unqueued); |
| 1094 |
w->working = false; |
| 1095 |
work_push(&p->leader_q, w); |
| 1096 |
} |
| 1097 |
pn_proactor_t *bp = batch_proactor(batch); /* Proactor events */ |
| 1098 |
if (bp == p) { |
| 1099 |
p->batch_working = false; |
| 1100 |
if (p->timeout_processed) { |
| 1101 |
p->timeout_processed = false; |
| 1102 |
check_for_inactive(p); |
| 1103 |
} |
| 1104 |
} |
| 1105 |
uv_mutex_unlock(&p->lock); |
| 1106 |
notify(p); |
| 1107 |
} |
| 1108 |
|
| 1109 |
pn_listener_t *pn_event_listener(pn_event_t *e) { |
| 1110 |
if (pn_event_class(e) == PN_CLASSCLASS(pn_listener)) { |
| 1111 |
return (pn_listener_t*)pn_event_context(e); |
| 1112 |
} else if (pn_event_class(e) == PN_CLASSCLASS(pn_listener_socket)) { |
| 1113 |
return ((lsocket_t*)pn_event_context(e))->parent; |
| 1114 |
} else { |
| 1115 |
return NULL; |
| 1116 |
} |
| 1117 |
} |
| 1118 |
|
| 1119 |
pn_proactor_t *pn_event_proactor(pn_event_t *e) { |
| 1120 |
if (pn_event_class(e) == PN_CLASSCLASS(pn_proactor)) { |
| 1121 |
return (pn_proactor_t*)pn_event_context(e); |
| 1122 |
} |
| 1123 |
pn_listener_t *l = pn_event_listener(e); |
| 1124 |
if (l) { |
| 1125 |
return l->work.proactor; |
| 1126 |
} |
| 1127 |
pn_connection_t *c = pn_event_connection(e); |
| 1128 |
if (c) { |
| 1129 |
return pn_connection_proactor(pn_event_connection(e)); |
| 1130 |
} |
| 1131 |
return NULL; |
| 1132 |
} |
| 1133 |
|
| 1134 |
void pn_proactor_interrupt(pn_proactor_t *p) { |
| 1135 |
/* NOTE: pn_proactor_interrupt must be async-signal-safe so we cannot use |
| 1136 |
locks to update shared proactor state here. Instead we use a dedicated |
| 1137 |
uv_async, the on_interrupt() callback will set the interrupt flag in the |
| 1138 |
safety of the leader thread. |
| 1139 |
*/ |
| 1140 |
uv_async_send(&p->interrupt); |
| 1141 |
} |
| 1142 |
|
| 1143 |
void pn_proactor_disconnect(pn_proactor_t *p, pn_condition_t *cond) { |
| 1144 |
uv_mutex_lock(&p->lock); |
| 1145 |
if (!p->disconnect) { |
| 1146 |
p->disconnect = true; |
| 1147 |
if (cond) { |
| 1148 |
pn_condition_copy(p->disconnect_cond, cond); |
| 1149 |
} else { |
| 1150 |
pn_condition_clear(p->disconnect_cond); |
| 1151 |
} |
| 1152 |
notify(p); |
| 1153 |
} |
| 1154 |
uv_mutex_unlock(&p->lock); |
| 1155 |
} |
| 1156 |
|
| 1157 |
void pn_proactor_set_timeout(pn_proactor_t *p, pn_millis_t t) { |
| 1158 |
uv_mutex_lock(&p->lock); |
| 1159 |
p->timeout = t; |
| 1160 |
// This timeout *replaces* any existing timeout |
| 1161 |
p->timeout_state = TM_REQUEST; |
| 1162 |
uv_mutex_unlock(&p->lock); |
| 1163 |
notify(p); |
| 1164 |
} |
| 1165 |
|
| 1166 |
void pn_proactor_cancel_timeout(pn_proactor_t *p) { |
| 1167 |
uv_mutex_lock(&p->lock); |
| 1168 |
if (p->timeout_state != TM_NONE) { |
| 1169 |
p->timeout_state = TM_NONE; |
| 1170 |
check_for_inactive(p); |
| 1171 |
notify(p); |
| 1172 |
} |
| 1173 |
uv_mutex_unlock(&p->lock); |
| 1174 |
} |
| 1175 |
|
| 1176 |
void pn_proactor_connect2(pn_proactor_t *p, pn_connection_t *c, pn_transport_t *t, const char *addr) { |
| 1177 |
pconnection_t *pc = pconnection(p, c, t, false); |
| 1178 |
assert(pc); /* TODO aconway 2017-03-31: memory safety */ |
| 1179 |
add_active(p); |
| 1180 |
pn_connection_open(pc->driver.connection); /* Auto-open */ |
| 1181 |
parse_addr(&pc->addr, addr); |
| 1182 |
work_start(&pc->work); |
| 1183 |
} |
| 1184 |
|
| 1185 |
void pn_proactor_listen(pn_proactor_t *p, pn_listener_t *l, const char *addr, int backlog) { |
| 1186 |
work_init(&l->work, p, T_LISTENER); |
| 1187 |
parse_addr(&l->addr, addr); |
| 1188 |
l->backlog = backlog; |
| 1189 |
add_active(l->work.proactor); /* Owned by proactor. Track it for PN_PROACTOR_INACTIVE. */; |
| 1190 |
work_start(&l->work); |
| 1191 |
} |
| 1192 |
|
| 1193 |
static void on_proactor_free(uv_handle_t* h, void* v) { |
| 1194 |
uv_safe_close(h, NULL); /* Close the handle */ |
| 1195 |
if (h->type == UV_TCP) { /* Put the corresponding work item on the leader_q for cleanup */ |
| 1196 |
work_t *w = NULL; |
| 1197 |
switch (*(struct_type*)h->data) { |
| 1198 |
case T_CONNECTION: |
| 1199 |
w = (work_t*)h->data; break; |
| 1200 |
case T_LSOCKET: |
| 1201 |
w = &((lsocket_t*)h->data)->parent->work; break; |
| 1202 |
default: break; |
| 1203 |
} |
| 1204 |
if (w && w->next == work_unqueued) { |
| 1205 |
work_push(&w->proactor->leader_q, w); /* Save to be freed after all closed */ |
| 1206 |
} |
| 1207 |
} |
| 1208 |
} |
| 1209 |
|
| 1210 |
static void work_free(work_t *w) { |
| 1211 |
switch (w->type) { |
| 1212 |
case T_CONNECTION: pconnection_free((pconnection_t*)w); break; |
| 1213 |
case T_LISTENER: pn_listener_free((pn_listener_t*)w); break; |
| 1214 |
default: break; |
| 1215 |
} |
| 1216 |
} |
| 1217 |
|
| 1218 |
pn_proactor_t *pn_proactor() { |
| 1219 |
uv_once(&global_init_once, global_init_fn); |
| 1220 |
pn_proactor_t *p = (pn_proactor_t*)calloc(1, sizeof(pn_proactor_t)); |
| 1221 |
p->collector = pn_collector(); |
| 1222 |
if (!p->collector) { |
| 1223 |
free(p); |
| 1224 |
return NULL; |
| 1225 |
} |
| 1226 |
p->batch.next_event = &proactor_batch_next; |
| 1227 |
uv_loop_init(&p->loop); |
| 1228 |
uv_mutex_init(&p->lock); |
| 1229 |
uv_cond_init(&p->cond); |
| 1230 |
uv_async_init(&p->loop, &p->notify, NULL); |
| 1231 |
uv_async_init(&p->loop, &p->interrupt, on_interrupt); |
| 1232 |
p->interrupt.data = p; |
| 1233 |
uv_timer_init(&p->loop, &p->timer); |
| 1234 |
p->timer.data = p; |
| 1235 |
p->disconnect_cond = pn_condition(); |
| 1236 |
return p; |
| 1237 |
} |
| 1238 |
|
| 1239 |
void pn_proactor_free(pn_proactor_t *p) { |
| 1240 |
/* Close all open handles */ |
| 1241 |
uv_walk(&p->loop, on_proactor_free, NULL); |
| 1242 |
while (uv_loop_alive(&p->loop)) { |
| 1243 |
uv_run(&p->loop, UV_RUN_DEFAULT); /* Finish closing the proactor handles */ |
| 1244 |
} |
| 1245 |
/* Free all work items */ |
| 1246 |
for (work_t *w = work_pop(&p->leader_q); w; w = work_pop(&p->leader_q)) { |
| 1247 |
work_free(w); |
| 1248 |
} |
| 1249 |
for (work_t *w = work_pop(&p->worker_q); w; w = work_pop(&p->worker_q)) { |
| 1250 |
work_free(w); |
| 1251 |
} |
| 1252 |
uv_loop_close(&p->loop); |
| 1253 |
uv_mutex_destroy(&p->lock); |
| 1254 |
uv_cond_destroy(&p->cond); |
| 1255 |
pn_collector_free(p->collector); |
| 1256 |
pn_condition_free(p->disconnect_cond); |
| 1257 |
free(p); |
| 1258 |
} |
| 1259 |
|
| 1260 |
pn_proactor_t *pn_connection_proactor(pn_connection_t* c) { |
| 1261 |
pconnection_t *pc = get_pconnection(c); |
| 1262 |
return pc ? pc->work.proactor : NULL; |
| 1263 |
} |
| 1264 |
|
| 1265 |
void pn_connection_wake(pn_connection_t* c) { |
| 1266 |
/* May be called from any thread */ |
| 1267 |
pconnection_t *pc = get_pconnection(c); |
| 1268 |
if (pc) { |
| 1269 |
bool notify = false; |
| 1270 |
uv_mutex_lock(&pc->lock); |
| 1271 |
if (pc->wake == W_NONE) { |
| 1272 |
pc->wake = W_PENDING; |
| 1273 |
notify = true; |
| 1274 |
} |
| 1275 |
uv_mutex_unlock(&pc->lock); |
| 1276 |
if (notify) { |
| 1277 |
work_notify(&pc->work); |
| 1278 |
} |
| 1279 |
} |
| 1280 |
} |
| 1281 |
|
| 1282 |
void pn_proactor_release_connection(pn_connection_t *c) { |
| 1283 |
pconnection_t *pc = get_pconnection(c); |
| 1284 |
if (pc) { |
| 1285 |
set_pconnection(c, NULL); |
| 1286 |
pn_connection_driver_release_connection(&pc->driver); |
| 1287 |
} |
| 1288 |
} |
| 1289 |
|
| 1290 |
pn_listener_t *pn_listener(void) { |
| 1291 |
pn_listener_t *l = (pn_listener_t*)calloc(1, sizeof(pn_listener_t)); |
| 1292 |
if (l) { |
| 1293 |
l->batch.next_event = listener_batch_next; |
| 1294 |
l->collector = pn_collector(); |
| 1295 |
l->condition = pn_condition(); |
| 1296 |
l->attachments = pn_record(); |
| 1297 |
if (!l->condition || !l->collector || !l->attachments) { |
| 1298 |
pn_listener_free(l); |
| 1299 |
return NULL; |
| 1300 |
} |
| 1301 |
uv_mutex_init(&l->lock); |
| 1302 |
} |
| 1303 |
return l; |
| 1304 |
} |
| 1305 |
|
| 1306 |
void pn_listener_close(pn_listener_t* l) { |
| 1307 |
/* May be called from any thread */ |
| 1308 |
uv_mutex_lock(&l->lock); |
| 1309 |
listener_close_lh(l); |
| 1310 |
uv_mutex_unlock(&l->lock); |
| 1311 |
} |
| 1312 |
|
| 1313 |
pn_proactor_t *pn_listener_proactor(pn_listener_t* l) { |
| 1314 |
return l ? l->work.proactor : NULL; |
| 1315 |
} |
| 1316 |
|
| 1317 |
pn_condition_t* pn_listener_condition(pn_listener_t* l) { |
| 1318 |
return l->condition; |
| 1319 |
} |
| 1320 |
|
| 1321 |
void *pn_listener_get_context(pn_listener_t *l) { |
| 1322 |
return l->context; |
| 1323 |
} |
| 1324 |
|
| 1325 |
void pn_listener_set_context(pn_listener_t *l, void *context) { |
| 1326 |
l->context = context; |
| 1327 |
} |
| 1328 |
|
| 1329 |
pn_record_t *pn_listener_attachments(pn_listener_t *l) { |
| 1330 |
return l->attachments; |
| 1331 |
} |
| 1332 |
|
| 1333 |
void pn_listener_accept2(pn_listener_t *l, pn_connection_t *c, pn_transport_t *t) { |
| 1334 |
pconnection_t *pc = pconnection(l->work.proactor, c, t, true); |
| 1335 |
assert(pc); |
| 1336 |
add_active(l->work.proactor); |
| 1337 |
uv_mutex_lock(&l->lock); |
| 1338 |
/* Get the socket from the accept event that we are processing */ |
| 1339 |
pn_event_t *e = pn_collector_prev(l->collector); |
| 1340 |
assert(pn_event_type(e) == PN_LISTENER_ACCEPT); |
| 1341 |
assert(pn_event_listener(e) == l); |
| 1342 |
pc->lsocket = (lsocket_t*)pn_event_context(e); |
| 1343 |
pc->connected = 1; /* Don't need to connect() */ |
| 1344 |
pconnection_push(&l->accept, pc); |
| 1345 |
uv_mutex_unlock(&l->lock); |
| 1346 |
work_notify(&l->work); |
| 1347 |
} |
| 1348 |
|
| 1349 |
const pn_netaddr_t *pn_transport_local_addr(pn_transport_t *t) { |
| 1350 |
pconnection_t *pc = get_pconnection(pn_transport_connection(t)); |
| 1351 |
return pc? &pc->local : NULL; |
| 1352 |
} |
| 1353 |
|
| 1354 |
const pn_netaddr_t *pn_transport_remote_addr(pn_transport_t *t) { |
| 1355 |
pconnection_t *pc = get_pconnection(pn_transport_connection(t)); |
| 1356 |
return pc ? &pc->remote : NULL; |
| 1357 |
} |
| 1358 |
|
| 1359 |
const pn_netaddr_t *pn_listener_addr(pn_listener_t *l) { |
| 1360 |
return l->addrs ? &l->addrs[0] : NULL; |
| 1361 |
} |
| 1362 |
|
| 1363 |
pn_millis_t pn_proactor_now(void) { |
| 1364 |
return (pn_millis_t) pn_proactor_now_64(); |
| 1365 |
} |
| 1366 |
|
| 1367 |
int64_t pn_proactor_now_64(void) { |
| 1368 |
return uv_hrtime() / 1000000; // uv_hrtime returns time in nanoseconds |
| 1369 |
} |
| 1370 |
|
| 1371 |
// Empty stubs for raw connection code |
| 1372 |
pn_raw_connection_t *pn_raw_connection(void) { return NULL; } |
| 1373 |
void pn_proactor_raw_connect(pn_proactor_t *p, pn_raw_connection_t *rc, const char *addr) {} |
| 1374 |
void pn_listener_raw_accept(pn_listener_t *l, pn_raw_connection_t *rc) {} |
| 1375 |
void pn_raw_connection_wake(pn_raw_connection_t *conn) {} |
| 1376 |
void pn_raw_connection_close(pn_raw_connection_t *conn) {} |
| 1377 |
void pn_raw_connection_read_close(pn_raw_connection_t *conn) {} |
| 1378 |
void pn_raw_connection_write_close(pn_raw_connection_t *conn) {} |
| 1379 |
const struct pn_netaddr_t *pn_raw_connection_local_addr(pn_raw_connection_t *connection) { return NULL; } |
| 1380 |
const struct pn_netaddr_t *pn_raw_connection_remote_addr(pn_raw_connection_t *connection) { return NULL; } |