From b74ad5954607d34e939f9cf32876bd7ba65998f9 Mon Sep 17 00:00:00 2001 From: Guo Wenxue <guowenxue@gmail.com> Date: Thu, 11 Apr 2024 13:08:05 +0800 Subject: [PATCH] Update socket example project --- project/socketd/booster/proc.c | 2 project/socketd/booster/socket.h | 12 ++++-- project/socketd/sock_client.c | 26 ++++-------- project/socketd/booster/socket.c | 68 +++++++++++++++++++++++---------- 4 files changed, 65 insertions(+), 43 deletions(-) diff --git a/project/socketd/booster/proc.c b/project/socketd/booster/proc.c index 3fa8907..b55b202 100644 --- a/project/socketd/booster/proc.c +++ b/project/socketd/booster/proc.c @@ -357,7 +357,7 @@ * *****************************************************************************/ int set_daemon_running(const char *pid_file) { - daemonize(0, 1); + daemon(0, 1); log_info("Program running as daemon [PID:%d].\n", getpid()); if (record_daemon_pid(pid_file) < 0) diff --git a/project/socketd/booster/socket.c b/project/socketd/booster/socket.c index 0206bb9..4049bfc 100644 --- a/project/socketd/booster/socket.c +++ b/project/socketd/booster/socket.c @@ -69,6 +69,7 @@ { close(sock->fd); sock->fd = -1; + sock->connected = 0; } return 0; @@ -92,6 +93,52 @@ set_socket_rlimit(); /* set max open socket count */ } #endif + +/* description: check socket connect status + * input args: + * $sock: socket context pointer + * return value: 1: connected 0:disconnected + */ +int socket_connected(socket_ctx_t *sock) +{ + struct tcp_info info; + int len=sizeof(info); + int changed = 0; + + if( !sock ) + { + return 0; + } + + if( sock->fd < 0 ) + { + /* socket is connected before but got disconnected now */ + changed = sock->connected ? 1 : 0; + sock->connected = 0; + goto out; + } + + getsockopt(sock->fd, IPPROTO_TCP, TCP_INFO, &info, (socklen_t *)&len); + if( TCP_ESTABLISHED==info.tcpi_state ) + { + /* socket is disconnected before but got connected now */ + changed = !sock->connected ? 1 : 0; + sock->connected = 1; + } + else + { + /* socket is connected before but got disconnected now */ + changed = sock->connected ? 1 : 0; + sock->connected = 0; + } + +out: + if( changed ) + { + log_info("socket status got %s\n", sock->connected?"connected":"disconnected"); + } + return sock->connected; +} /* description: socket connect to server in block mode * input args: @@ -288,30 +335,9 @@ } - /*+-------------------------------------------------------------------+ *| socket utils function | *+-------------------------------------------------------------------+*/ - - -/* socket connected or not: <0: failure 0:ok */ -int sock_check_connect(int sockfd) -{ - struct tcp_info info; - int len=sizeof(info); - - if( sockfd < 0 ) - return -1; - - getsockopt(sockfd, IPPROTO_TCP, TCP_INFO, &info, (socklen_t *)&len); - - if( TCP_CLOSE==info.tcpi_state || TCP_CLOSING==info.tcpi_state || TCP_CLOSE_WAIT==info.tcpi_state ) - { - return -3; - } - - return -0; -} /* description: set socket listen port as reusable, fix port already used bug */ int socket_set_reuseaddr(int sockfd) diff --git a/project/socketd/booster/socket.h b/project/socketd/booster/socket.h index f0c5fb4..dc41d69 100644 --- a/project/socketd/booster/socket.h +++ b/project/socketd/booster/socket.h @@ -21,6 +21,7 @@ char host[HOSTNAME_LEN]; /* CLIENT: Connect server hostname; SERVER: Unused */ int port; /* CLIENT: Connect server port; SERVER: listen port */ int fd; /* socket descriptor */ + int connected; /* socket connect status: 1->connected 0->disconnected */ } socket_ctx_t; /* description: initial socket context @@ -45,6 +46,13 @@ * return value: <0: failure 0:ok */ extern int socket_listen(socket_ctx_t *sock); + +/* description: check socket connect status + * input args: + * $sock: socket context pointer + * return value: 1: connected 0:disconnected + */ +extern int socket_connected(socket_ctx_t *sock); /* description: socket client connect to server * input args: @@ -76,10 +84,6 @@ /*+-------------------------------------------------------------------+ *| socket utils function | *+-------------------------------------------------------------------+*/ - - -/* socket connected or not: <0: failure 0:ok */ -extern int sock_check_connect(int sockfd); /* description: set socket listen port as reusable, fix port already used bug */ extern int socket_set_reuseaddr(int sockfd); diff --git a/project/socketd/sock_client.c b/project/socketd/sock_client.c index 20f1898..6a594d9 100644 --- a/project/socketd/sock_client.c +++ b/project/socketd/sock_client.c @@ -69,7 +69,9 @@ char pack_buf[1024]; int pack_bytes = 0; pack_info_t pack_info; - pack_proc_t pack_proc = packet_segmented_pack; /* use string packet */ + pack_proc_t pack_proc = packet_segmented_pack; /* use segmented string packet */ + //pack_proc_t pack_proc = packet_json_pack; /* use JSON string packet */ + //pack_proc_t pack_proc = packet_tlv_pack; /* use TLV(Tag Length Value) packet */ struct option opts[] = { {"ipaddr", required_argument, NULL, 'i'}, @@ -180,25 +182,15 @@ * +---------------------------------+*/ /* start connect to server if not connected */ - if( sock.fd < 0 ) + if( !socket_connected(&sock) ) { socket_connect(&sock); - } - - /* check socket connected or not */ - if( sock_check_connect(sock.fd) < 0 ) - { - if( sock.fd > 0 ) - { - log_error("socket got disconnected, terminate it and reconnect now.\n"); - socket_term(&sock); /* close the soket */ - } } /* +-------------------------------+ * | socket disconnect | * +-------------------------------+*/ - if( sock.fd < 0 ) + if( !socket_connected(&sock) ) { if( sample_flag ) { @@ -220,7 +212,7 @@ { log_warn("socket send sample packet failure, save it in database now.\n"); database_push_packet(pack_buf, pack_bytes); - socket_term(&sock); /* close the soket */ + continue; } } @@ -231,7 +223,7 @@ if( socket_send(&sock, pack_buf, pack_bytes) < 0 ) { log_error("socket send database packet failure"); - socket_term(&sock); /* close the soket */ + continue; } else { @@ -240,7 +232,7 @@ } } - msleep(50); + msleep(5); } cleanup: @@ -259,7 +251,7 @@ time(&now); - if( now >= *last_time+interval ) + if( difftime(now, *last_time)>interval ) { need = 1; /* need sample now */ *last_time = now; -- Gitblit v1.9.1