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

@ -1538,6 +1538,24 @@ void kill_messenger(Messenger *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. */
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
* 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) {
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;
}
check_friend_request_timed_out(m, i, temp_time);
}
IP_Port friendip;
@ -1948,7 +1960,30 @@ void do_messenger(Messenger *m)
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) {
sprintf(logbuffer, "Friend num in DHT %u != friend num in msger %u\n",
@ -1956,33 +1991,33 @@ void do_messenger(Messenger *m)
loglog(logbuffer);
}
uint32_t friend, ping_lastrecv;
uint32_t ping_lastrecv;
Friend *msgfptr;
DHT_Friend *dhtfptr;
for (friend = 0; friend < num_friends; friend++) {
Friend *msgfptr = &m->friendlist[friend];
DHT_Friend *dhtfptr = &m->dht->friends_list[friend];
for (friend = 0; friend < num_dhtfriends; friend++) {
if (dht2m[friend] >= 0)
msgfptr = &m->friendlist[dht2m[friend]];
else
msgfptr = NULL;
if (memcmp(msgfptr->client_id, dhtfptr->client_id, CLIENT_ID_SIZE)) {
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);
loglog(logbuffer);
}
dhtfptr = &m->dht->friends_list[friend];
if (msgfptr) {
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,
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);
}
for (client = 0; client < MAX_FRIEND_CLIENTS; client++) {
Client_data *cptr = &dhtfptr->client_list[client];

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);
}
int tox_set_userstatus(Tox *tox, TOX_USERSTATUS status)
int tox_set_user_status(Tox *tox, TOX_USERSTATUS status)
{
Messenger *m = tox;
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.
*/
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.
* Pass it into malloc