aboutsummaryrefslogtreecommitdiffstats
path: root/src/message.h
diff options
context:
space:
mode:
authorMattias Andrée <maandree@kth.se>2016-07-11 20:28:28 +0200
committerMattias Andrée <maandree@kth.se>2016-07-11 20:28:28 +0200
commit564cbb378e1480ff736a14c832d39fe6db9f7891 (patch)
tree4f5e3377f21c173b6a7f72a11071fa8aa0637fd9 /src/message.h
parentAdd signal handlers (diff)
downloadcoopgammad-564cbb378e1480ff736a14c832d39fe6db9f7891.tar.gz
coopgammad-564cbb378e1480ff736a14c832d39fe6db9f7891.tar.bz2
coopgammad-564cbb378e1480ff736a14c832d39fe6db9f7891.tar.xz
whitespace + Add message.[ch]
Signed-off-by: Mattias Andrée <maandree@kth.se>
Diffstat (limited to '')
-rw-r--r--src/message.h132
1 files changed, 132 insertions, 0 deletions
diff --git a/src/message.h b/src/message.h
new file mode 100644
index 0000000..163d806
--- /dev/null
+++ b/src/message.h
@@ -0,0 +1,132 @@
+/**
+ * gammad -- Cooperative gamma server
+ * Copyright (C) 2016 Mattias Andrée (maandree@kth.se)
+ *
+ * This library is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this library. If not, see <http://www.gnu.org/licenses/>.
+ */
+#include <stddef.h>
+
+
+
+/**
+ * Message passed between a server and a client
+ */
+struct message
+{
+ /**
+ * The headers in the message, each element in this list
+ * as an unparsed header, it consists of both the header
+ * name and its associated value, joined by ": ". A header
+ * cannot be `NULL` (unless its memory allocation failed,)
+ * but `headers` itself is `NULL` if there are no headers.
+ * The "Length" header should be included in this list.
+ */
+ char** headers;
+
+ /**
+ * The number of headers in the message
+ */
+ size_t header_count;
+
+ /**
+ * The payload of the message, `NULL` if none (of zero-length)
+ */
+ char* payload;
+
+ /**
+ * The size of the payload
+ */
+ size_t payload_size;
+
+ /**
+ * How much of the payload that has been stored (internal data)
+ */
+ size_t payload_ptr;
+
+ /**
+ * Internal buffer for the reading function (internal data)
+ */
+ char* buffer;
+
+ /**
+ * The size allocated to `buffer` (internal data)
+ */
+ size_t buffer_size;
+
+ /**
+ * The number of bytes used in `buffer` (internal data)
+ */
+ size_t buffer_ptr;
+
+ /**
+ * 0 while reading headers, 1 while reading payload, and 2 when done (internal data)
+ */
+ int stage;
+
+};
+
+
+
+/**
+ * Initialise a message slot so that it can
+ * be used by to read messages
+ *
+ * @param this Memory slot in which to store the new message
+ * @return Non-zero on error, `errno` will be set accordingly
+ */
+int message_initialise(struct message* this);
+
+/**
+ * Release all resources in a message, should
+ * be done even if initialisation fails
+ *
+ * @param this The message
+ */
+void message_destroy(struct message* this);
+
+/**
+ * Marshal a message for state serialisation
+ *
+ * @param this The message
+ * @param buf Output buffer for the marshalled data,
+ * `NULL` just measure how large the buffers
+ * needs to be
+ * @return The number of marshalled byte
+ */
+size_t message_marshal(const struct message* this, void* buf);
+
+/**
+ * Unmarshal a message for state deserialisation
+ *
+ * @param this Memory slot in which to store the new message
+ * @param buf In buffer with the marshalled data
+ * @return The number of unmarshalled bytes, 0 on error
+ */
+size_t message_unmarshal(struct message* this, const void* buf);
+
+/**
+ * Read the next message from a file descriptor
+ *
+ * @param this Memory slot in which to store the new message
+ * @param fd The file descriptor
+ * @return Non-zero on error or interruption, `errno` will be
+ * set accordingly. Destroy the message on error,
+ * be aware that the reading could have been
+ * interrupted by a signal rather than canonical error.
+ * If -2 is returned `errno` will not have been set,
+ * -2 indicates that the message is malformated,
+ * which is a state that cannot be recovered from.
+ */
+int message_read(struct message* this, int fd);
+