ssize_t unpack_longstr(mnbytestream_t *bs, int fd, mnbytes_t **v) { uint32_t sz; if (unpack_long(bs, fd, &sz) < 0) { TRRET(UNPACK_ECONSUME); } /* * reserve for terminating zero, not to be counted in AMQP */ *v = bytes_new(sz + 1); while (SAVAIL(bs) < (ssize_t)sz) { if (bytestream_consume_data(bs, fd) != 0) { TRRET(UNPACK_ECONSUME); } } memcpy(BDATA(*v), SPDATA(bs), sz); BDATA(*v)[sz] = '\0'; SADVANCEPOS(bs, sz); BYTES_INCREF(*v); return sizeof(uint32_t) + sz; }
void *malloc(size_t size) { t_block *b; b = malloc_b(size); return (BDATA(b)); }
int amqp_rpc_setup_client(amqp_rpc_t *rpc, amqp_channel_t *chan) { int res; res = 0; rpc->chan = chan; if (rpc->reply_to == NULL) { if (amqp_channel_declare_queue_ex(chan, "", DECLARE_QUEUE_FEXCLUSIVE | DECLARE_EXCHANGE_FAUTODELETE, NULL, amqp_rpc_setup_client_cb0, NULL, rpc) != 0) { res = AMQP_RPC_SETUP_CLIENT + 1; goto err; } assert(rpc->reply_to != NULL); if (*rpc->exchange != '\0') { if (amqp_channel_bind_queue(chan, (char *)BDATA(rpc->reply_to), rpc->exchange, (char *)BDATA(rpc->reply_to), 0) != 0) { res = AMQP_RPC_SETUP_CLIENT + 2; goto err; } } } if ((rpc->cons = amqp_channel_create_consumer(chan, (char *)BDATA(rpc->reply_to), NULL, CONSUME_FNOACK)) == NULL) { res = AMQP_RPC_SETUP_CLIENT + 3; goto err; } rpc->cccb = amqp_rpc_client_cb; rpc->clcb = amqp_rpc_cancel_cb; end: return res; err: TR(res); goto end; }
/* * server */ static int amqp_rpc_server_cb(UNUSED amqp_frame_t *method, amqp_frame_t *header, char *data, void *udata) { int res; amqp_rpc_t *rpc; amqp_header_t *callback_header; char *callback_data; rpc = udata; callback_header = NULL; callback_data = NULL; assert(rpc != NULL); rpc->server_handler(header->payload.header, data, &callback_header, &callback_data, rpc->server_udata); res = 0; if (header->payload.header->reply_to != NULL) { if (callback_header != NULL) { if (header->payload.header->correlation_id != NULL) { AMQP_HEADER_SET_REF(correlation_id)(callback_header, header->payload.header->correlation_id); } callback_header->class_id = AMQP_BASIC; res = amqp_channel_publish_ex( rpc->chan, rpc->exchange, (char *)BDATA(header->payload.header->reply_to), 0, callback_header, callback_data); /* take header over, no free() on the handler side */ callback_header = NULL; } else { CTRACE("server handler returned NULL callback header, " "discarding reply"); } } else { amqp_header_destroy(&callback_header); CTRACE("no reply_to in the incoming call, discarding server reply"); } if (callback_data != NULL) { free(callback_data); } if (data != NULL) { free(data); } return res; }
t_block *fusion_block(t_block *beg) { t_block *end; end = beg; while (beg->prev != NULL && beg->prev->is_free) beg = beg->prev; while (end->next != NULL && end->next->is_free) end = end->next; if (beg == end) { beg->is_free = 1; return (beg); } beg->size = BDATA(end) + end->size - BDATA(beg); beg->next = end->next; beg->is_free = 1; return (beg); }
t_block *ft_find_ptr_in_page(t_page *page, void *ptr) { t_block *block; block = page->first; while (block != NULL) { if (ptr == BDATA(block)) return (block); block = block->next; } return (NULL); }
/* * longstr */ void pack_longstr(mnbytestream_t *bs, mnbytes_t *s) { union { uint32_t sz; char c; } u; u.sz = htobe32((uint32_t)s->sz - 1); /* * discard terminating zero, not to be counted in AMQP */ (void)bytestream_cat(bs, sizeof(uint32_t), &u.c); (void)bytestream_cat(bs, s->sz - 1, (char *)BDATA(s)); }
/* * shortstr */ void pack_shortstr(mnbytestream_t *bs, mnbytes_t *s) { union { uint8_t sz; char c; } u; /* * discard terminating zero, not to be counted in AMQP */ u.sz = (uint8_t)s->sz - 1; (void)bytestream_cat(bs, sizeof(uint8_t), &u.c); (void)bytestream_cat(bs, u.sz, (char *)BDATA(s)); }
void split_block(t_block *b, size_t mem_width) { t_block *nb; if (b->size + BLOCK_SIZE == mem_width) return ; if (mem_width < b->size + BLOCK_SIZE * 2) { b->size = mem_width - BLOCK_SIZE; return ; } nb = (t_block*)(BDATA(b) + b->size - 1); init_block(nb, mem_width - BLOCK_SIZE * 2 - b->size); nb->next = b->next; b->next = nb; nb->prev = b; nb->is_free = 1; }
int amqp_rpc_teardown(amqp_rpc_t *rpc) { int res; res = 0; if (rpc->cons != NULL) { if ((res = amqp_close_consumer(rpc->cons)) != 0) { TR(res); } rpc->cons = NULL; } if (rpc->chan != NULL && rpc->reply_to != NULL) { if ((res = amqp_channel_delete_queue(rpc->chan, (char *)BDATA(rpc->reply_to), 0)) != 0) { TR(res); } } return res; }
static int amqp_rpc_client_cb(UNUSED amqp_frame_t *method, amqp_frame_t *header, char *data, void *udata) { int res; amqp_rpc_t *rpc; rpc = udata; res = 0; if (header->payload.header->correlation_id != NULL) { mnhash_item_t *dit; if ((dit = hash_get_item(&rpc->calls, header->payload.header->correlation_id)) == NULL) { CTRACE("no pending call for correlation_id %s, ignoring", (char *)BDATA(header->payload.header->correlation_id)); if (data != NULL) { free(data); } } else { rpc_call_completion_t *cc; cc = dit->value; if (cc->response_cb != NULL) { res = cc->response_cb(method, header, data, cc->udata); } mrkthr_signal_send(&cc->sig); } } else { CTRACE("no correlation_id in header, ignoring:"); amqp_frame_dump(header); if (data != NULL) { free(data); } } return res; }
int main(UNUSED int argc, char **argv) { char ch; int res; mrkpbc_ctx_t ctx; mnbytes_t *namein, *nameout0, *nameout1; FILE *in, *out0, *out1; #ifdef HAVE_MALLOC_H # ifndef NDEBUG /* * malloc options */ if (mallopt(M_CHECK_ACTION, 1) != 1) { FAIL("mallopt"); } if (mallopt(M_PERTURB, 0x5a) != 1) { FAIL("mallopt"); } # endif #endif while ((ch = getopt_long(argc, argv, "h", longopts, NULL)) != -1) { switch (ch) { case 'h': usage(argv[0]); exit(0); break; case ':': /* missing option argument */ usage(argv[0]); errx(1, "Missing option argument"); break; case '?': /* unknown option */ usage(argv[0]); errx(1, "Unknown option"); break; default: usage(argv[0]); errx(1, "Unknown error"); break; } } argc -= optind; argv += optind; mrkpbc_ctx_init(&ctx); if (argc < 1) { namein = bytes_new_from_str("test"); in = stdin; //nameout0 = namein; nameout0 = NULL; out0 = stdout; nameout1 = namein; out1 = stdout; } else { char *s0, *s1; char *fname; if ((s0 = strdup(argv[0])) == NULL) { errx(1, "strdup error"); } if ((s1 = strdup(argv[0])) == NULL) { errx(1, "strdup error"); } fname = basename(s1); namein = bytes_new_from_str(fname); nameout0 = bytes_printf("%s.h", argv[0]); nameout1 = bytes_printf("%s.c", argv[0]); if ((in = fopen(argv[0], "r")) == NULL) { errx(1, "fopen error on argv[0]"); } if ((out0 = fopen((char *)BDATA(nameout0), "w")) == NULL) { errx(1, "fopen error on hfile"); } if ((out1 = fopen((char *)BDATA(nameout1), "w")) == NULL) { errx(1, "fopen error on hfile"); } free(s0); free(s1); } mrkpbc_ctx_init_c(&ctx, namein, in, nameout0, out0, nameout1, out1); if ((res = mrkpbc_scan(&ctx)) != 0) { goto end; } if ((mrkpbc_ctx_validate(&ctx)) != 0) { goto end; } mrkpbc_ctx_render_c(&ctx); end: mrkpbc_ctx_fini(&ctx); return 0; }
static int table_str_cb(mnbytes_t *key, amqp_value_t *val, mnbytestream_t *bs) { bytestream_nprintf(bs, 1024, "%s=", BDATA(key)); switch (val->ty->tag) { case AMQP_TBOOL: (void)bytestream_nprintf(bs, 1024, "%s ", val->value.b ? "#t" : "#f"); break; case AMQP_TINT8: (void)bytestream_nprintf(bs, 1024, "%hhd ", val->value.i8); break; case AMQP_TUINT8: (void)bytestream_nprintf(bs, 1024, "0x%02hhx ", val->value.u8); break; case AMQP_TINT16: (void)bytestream_nprintf(bs, 1024, "%hd ", val->value.i16); break; case AMQP_TUINT16: (void)bytestream_nprintf(bs, 1024, "0x%04hx ", val->value.u16); break; case AMQP_TINT32: (void)bytestream_nprintf(bs, 1024, "%d ", val->value.i32); break; case AMQP_TUINT32: (void)bytestream_nprintf(bs, 1024, "0x%08x ", val->value.u32); break; case AMQP_TINT64: (void)bytestream_nprintf(bs, 1024, "%ld ", val->value.i64); break; case AMQP_TUINT64: (void)bytestream_nprintf(bs, 1024, "0x%016lx ", val->value.u64); break; case AMQP_TFLOAT: (void)bytestream_nprintf(bs, 1024, "%f ", val->value.f); break; case AMQP_TDOUBLE: (void)bytestream_nprintf(bs, 1024, "%d ", val->value.d); break; case AMQP_TSSTR: case AMQP_TLSTR: if (val->value.str != NULL) { (void)bytestream_nprintf(bs, 1024, "'%s' ", BDATA(val->value.str)); } else { (void)bytestream_nprintf(bs, 1024, "%s ", NULL); } break; case AMQP_TTABLE: table_str(&val->value.t, bs); break; default: (void)bytestream_nprintf(bs, 1024, "... "); } return 0; }