diff --git a/lib/cbor-parser.c b/lib/cbor-parser.c index cda3da60..c8e8b68c 100644 --- a/lib/cbor-parser.c +++ b/lib/cbor-parser.c @@ -325,3 +325,12 @@ cbor_stream_attach(struct cbor_stream *stream, sock *sk) sk->data = stream; sk->rx_hook = cbor_stream_rx; } + +void +cbor_channel_done(struct cbor_channel *channel) +{ + struct cbor_stream *stream = stream; + HASH_REMOVE(stream->channels, CCH, channel); + rp_free(channel->p); + sl_free(channel); +} diff --git a/lib/cbor.c b/lib/cbor.c index 233758ec..9d775af1 100644 --- a/lib/cbor.c +++ b/lib/cbor.c @@ -194,6 +194,25 @@ bool cbor_put_close(struct cbor_writer *w, u64 actual_size, bool strict) /* Tags: TODO! */ +/* Writer contexts */ +struct cbor_writer * +cbor_reply_init(struct cbor_channel *cch) +{ + struct cbor_writer *cw = &cch->writer; + if (cch->stream->s->tbuf != cch->stream->s->tpos) + bug("Not implemented reply to not-fully-flushed buffer"); + + cbor_writer_init(cw, cch->stream->writer_depth, cch->stream->s->tbuf, cch->stream->s->tbsize); + return cw; +} + +void +cbor_reply_send(struct cbor_channel *cch, struct cbor_writer *cw) +{ + ASSERT_DIE(cw == &cch->writer); + sk_send(cch->stream->s, cw->data.pos - cw->data.start); +} + #if 0 void cbor_epoch_time(struct cbor_writer *writer, int64_t time, int shift) diff --git a/lib/cbor.h b/lib/cbor.h index 1a29f009..2add96ae 100644 --- a/lib/cbor.h +++ b/lib/cbor.h @@ -212,6 +212,7 @@ struct cbor_stream { CSTR_FINISH, CSTR_CLEANUP, } state; + uint writer_depth; struct cbor_parser_context parser; }; @@ -228,6 +229,7 @@ struct cbor_channel { pool *p; u64 id; u64 idhash; + struct cbor_writer writer; }; extern struct cbor_channel cbor_channel_parse_error; @@ -236,7 +238,7 @@ extern struct cbor_channel cbor_channel_parse_error; struct cbor_channel *cbor_channel_new(struct cbor_stream *); /* Drop the channel */ -void cbor_done_channel(struct cbor_channel *); +void cbor_channel_done(struct cbor_channel *); struct cbor_writer *cbor_reply_init(struct cbor_channel *); void cbor_reply_send(struct cbor_channel *, struct cbor_writer *);