Internal Message Header¶
This header contains the C functions needed for using the message format in the p2p.today project.
It automatically includes base.h.
-
typedef struct
InternalMessageStruct¶ -
char *
msg_type¶ The type of message this is. These are described in protocol/flags.
-
size_t
msg_type_len¶
-
char *
sender¶ The message sender’s ID
-
size_t
sender_len¶
-
unsigned long long
timestamp¶ The time at which this message was sent, in UTC seconds since 1/1/1970.
-
char **
payload¶ An array of “payload” packets in this message. In other words, every item which isn’t metadata.
-
size_t *
payload_lens¶ The length of each payload item, in the same order
-
size_t
num_payload¶ The number of payload packets
Note
Members after these are not garunteed to be present. A function to ensure their existence will be provided as noted. Otherwise check if their length term is
0to determine existence.-
char **
compression¶ An array of possible compression algorigthms
This can be initialized with
setInternalMessageCompressions()
-
size_t *
compression_lens¶ The length of each compression string, in the same order
-
size_t
num_compression¶ The number of compression methods
-
char *
id¶ The checksum/ID of this message
To ensure this value is set, call
ensureInternalMessageID()
-
size_t
id_len¶
-
char *
str¶ The serialized version of this message
-
size_t
str_len¶
-
char *
-
static InternalMessageStruct *
constructInternalMessage(const char *type, size_t type_len, const char *sender, size_t sender_len, char **payload, size_t *payload_lens, size_t num_payload)¶ Constructs an InternalMessageStruct. This copies all given data into a struct, then returns this struct’s pointer.
Parameters: - type – The item to place in
InternalMessageStruct.msg_type - type_len – The length of the above
- sender – The item to place in
InternalMessageStruct.sender - sender_len – The length of the above
- payload – The array to place in
InternalMessageStruct.payload - payload_lens – The length for each string in the above
- num_payload – The number of items in the above
Returns: A pointer to the resulting
InternalMessageStructWarning
You must use
destroyInternalMessage()on the resulting object, or you will develop a memory leak- type – The item to place in
-
static void
destroyInternalMessage(InternalMessageStruct *des)¶ free()anInteralMessageStructand its membersParameters: - des – A pointer to the InternalMessageStruct you wish to destroy
-
static void
setInternalMessageCompressions(InternalMessageStruct *des, char **compression, size_t *compression_lens, size_t num_compressions)¶ Sets the compression methods for a particular
InternalMessageStruct. These methods are formatted as an array of strings, an array of lengths, and a number of methods. The data is copied, so you inputs can be local variables.Parameters: - des – A pointer to the relevant InternalMessageStruct
- compression – An array of compression methods
- compression_lens – An array of lengths for each compression method
- num_compressions – The number of compression methods
-
static void
ensureInternalMessageID(InternalMessageStruct *des)¶ Ensures that the InternalMessageStruct has an ID calculated and assigned
Parameters: - des – A pointer to the relevant InternalMessageStruct
-
static void
ensureInternalMessageStr(InternalMessageStruct *des)¶ Ensures that the InternalMessageStruct has a serialized string calculated and assigned
Parameters: - des – A pointer to the relevant InternalMessageStruct
-
static InternalMessageStruct *
deserializeInternalMessage(const char *serialized, size_t len, int sizeless)¶ Deserializes an uncompressed
InternalMessageStruct. Thesizelessparameter indicates whether the network size header is still present on the given string.Parameters: - serialized – The serialized message
- len – The length of the serialized message
- sizeless – A boolean which indicates whether the network size header is still present on the given string
- errored – A pointer to a boolean. If this is set with a non-zero value, it indicates that the checksum test failed
Returns: An equivalent
InternalMessageStruct, orNULLif there was an error
-
static InternalMessageStruct *
deserializeCompressedInternalMessage(const char *serialized, size_t len, int sizeless, char **compression, size_t *compression_lens, size_t num_compressions)¶ Deserializes a compressed
InternalMessageStruct. Thesizelessparameter indicates whether the network size header is still present on the given string.Parameters: - serialized – See
deserializeInternalMessage() - len – See
deserializeInternalMessage() - sizeless – See
deserializeInternalMessage() - errored – See
deserializeInternalMessage() - compression – See
setInternalMessageCompressions() - compression_lens – See
setInternalMessageCompressions() - num_compressions – See
setInternalMessageCompressions()
Returns: An equivalent
InternalMessageStruct, orNULLif there was an error- serialized – See