1
0
mirror of https://github.com/fumiama/simple-dict.git synced 2026-06-27 07:20:25 +08:00

优化代码结构

This commit is contained in:
源文雨
2022-04-17 18:58:03 +08:00
parent 0bb8889ca4
commit 622bbdc0a4

View File

@@ -25,11 +25,9 @@
#ifdef LISTEN_ON_IPV6 #ifdef LISTEN_ON_IPV6
static socklen_t struct_len = sizeof(struct sockaddr_in6); static socklen_t struct_len = sizeof(struct sockaddr_in6);
static struct sockaddr_in6 server_addr; static struct sockaddr_in6 server_addr;
static struct sockaddr_in6 client_addr;
#else #else
static socklen_t struct_len = sizeof(struct sockaddr_in); static socklen_t struct_len = sizeof(struct sockaddr_in);
static struct sockaddr_in server_addr; static struct sockaddr_in server_addr;
static struct sockaddr_in client_addr;
#endif #endif
struct THREADTIMER { struct THREADTIMER {
@@ -43,7 +41,7 @@ struct THREADTIMER {
typedef struct THREADTIMER THREADTIMER; typedef struct THREADTIMER THREADTIMER;
static THREADTIMER timers[THREADCNT]; static THREADTIMER timers[THREADCNT];
#define timer_pointer_of(x) ((THREADTIMER*)(x)) #define timer_pointer_of(x) ((THREADTIMER*)(x))
#define touch_timer(x) timer_pointer_of(x)->touch = time(NULL) #define touch_timer(x) (timer_pointer_of(x)->touch = time(NULL))
static int fd; // server fd static int fd; // server fd
static pthread_t accept_threads[THREADCNT]; static pthread_t accept_threads[THREADCNT];
@@ -131,7 +129,7 @@ static int send_data(int accept_fd, int index, enum SERVERACK cmd, char *data, s
cmdpacket_encrypt(p, index, cfg.pwd); cmdpacket_encrypt(p, index, cfg.pwd);
int total = CMDPACKET_HEAD_LEN+p->datalen; int total = CMDPACKET_HEAD_LEN+p->datalen;
if(!~send(accept_fd, buf, total, 0)) { if(!~send(accept_fd, buf, total, 0)) {
puts("Send data error"); perror("Send data error: ");
return 0; return 0;
} else { } else {
printf("Send %d bytes data: ", total); printf("Send %d bytes data: ", total);
@@ -302,7 +300,7 @@ static int s3_set_data(THREADTIMER *timer) {
memcpy(setdict->data, timer->dat, datasize); memcpy(setdict->data, timer->dat, datasize);
if(!set_pb(get_dict_fp_wr(), items_len, sizeof(DICT), setdict)) { if(!set_pb(get_dict_fp_wr(), items_len, sizeof(DICT), setdict)) {
printf("Error set data: dict[%s]=%s\n", setdict->key, timer->dat); fprintf(stderr, "Error set data: dict[%s]=%s\n", setdict->key, timer->dat);
return close_and_send(timer, ACKERRO, "erro", 4); return close_and_send(timer, ACKERRO, "erro", 4);
} else { } else {
printf("Set data: dict[%s]=%s\n", setdict->key, timer->dat); printf("Set data: dict[%s]=%s\n", setdict->key, timer->dat);
@@ -431,18 +429,21 @@ static void kill_thread(THREADTIMER* timer) {
} }
static void handle_pipe(int signo) { static void handle_pipe(int signo) {
printf("Pipe error: %d\n", signo); fprintf(stderr, "Pipe error: %d\n", signo);
pthread_exit(NULL); pthread_exit(NULL);
} }
static void handle_accept(void *p) { static void handle_accept(void *p) {
int accept_fd = timer_pointer_of(p)->accept_fd; puts("\nConnected to the client, handling accept...");
if(accept_fd > 0) {
puts("\nConnected to the client");
pthread_t thread; pthread_t thread;
if (pthread_create(&thread, &attr, (void *)&accept_timer, p)) {
perror("Error creating timer thread: ");
kill_thread(timer_pointer_of(p));
return;
}
puts("Creating timer thread succeeded");
pthread_cleanup_push((void*)&kill_thread, p); pthread_cleanup_push((void*)&kill_thread, p);
if (pthread_create(&thread, &attr, (void *)&accept_timer, p)) puts("Error creating timer thread"); int accept_fd = timer_pointer_of(p)->accept_fd;
else puts("Creating timer thread succeeded");
uint32_t index = timer_pointer_of(p)->index; uint32_t index = timer_pointer_of(p)->index;
char *buff = malloc(BUFSIZ*sizeof(char)); char *buff = malloc(BUFSIZ*sizeof(char));
if(buff) { if(buff) {
@@ -543,10 +544,9 @@ static void handle_accept(void *p) {
#endif #endif
} }
CONV_END: puts("Conversation end"); CONV_END: puts("Conversation end");
} else puts("Error allocating buffer"); } else perror("Error allocating buffer: ");
pthread_cleanup_pop(1); pthread_cleanup_pop(1);
puts("Thread exited normally"); puts("Thread exited normally");
} else puts("Error accepting client");
} }
static pid_t pid; static pid_t pid;
@@ -563,18 +563,25 @@ static void accept_client() {
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
init_crypto(); init_crypto();
init_dict_pool(get_dict_fp_rd()); init_dict_pool(get_dict_fp_rd());
if(pid < 0) puts("Error when forking a subprocess"); if(pid < 0) perror("Error when forking a subprocess: ");
else while(1) { else while(1) {
puts("Ready for accept, waitting..."); puts("Ready for accept, waitting...");
int p = 0; int p = 0;
while(p < THREADCNT && accept_threads[p] && !pthread_kill(accept_threads[p], 0)) p++; while(p < THREADCNT && accept_threads[p] && !pthread_kill(accept_threads[p], 0)) p++;
if(p < THREADCNT) { if(p >= THREADCNT) {
puts("Max thread cnt exceeded");
sleep(1);
continue;
}
printf("Thread slot is empty at No.%d\n", p); printf("Thread slot is empty at No.%d\n", p);
THREADTIMER* timer = &timers[p]; #ifdef LISTEN_ON_IPV6
timer->accept_fd = accept(fd, (struct sockaddr *)&client_addr, &struct_len); struct sockaddr_in6 client_addr;
if(timer->accept_fd <= 0) { #else
puts("Accept client error"); struct sockaddr_in client_addr;
} else { #endif
int accept_fd;
while((accept_fd=accept(fd, (struct sockaddr *)&client_addr, &struct_len))<=0)
perror("Accept client error: ");
#ifdef LISTEN_ON_IPV6 #ifdef LISTEN_ON_IPV6
uint16_t port = ntohs(client_addr.sin6_port); uint16_t port = ntohs(client_addr.sin6_port);
struct in6_addr in = client_addr.sin6_addr; struct in6_addr in = client_addr.sin6_addr;
@@ -587,17 +594,18 @@ static void accept_client() {
inet_ntop(AF_INET, &in, str, sizeof(str)); inet_ntop(AF_INET, &in, str, sizeof(str));
#endif #endif
printf("Accept client %s:%u\n", str, port); printf("Accept client %s:%u\n", str, port);
THREADTIMER* timer = &timers[p];
timer->accept_fd = accept_fd;
timer->index = p; timer->index = p;
timer->touch = time(NULL); timer->touch = time(NULL);
timer->ptr = NULL; timer->ptr = NULL;
reset_seq(p); reset_seq(p);
if (pthread_create(accept_threads + p, &attr, (void *)&handle_accept, timer)) puts("Error creating thread"); if (pthread_create(accept_threads + p, &attr, (void *)&handle_accept, timer)) {
else puts("Creating thread succeeded"); perror("Error creating thread: ");
} kill_thread(timer);
} else { continue;
puts("Max thread cnt exceeded");
sleep(1);
} }
puts("Creating thread succeeded");
} }
} }
@@ -652,13 +660,19 @@ int main(int argc, char *argv[]) {
items_len = align_struct(sizeof(DICT), 2, d.key, d.data); items_len = align_struct(sizeof(DICT), 2, d.key, d.data);
if(items_len) { if(items_len) {
if(bind_server(port, times)) if(listen_socket(times)) accept_client(); if(bind_server(port, times)) if(listen_socket(times)) accept_client();
} else puts("Align struct error"); } else fputs("Align struct error", stderr);
} else printf("Error opening config file: %s\n", argv[as_daemon?5:4]); } else {
fprintf(stderr, "Error opening config file: %s : ", argv[as_daemon?5:4]);
perror("");
} }
} else printf("Error opening dict file: %s\n", argv[as_daemon?4:3]); }
} else puts("Start daemon error"); } else {
} else printf("Error times: %d\n", times); fprintf(stderr, "Error opening dict file: %s : ", argv[as_daemon?4:3]);
} else printf("Error port: %d\n", port); perror("");
}
} else perror("Start daemon error: ");
} else fprintf(stderr, "Error times: %d\n", times);
} else fprintf(stderr, "Error port: %d\n", port);
} }
close(fd); close(fd);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);