PROTON-1942: [c] separate pn_message_encode2 from pn_message_send
Project: http://git-wip-us.apache.org/repos/asf/qpid-proton/repo Commit: http://git-wip-us.apache.org/repos/asf/qpid-proton/commit/b429db0f Tree: http://git-wip-us.apache.org/repos/asf/qpid-proton/tree/b429db0f Diff: http://git-wip-us.apache.org/repos/asf/qpid-proton/diff/b429db0f Branch: refs/heads/go1 Commit: b429db0f70f5b37c907224d6c7ee0f2a698fd6e9 Parents: 77c6354 Author: Alan Conway <acon...@redhat.com> Authored: Thu Sep 27 13:17:32 2018 -0400 Committer: Alan Conway <acon...@redhat.com> Committed: Thu Sep 27 14:07:55 2018 -0400 ---------------------------------------------------------------------- c/include/proton/message.h | 34 ++++++++++++++++++++-------------- c/src/core/message.c | 25 ++++++++++++++++--------- 2 files changed, 36 insertions(+), 23 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/b429db0f/c/include/proton/message.h ---------------------------------------------------------------------- diff --git a/c/include/proton/message.h b/c/include/proton/message.h index 9340d03..41c37af 100644 --- a/c/include/proton/message.h +++ b/c/include/proton/message.h @@ -737,6 +737,20 @@ PN_EXTERN int pn_message_decode(pn_message_t *msg, const char *bytes, size_t siz */ PN_EXTERN int pn_message_encode(pn_message_t *msg, char *bytes, size_t *size); +/** + * **Unsettled API**: Encode a message, allocating space if necessary + * + * @param[in] msg A message object. + * @param[inout] buf Used to encode msg. + * If buf->start == NULL memory is allocated with malloc(). + * If buf->size is not large enough, buffer is expanded with realloc(). + * On return buf holds the address and size of the final buffer. + * buf->size may be larger than the length of the encoded message. + * @return The length of the encoded message or an error code (<0). + * On error pn_message_error(msg) will provide more information. + */ +PN_EXTERN ssize_t pn_message_encode2(pn_message_t *msg, pn_rwbytes_t *buf); + struct pn_link_t; /** @@ -745,8 +759,7 @@ struct pn_link_t; * Encode and send a message on a sender link. * * Performs the following steps: - * - create or expand the buffer @p buf as required - * - call pn_message_encode() to encode the message to a buffer + * - call pn_message_encode2() to encode the message to a buffer * - call pn_link_send() to send the encoded message bytes * - call pn_link_advance() to indicate the message is complete * @@ -755,18 +768,11 @@ struct pn_link_t; * * @param[in] msg A message object. * @param[in] sender A sending link. - * The message will be encoded and sent with pn_link_send() - * @param[inout] buf Used to encode the message. - * - if buf == NULL, temporary space will be allocated and freed with malloc()/free() - * - if buf->start != NULL and buf->size is large enough, the message is encoded to - * buf->start - * - if buf->start == NULL or buf->size is not enough, the buffer will be extended like this: - * - * buf->size = new_size; buf->start = realloc(buf->start, new_size) - * - * it is possible for the buffer to be extended more than once. - * @return The number of bytes encoded and sent on success. - * Returns an error code (< 0) on failure and sets pn_message_error() on msg + * @param[inout] buf See pn_message_encode2. If buf == NULL then + * any memory needed for encoding will be allocated and freed by pn_message_send(). + * + * @return The length of the encoded message or an error code (<0). + * On error pn_message_error(msg) will provide more information. */ PN_EXTERN ssize_t pn_message_send(pn_message_t *msg, pn_link_t *sender, pn_rwbytes_t *buf); http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/b429db0f/c/src/core/message.c ---------------------------------------------------------------------- diff --git a/c/src/core/message.c b/c/src/core/message.c index dbbdacf..41ccd08 100644 --- a/c/src/core/message.c +++ b/c/src/core/message.c @@ -897,13 +897,11 @@ pn_data_t *pn_message_body(pn_message_t *msg) return msg ? msg->body : NULL; } -PN_EXTERN ssize_t pn_message_send(pn_message_t *msg, pn_link_t *sender, pn_rwbytes_t *buffer) { +ssize_t pn_message_encode2(pn_message_t *msg, pn_rwbytes_t *buffer) { static const size_t initial_size = 256; - pn_rwbytes_t local_buf = { 0 }; - ssize_t err = 0; + int err = 0; size_t size = 0; - if (buffer == NULL) buffer = &local_buf; if (buffer->start == NULL) { buffer->start = (char*)malloc(initial_size); buffer->size = initial_size; @@ -916,9 +914,18 @@ PN_EXTERN ssize_t pn_message_send(pn_message_t *msg, pn_link_t *sender, pn_rwbyt if (buffer->start == NULL) return PN_OUT_OF_MEMORY; size = buffer->size; } - if (err >= 0) err = pn_link_send(sender, buffer->start, size); - if (err >= 0) err = pn_link_advance(sender); - if (err < 0) pn_error_copy(pn_message_error(msg), pn_link_error(sender)); - free(local_buf.start); - return err; + return err == 0 ? (ssize_t)size : err; +} + +ssize_t pn_message_send(pn_message_t *msg, pn_link_t *sender, pn_rwbytes_t *buffer) { + pn_rwbytes_t local_buf = { 0 }; + if (!buffer) buffer = &local_buf; + ssize_t ret = pn_message_encode2(msg, buffer); + if (ret >= 0) { + ret = pn_link_send(sender, buffer->start, ret); + if (ret >= 0) ret = pn_link_advance(sender); + if (ret < 0) pn_error_copy(pn_message_error(msg), pn_link_error(sender)); + } + if (local_buf.start) free(local_buf.start); + return ret; } --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@qpid.apache.org For additional commands, e-mail: commits-h...@qpid.apache.org