1
0
mirror of https://github.com/fumiama/simple-dict.git synced 2026-07-02 01:40:24 +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
static socklen_t struct_len = sizeof(struct sockaddr_in6);
static struct sockaddr_in6 server_addr;
static struct sockaddr_in6 client_addr;
#else
static socklen_t struct_len = sizeof(struct sockaddr_in);
static struct sockaddr_in server_addr;
static struct sockaddr_in client_addr;
#endif
struct THREADTIMER {
@@ -43,7 +41,7 @@ struct THREADTIMER {
typedef struct THREADTIMER THREADTIMER;
static THREADTIMER timers[THREADCNT];
#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 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);
int total = CMDPACKET_HEAD_LEN+p->datalen;
if(!~send(accept_fd, buf, total, 0)) {
puts("Send data error");
perror("Send data error: ");
return 0;
} else {
printf("Send %d bytes data: ", total);
@@ -302,7 +300,7 @@ static int s3_set_data(THREADTIMER *timer) {
memcpy(setdict->data, timer->dat, datasize);
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);
} else {
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) {
printf("Pipe error: %d\n", signo);
fprintf(stderr, "Pipe error: %d\n", signo);
pthread_exit(NULL);
}
static void handle_accept(void *p) {
int accept_fd = timer_pointer_of(p)->accept_fd;
if(accept_fd > 0) {
puts("\nConnected to the client");
puts("\nConnected to the client, handling accept...");
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);
if (pthread_create(&thread, &attr, (void *)&accept_timer, p)) puts("Error creating timer thread");
else puts("Creating timer thread succeeded");
int accept_fd = timer_pointer_of(p)->accept_fd;
uint32_t index = timer_pointer_of(p)->index;
char *buff = malloc(BUFSIZ*sizeof(char));
if(buff) {
@@ -543,10 +544,9 @@ static void handle_accept(void *p) {
#endif
}
CONV_END: puts("Conversation end");
} else puts("Error allocating buffer");
} else perror("Error allocating buffer: ");
pthread_cleanup_pop(1);
puts("Thread exited normally");
} else puts("Error accepting client");
}
static pid_t pid;
@@ -563,18 +563,25 @@ static void accept_client() {
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
init_crypto();
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) {
puts("Ready for accept, waitting...");
int p = 0;
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);
THREADTIMER* timer = &timers[p];
timer->accept_fd = accept(fd, (struct sockaddr *)&client_addr, &struct_len);
if(timer->accept_fd <= 0) {
puts("Accept client error");
} else {
#ifdef LISTEN_ON_IPV6
struct sockaddr_in6 client_addr;
#else
struct sockaddr_in client_addr;
#endif
int accept_fd;
while((accept_fd=accept(fd, (struct sockaddr *)&client_addr, &struct_len))<=0)
perror("Accept client error: ");
#ifdef LISTEN_ON_IPV6
uint16_t port = ntohs(client_addr.sin6_port);
struct in6_addr in = client_addr.sin6_addr;
@@ -587,17 +594,18 @@ static void accept_client() {
inet_ntop(AF_INET, &in, str, sizeof(str));
#endif
printf("Accept client %s:%u\n", str, port);
THREADTIMER* timer = &timers[p];
timer->accept_fd = accept_fd;
timer->index = p;
timer->touch = time(NULL);
timer->ptr = NULL;
reset_seq(p);
if (pthread_create(accept_threads + p, &attr, (void *)&handle_accept, timer)) puts("Error creating thread");
else puts("Creating thread succeeded");
}
} else {
puts("Max thread cnt exceeded");
sleep(1);
if (pthread_create(accept_threads + p, &attr, (void *)&handle_accept, timer)) {
perror("Error creating thread: ");
kill_thread(timer);
continue;
}
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);
if(items_len) {
if(bind_server(port, times)) if(listen_socket(times)) accept_client();
} else puts("Align struct error");
} else printf("Error opening config file: %s\n", argv[as_daemon?5:4]);
} else fputs("Align struct error", stderr);
} 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 printf("Error times: %d\n", times);
} else printf("Error port: %d\n", port);
}
} else {
fprintf(stderr, "Error opening dict file: %s : ", argv[as_daemon?4:3]);
perror("");
}
} else perror("Start daemon error: ");
} else fprintf(stderr, "Error times: %d\n", times);
} else fprintf(stderr, "Error port: %d\n", port);
}
close(fd);
exit(EXIT_FAILURE);