Skip to content

Commit 0d96d26

Browse files
committed
fix: refactor control keepalive mechanism with improved initialization and error handling
Signed-off-by: Dengfeng Liu <[email protected]>
1 parent 73a8e58 commit 0d96d26

File tree

1 file changed

+33
-6
lines changed

1 file changed

+33
-6
lines changed

control.c

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -669,17 +669,44 @@ connect_event_cb (struct bufferevent *bev, short what, void *ctx)
669669
}
670670
}
671671

672-
static void
673-
keep_control_alive()
672+
static int init_ping_ticker(struct control *ctl) {
673+
if (!ctl || !ctl->connect_base) {
674+
debug(LOG_ERR, "Invalid control structure or event base");
675+
return -1;
676+
}
677+
678+
struct event *ticker = evtimer_new(ctl->connect_base, hb_sender_cb, NULL);
679+
if (!ticker) {
680+
debug(LOG_ERR, "Failed to create ping ticker event");
681+
return -1;
682+
}
683+
684+
ctl->ticker_ping = ticker;
685+
return 0;
686+
}
687+
688+
static void keep_control_alive()
674689
{
675-
debug(LOG_DEBUG, "start keep_control_alive");
676-
main_ctl->ticker_ping = evtimer_new(main_ctl->connect_base, hb_sender_cb, NULL);
677-
if ( !main_ctl->ticker_ping) {
678-
debug(LOG_ERR, "Ping Ticker init failed!");
690+
debug(LOG_DEBUG, "Initializing control keepalive");
691+
692+
// Initialize ping ticker
693+
if (init_ping_ticker(main_ctl) != 0) {
694+
debug(LOG_ERR, "Failed to initialize control keepalive");
679695
return;
680696
}
697+
698+
// Set initial pong time
681699
pong_time = time(NULL);
700+
if (pong_time == (time_t)-1) {
701+
debug(LOG_ERR, "Failed to get current time");
702+
event_free(main_ctl->ticker_ping);
703+
main_ctl->ticker_ping = NULL;
704+
return;
705+
}
706+
707+
// Start ticker timer
682708
set_ticker_ping_timer(main_ctl->ticker_ping);
709+
debug(LOG_DEBUG, "Control keepalive initialized successfully");
683710
}
684711

685712
static int init_server_connection(struct bufferevent **bev_out,

0 commit comments

Comments
 (0)