Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add messenger state plugin system.
Browse files Browse the repository at this point in the history
This is for modules like groups to hook into to have their own state
management in the `tox_savedata` format.
iphydf authored and e0ff committed Aug 25, 2018
1 parent d380c41 commit 412ab82
Showing 5 changed files with 396 additions and 211 deletions.
17 changes: 4 additions & 13 deletions toxcore/DHT.c
Original file line number Diff line number Diff line change
@@ -2823,26 +2823,16 @@ uint32_t dht_size(const DHT *dht)
return size32 + sizesubhead + packed_node_size(net_family_ipv4) * numv4 + packed_node_size(net_family_ipv6) * numv6;
}

static uint8_t *dht_save_subheader(uint8_t *data, uint32_t len, uint16_t type)
{
host_to_lendian32(data, len);
data += sizeof(uint32_t);
host_to_lendian32(data, (host_tolendian16(DHT_STATE_COOKIE_TYPE) << 16) | host_tolendian16(type));
data += sizeof(uint32_t);
return data;
}


/* Save the DHT in data where data is an array of size dht_size(). */
void dht_save(const DHT *dht, uint8_t *data)
{
host_to_lendian32(data, DHT_STATE_COOKIE_GLOBAL);
host_to_lendian32(data, DHT_STATE_COOKIE_GLOBAL);
data += sizeof(uint32_t);

uint8_t *const old_data = data;

/* get right offset. we write the actual header later. */
data = dht_save_subheader(data, 0, 0);
data = state_write_section_header(data, DHT_STATE_COOKIE_TYPE, 0, 0);

Node_format clients[MAX_SAVED_DHT_NODES];

@@ -2880,7 +2870,8 @@ void dht_save(const DHT *dht, uint8_t *data)
}
}

dht_save_subheader(old_data, pack_nodes(data, sizeof(Node_format) * num, clients, num), DHT_STATE_TYPE_NODES);
state_write_section_header(old_data, DHT_STATE_COOKIE_TYPE, pack_nodes(data, sizeof(Node_format) * num, clients, num),
DHT_STATE_TYPE_NODES);
}

/* Bootstrap from this number of nodes every time dht_connect_after_load() is called */
534 changes: 338 additions & 196 deletions toxcore/Messenger.c

Large diffs are not rendered by default.

44 changes: 42 additions & 2 deletions toxcore/Messenger.h
Original file line number Diff line number Diff line change
@@ -29,6 +29,7 @@
#include "friend_requests.h"
#include "logger.h"
#include "net_crypto.h"
#include "state.h"

#define MAX_NAME_LENGTH 128
/* TODO(irungentoo): this must depend on other variable. */
@@ -50,6 +51,36 @@ typedef enum Message_Type {
MESSAGE_ACTION
} Message_Type;

typedef struct Messenger Messenger;

// Returns the size of the data
typedef uint32_t m_state_size_cb(const Messenger *m);

// Returns the new pointer to data
typedef uint8_t *m_state_save_cb(const Messenger *m, uint8_t *data);

// Returns if there were any erros during loading
typedef State_Load_Status m_state_load_cb(Messenger *m, const uint8_t *data, uint32_t length);

typedef enum Messenger_State_Type {
MESSENGER_STATE_TYPE_NOSPAMKEYS = 1,
MESSENGER_STATE_TYPE_DHT = 2,
MESSENGER_STATE_TYPE_FRIENDS = 3,
MESSENGER_STATE_TYPE_NAME = 4,
MESSENGER_STATE_TYPE_STATUSMESSAGE = 5,
MESSENGER_STATE_TYPE_STATUS = 6,
MESSENGER_STATE_TYPE_TCP_RELAY = 10,
MESSENGER_STATE_TYPE_PATH_NODE = 11,
MESSENGER_STATE_TYPE_END = 255,
} Messenger_State_Type;

typedef struct Messenger_State_Plugin {
Messenger_State_Type type;
m_state_size_cb *size;
m_state_save_cb *save;
m_state_load_cb *load;
} Messenger_State_Plugin;

typedef struct Messenger_Options {
bool ipv6enabled;
bool udp_disabled;
@@ -63,6 +94,9 @@ typedef struct Messenger_Options {
logger_cb *log_callback;
void *log_context;
void *log_user_data;

Messenger_State_Plugin *state_plugins;
uint8_t state_plugins_length;
} Messenger_Options;


@@ -156,8 +190,6 @@ typedef enum Filekind {
} Filekind;


typedef struct Messenger Messenger;

typedef void m_self_connection_status_cb(Messenger *m, unsigned int connection_status, void *user_data);
typedef void m_friend_status_cb(Messenger *m, uint32_t friend_number, unsigned int status, void *user_data);
typedef void m_friend_connection_status_cb(Messenger *m, uint32_t friend_number, unsigned int connection_status,
@@ -758,6 +790,14 @@ uint32_t messenger_run_interval(const Messenger *m);

/* SAVING AND LOADING FUNCTIONS: */

/* Registers a state plugin for saving, loadding, and getting the size of a section of the save
*
* returns true on success
* returns false on error
*/
bool m_register_state_plugin(Messenger *m, Messenger_State_Type type, m_state_size_cb size_callback,
m_state_load_cb load_callback, m_state_save_cb save_callback);

/* return size of the messenger data (for saving). */
uint32_t messenger_size(const Messenger *m);

10 changes: 10 additions & 0 deletions toxcore/state.c
Original file line number Diff line number Diff line change
@@ -45,6 +45,7 @@ int state_load(const Logger *log, state_load_cb *state_load_callback, void *oute
break;

case STATE_LOAD_STATUS_ERROR:
LOGGER_ERROR(log, "Error occcured in state file (type: %u).", type);
return -1;

case STATE_LOAD_STATUS_END:
@@ -60,6 +61,15 @@ int state_load(const Logger *log, state_load_cb *state_load_callback, void *oute
return 0;
}

uint8_t *state_write_section_header(uint8_t *data, uint16_t cookie_type, uint32_t len, uint32_t section_type)
{
host_to_lendian32(data, len);
data += sizeof(uint32_t);
host_to_lendian32(data, (host_tolendian16(cookie_type) << 16) | host_tolendian16(section_type));
data += sizeof(uint32_t);
return data;
}

uint16_t lendian_to_host16(uint16_t lendian)
{
#ifdef WORDS_BIGENDIAN
2 changes: 2 additions & 0 deletions toxcore/state.h
Original file line number Diff line number Diff line change
@@ -34,6 +34,8 @@ typedef State_Load_Status state_load_cb(void *outer, const uint8_t *data, uint32
int state_load(const Logger *log, state_load_cb *state_load_callback, void *outer,
const uint8_t *data, uint32_t length, uint16_t cookie_inner);

uint8_t *state_write_section_header(uint8_t *data, uint16_t cookie_type, uint32_t len, uint32_t section_type);

// Utilities for state data serialisation.

uint16_t lendian_to_host16(uint16_t lendian);

0 comments on commit 412ab82

Please sign in to comment.