Lines Matching refs:ctx
60 #define donefunc_p(ctx) ((ctx).donefunc != NULL) argument
77 struct ctl_cctx * ctx; member
154 struct ctl_cctx *ctx; in ctl_client() local
159 ctx = memget(sizeof *ctx); in ctl_client()
160 if (ctx == NULL) { in ctl_client()
164 ctx->state = initializing; in ctl_client()
165 ctx->ev = lev; in ctl_client()
166 ctx->logger = logger; in ctl_client()
167 ctx->timeout = evConsTime(timeout, 0); in ctl_client()
168 ctx->donefunc = donefunc; in ctl_client()
169 ctx->uap = uap; in ctl_client()
170 ctx->coID.opaque = NULL; in ctl_client()
171 ctx->tiID.opaque = NULL; in ctl_client()
172 ctx->rdID.opaque = NULL; in ctl_client()
173 ctx->wrID.opaque = NULL; in ctl_client()
174 buffer_init(ctx->inbuf); in ctl_client()
175 INIT_LIST(ctx->tran); in ctl_client()
176 INIT_LIST(ctx->wtran); in ctl_client()
177 ctx->sock = socket(sap->sa_family, SOCK_STREAM, PF_UNSPEC); in ctl_client()
178 if (ctx->sock > evHighestFD(ctx->ev)) { in ctl_client()
179 ctx->sock = -1; in ctl_client()
182 if (ctx->sock < 0) { in ctl_client()
183 (*ctx->logger)(ctl_error, "%s: socket: %s", in ctl_client()
188 if (setsockopt(ctx->sock, SOL_SOCKET, SO_REUSEADDR, in ctl_client()
190 (*ctx->logger)(ctl_warning, in ctl_client()
195 if (bind(ctx->sock, captmp, cap_len) < 0) { in ctl_client()
196 (*ctx->logger)(ctl_error, "%s: bind: %s", me, in ctl_client()
201 if (evConnect(lev, ctx->sock, (const struct sockaddr *)sap, sap_len, in ctl_client()
202 conn_done, ctx, &ctx->coID) < 0) { in ctl_client()
203 (*ctx->logger)(ctl_error, "%s: evConnect(fd %d): %s", in ctl_client()
204 me, ctx->sock, strerror(errno)); in ctl_client()
206 if (ctx != NULL) { in ctl_client()
207 if (ctx->sock >= 0) in ctl_client()
208 close(ctx->sock); in ctl_client()
209 memput(ctx, sizeof *ctx); in ctl_client()
213 new_state(ctx, connecting); in ctl_client()
214 return (ctx); in ctl_client()
223 ctl_endclient(struct ctl_cctx *ctx) { in ctl_endclient() argument
224 if (ctx->state != destroyed) in ctl_endclient()
225 destroy(ctx, 0); in ctl_endclient()
226 memput(ctx, sizeof *ctx); in ctl_endclient()
236 ctl_command(struct ctl_cctx *ctx, const char *cmd, size_t len, in ctl_command() argument
243 switch (ctx->state) { in ctl_command()
257 tran = new_tran(ctx, donefunc, uap, 1); in ctl_command()
260 if (ctl_bufget(&tran->outbuf, ctx->logger) < 0) in ctl_command()
268 start_write(ctx); in ctl_command()
275 new_tran(struct ctl_cctx *ctx, ctl_clntdone donefunc, void *uap, int w) { in new_tran() argument
280 new->ctx = ctx; in new_tran()
286 APPEND(ctx->tran, new, link); in new_tran()
288 APPEND(ctx->wtran, new, wlink); in new_tran()
293 start_write(struct ctl_cctx *ctx) { in start_write() argument
299 REQUIRE(ctx->state == connecting || ctx->state == connected); in start_write()
301 if (ctx->wrID.opaque != NULL) in start_write()
304 if (EMPTY(ctx->wtran)) { in start_write()
305 if (ctx->tiID.opaque != NULL) in start_write()
306 stop_timer(ctx); in start_write()
310 tran = HEAD(ctx->wtran); in start_write()
311 UNLINK(ctx->wtran, tran, wlink); in start_write()
313 if (ctx->tiID.opaque != NULL) in start_write()
314 touch_timer(ctx); in start_write()
316 start_timer(ctx); in start_write()
317 if (ctx->state == destroyed) in start_write()
323 if (evWrite(ctx->ev, ctx->sock, iov, iovp - iov, in start_write()
324 write_done, tran, &ctx->wrID) < 0) { in start_write()
325 (*ctx->logger)(ctl_error, "%s: evWrite: %s", me, in start_write()
327 error(ctx); in start_write()
330 if (evTimeRW(ctx->ev, ctx->wrID, ctx->tiID) < 0) { in start_write()
331 (*ctx->logger)(ctl_error, "%s: evTimeRW: %s", me, in start_write()
333 error(ctx); in start_write()
339 destroy(struct ctl_cctx *ctx, int notify) { in destroy() argument
342 if (ctx->sock != -1) { in destroy()
343 (void) close(ctx->sock); in destroy()
344 ctx->sock = -1; in destroy()
346 switch (ctx->state) { in destroy()
348 REQUIRE(ctx->wrID.opaque == NULL); in destroy()
349 REQUIRE(EMPTY(ctx->tran)); in destroy()
354 if (ctx->coID.opaque != NULL) { in destroy()
355 (void)evCancelConn(ctx->ev, ctx->coID); in destroy()
356 ctx->coID.opaque = NULL; in destroy()
360 REQUIRE(ctx->coID.opaque == NULL); in destroy()
361 if (ctx->wrID.opaque != NULL) { in destroy()
362 (void)evCancelRW(ctx->ev, ctx->wrID); in destroy()
363 ctx->wrID.opaque = NULL; in destroy()
365 if (ctx->rdID.opaque != NULL) in destroy()
366 stop_read(ctx); in destroy()
373 if (allocated_p(ctx->inbuf)) in destroy()
374 ctl_bufput(&ctx->inbuf); in destroy()
375 for (this = HEAD(ctx->tran); this != NULL; this = next) { in destroy()
380 (*this->donefunc)(ctx, this->uap, NULL, 0); in destroy()
383 if (ctx->tiID.opaque != NULL) in destroy()
384 stop_timer(ctx); in destroy()
385 new_state(ctx, destroyed); in destroy()
389 error(struct ctl_cctx *ctx) { in error() argument
390 REQUIRE(ctx->state != destroyed); in error()
391 destroy(ctx, 1); in error()
395 new_state(struct ctl_cctx *ctx, enum state new_state) { in new_state() argument
398 (*ctx->logger)(ctl_debug, "%s: %s -> %s", me, in new_state()
399 state_names[ctx->state], state_names[new_state]); in new_state()
400 ctx->state = new_state; in new_state()
409 struct ctl_cctx *ctx = uap; in conn_done() local
418 ctx->coID.opaque = NULL; in conn_done()
420 (*ctx->logger)(ctl_error, "%s: evConnect: %s", me, in conn_done()
422 error(ctx); in conn_done()
425 new_state(ctx, connected); in conn_done()
426 tran = new_tran(ctx, ctx->donefunc, ctx->uap, 0); in conn_done()
428 (*ctx->logger)(ctl_error, "%s: new_tran failed: %s", me, in conn_done()
430 error(ctx); in conn_done()
433 start_read(ctx); in conn_done()
434 if (ctx->state == destroyed) { in conn_done()
435 (*ctx->logger)(ctl_error, "%s: start_read failed: %s", in conn_done()
437 error(ctx); in conn_done()
445 struct ctl_cctx *ctx = tran->ctx; in write_done() local
450 ctx->wrID.opaque = NULL; in write_done()
451 if (ctx->tiID.opaque != NULL) in write_done()
452 touch_timer(ctx); in write_done()
454 start_write(ctx); in write_done()
456 destroy(ctx, 1); in write_done()
458 start_read(ctx); in write_done()
462 start_read(struct ctl_cctx *ctx) { in start_read() argument
465 REQUIRE(ctx->state == connecting || ctx->state == connected); in start_read()
466 REQUIRE(ctx->rdID.opaque == NULL); in start_read()
467 if (evSelectFD(ctx->ev, ctx->sock, EV_READ, readable, ctx, in start_read()
468 &ctx->rdID) < 0) in start_read()
470 (*ctx->logger)(ctl_error, "%s: evSelect(fd %d): %s", me, in start_read()
471 ctx->sock, strerror(errno)); in start_read()
472 error(ctx); in start_read()
478 stop_read(struct ctl_cctx *ctx) { in stop_read() argument
479 REQUIRE(ctx->coID.opaque == NULL); in stop_read()
480 REQUIRE(ctx->rdID.opaque != NULL); in stop_read()
481 (void)evDeselectFD(ctx->ev, ctx->rdID); in stop_read()
482 ctx->rdID.opaque = NULL; in stop_read()
488 struct ctl_cctx *ctx = uap; in readable() local
495 REQUIRE(ctx != NULL); in readable()
498 REQUIRE(ctx->state == connected); in readable()
499 REQUIRE(!EMPTY(ctx->tran)); in readable()
500 tran = HEAD(ctx->tran); in readable()
501 if (!allocated_p(ctx->inbuf) && in readable()
502 ctl_bufget(&ctx->inbuf, ctx->logger) < 0) { in readable()
503 (*ctx->logger)(ctl_error, "%s: can't get an input buffer", me); in readable()
504 error(ctx); in readable()
507 n = read(ctx->sock, ctx->inbuf.text + ctx->inbuf.used, in readable()
508 MAX_LINELEN - ctx->inbuf.used); in readable()
510 (*ctx->logger)(ctl_warning, "%s: read: %s", me, in readable()
512 error(ctx); in readable()
515 if (ctx->tiID.opaque != NULL) in readable()
516 touch_timer(ctx); in readable()
517 ctx->inbuf.used += n; in readable()
518 (*ctx->logger)(ctl_debug, "%s: read %d, used %d", me, in readable()
519 n, ctx->inbuf.used); in readable()
521 eos = memchr(ctx->inbuf.text, '\n', ctx->inbuf.used); in readable()
522 if (eos != NULL && eos != ctx->inbuf.text && eos[-1] == '\r') { in readable()
526 if (!arpacode_p(ctx->inbuf.text)) { in readable()
528 (*ctx->logger)(ctl_error, "%s: no arpa code (%s)", me, in readable()
529 ctx->inbuf.text); in readable()
530 error(ctx); in readable()
533 if (arpadone_p(ctx->inbuf.text)) in readable()
535 else if (arpacont_p(ctx->inbuf.text)) in readable()
539 (*ctx->logger)(ctl_error, "%s: no arpa flag (%s)", me, in readable()
540 ctx->inbuf.text); in readable()
541 error(ctx); in readable()
544 (*tran->donefunc)(ctx, tran->uap, ctx->inbuf.text, in readable()
546 ctx->inbuf.used -= ((eos - ctx->inbuf.text) + 1); in readable()
547 if (ctx->inbuf.used == 0U) in readable()
548 ctl_bufput(&ctx->inbuf); in readable()
550 memmove(ctx->inbuf.text, eos + 1, ctx->inbuf.used); in readable()
552 UNLINK(ctx->tran, tran, link); in readable()
554 stop_read(ctx); in readable()
555 start_write(ctx); in readable()
558 if (allocated_p(ctx->inbuf)) in readable()
562 if (ctx->inbuf.used == (size_t)MAX_LINELEN) { in readable()
563 (*ctx->logger)(ctl_error, "%s: line too long (%-10s...)", me, in readable()
564 ctx->inbuf.text); in readable()
565 error(ctx); in readable()
572 start_timer(struct ctl_cctx *ctx) { in start_timer() argument
575 REQUIRE(ctx->tiID.opaque == NULL); in start_timer()
576 if (evSetIdleTimer(ctx->ev, timer, ctx, ctx->timeout, &ctx->tiID) < 0){ in start_timer()
577 (*ctx->logger)(ctl_error, "%s: evSetIdleTimer: %s", me, in start_timer()
579 error(ctx); in start_timer()
585 stop_timer(struct ctl_cctx *ctx) { in stop_timer() argument
588 REQUIRE(ctx->tiID.opaque != NULL); in stop_timer()
589 if (evClearIdleTimer(ctx->ev, ctx->tiID) < 0) { in stop_timer()
590 (*ctx->logger)(ctl_error, "%s: evClearIdleTimer: %s", me, in stop_timer()
592 error(ctx); in stop_timer()
595 ctx->tiID.opaque = NULL; in stop_timer()
599 touch_timer(struct ctl_cctx *ctx) { in touch_timer() argument
600 REQUIRE(ctx->tiID.opaque != NULL); in touch_timer()
602 evTouchIdleTimer(ctx->ev, ctx->tiID); in touch_timer()
608 struct ctl_cctx *ctx = uap; in timer() local
614 ctx->tiID.opaque = NULL; in timer()
615 (*ctx->logger)(ctl_error, "%s: timeout after %u seconds while %s", me, in timer()
616 ctx->timeout.tv_sec, state_names[ctx->state]); in timer()
617 error(ctx); in timer()