Create a message with a map { "sequence" : number } encode it and return the encoded buffer. */
| 59 | |
| 60 | /* Create a message with a map { "sequence" : number } encode it and return the encoded buffer. */ |
| 61 | static pn_bytes_t encode_message(app_data_t* app) { |
| 62 | /* Construct a message with the map { "sequence": app.sent } */ |
| 63 | pn_message_t* message = pn_message(); |
| 64 | pn_data_t* body = pn_message_body(message); |
| 65 | pn_message_set_id(message, (pn_atom_t){.type=PN_ULONG, .u.as_ulong=app->sent}); |
| 66 | pn_data_put_map(body); |
| 67 | pn_data_enter(body); |
| 68 | pn_data_put_string(body, pn_bytes(sizeof("sequence")-1, "sequence")); |
| 69 | pn_data_put_int(body, app->sent); /* The sequence number */ |
| 70 | pn_data_exit(body); |
| 71 | |
| 72 | /* encode the message, expanding the encode buffer as needed */ |
| 73 | if (app->message_buffer.start == NULL) { |
| 74 | static const size_t initial_size = 128; |
| 75 | app->message_buffer = pn_rwbytes(initial_size, (char*)malloc(initial_size)); |
| 76 | } |
| 77 | /* app->message_buffer is the total buffer space available. */ |
| 78 | /* mbuf wil point at just the portion used by the encoded message */ |
| 79 | { |
| 80 | pn_rwbytes_t mbuf = pn_rwbytes(app->message_buffer.size, app->message_buffer.start); |
| 81 | int status = 0; |
| 82 | while ((status = pn_message_encode(message, mbuf.start, &mbuf.size)) == PN_OVERFLOW) { |
| 83 | app->message_buffer.size *= 2; |
| 84 | app->message_buffer.start = (char*)realloc(app->message_buffer.start, app->message_buffer.size); |
| 85 | mbuf.size = app->message_buffer.size; |
| 86 | mbuf.start = app->message_buffer.start; |
| 87 | } |
| 88 | if (status != 0) { |
| 89 | fprintf(stderr, "error encoding message: %s\n", pn_error_text(pn_message_error(message))); |
| 90 | exit(1); |
| 91 | } |
| 92 | pn_message_free(message); |
| 93 | return pn_bytes(mbuf.size, mbuf.start); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | /* Returns true to continue, false if finished */ |
| 98 | static bool handle(app_data_t* app, pn_event_t* event) { |
no test coverage detected