aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorMattias Andrée <maandree@operamail.com>2014-05-15 15:45:13 +0200
committerMattias Andrée <maandree@operamail.com>2014-05-15 15:45:13 +0200
commit566054194d82b4bfabaaca44d14008a073e29a6b (patch)
tree5fc6f39d75cc49402071906151d3b0b6b41f1f00 /src
parentDo not echo back messages to the sender (diff)
downloadmds-566054194d82b4bfabaaca44d14008a073e29a6b.tar.gz
mds-566054194d82b4bfabaaca44d14008a073e29a6b.tar.bz2
mds-566054194d82b4bfabaaca44d14008a073e29a6b.tar.xz
m + fix message sending
Signed-off-by: Mattias Andrée <maandree@operamail.com>
Diffstat (limited to '')
-rw-r--r--src/libmdsserver/mds-message.c86
-rw-r--r--src/libmdsserver/mds-message.h39
-rw-r--r--src/mds-server/client.c10
-rw-r--r--src/mds-server/mds-server.c5
4 files changed, 90 insertions, 50 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);