When link can't send packets anymore, stop the request chunk callback.

Removed time variable in packet struct and replaced it with sent
variable.
This commit is contained in:
irungentoo 2015-03-13 16:51:41 -04:00
parent 29857a3da4
commit 11e8e8bd3a
No known key found for this signature in database
GPG Key ID: 10349DC9BED89E98
3 changed files with 65 additions and 35 deletions

View File

@ -1386,6 +1386,11 @@ static void do_reqchunk_filecb(Messenger *m, int32_t friendnumber)
ft->requested += length; ft->requested += length;
--free_slots; --free_slots;
if (max_speed_reached(m->net_crypto, friend_connection_crypt_connection_id(m->fr_c,
m->friendlist[friendnumber].friendcon_id))) {
free_slots = 0;
}
} }
if (num == 0) if (num == 0)

View File

@ -722,7 +722,7 @@ static int handle_request_packet(Packets_Array *send_array, const uint8_t *data,
if (n == data[0]) { if (n == data[0]) {
if (send_array->buffer[num]) { if (send_array->buffer[num]) {
send_array->buffer[num]->time = 0; send_array->buffer[num]->sent = 0;
} }
++data; ++data;
@ -809,6 +809,43 @@ static int send_data_packet_helper(Net_Crypto *c, int crypt_connection_id, uint3
return send_data_packet(c, crypt_connection_id, packet, sizeof(packet)); return send_data_packet(c, crypt_connection_id, packet, sizeof(packet));
} }
static int reset_max_speed_reached(Net_Crypto *c, int crypt_connection_id)
{
Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id);
if (conn == 0)
return -1;
/* If last packet send failed, try to send packet again.
If sending it fails we won't be able to send the new packet. */
if (conn->maximum_speed_reached) {
Packet_Data *dt = NULL;
uint32_t packet_num = conn->send_array.buffer_end - 1;
int ret = get_data_pointer(&conn->send_array, &dt, packet_num);
uint8_t send_failed = 0;
if (ret == 1) {
if (!dt->sent) {
if (send_data_packet_helper(c, crypt_connection_id, conn->recv_array.buffer_start, packet_num, dt->data,
dt->length) != 0) {
send_failed = 1;
} else {
dt->sent = 1;
}
}
}
if (!send_failed) {
conn->maximum_speed_reached = 0;
} else {
return -1;
}
}
return 0;
}
/* return -1 if data could not be put in packet queue. /* return -1 if data could not be put in packet queue.
* return positive packet number if data was put into the queue. * return positive packet number if data was put into the queue.
*/ */
@ -823,39 +860,16 @@ static int64_t send_lossless_packet(Net_Crypto *c, int crypt_connection_id, cons
if (conn == 0) if (conn == 0)
return -1; return -1;
uint64_t temp_time = current_time_monotonic();
/* If last packet send failed, try to send packet again. /* If last packet send failed, try to send packet again.
If sending it fails we won't be able to send the new packet. */ If sending it fails we won't be able to send the new packet. */
if (conn->maximum_speed_reached) { reset_max_speed_reached(c, crypt_connection_id);
Packet_Data *dt = NULL;
uint32_t packet_num = conn->send_array.buffer_end - 1;
int ret = get_data_pointer(&conn->send_array, &dt, packet_num);
uint8_t send_failed = 0; if (conn->maximum_speed_reached && congestion_control) {
return -1;
if (ret == 1) {
if (!dt->time) {
if (send_data_packet_helper(c, crypt_connection_id, conn->recv_array.buffer_start, packet_num, dt->data,
dt->length) != 0) {
if (congestion_control) {
return -1;
} else {
send_failed = 1;
}
} else {
dt->time = temp_time;
}
}
}
if (!send_failed) {
conn->maximum_speed_reached = 0;
}
} }
Packet_Data dt; Packet_Data dt;
dt.time = 0; dt.sent = 0;
dt.length = length; dt.length = length;
memcpy(dt.data, data, length); memcpy(dt.data, data, length);
pthread_mutex_lock(&conn->mutex); pthread_mutex_lock(&conn->mutex);
@ -873,7 +887,7 @@ static int64_t send_lossless_packet(Net_Crypto *c, int crypt_connection_id, cons
Packet_Data *dt1 = NULL; Packet_Data *dt1 = NULL;
if (get_data_pointer(&conn->send_array, &dt1, packet_num) == 1) if (get_data_pointer(&conn->send_array, &dt1, packet_num) == 1)
dt1->time = temp_time; dt1->sent = 1;
} else { } else {
conn->maximum_speed_reached = 1; conn->maximum_speed_reached = 1;
LOGGER_ERROR("send_data_packet failed\n"); LOGGER_ERROR("send_data_packet failed\n");
@ -983,15 +997,15 @@ static int send_requested_packets(Net_Crypto *c, int crypt_connection_id, uint16
continue; continue;
} }
if (dt->time != 0) { if (dt->sent) {
continue; continue;
} }
dt->time = current_time_monotonic();
if (send_data_packet_helper(c, crypt_connection_id, conn->recv_array.buffer_start, packet_num, dt->data, if (send_data_packet_helper(c, crypt_connection_id, conn->recv_array.buffer_start, packet_num, dt->data,
dt->length) == 0) dt->length) == 0) {
dt->sent = 1;
++num_sent; ++num_sent;
}
if (num_sent >= max_num) if (num_sent >= max_num)
break; break;
@ -1189,7 +1203,6 @@ static int handle_data_packet_helper(const Net_Crypto *c, int crypt_connection_i
set_buffer_end(&conn->recv_array, num); set_buffer_end(&conn->recv_array, num);
} else if (real_data[0] >= CRYPTO_RESERVED_PACKETS && real_data[0] < PACKET_ID_LOSSY_RANGE_START) { } else if (real_data[0] >= CRYPTO_RESERVED_PACKETS && real_data[0] < PACKET_ID_LOSSY_RANGE_START) {
Packet_Data dt; Packet_Data dt;
dt.time = current_time_monotonic();
dt.length = real_length; dt.length = real_length;
memcpy(dt.data, real_data, real_length); memcpy(dt.data, real_data, real_length);
@ -2575,6 +2588,13 @@ static void send_crypto_packets(Net_Crypto *c)
} }
} }
/* Return 1 if max speed was reached for this connection (no more data can be physically through the pipe).
* Return 0 if it wasn't reached.
*/
_Bool max_speed_reached(Net_Crypto *c, int crypt_connection_id)
{
return reset_max_speed_reached(c, crypt_connection_id) != 0;
}
/* returns the number of packet slots left in the sendbuffer. /* returns the number of packet slots left in the sendbuffer.
* return 0 if failure. * return 0 if failure.

View File

@ -89,7 +89,7 @@
#define CONGESTION_QUEUE_ARRAY_SIZE 24 #define CONGESTION_QUEUE_ARRAY_SIZE 24
typedef struct { typedef struct {
uint64_t time; _Bool sent;
uint16_t length; uint16_t length;
uint8_t data[MAX_CRYPTO_DATA_SIZE]; uint8_t data[MAX_CRYPTO_DATA_SIZE];
} Packet_Data; } Packet_Data;
@ -317,6 +317,11 @@ int nc_dht_pk_callback(Net_Crypto *c, int crypt_connection_id, void (*function)(
*/ */
uint32_t crypto_num_free_sendqueue_slots(const Net_Crypto *c, int crypt_connection_id); uint32_t crypto_num_free_sendqueue_slots(const Net_Crypto *c, int crypt_connection_id);
/* Return 1 if max speed was reached for this connection (no more data can be physically through the pipe).
* Return 0 if it wasn't reached.
*/
_Bool max_speed_reached(Net_Crypto *c, int crypt_connection_id);
/* Sends a lossless cryptopacket. /* Sends a lossless cryptopacket.
* *
* return -1 if data could not be put in packet queue. * return -1 if data could not be put in packet queue.