Skip to content

Commit

Permalink
Adding a new connection while a lossy packet is being sent could
Browse files Browse the repository at this point in the history
also cause thread related issues.
  • Loading branch information
irungentoo committed Jul 31, 2014
1 parent d270cf5 commit 946a09f
Showing 1 changed file with 19 additions and 6 deletions.
25 changes: 19 additions & 6 deletions toxcore/net_crypto.c
Original file line number Diff line number Diff line change
Expand Up @@ -1268,13 +1268,26 @@ static int create_crypto_connection(Net_Crypto *c)
return i;
}

if (realloc_cryptoconnection(c, c->crypto_connections_length + 1) == -1)
return -1;
while (1) { /* TODO: is this really the best way to do this? */
pthread_mutex_lock(&c->connections_mutex);

if (!c->connection_use_counter) {
break;
}

memset(&(c->crypto_connections[c->crypto_connections_length]), 0, sizeof(Crypto_Connection));
int id = c->crypto_connections_length;
pthread_mutex_init(&c->crypto_connections[id].mutex, NULL);
++c->crypto_connections_length;
pthread_mutex_unlock(&c->connections_mutex);
}

int id = -1;

if (realloc_cryptoconnection(c, c->crypto_connections_length + 1) == 0) {
memset(&(c->crypto_connections[c->crypto_connections_length]), 0, sizeof(Crypto_Connection));
id = c->crypto_connections_length;
pthread_mutex_init(&c->crypto_connections[id].mutex, NULL);
++c->crypto_connections_length;
}

pthread_mutex_unlock(&c->connections_mutex);
return id;
}

Expand Down

0 comments on commit 946a09f

Please sign in to comment.