Skip to content

Commit

Permalink
fix: refactor control keepalive mechanism with improved initializatio…
Browse files Browse the repository at this point in the history
…n and error handling

Signed-off-by: Dengfeng Liu <liudf0716@gmail.com>
  • Loading branch information
liudf0716 committed Nov 14, 2024
1 parent 73a8e58 commit 0d96d26
Showing 1 changed file with 33 additions and 6 deletions.
39 changes: 33 additions & 6 deletions control.c
Original file line number Diff line number Diff line change
Expand Up @@ -669,17 +669,44 @@ connect_event_cb (struct bufferevent *bev, short what, void *ctx)
}
}

static void
keep_control_alive()
static int init_ping_ticker(struct control *ctl) {
if (!ctl || !ctl->connect_base) {
debug(LOG_ERR, "Invalid control structure or event base");
return -1;
}

struct event *ticker = evtimer_new(ctl->connect_base, hb_sender_cb, NULL);
if (!ticker) {
debug(LOG_ERR, "Failed to create ping ticker event");
return -1;
}

ctl->ticker_ping = ticker;
return 0;
}

static void keep_control_alive()
{
debug(LOG_DEBUG, "start keep_control_alive");
main_ctl->ticker_ping = evtimer_new(main_ctl->connect_base, hb_sender_cb, NULL);
if ( !main_ctl->ticker_ping) {
debug(LOG_ERR, "Ping Ticker init failed!");
debug(LOG_DEBUG, "Initializing control keepalive");

// Initialize ping ticker
if (init_ping_ticker(main_ctl) != 0) {
debug(LOG_ERR, "Failed to initialize control keepalive");
return;
}

// Set initial pong time
pong_time = time(NULL);
if (pong_time == (time_t)-1) {
debug(LOG_ERR, "Failed to get current time");
event_free(main_ctl->ticker_ping);
main_ctl->ticker_ping = NULL;
return;
}

// Start ticker timer
set_ticker_ping_timer(main_ctl->ticker_ping);
debug(LOG_DEBUG, "Control keepalive initialized successfully");
}

static int init_server_connection(struct bufferevent **bev_out,
Expand Down

0 comments on commit 0d96d26

Please sign in to comment.