diff options
-rw-r--r-- | src/libmdsserver/mds-message.c | 86 | ||||
-rw-r--r-- | src/libmdsserver/mds-message.h | 39 | ||||
-rw-r--r-- | src/mds-server/client.c | 10 | ||||
-rw-r--r-- | src/mds-server/mds-server.c | 5 | ||||
-rwxr-xr-x | test.d/mds-server | 8 |
5 files changed, 97 insertions, 51 deletions
diff --git a/src/libmdsserver/mds-message.c b/src/libmdsserver/mds-message.c index c0924dd..dccd7d8 100644 --- a/src/libmdsserver/mds-message.c +++ b/src/libmdsserver/mds-message.c @@ -27,7 +27,7 @@ /** - * Initialsie a message slot so that it can + * Initialise a message slot so that it can * be used by `mds_message_read` * * @param this Memory slot in which to store the new message @@ -292,33 +292,28 @@ int mds_message_read(mds_message_t* restrict this, int fd) * Get the required allocation size for `data` of the * function `mds_message_marshal` * - * @param this The message - * @param include_buffer Whether buffer should be marshalled (state serialisation, not communication) - * @return The size of the message when marshalled + * @param this The message + * @return The size of the message when marshalled */ -size_t mds_message_marshal_size(const mds_message_t* restrict this, int include_buffer) +size_t mds_message_marshal_size(const mds_message_t* restrict this) { size_t rc = this->header_count + this->payload_size; size_t i; for (i = 0; i < this->header_count; i++) rc += strlen(this->headers[i]); rc *= sizeof(char); - rc += (include_buffer ? 4 : 2) * sizeof(size_t); - rc += (include_buffer ? 2 : 1) * sizeof(int); + rc += 4 * sizeof(size_t) + 1 * sizeof(int); return rc; } /** - * Marshal a message, this can be used both when serialising - * the servers state or to get the byte stream to send to - * the recipient of the message + * Marshal a message for state serialisation * - * @param this The message - * @param data Output buffer for the marshalled data - * @param include_buffer Whether buffer should be marshalled (state serialisation, not communication) + * @param this The message + * @param data Output buffer for the marshalled data */ -void mds_message_marshal(const mds_message_t* restrict this, char* restrict data, int include_buffer) +void mds_message_marshal(const mds_message_t* restrict this, char* restrict data) { size_t i, n; @@ -326,16 +321,9 @@ void mds_message_marshal(const mds_message_t* restrict this, char* restrict data buf_set_next(data, size_t, this->header_count); buf_set_next(data, size_t, this->payload_size); - if (include_buffer) - { - buf_set_next(data, size_t, this->payload_ptr); - buf_set_next(data, size_t, this->buffer_ptr); - } - - if (include_buffer) - { - buf_set_next(data, int, this->stage); - } + buf_set_next(data, size_t, this->payload_ptr); + buf_set_next(data, size_t, this->buffer_ptr); + buf_set_next(data, int, this->stage); for (i = 0; i < this->header_count; i++) { @@ -346,16 +334,13 @@ void mds_message_marshal(const mds_message_t* restrict this, char* restrict data memcpy(data, this->payload, this->payload_size * sizeof(char)); - if (include_buffer) - { - buf_next(data, char, this->payload_size); - memcpy(data, this->buffer, this->buffer_ptr * sizeof(char)); - } + buf_next(data, char, this->payload_size); + memcpy(data, this->buffer, this->buffer_ptr * sizeof(char)); } /** - * Unmarshal a message, it is assumed that the buffer is marshalled + * Unmarshal a message for state deserialisation * * @param this Memory slot in which to store the new message * @param data In buffer with the marshalled data @@ -436,3 +421,44 @@ int mds_message_unmarshal(mds_message_t* restrict this, char* restrict data) return 0; } + +/** + * Get the required allocation size for `data` of the + * function `mds_message_compose` + * + * @param this The message + * @return The size of the message when marshalled + */ +size_t mds_message_compose_size(const mds_message_t* restrict this) +{ + size_t rc = 1 + this->payload_size; + size_t i; + for (i = 0; i < this->header_count; i++) + rc += strlen(this->headers[i]) + 1; + return rc * sizeof(char); +} + + +/** + * Marshal a message for communication + * + * @param this The message + * @param data Output buffer for the marshalled data + */ +void mds_message_compose(const mds_message_t* restrict this, char* restrict data) +{ + size_t i, n; + + for (i = 0; i < this->header_count; i++) + { + n = strlen(this->headers[i]); + memcpy(data, this->headers[i], n * sizeof(char)); + data += n; + buf_set_next(data, char, '\n'); + } + buf_set_next(data, char, '\n'); + + if (this->payload_size > 0) + memcpy(data, this->payload, this->payload_size * sizeof(char)); +} + diff --git a/src/libmdsserver/mds-message.h b/src/libmdsserver/mds-message.h index 885dd03..6f0a61d 100644 --- a/src/libmdsserver/mds-message.h +++ b/src/libmdsserver/mds-message.h @@ -83,7 +83,7 @@ typedef struct mds_message /** - * Initialsie a message slot so that it can + * Initialise a message slot so that it can * be used by `mds_message_read` * * @param this Memory slot in which to store the new message @@ -119,25 +119,21 @@ int mds_message_read(mds_message_t* restrict this, int fd); * Get the required allocation size for `data` of the * function `mds_message_marshal` * - * @param this The message - * @param include_buffer Whether buffer should be marshalled (state serialisation, not communication) - * @return The size of the message when marshalled + * @param this The message + * @return The size of the message when marshalled */ -size_t mds_message_marshal_size(const mds_message_t* restrict this, int include_buffer) __attribute__((pure)); +size_t mds_message_marshal_size(const mds_message_t* restrict this) __attribute__((pure)); /** - * Marshal a message, this can be used both when serialising - * the servers state or to get the byte stream to send to - * the recipient of the message + * Marshal a message for state serialisation * - * @param this The message - * @param data Output buffer for the marshalled data - * @param include_buffer Whether buffer should be marshalled (state serialisation, not communication) + * @param this The message + * @param data Output buffer for the marshalled data */ -void mds_message_marshal(const mds_message_t* restrict this, char* restrict data, int include_buffer); +void mds_message_marshal(const mds_message_t* restrict this, char* restrict data); /** - * Unmarshal a message, it is assumed that the buffer is marshalled + * Unmarshal a message for state deserialisation * * @param this Memory slot in which to store the new message * @param data In buffer with the marshalled data @@ -146,6 +142,23 @@ void mds_message_marshal(const mds_message_t* restrict this, char* restrict data */ int mds_message_unmarshal(mds_message_t* restrict this, char* restrict data); +/** + * Get the required allocation size for `data` of the + * function `mds_message_compose` + * + * @param this The message + * @return The size of the message when marshalled + */ +size_t mds_message_compose_size(const mds_message_t* restrict this) __attribute__((pure)); + +/** + * Marshal a message for communication + * + * @param this The message + * @param data Output buffer for the marshalled data + */ +void mds_message_compose(const mds_message_t* restrict this, char* restrict data); + #endif diff --git a/src/mds-server/client.c b/src/mds-server/client.c index c8e25ea..15afc18 100644 --- a/src/mds-server/client.c +++ b/src/mds-server/client.c @@ -76,13 +76,13 @@ size_t client_marshal_size(const client_t* restrict this) size_t n = sizeof(ssize_t) + 2 * sizeof(int) + sizeof(uint64_t) + 5 * sizeof(size_t); size_t i; - n += mds_message_marshal_size(&(this->message), 1); + n += mds_message_marshal_size(&(this->message)); for (i = 0; i < this->interception_conditions_count; i++) n += interception_condition_marshal_size(this->interception_conditions + i); for (i = 0; i < this->multicasts_count; i++) n += multicast_marshal_size(this->multicasts + i); n += this->send_pending_size * sizeof(char); - n += this->modify_message == NULL ? 0 : mds_message_marshal_size(this->modify_message, 1); + n += this->modify_message == NULL ? 0 : mds_message_marshal_size(this->modify_message); return n; } @@ -102,9 +102,9 @@ size_t client_marshal(const client_t* restrict this, char* restrict data) buf_set_next(data, int, this->socket_fd); buf_set_next(data, int, this->open); buf_set_next(data, uint64_t, this->id); - n = mds_message_marshal_size(&(this->message), 1);; + n = mds_message_marshal_size(&(this->message)); buf_set_next(data, size_t, n); - mds_message_marshal(&(this->message), data, 1); + mds_message_marshal(&(this->message), data); data += n / sizeof(char); buf_set_next(data, size_t, this->interception_conditions_count); for (i = 0; i < this->interception_conditions_count; i++) @@ -117,7 +117,7 @@ size_t client_marshal(const client_t* restrict this, char* restrict data) memcpy(data, this->send_pending, this->send_pending_size * sizeof(char)); data += this->send_pending_size; if (this->modify_message != NULL) - mds_message_marshal(this->modify_message, data, 1); + mds_message_marshal(this->modify_message, data); return client_marshal_size(this); } diff --git a/src/mds-server/mds-server.c b/src/mds-server/mds-server.c index d0757fd..b774e77 100644 --- a/src/mds-server/mds-server.c +++ b/src/mds-server/mds-server.c @@ -871,6 +871,7 @@ int message_received(client_t* client) { perror(*argv); free(old_buf); + pthread_mutex_unlock(&(client->mutex)); return 0; } } @@ -896,13 +897,13 @@ int message_received(client_t* client) /* Multicast the message. */ - n = mds_message_marshal_size(&message, 0); + n = mds_message_compose_size(&message); if ((msgbuf = malloc(n)) == NULL) { perror(*argv); return 0; } - mds_message_marshal(&message, msgbuf, 0); + mds_message_compose(&message, msgbuf); queue_message_multicast(msgbuf, n / sizeof(char), client); diff --git a/test.d/mds-server b/test.d/mds-server index 04a6d7f..170e444 100755 --- a/test.d/mds-server +++ b/test.d/mds-server @@ -1,2 +1,8 @@ #!/bin/sh -valgrind --leak-check=full --show-leak-kinds=all "$0".real "$@" + +if [ -z "${NO_VALGRING}" ]; then + exec valgrind --leak-check=full --show-leak-kinds=all "$0".real "$@" +else + exec "$0".real "$@" +fi + |