@ -157,6 +157,27 @@ static void keepalive_timer_cb(void* arg) {
goto restart_timer ;
}
// Check keepalive timeout
uint64_t now = get_current_time_units ( ) ;
uint64_t timeout_units = ( uint64_t ) link - > keepalive_timeout * 10 ; // ms -> 0.1ms units
uint64_t elapsed = now - link - > last_recv_local_time ;
if ( elapsed > timeout_units ) {
if ( link - > link_status ! = 0 ) {
link - > link_status = 0 ; // down
DEBUG_WARN ( DEBUG_CATEGORY_CONNECTION , " [%s] Link %p (local_id=%d) status changed to DOWN - "
" no packets received for %llu ms (timeout=%u ms) " ,
link - > etcp - > log_name , link , link - > local_link_id ,
( unsigned long long ) ( elapsed / 10 ) , link - > keepalive_timeout ) ;
}
} else {
if ( link - > link_status ! = 1 ) {
link - > link_status = 1 ; // up
DEBUG_INFO ( DEBUG_CATEGORY_CONNECTION , " [%s] Link %p (local_id=%d) status changed to UP " ,
link - > etcp - > log_name , link , link - > local_link_id ) ;
}
}
// Send keepalive only if no packets were sent since last tick
if ( ! link - > pkt_sent_since_keepalive ) {
etcp_link_send_keepalive ( link ) ;
@ -477,6 +498,14 @@ struct ETCP_LINK* etcp_link_new(struct ETCP_CONN* etcp, struct ETCP_SOCKET* conn
link - > init_timer = NULL ;
link - > init_timeout = 0 ;
link - > init_retry_count = 0 ;
link - > link_status = 0 ; // down initially
// Initialize keepalive timeout from global config
if ( etcp - > instance & & etcp - > instance - > config ) {
link - > keepalive_timeout = etcp - > instance - > config - > global . keepalive_timeout ;
} else {
link - > keepalive_timeout = 2000 ; // Default 2 seconds
}
// Выделяем свободный local_link_id
int free_id = etcp_find_free_local_link_id ( etcp ) ;
@ -491,6 +520,7 @@ struct ETCP_LINK* etcp_link_new(struct ETCP_CONN* etcp, struct ETCP_SOCKET* conn
// link->last_activity = time(NULL);
link - > ip_port_hash = sockaddr_hash ( remote_addr ) ;
link - > last_recv_local_time = get_current_time_units ( ) ; // Initialize to prevent immediate timeout
insert_link ( conn , link ) ;
@ -803,6 +833,13 @@ process_decrypted:
link - > last_recv_timestamp = pkt - > timestamp ;
link - > last_recv_updated = 1 ;
// Mark link as up when receiving packets
if ( link - > link_status ! = 1 ) {
link - > link_status = 1 ;
DEBUG_INFO ( DEBUG_CATEGORY_CONNECTION , " [%s] Link %p (local_id=%d) status changed to UP - packet received " ,
link - > etcp - > log_name , link , link - > local_link_id ) ;
}
size_t offset = 0 ;
uint8_t code = pkt - > data [ offset + + ] ;