This commit is contained in:
irungentoo 2013-12-05 19:40:32 -05:00
commit 45785ffed0
3 changed files with 68 additions and 33 deletions

View File

@ -1455,7 +1455,7 @@ int m_msi_packet(Messenger *m, int friendnumber, uint8_t *data, uint16_t length)
} }
/* Function to filter out some friend requests*/ /* Function to filter out some friend requests*/
static int friend_already_added(uint8_t * client_id, void * data) static int friend_already_added(uint8_t *client_id, void *data)
{ {
Messenger *m = data; Messenger *m = data;
@ -1538,6 +1538,24 @@ void kill_messenger(Messenger *m)
free(m); free(m);
} }
/* Check for and handle a timed-out friend request. If the request has
* timed-out then the friend status is set back to FRIEND_ADDED.
* i: friendlist index of the timed-out friend
* t: time
*/
static void check_friend_request_timed_out(Messenger *m, uint32_t i, uint64_t t)
{
Friend *f = &m->friendlist[i];
if (f->friendrequest_lastsent + f->friendrequest_timeout < t) {
set_friend_status(m, i, FRIEND_ADDED);
/* Double the default timeout everytime if friendrequest is assumed
* to have been sent unsuccessfully.
*/
f->friendrequest_timeout *= 2;
}
}
/* TODO: Make this function not suck. */ /* TODO: Make this function not suck. */
void do_friends(Messenger *m) void do_friends(Messenger *m)
{ {
@ -1565,13 +1583,7 @@ void do_friends(Messenger *m)
/* If we didn't connect to friend after successfully sending him a friend request the request is deemed /* If we didn't connect to friend after successfully sending him a friend request the request is deemed
* unsuccessful so we set the status back to FRIEND_ADDED and try again. * unsuccessful so we set the status back to FRIEND_ADDED and try again.
*/ */
if (m->friendlist[i].friendrequest_lastsent + m->friendlist[i].friendrequest_timeout < temp_time) { check_friend_request_timed_out(m, i, temp_time);
set_friend_status(m, i, FRIEND_ADDED);
/* Double the default timeout everytime if friendrequest is assumed to have been
* sent unsuccessfully.
*/
m->friendlist[i].friendrequest_timeout *= 2;
}
} }
IP_Port friendip; IP_Port friendip;
@ -1948,7 +1960,30 @@ void do_messenger(Messenger *m)
loglog(" = = = = = = = = \n"); loglog(" = = = = = = = = \n");
uint32_t num_friends = MIN(m->numfriends, m->dht->num_friends); uint32_t friend, dhtfriend;
/* dht contains additional "friends" (requests) */
uint32_t num_dhtfriends = m->dht->num_friends;
int32_t m2dht[num_dhtfriends];
int32_t dht2m[num_dhtfriends];
for (friend = 0; friend < num_dhtfriends; friend++) {
m2dht[friend] = -1;
dht2m[friend] = -1;
if (friend >= m->numfriends)
continue;
for (dhtfriend = 0; dhtfriend < m->dht->num_friends; dhtfriend++)
if (id_equal(m->friendlist[friend].client_id, m->dht->friends_list[dhtfriend].client_id)) {
m2dht[friend] = dhtfriend;
break;
}
}
for (friend = 0; friend < num_dhtfriends; friend++)
if (m2dht[friend] >= 0)
dht2m[m2dht[friend]] = friend;
if (m->numfriends != m->dht->num_friends) { if (m->numfriends != m->dht->num_friends) {
sprintf(logbuffer, "Friend num in DHT %u != friend num in msger %u\n", sprintf(logbuffer, "Friend num in DHT %u != friend num in msger %u\n",
@ -1956,34 +1991,34 @@ void do_messenger(Messenger *m)
loglog(logbuffer); loglog(logbuffer);
} }
uint32_t friend, ping_lastrecv; uint32_t ping_lastrecv;
Friend *msgfptr;
DHT_Friend *dhtfptr;
for (friend = 0; friend < num_friends; friend++) { for (friend = 0; friend < num_dhtfriends; friend++) {
Friend *msgfptr = &m->friendlist[friend]; if (dht2m[friend] >= 0)
DHT_Friend *dhtfptr = &m->dht->friends_list[friend]; msgfptr = &m->friendlist[dht2m[friend]];
else
msgfptr = NULL;
if (memcmp(msgfptr->client_id, dhtfptr->client_id, CLIENT_ID_SIZE)) { dhtfptr = &m->dht->friends_list[friend];
if (sizeof(logbuffer) > 2 * CLIENT_ID_SIZE + 64) {
sprintf(logbuffer, "F[%2u] ID(m) %s != ID(d) ", friend,
ID2String(msgfptr->client_id));
strcat(logbuffer + strlen(logbuffer), ID2String(dhtfptr->client_id));
strcat(logbuffer + strlen(logbuffer), "\n");
} else
sprintf(logbuffer, "F[%2u] ID(m) != ID(d) ", friend);
if (msgfptr) {
ping_lastrecv = lastdump - msgfptr->ping_lastrecv;
if (ping_lastrecv > 999)
ping_lastrecv = 999;
snprintf(logbuffer, sizeof(logbuffer), "F[%2u:%2u] <%s> %02i [%03u] %s\n",
dht2m[friend], friend, msgfptr->name, msgfptr->crypt_connection_id,
ping_lastrecv, ID2String(msgfptr->client_id));
loglog(logbuffer);
} else {
snprintf(logbuffer, sizeof(logbuffer), "F[--:%2u] %s\n",
friend, ID2String(dhtfptr->client_id));
loglog(logbuffer); loglog(logbuffer);
} }
ping_lastrecv = lastdump - msgfptr->ping_lastrecv;
if (ping_lastrecv > 999)
ping_lastrecv = 999;
snprintf(logbuffer, sizeof(logbuffer), "F[%2u] <%s> %02u [%03u] %s\n",
friend, msgfptr->name, msgfptr->crypt_connection_id,
ping_lastrecv, ID2String(msgfptr->client_id));
loglog(logbuffer);
for (client = 0; client < MAX_FRIEND_CLIENTS; client++) { for (client = 0; client < MAX_FRIEND_CLIENTS; client++) {
Client_data *cptr = &dhtfptr->client_list[client]; Client_data *cptr = &dhtfptr->client_list[client];
IPPTsPng *assoc = NULL; IPPTsPng *assoc = NULL;

View File

@ -222,7 +222,7 @@ int tox_set_status_message(Tox *tox, uint8_t *status, uint16_t length)
return m_set_statusmessage(m, status, length); return m_set_statusmessage(m, status, length);
} }
int tox_set_userstatus(Tox *tox, TOX_USERSTATUS status) int tox_set_user_status(Tox *tox, TOX_USERSTATUS status)
{ {
Messenger *m = tox; Messenger *m = tox;
return m_set_userstatus(m, (USERSTATUS)status); return m_set_userstatus(m, (USERSTATUS)status);

View File

@ -247,7 +247,7 @@ int tox_get_name(Tox *tox, int friendnumber, uint8_t *name);
* returns -1 on failure. * returns -1 on failure.
*/ */
int tox_set_status_message(Tox *tox, uint8_t *status, uint16_t length); int tox_set_status_message(Tox *tox, uint8_t *status, uint16_t length);
int tox_set_userstatus(Tox *tox, TOX_USERSTATUS status); int tox_set_user_status(Tox *tox, TOX_USERSTATUS status);
/* return the length of friendnumber's status message, including null. /* return the length of friendnumber's status message, including null.
* Pass it into malloc * Pass it into malloc