Skip to content

Commit

Permalink
Move friend-connection-lost to a separate state
Browse files Browse the repository at this point in the history
  • Loading branch information
gjedeer committed Nov 17, 2018
1 parent 03d8360 commit 121acd7
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 12 deletions.
47 changes: 36 additions & 11 deletions client.c
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,7 @@ int do_client_loop(uint8_t *tox_id_str)
uint32_t friendnumber = 0;
TOX_CONNECTION last_friend_connection_status = TOX_CONNECTION_NONE;
time_t last_friend_connection_status_received = 0;
time_t connection_lost_timestamp = 0;
struct timeval tv;
fd_set fds;
static time_t invitation_sent_time = 0;
Expand Down Expand Up @@ -616,17 +617,8 @@ int do_client_loop(uint8_t *tox_id_str)

if(friend_connection_status == TOX_CONNECTION_NONE)
{
/* https://github.com/TokTok/c-toxcore/blob/acb6b2d8543c8f2ea0c2e60dc046767cf5cc0de8/toxcore/tox.h#L1267 */
TOX_ERR_FRIEND_DELETE tox_delete_error;

log_printf(L_WARNING, "Lost connection to server, closing all tunnels and re-adding friend\n");
client_close_all_connections();
tox_friend_delete(tox, friendnumber, &tox_delete_error);
if(tox_delete_error)
{
log_printf(L_ERROR, "Error when deleting server from friend list: %d\n", tox_delete_error);
}
state = CLIENT_STATE_INITIAL;
state = CLIENT_STATE_CONNECTION_LOST;
connection_lost_timestamp = time(NULL);
}
}

Expand All @@ -636,6 +628,39 @@ int do_client_loop(uint8_t *tox_id_str)
}
}
break;
case CLIENT_STATE_CONNECTION_LOST:
{
TOX_CONNECTION friend_connection_status;
friend_connection_status = tox_friend_get_connection_status(tox, friendnumber, &friend_query_error);
if(friend_query_error != TOX_ERR_FRIEND_QUERY_OK)
{
log_printf(L_DEBUG, "tox_friend_get_connection_status: error %u\n", friend_query_error);
}
else
{
if(friend_connection_status == TOX_CONNECTION_NONE)
{
/* https://github.com/TokTok/c-toxcore/blob/acb6b2d8543c8f2ea0c2e60dc046767cf5cc0de8/toxcore/tox.h#L1267 */
TOX_ERR_FRIEND_DELETE tox_delete_error;

log_printf(L_WARNING, "Lost connection to server, closing all tunnels and re-adding friend\n");
client_close_all_connections();
tox_friend_delete(tox, friendnumber, &tox_delete_error);
if(tox_delete_error)
{
log_printf(L_ERROR, "Error when deleting server from friend list: %d\n", tox_delete_error);
}
state = CLIENT_STATE_INITIAL;
}
else
{
state = CLIENT_STATE_FORWARDING;
}
}
}
break;
case 0xffffffff:
log_printf(L_ERROR, "You forgot a break statement\n");
case CLIENT_STATE_SHUTDOWN:
exit(0);
break;
Expand Down
1 change: 1 addition & 0 deletions client.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#define CLIENT_STATE_SHUTDOWN 11
#define CLIENT_STATE_BIND_PORT 12
#define CLIENT_STATE_SETUP_PIPE 13
#define CLIENT_STATE_CONNECTION_LOST 14

int handle_pong_frame();
int handle_acktunnel_frame(protocol_frame *rcvd_frame);
Expand Down
2 changes: 1 addition & 1 deletion gitversion.h
Original file line number Diff line number Diff line change
@@ -1 +1 @@
#define GITVERSION "3adc626eb1646ee3456ca282e1855142f9f935e0"
#define GITVERSION "03d83602d3a328d89b806e23c35644ec3908dc1f"
1 change: 1 addition & 0 deletions main.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <arpa/inet.h>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <netdb.h>
#include <netinet/in.h>
#include <pwd.h>
Expand Down

0 comments on commit 121acd7

Please sign in to comment.