project/socketd/booster/proc.c | ●●●●● patch | view | raw | blame | history | |
project/socketd/booster/socket.c | ●●●●● patch | view | raw | blame | history | |
project/socketd/booster/socket.h | ●●●●● patch | view | raw | blame | history | |
project/socketd/sock_client.c | ●●●●● patch | view | raw | blame | history |
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) 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) 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); 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;