@ -427,7 +427,7 @@ int etcp_int_send(struct ETCP_CONN* etcp, const void* data, uint16_t len) {
DEBUG_DEBUG ( DEBUG_CATEGORY_ETCP , " [%s] created PACKET %p with data %p (len=%zu) " , etcp - > log_name , pkt , packet_data , len ) ;
DEBUG_DEBUG ( DEBUG_CATEGORY_ETCP , " [%s] created PACKET %p with data %p (len=%zu) " , etcp - > log_name , pkt , packet_data , len ) ;
// Add to input queue - input_queue_cb will process it
// Add to input queue - input_queue_cb will process it
if ( queue_data_put ( etcp - > input_queue , ( struct ll_entry * ) pkt , 0 ) ! = 0 ) {
if ( queue_data_put ( etcp - > input_queue , ( struct ll_entry * ) pkt ) ! = 0 ) {
DEBUG_ERROR ( DEBUG_CATEGORY_ETCP , " [%s] failed to add to input queue " , etcp - > log_name ) ;
DEBUG_ERROR ( DEBUG_CATEGORY_ETCP , " [%s] failed to add to input queue " , etcp - > log_name ) ;
queue_dgram_free ( & pkt - > ll ) ;
queue_dgram_free ( & pkt - > ll ) ;
queue_entry_free ( & pkt - > ll ) ;
queue_entry_free ( & pkt - > ll ) ;
@ -536,7 +536,7 @@ static void input_queue_cb(struct ll_queue* q, void* arg) {
}
}
// Setup inflight packet (based on protocol.txt)
// Setup inflight packet (based on protocol.txt)
memset ( p , 0 , sizeof ( * p ) ) ;
// memset(p, 0, sizeof(*p));
p - > seq = etcp - > next_tx_id + + ; // Assign seq
p - > seq = etcp - > next_tx_id + + ; // Assign seq
p - > state = INFLIGHT_STATE_WAIT_SEND ;
p - > state = INFLIGHT_STATE_WAIT_SEND ;
p - > last_timestamp = 0 ;
p - > last_timestamp = 0 ;
@ -551,7 +551,7 @@ static void input_queue_cb(struct ll_queue* q, void* arg) {
int len = p - > ll . len ; // сохраним len
int len = p - > ll . len ; // сохраним len
// Add to send queue
// Add to send queue
if ( queue_data_put ( etcp - > input_send_q , & p - > ll , p - > seq ) ! = 0 ) {
if ( queue_data_put_with_index ( etcp - > input_send_q , & p - > ll , 0 , 4 ) ! = 0 ) {
DEBUG_ERROR ( DEBUG_CATEGORY_ETCP , " [%s] failed to add packet seq=%u to input_send_q " , etcp - > log_name , p - > seq ) ;
DEBUG_ERROR ( DEBUG_CATEGORY_ETCP , " [%s] failed to add packet seq=%u to input_send_q " , etcp - > log_name , p - > seq ) ;
queue_dgram_free ( & p - > ll ) ;
queue_dgram_free ( & p - > ll ) ;
queue_entry_free ( & p - > ll ) ;
queue_entry_free ( & p - > ll ) ;
@ -599,7 +599,7 @@ static void ack_timeout_check(struct ETCP_CONN* etcp) {
// Change state and add to send_q for retransmission
// Change state and add to send_q for retransmission
pkt - > state = INFLIGHT_STATE_WAIT_SEND ;
pkt - > state = INFLIGHT_STATE_WAIT_SEND ;
queue_data_put ( etcp - > input_send_q , ( struct ll_entry * ) pkt , pkt - > seq ) ;
queue_data_put_with_index ( etcp - > input_send_q , ( struct ll_entry * ) pkt , 0 , 4 ) ;
// Update stats
// Update stats
etcp - > retransmissions_count + + ;
etcp - > retransmissions_count + + ;
@ -767,7 +767,7 @@ struct ETCP_DGRAM* etcp_request_pkt(struct ETCP_CONN* etcp) {
inf_pkt - > send_count + + ;
inf_pkt - > send_count + + ;
inf_pkt - > state = INFLIGHT_STATE_WAIT_ACK ;
inf_pkt - > state = INFLIGHT_STATE_WAIT_ACK ;
queue_data_put ( etcp - > input_wait_ack , & inf_pkt - > ll , inf_pkt - > seq ) ; // move dgram to wait_ack queue
queue_data_put_with_index ( etcp - > input_wait_ack , & inf_pkt - > ll , 0 , 4 ) ; // move dgram to wait_ack queue
}
}
size_t ack_q_size = queue_entry_count ( etcp - > ack_q ) ;
size_t ack_q_size = queue_entry_count ( etcp - > ack_q ) ;
@ -889,7 +889,7 @@ void etcp_output_try_assembly(struct ETCP_CONN* etcp) {
// Look for contiguous packets starting from next_expected_id
// Look for contiguous packets starting from next_expected_id
while ( 1 ) {
while ( 1 ) {
struct ETCP_FRAGMENT * rx_pkt = ( struct ETCP_FRAGMENT * ) queue_find_data_by_id ( etcp - > recv_q , next_expected_id ) ;
struct ETCP_FRAGMENT * rx_pkt = ( struct ETCP_FRAGMENT * ) queue_find_data_by_in dex ( etcp - > recv_q , & next_expected_id , 4 ) ;
if ( ! rx_pkt ) {
if ( ! rx_pkt ) {
// No more contiguous packets found
// No more contiguous packets found
DEBUG_DEBUG ( DEBUG_CATEGORY_ETCP , " [%s] no packet found for id=%u, stopping " , etcp - > log_name , next_expected_id ) ;
DEBUG_DEBUG ( DEBUG_CATEGORY_ETCP , " [%s] no packet found for id=%u, stopping " , etcp - > log_name , next_expected_id ) ;
@ -906,7 +906,7 @@ void etcp_output_try_assembly(struct ETCP_CONN* etcp) {
DEBUG_TRACE ( DEBUG_CATEGORY_ETCP , " move: ETCP -> PN " ) ;
DEBUG_TRACE ( DEBUG_CATEGORY_ETCP , " move: ETCP -> PN " ) ;
// Add to output_queue using the same ETCP_FRAGMENT structure
// Add to output_queue using the same ETCP_FRAGMENT structure
if ( queue_data_put ( etcp - > output_queue , ( struct ll_entry * ) rx_pkt , next_expected_id ) = = 0 ) {
if ( queue_data_put ( etcp - > output_queue , ( struct ll_entry * ) rx_pkt ) = = 0 ) {
delivered_bytes + = rx_pkt - > ll . len ;
delivered_bytes + = rx_pkt - > ll . len ;
delivered_count + + ;
delivered_count + + ;
DEBUG_TRACE ( DEBUG_CATEGORY_ETCP , " [%s] moved packet id=%u to output_queue (qlen=%d) " , etcp - > log_name ,
DEBUG_TRACE ( DEBUG_CATEGORY_ETCP , " [%s] moved packet id=%u to output_queue (qlen=%d) " , etcp - > log_name ,
@ -915,7 +915,7 @@ void etcp_output_try_assembly(struct ETCP_CONN* etcp) {
DEBUG_ERROR ( DEBUG_CATEGORY_ETCP , " [%s] failed to add packet id=%u to output_queue " , etcp - > log_name ,
DEBUG_ERROR ( DEBUG_CATEGORY_ETCP , " [%s] failed to add packet id=%u to output_queue " , etcp - > log_name ,
next_expected_id ) ;
next_expected_id ) ;
// Put it back in recv_q if we can't add to output_queue
// Put it back in recv_q if we can't add to output_queue
queue_data_put ( etcp - > recv_q , ( struct ll_entry * ) rx_pkt , next_expected_id ) ;
queue_data_put_with_index ( etcp - > recv_q , ( struct ll_entry * ) rx_pkt , 0 , 4 ) ;
break ;
break ;
}
}
@ -937,13 +937,13 @@ void etcp_ack_recv(struct ETCP_CONN* etcp, uint32_t seq, uint16_t ts, uint16_t d
// Find the acknowledged packet in the wait_ack queue
// Find the acknowledged packet in the wait_ack queue
struct INFLIGHT_PACKET * acked_pkt ;
struct INFLIGHT_PACKET * acked_pkt ;
acked_pkt = ( struct INFLIGHT_PACKET * ) queue_find_data_by_id ( etcp - > input_wait_ack , seq ) ;
acked_pkt = ( struct INFLIGHT_PACKET * ) queue_find_data_by_in dex ( etcp - > input_wait_ack , & seq , 4 ) ;
if ( acked_pkt ) {
if ( acked_pkt ) {
etcp - > cnt_ack_hit_inf + + ;
etcp - > cnt_ack_hit_inf + + ;
queue_remove_data ( etcp - > input_wait_ack , ( struct ll_entry * ) acked_pkt ) ;
queue_remove_data ( etcp - > input_wait_ack , ( struct ll_entry * ) acked_pkt ) ;
}
}
else {
else {
acked_pkt = ( struct INFLIGHT_PACKET * ) queue_find_data_by_id ( etcp - > input_send_q , seq ) ;
acked_pkt = ( struct INFLIGHT_PACKET * ) queue_find_data_by_in dex ( etcp - > input_send_q , & seq , 4 ) ;
if ( acked_pkt ) {
if ( acked_pkt ) {
etcp - > cnt_ack_hit_sndq + + ;
etcp - > cnt_ack_hit_sndq + + ;
queue_remove_data ( etcp - > input_send_q , ( struct ll_entry * ) acked_pkt ) ;
queue_remove_data ( etcp - > input_send_q , ( struct ll_entry * ) acked_pkt ) ;
@ -1128,12 +1128,12 @@ void etcp_conn_input(struct ETCP_DGRAM* pkt) {
p - > seq = seq ;
p - > seq = seq ;
p - > pkt_timestamp = pkt - > timestamp ;
p - > pkt_timestamp = pkt - > timestamp ;
p - > recv_timestamp = get_current_timestamp ( ) ;
p - > recv_timestamp = get_current_timestamp ( ) ;
queue_data_put ( etcp - > ack_q , ( struct ll_entry * ) p , p - > seq ) ;
queue_data_put_with_index ( etcp - > ack_q , ( struct ll_entry * ) p , 0 , 4 ) ;
if ( etcp - > ack_resp_timer = = NULL ) {
if ( etcp - > ack_resp_timer = = NULL ) {
etcp - > ack_resp_timer = uasync_set_timeout ( etcp - > instance - > ua , ACK_DELAY_TB , etcp , ack_response_timer_cb ) ;
etcp - > ack_resp_timer = uasync_set_timeout ( etcp - > instance - > ua , ACK_DELAY_TB , etcp , ack_response_timer_cb ) ;
DEBUG_DEBUG ( DEBUG_CATEGORY_ETCP , " [%s] set ack_timer for delayed ACK send " , etcp - > log_name ) ;
DEBUG_DEBUG ( DEBUG_CATEGORY_ETCP , " [%s] set ack_timer for delayed ACK send " , etcp - > log_name ) ;
}
}
if ( ( int32_t ) ( etcp - > last_delivered_id - seq ) < 0 ) if ( queue_find_data_by_id ( etcp - > recv_q , seq ) = = NULL ) { // проверяем есть ли пакет с этим seq
if ( ( int32_t ) ( etcp - > last_delivered_id - seq ) < 0 ) if ( queue_find_data_by_in dex ( etcp - > recv_q , & seq , 4 ) = = NULL ) { // проверяем есть ли пакет с этим seq
uint32_t pkt_len = len - 5 ;
uint32_t pkt_len = len - 5 ;
DEBUG_DEBUG ( DEBUG_CATEGORY_ETCP , " [%s] adding packet seq=%u to recv_q (last_delivered_id=%u) " , etcp - > log_name , seq , etcp - > last_delivered_id ) ;
DEBUG_DEBUG ( DEBUG_CATEGORY_ETCP , " [%s] adding packet seq=%u to recv_q (last_delivered_id=%u) " , etcp - > log_name , seq , etcp - > last_delivered_id ) ;
// отправляем пакет в очередь на сборку
// отправляем пакет в очередь на сборку
@ -1158,7 +1158,7 @@ void etcp_conn_input(struct ETCP_DGRAM* pkt) {
rx_pkt - > ll . memlen = etcp - > instance - > data_pool - > object_size ;
rx_pkt - > ll . memlen = etcp - > instance - > data_pool - > object_size ;
// Copy the actual payload data
// Copy the actual payload data
memcpy ( payload_data , data + 5 , pkt_len ) ;
memcpy ( payload_data , data + 5 , pkt_len ) ;
queue_data_put ( etcp - > recv_q , ( struct ll_entry * ) rx_pkt , seq ) ;
queue_data_put_with_index ( etcp - > recv_q , ( struct ll_entry * ) rx_pkt , 0 , 4 ) ;
DEBUG_DEBUG ( DEBUG_CATEGORY_ETCP , " [%s] packet seq=%u added to recv_q, calling assembly (last_delivered_id=%u) " , etcp - > log_name , seq , etcp - > last_delivered_id ) ;
DEBUG_DEBUG ( DEBUG_CATEGORY_ETCP , " [%s] packet seq=%u added to recv_q, calling assembly (last_delivered_id=%u) " , etcp - > log_name , seq , etcp - > last_delivered_id ) ;
if ( ( int32_t ) ( seq - etcp - > last_delivered_id ) = = 1 ) etcp_output_try_assembly ( etcp ) ; // пробуем собрать выходную очередь из фрагментов
if ( ( int32_t ) ( seq - etcp - > last_delivered_id ) = = 1 ) etcp_output_try_assembly ( etcp ) ; // пробуем собрать выходную очередь из фрагментов
}
}