Skip to content

Commit

Permalink
Save some path nodes to speed up joining network on pure TCP.
Browse files Browse the repository at this point in the history
  • Loading branch information
irungentoo committed Aug 14, 2014
1 parent 5376d40 commit fb9fc81
Showing 1 changed file with 32 additions and 1 deletion.
33 changes: 32 additions & 1 deletion toxcore/Messenger.c
Original file line number Diff line number Diff line change
Expand Up @@ -2537,9 +2537,11 @@ void do_messenger(Messenger *m)
#define MESSENGER_STATE_TYPE_STATUSMESSAGE 5
#define MESSENGER_STATE_TYPE_STATUS 6
#define MESSENGER_STATE_TYPE_TCP_RELAY 10
#define MESSENGER_STATE_TYPE_PATH_NODE 11

#define SAVED_FRIEND_REQUEST_SIZE 1024
#define NUM_SAVED_TCP_RELAYS 8
#define NUM_SAVED_PATH_NODES 8
struct SAVED_FRIEND {
uint8_t status;
uint8_t client_id[CLIENT_ID_SIZE];
Expand Down Expand Up @@ -2653,6 +2655,7 @@ uint32_t messenger_size(const Messenger *m)
+ sizesubhead + m->statusmessage_length // status message
+ sizesubhead + 1 // status
+ sizesubhead + NUM_SAVED_TCP_RELAYS * sizeof(Node_format) //TCP relays
+ sizesubhead + NUM_SAVED_PATH_NODES * sizeof(Node_format) //saved path nodes
;
}

Expand Down Expand Up @@ -2725,6 +2728,15 @@ void messenger_save(const Messenger *m, uint8_t *data)
memset(relays, 0, len);
copy_connected_tcp_relays(m->net_crypto, relays, NUM_SAVED_TCP_RELAYS);
memcpy(data, relays, len);
data += len;

Node_format nodes[NUM_SAVED_PATH_NODES];
len = sizeof(nodes);
type = MESSENGER_STATE_TYPE_PATH_NODE;
data = z_state_save_subheader(data, len, type);
memset(nodes, 0, len);
onion_backup_nodes(m->onion_c, nodes, NUM_SAVED_PATH_NODES);
memcpy(data, nodes, len);
}

static int messenger_load_state_callback(void *outer, const uint8_t *data, uint32_t length, uint16_t type)
Expand Down Expand Up @@ -2789,8 +2801,27 @@ static int messenger_load_state_callback(void *outer, const uint8_t *data, uint3
for (i = 0; i < NUM_SAVED_TCP_RELAYS; ++i) {
add_tcp_relay(m->net_crypto, relays[i].ip_port, relays[i].client_id);
}

break;
}
break;

case MESSENGER_STATE_TYPE_PATH_NODE: {
Node_format nodes[NUM_SAVED_PATH_NODES];

if (length != sizeof(nodes)) {
return -1;
}

memcpy(nodes, data, length);
uint32_t i;

for (i = 0; i < NUM_SAVED_PATH_NODES; ++i) {
onion_add_path_node(m->onion_c, nodes[i].ip_port, nodes[i].client_id);
}

break;
}

#ifdef DEBUG

default:
Expand Down

0 comments on commit fb9fc81

Please sign in to comment.