Skip to content

Commit a8fd709

Browse files
committed
fix: refactor server connection initialization with improved error handling
Signed-off-by: Dengfeng Liu <[email protected]>
1 parent d69454f commit a8fd709

File tree

1 file changed

+22
-8
lines changed

1 file changed

+22
-8
lines changed

control.c

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -801,20 +801,34 @@ get_main_control()
801801
return main_ctl;
802802
}
803803

804-
void
805-
start_login_frp_server(struct event_base *base)
806-
{
804+
static int init_frp_connection(struct bufferevent **bev_out, struct event_base *base) {
807805
struct common_conf *c_conf = get_common_config();
806+
if (!c_conf) {
807+
debug(LOG_ERR, "Failed to get common config");
808+
return -1;
809+
}
810+
808811
struct bufferevent *bev = connect_server(base, c_conf->server_addr, c_conf->server_port);
809812
if (!bev) {
810-
debug(LOG_DEBUG,
811-
"Connect server [%s:%d] failed",
812-
c_conf->server_addr,
813-
c_conf->server_port);
813+
debug(LOG_ERR, "Failed to connect to server [%s:%d]",
814+
c_conf->server_addr, c_conf->server_port);
815+
return -1;
816+
}
817+
818+
*bev_out = bev;
819+
return 0;
820+
}
821+
822+
void start_login_frp_server(struct event_base *base)
823+
{
824+
struct bufferevent *bev = NULL;
825+
if (init_frp_connection(&bev, base) != 0) {
814826
return;
815827
}
816828

817-
debug(LOG_INFO, "Xfrpc login: connect server [%s:%d] ...", c_conf->server_addr, c_conf->server_port);
829+
struct common_conf *c_conf = get_common_config();
830+
debug(LOG_INFO, "Xfrpc login: connecting to server [%s:%d]...",
831+
c_conf->server_addr, c_conf->server_port);
818832

819833
bufferevent_enable(bev, EV_WRITE|EV_READ);
820834
bufferevent_setcb(bev, NULL, NULL, connect_event_cb, NULL);

0 commit comments

Comments
 (0)