mirror of
https://github.com/fumiama/simple-dict.git
synced 2026-06-20 02:40:24 +08:00
优化发送速度,丰富client功能,解决半开问题
This commit is contained in:
55
client.c
55
client.c
@@ -7,23 +7,42 @@
|
|||||||
#include <netinet/in.h>
|
#include <netinet/in.h>
|
||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
#include <arpa/inet.h>
|
#include <arpa/inet.h>
|
||||||
|
#include <sys/stat.h>
|
||||||
#include <pthread.h>
|
#include <pthread.h>
|
||||||
|
|
||||||
|
#if !__APPLE__
|
||||||
|
#include <sys/sendfile.h>
|
||||||
|
#else
|
||||||
|
struct sf_hdtr hdtr;
|
||||||
|
#endif
|
||||||
|
|
||||||
int sockfd;
|
int sockfd;
|
||||||
char buf[BUFSIZ];
|
char buf[BUFSIZ];
|
||||||
char bufr[BUFSIZ];
|
char bufr[BUFSIZ];
|
||||||
struct sockaddr_in their_addr;
|
struct sockaddr_in their_addr;
|
||||||
pthread_t thread;
|
pthread_t thread;
|
||||||
|
uint32_t file_size;
|
||||||
|
int recv_bin = 0;
|
||||||
|
|
||||||
void getMessage(void *p) {
|
void getMessage(void *p) {
|
||||||
int c = 0;
|
int c = 0;
|
||||||
while((c = recv(sockfd, bufr, BUFSIZ, 0)) > 0) {
|
while((c = recv(sockfd, bufr, BUFSIZ, 0)) > 0) {
|
||||||
printf("Recv %d bytes: ", c);
|
printf("Recv %d bytes: ", c);
|
||||||
for(int i = 0; i < c; i++) putchar(bufr[i]);
|
if(recv_bin) {
|
||||||
|
FILE* fp = fopen("dump.bin", "w+");
|
||||||
|
fwrite(bufr, c, 1, fp);
|
||||||
|
fclose(fp);
|
||||||
|
} else for(int i = 0; i < c; i++) putchar(bufr[i]);
|
||||||
putchar('\n');
|
putchar('\n');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
off_t fileSize(const char* fname) {
|
||||||
|
struct stat statbuf;
|
||||||
|
if(stat(fname, &statbuf)==0) return statbuf.st_size;
|
||||||
|
else return -1;
|
||||||
|
}
|
||||||
|
|
||||||
int main(int argc,char *argv[]) { //usage: ./client host port
|
int main(int argc,char *argv[]) { //usage: ./client host port
|
||||||
ssize_t numbytes;
|
ssize_t numbytes;
|
||||||
puts("break!");
|
puts("break!");
|
||||||
@@ -44,10 +63,40 @@ int main(int argc,char *argv[]) { //usage: ./client host port
|
|||||||
while(1) {
|
while(1) {
|
||||||
printf("Enter command:");
|
printf("Enter command:");
|
||||||
scanf("%s",buf);
|
scanf("%s",buf);
|
||||||
numbytes = send(sockfd, buf, strlen(buf), 0);
|
if(!strcmp(buf, "bin")) recv_bin = !recv_bin;
|
||||||
|
else if(!strcmp(buf, "file")) {
|
||||||
|
printf("Enter file path:");
|
||||||
|
scanf("%s",buf);
|
||||||
|
printf("Open:");
|
||||||
|
puts(buf);
|
||||||
|
FILE *fp = NULL;
|
||||||
|
fp = fopen(buf, "rb");
|
||||||
|
if(fp) {
|
||||||
|
off_t len = 0;
|
||||||
|
file_size = (uint32_t)fileSize(buf);
|
||||||
|
#if __APPLE__
|
||||||
|
struct iovec headers;
|
||||||
|
headers.iov_base = &file_size;
|
||||||
|
headers.iov_len = sizeof(uint32_t);
|
||||||
|
hdtr.headers = &headers;
|
||||||
|
hdtr.hdr_cnt = 1;
|
||||||
|
if(!sendfile(fileno(fp), sockfd, 0, &len, &hdtr, 0)) puts("Send file success.");
|
||||||
|
else puts("Send file error.");
|
||||||
|
#else
|
||||||
|
send(sockfd, &file_size, sizeof(uint32_t), 0);
|
||||||
|
if(!sendfile(sockfd, fileno(fp), &len, file_size)) puts("Send file success.");
|
||||||
|
else puts("Send file error.");
|
||||||
|
#endif
|
||||||
|
fclose(fp);
|
||||||
|
printf("Send count:%lld\n", len);
|
||||||
|
} else puts("Open file error!");
|
||||||
|
} else {
|
||||||
|
send(sockfd, buf, strlen(buf), 0);
|
||||||
|
if(!strcmp(buf, "quit")) exit(EXIT_SUCCESS);
|
||||||
|
}
|
||||||
sleep(1);
|
sleep(1);
|
||||||
}
|
}
|
||||||
} else perror("Create msg thread failed");
|
} else perror("Create msg thread failed");
|
||||||
close(sockfd);
|
close(sockfd);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
166
server.c
166
server.c
@@ -14,7 +14,12 @@
|
|||||||
#include <pthread.h>
|
#include <pthread.h>
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
|
|
||||||
|
#if !__APPLE__
|
||||||
|
#include <sys/sendfile.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
#define PASSWORD "fumiama"
|
#define PASSWORD "fumiama"
|
||||||
|
#define SETPASS "minamoto"
|
||||||
|
|
||||||
int fd;
|
int fd;
|
||||||
//ssize_t numbytes;
|
//ssize_t numbytes;
|
||||||
@@ -50,21 +55,22 @@ DICTBLK dict;
|
|||||||
|
|
||||||
#define showUsage(program) printf("Usage: %s [-d] listen_port try_times dict_file\n\t-d: As daemon\n", program)
|
#define showUsage(program) printf("Usage: %s [-d] listen_port try_times dict_file\n\t-d: As daemon\n", program)
|
||||||
|
|
||||||
void acceptClient();
|
void accept_client();
|
||||||
void acceptTimer(void *p);
|
void accept_timer(void *p);
|
||||||
int bindServer(uint16_t port, u_int try_times);
|
int bind_server(uint16_t port, u_int try_times);
|
||||||
int checkBuffer(THREADTIMER *timer);
|
int check_buffer(THREADTIMER *timer);
|
||||||
int freeAfterSend(int accept_fd, char *data, size_t length);
|
void close_dict(FILE *fp);
|
||||||
void closeDict(FILE *fp);
|
int close_and_send(THREADTIMER *timer, char *data, size_t numbytes);
|
||||||
int closeDictAndSend(THREADTIMER *timer, char *data, size_t numbytes);
|
|
||||||
off_t fileSize(const char* fname);
|
off_t fileSize(const char* fname);
|
||||||
void handleAccept(void *accept_fd_p);
|
int free_after_send(int accept_fd, char *data, size_t length);
|
||||||
|
void handle_accept(void *accept_fd_p);
|
||||||
void handle_pipe(int signo);
|
void handle_pipe(int signo);
|
||||||
void handle_quit(int signo);
|
void handle_quit(int signo);
|
||||||
int listenSocket(u_int try_times);
|
void kill_thread(THREADTIMER* timer);
|
||||||
FILE *openDict(int lock_type);
|
int listen_socket(u_int try_times);
|
||||||
int sendAll(THREADTIMER *timer);
|
FILE *open_dict(int lock_type);
|
||||||
int sendData(int accept_fd, char *data, size_t length);
|
int send_all(THREADTIMER *timer);
|
||||||
|
int send_data(int accept_fd, char *data, size_t length);
|
||||||
int sm1_pwd(THREADTIMER *timer);
|
int sm1_pwd(THREADTIMER *timer);
|
||||||
int s0_init(THREADTIMER *timer);
|
int s0_init(THREADTIMER *timer);
|
||||||
int s1_get(THREADTIMER *timer);
|
int s1_get(THREADTIMER *timer);
|
||||||
@@ -73,7 +79,7 @@ int s3_setData(THREADTIMER *timer);
|
|||||||
int s4_del(THREADTIMER *timer);
|
int s4_del(THREADTIMER *timer);
|
||||||
int s5_list(THREADTIMER *timer);
|
int s5_list(THREADTIMER *timer);
|
||||||
|
|
||||||
int bindServer(uint16_t port, u_int try_times) {
|
int bind_server(uint16_t port, u_int try_times) {
|
||||||
int fail_count = 0;
|
int fail_count = 0;
|
||||||
int result = -1;
|
int result = -1;
|
||||||
server_addr.sin_family = AF_INET;
|
server_addr.sin_family = AF_INET;
|
||||||
@@ -92,7 +98,7 @@ int bindServer(uint16_t port, u_int try_times) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int listenSocket(u_int try_times) {
|
int listen_socket(u_int try_times) {
|
||||||
int fail_count = 0;
|
int fail_count = 0;
|
||||||
int result = -1;
|
int result = -1;
|
||||||
while(!~(result = listen(fd, 10)) && fail_count++ < try_times) sleep(1);
|
while(!~(result = listen(fd, 10)) && fail_count++ < try_times) sleep(1);
|
||||||
@@ -105,13 +111,7 @@ int listenSocket(u_int try_times) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int freeAfterSend(int accept_fd, char *data, size_t length) {
|
int send_data(int accept_fd, char *data, size_t length) {
|
||||||
int re = sendData(accept_fd, data, length);
|
|
||||||
free(data);\
|
|
||||||
return re;
|
|
||||||
}
|
|
||||||
|
|
||||||
int sendData(int accept_fd, char *data, size_t length) {
|
|
||||||
if(!~send(accept_fd, data, length, 0)) {
|
if(!~send(accept_fd, data, length, 0)) {
|
||||||
puts("Send data error");
|
puts("Send data error");
|
||||||
return 0;
|
return 0;
|
||||||
@@ -122,18 +122,37 @@ int sendData(int accept_fd, char *data, size_t length) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int sendAll(THREADTIMER *timer) {
|
int free_after_send(int accept_fd, char *data, size_t length) {
|
||||||
|
int re = send_data(accept_fd, data, length);
|
||||||
|
free(data);\
|
||||||
|
return re;
|
||||||
|
}
|
||||||
|
|
||||||
|
int send_all(THREADTIMER *timer) {
|
||||||
int re = 1;
|
int re = 1;
|
||||||
FILE *fp = openDict(LOCK_SH);
|
FILE *fp = open_dict(LOCK_SH);
|
||||||
size_t numbytes;
|
size_t numbytes;
|
||||||
if(fp) {
|
if(fp) {
|
||||||
timer->fp = fp;
|
timer->fp = fp;
|
||||||
timer->is_open = 1;
|
timer->is_open = 1;
|
||||||
|
off_t len = 0;
|
||||||
sprintf(timer->data, "%zd", fileSize(file_path));
|
sprintf(timer->data, "%zd", fileSize(file_path));
|
||||||
re = sendData(timer->accept_fd, timer->data, strlen(timer->data));
|
printf("Get file size: %s bytes.\n", timer->data);
|
||||||
while(re && (numbytes = fread(timer->data, 1, BUFSIZ, fp)) > 0)
|
uint32_t head_len = strlen(timer->data);
|
||||||
re = sendData(timer->accept_fd, timer->data, numbytes);
|
#if __APPLE__
|
||||||
closeDict(fp);
|
struct sf_hdtr hdtr;
|
||||||
|
struct iovec headers;
|
||||||
|
headers.iov_base = timer->data;
|
||||||
|
headers.iov_len = head_len;
|
||||||
|
hdtr.headers = &headers;
|
||||||
|
hdtr.hdr_cnt = 1;
|
||||||
|
sendfile(fileno(fp), timer->accept_fd, 0, &len, &hdtr, 0);
|
||||||
|
#else
|
||||||
|
send_data(timer->accept_fd, timer->data, head_len);
|
||||||
|
sendfile(timer->accept_fd, fileno(fp), &len, file_size);
|
||||||
|
#endif
|
||||||
|
printf("Send %lld bytes.\n", len);
|
||||||
|
close_dict(fp);
|
||||||
timer->is_open = 0;
|
timer->is_open = 0;
|
||||||
}
|
}
|
||||||
return re;
|
return re;
|
||||||
@@ -146,16 +165,16 @@ int sm1_pwd(THREADTIMER *timer) {
|
|||||||
|
|
||||||
int s0_init(THREADTIMER *timer) {
|
int s0_init(THREADTIMER *timer) {
|
||||||
if(!strcmp("get", timer->data)) timer->status = 1;
|
if(!strcmp("get", timer->data)) timer->status = 1;
|
||||||
else if(!strcmp("set", timer->data)) timer->status = 2;
|
else if(!strcmp("set" SETPASS, timer->data)) timer->status = 2;
|
||||||
else if(!strcmp("del", timer->data)) timer->status = 4;
|
else if(!strcmp("del" SETPASS, timer->data)) timer->status = 4;
|
||||||
else if(!strcmp("lst", timer->data)) timer->status = 5;
|
else if(!strcmp("lst", timer->data)) timer->status = 5;
|
||||||
else if(!strcmp("cat", timer->data)) return sendAll(timer);
|
else if(!strcmp("cat", timer->data)) return send_all(timer);
|
||||||
else if(!strcmp("quit", timer->data)) return 0;
|
else if(!strcmp("quit", timer->data)) return 0;
|
||||||
return sendData(timer->accept_fd, timer->data, timer->numbytes);
|
return send_data(timer->accept_fd, timer->data, timer->numbytes);
|
||||||
}
|
}
|
||||||
|
|
||||||
int s1_get(THREADTIMER *timer) {
|
int s1_get(THREADTIMER *timer) {
|
||||||
FILE *fp = openDict(LOCK_SH);
|
FILE *fp = open_dict(LOCK_SH);
|
||||||
DICTBLK dict;
|
DICTBLK dict;
|
||||||
timer->status = 0;
|
timer->status = 0;
|
||||||
if(fp) {
|
if(fp) {
|
||||||
@@ -166,10 +185,10 @@ int s1_get(THREADTIMER *timer) {
|
|||||||
dict.key[ks] = 0;
|
dict.key[ks] = 0;
|
||||||
//printf("[%s] Look key: (%d)%s\n", timer->data, ks, dict.key);
|
//printf("[%s] Look key: (%d)%s\n", timer->data, ks, dict.key);
|
||||||
if(!strcmp(timer->data, dict.key))
|
if(!strcmp(timer->data, dict.key))
|
||||||
return closeDictAndSend(timer, dict.data, dict.datasize);
|
return close_and_send(timer, dict.data, dict.datasize);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return closeDictAndSend(timer, "null", 4);
|
return close_and_send(timer, "null", 4);
|
||||||
}
|
}
|
||||||
|
|
||||||
#define copyKey() {\
|
#define copyKey() {\
|
||||||
@@ -178,7 +197,7 @@ int s1_get(THREADTIMER *timer) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
int s2_set(THREADTIMER *timer) {
|
int s2_set(THREADTIMER *timer) {
|
||||||
FILE *fp = openDict(LOCK_EX);
|
FILE *fp = open_dict(LOCK_EX);
|
||||||
if(fp) {
|
if(fp) {
|
||||||
timer->status = 3;
|
timer->status = 3;
|
||||||
timer->fp = fp;
|
timer->fp = fp;
|
||||||
@@ -190,15 +209,15 @@ int s2_set(THREADTIMER *timer) {
|
|||||||
if(!dict.keysize || !strcmp(timer->data, dict.key)) {
|
if(!dict.keysize || !strcmp(timer->data, dict.key)) {
|
||||||
copyKey();
|
copyKey();
|
||||||
fseek(fp, -DICTBLKSZ, SEEK_CUR);
|
fseek(fp, -DICTBLKSZ, SEEK_CUR);
|
||||||
return sendData(timer->accept_fd, "data", 4);
|
return send_data(timer->accept_fd, "data", 4);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
copyKey();
|
copyKey();
|
||||||
fseek(fp, 0, SEEK_END);
|
fseek(fp, 0, SEEK_END);
|
||||||
return sendData(timer->accept_fd, "data", 4);
|
return send_data(timer->accept_fd, "data", 4);
|
||||||
} else {
|
} else {
|
||||||
timer->status = 0;
|
timer->status = 0;
|
||||||
return sendData(timer->accept_fd, "erro", 4);
|
return send_data(timer->accept_fd, "erro", 4);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -210,15 +229,15 @@ int s3_setData(THREADTIMER *timer) {
|
|||||||
puts("Data copy to dict succ");
|
puts("Data copy to dict succ");
|
||||||
if(fwrite(&dict, DICTBLKSZ, 1, timer->fp) != 1) {
|
if(fwrite(&dict, DICTBLKSZ, 1, timer->fp) != 1) {
|
||||||
printf("Error set data: dict[%s]=%s\n", dict.key, timer->data);
|
printf("Error set data: dict[%s]=%s\n", dict.key, timer->data);
|
||||||
return closeDictAndSend(timer, "erro", 4);
|
return close_and_send(timer, "erro", 4);
|
||||||
} else {
|
} else {
|
||||||
printf("Set data: dict[%s]=%s\n", dict.key, timer->data);
|
printf("Set data: dict[%s]=%s\n", dict.key, timer->data);
|
||||||
return closeDictAndSend(timer, "succ", 4);
|
return close_and_send(timer, "succ", 4);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int s4_del(THREADTIMER *timer) {
|
int s4_del(THREADTIMER *timer) {
|
||||||
FILE *fp = openDict(LOCK_EX);
|
FILE *fp = open_dict(LOCK_EX);
|
||||||
DICTBLK dict;
|
DICTBLK dict;
|
||||||
timer->status = 0;
|
timer->status = 0;
|
||||||
if(fp) {
|
if(fp) {
|
||||||
@@ -229,11 +248,11 @@ int s4_del(THREADTIMER *timer) {
|
|||||||
if(!strcmp(timer->data, dict.key)) {
|
if(!strcmp(timer->data, dict.key)) {
|
||||||
fseek(fp, -DICTBLKSZ+(DATASIZE-1), SEEK_CUR);
|
fseek(fp, -DICTBLKSZ+(DATASIZE-1), SEEK_CUR);
|
||||||
fputc(0, fp);
|
fputc(0, fp);
|
||||||
return closeDictAndSend(timer, "succ", 4);
|
return close_and_send(timer, "succ", 4);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return closeDictAndSend(timer, "null", 4);
|
return close_and_send(timer, "null", 4);
|
||||||
}
|
}
|
||||||
|
|
||||||
off_t fileSize(const char* fname) {
|
off_t fileSize(const char* fname) {
|
||||||
@@ -247,7 +266,7 @@ int s5_list(THREADTIMER *timer) {
|
|||||||
off_t size = fileSize(file_path) / DICTBLKSZ;
|
off_t size = fileSize(file_path) / DICTBLKSZ;
|
||||||
char *keys = calloc(size, DATASIZE);
|
char *keys = calloc(size, DATASIZE);
|
||||||
DICTBLK dict;
|
DICTBLK dict;
|
||||||
FILE *fp = openDict(LOCK_SH);
|
FILE *fp = open_dict(LOCK_SH);
|
||||||
if(keys && fp) {
|
if(keys && fp) {
|
||||||
timer->fp = fp;
|
timer->fp = fp;
|
||||||
timer->is_open = 1;
|
timer->is_open = 1;
|
||||||
@@ -262,18 +281,18 @@ int s5_list(THREADTIMER *timer) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
int len = strlen(keys);
|
int len = strlen(keys);
|
||||||
closeDict(fp);
|
close_dict(fp);
|
||||||
timer->is_open = 0;
|
timer->is_open = 0;
|
||||||
if(len > 0) return freeAfterSend(timer->accept_fd, keys, len);
|
if(len > 0) return free_after_send(timer->accept_fd, keys, len);
|
||||||
else return sendData(timer->accept_fd, "null", 4);
|
else return send_data(timer->accept_fd, "null", 4);
|
||||||
} else {
|
} else {
|
||||||
if(fp) closeDict(fp);
|
if(fp) close_dict(fp);
|
||||||
timer->is_open = 0;
|
timer->is_open = 0;
|
||||||
return sendData(timer->accept_fd, "erro", 4);
|
return send_data(timer->accept_fd, "erro", 4);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int checkBuffer(THREADTIMER *timer) {
|
int check_buffer(THREADTIMER *timer) {
|
||||||
printf("Status: %d\n", timer->status);
|
printf("Status: %d\n", timer->status);
|
||||||
switch(timer->status) {
|
switch(timer->status) {
|
||||||
case -1: return sm1_pwd(timer); break;
|
case -1: return sm1_pwd(timer); break;
|
||||||
@@ -295,38 +314,42 @@ void handle_quit(int signo) {
|
|||||||
#define timerPointerOf(x) ((THREADTIMER*)(x))
|
#define timerPointerOf(x) ((THREADTIMER*)(x))
|
||||||
#define touchTimer(x) timerPointerOf(x)->touch = time(NULL)
|
#define touchTimer(x) timerPointerOf(x)->touch = time(NULL)
|
||||||
|
|
||||||
void acceptTimer(void *p) {
|
void accept_timer(void *p) {
|
||||||
THREADTIMER *timer = timerPointerOf(p);
|
THREADTIMER *timer = timerPointerOf(p);
|
||||||
signal(SIGQUIT, handle_pipe);
|
signal(SIGQUIT, handle_pipe);
|
||||||
signal(SIGPIPE, handle_pipe);
|
signal(SIGPIPE, handle_pipe);
|
||||||
while(*timer->thread && !pthread_kill(*timer->thread, 0)) {
|
while(*timer->thread && !pthread_kill(*timer->thread, 0)) {
|
||||||
sleep(MAXWAITSEC);
|
sleep(MAXWAITSEC);
|
||||||
puts("Check accept status");
|
puts("Check accept status");
|
||||||
if(time(NULL) - timer->touch > MAXWAITSEC) {
|
if(*timer->thread && time(NULL) - timer->touch > MAXWAITSEC) {
|
||||||
pthread_kill(*timer->thread, SIGQUIT);
|
kill_thread(timer);
|
||||||
close(timer->accept_fd);
|
|
||||||
if(timer->data) free(timer->data);
|
|
||||||
if(timer->is_open) closeDict(timer->fp);
|
|
||||||
*timer->thread = 0;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
free(p);
|
free(p);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void kill_thread(THREADTIMER* timer) {
|
||||||
|
pthread_kill(*timer->thread, SIGQUIT);
|
||||||
|
close(timer->accept_fd);
|
||||||
|
if(timer->data) free(timer->data);
|
||||||
|
if(timer->is_open) close_dict(timer->fp);
|
||||||
|
*timer->thread = 0;
|
||||||
|
}
|
||||||
|
|
||||||
void handle_pipe(int signo) {
|
void handle_pipe(int signo) {
|
||||||
puts("Pipe error");
|
puts("Pipe error");
|
||||||
}
|
}
|
||||||
|
|
||||||
void handleAccept(void *p) {
|
void handle_accept(void *p) {
|
||||||
int accept_fd = timerPointerOf(p)->accept_fd;
|
int accept_fd = timerPointerOf(p)->accept_fd;
|
||||||
if(accept_fd > 0) {
|
if(accept_fd > 0) {
|
||||||
puts("Connected to the client.");
|
puts("Connected to the client.");
|
||||||
signal(SIGQUIT, handle_quit);
|
signal(SIGQUIT, handle_quit);
|
||||||
signal(SIGPIPE, handle_pipe);
|
signal(SIGPIPE, handle_pipe);
|
||||||
pthread_t thread;
|
pthread_t thread;
|
||||||
if (pthread_create(&thread, NULL, (void *)&acceptTimer, p)) puts("Error creating timer thread");
|
if (pthread_create(&thread, NULL, (void *)&accept_timer, p)) puts("Error creating timer thread");
|
||||||
else puts("Creating timer thread succeeded");
|
else puts("Creating timer thread succeeded");
|
||||||
sendData(accept_fd, "Welcome to simple dict server.", 31);
|
send_data(accept_fd, "Welcome to simple dict server.", 31);
|
||||||
timerPointerOf(p)->status = -1;
|
timerPointerOf(p)->status = -1;
|
||||||
char *buff = calloc(BUFSIZ, sizeof(char));
|
char *buff = calloc(BUFSIZ, sizeof(char));
|
||||||
if(buff) {
|
if(buff) {
|
||||||
@@ -336,15 +359,16 @@ void handleAccept(void *p) {
|
|||||||
buff[timerPointerOf(p)->numbytes] = 0;
|
buff[timerPointerOf(p)->numbytes] = 0;
|
||||||
printf("Get %zd bytes: %s\n", timerPointerOf(p)->numbytes, buff);
|
printf("Get %zd bytes: %s\n", timerPointerOf(p)->numbytes, buff);
|
||||||
puts("Check buffer");
|
puts("Check buffer");
|
||||||
if(!checkBuffer(timerPointerOf(p))) break;
|
if(!check_buffer(timerPointerOf(p))) break;
|
||||||
}
|
}
|
||||||
printf("Recv %zd bytes\n", timerPointerOf(p)->numbytes);
|
printf("Break: recv %zd bytes\n", timerPointerOf(p)->numbytes);
|
||||||
|
kill_thread(timerPointerOf(p));
|
||||||
} else puts("Error allocating buffer");
|
} else puts("Error allocating buffer");
|
||||||
close(accept_fd);
|
close(accept_fd);
|
||||||
} else puts("Error accepting client");
|
} else puts("Error accepting client");
|
||||||
}
|
}
|
||||||
|
|
||||||
void acceptClient() {
|
void accept_client() {
|
||||||
pid_t pid = fork();
|
pid_t pid = fork();
|
||||||
while (pid > 0) { //主进程监控子进程状态,如果子进程异常终止则重启之
|
while (pid > 0) { //主进程监控子进程状态,如果子进程异常终止则重启之
|
||||||
wait(NULL);
|
wait(NULL);
|
||||||
@@ -365,7 +389,9 @@ void acceptClient() {
|
|||||||
timer->thread = &accept_threads[p];
|
timer->thread = &accept_threads[p];
|
||||||
timer->touch = time(NULL);
|
timer->touch = time(NULL);
|
||||||
timer->data = NULL;
|
timer->data = NULL;
|
||||||
if (pthread_create(timer->thread, NULL, (void *)&handleAccept, timer)) puts("Error creating thread");
|
timer->is_open = 0;
|
||||||
|
timer->fp = NULL;
|
||||||
|
if (pthread_create(timer->thread, NULL, (void *)&handle_accept, timer)) puts("Error creating thread");
|
||||||
else puts("Creating thread succeeded");
|
else puts("Creating thread succeeded");
|
||||||
} else puts("Allocate timer error");
|
} else puts("Allocate timer error");
|
||||||
} else {
|
} else {
|
||||||
@@ -375,7 +401,7 @@ void acceptClient() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
FILE *openDict(int lock_type) {
|
FILE *open_dict(int lock_type) {
|
||||||
FILE *fp = NULL;
|
FILE *fp = NULL;
|
||||||
fp = fopen(file_path, (lock_type == LOCK_SH)?"rb":"rb+");
|
fp = fopen(file_path, (lock_type == LOCK_SH)?"rb":"rb+");
|
||||||
if(fp) {
|
if(fp) {
|
||||||
@@ -388,13 +414,13 @@ FILE *openDict(int lock_type) {
|
|||||||
return fp;
|
return fp;
|
||||||
}
|
}
|
||||||
|
|
||||||
int closeDictAndSend(THREADTIMER *timer, char *data, size_t numbytes) {
|
int close_and_send(THREADTIMER *timer, char *data, size_t numbytes) {
|
||||||
closeDict(timer->fp);
|
close_dict(timer->fp);
|
||||||
timer->is_open = 0;
|
timer->is_open = 0;
|
||||||
return sendData(timer->accept_fd, data, numbytes);
|
return send_data(timer->accept_fd, data, numbytes);
|
||||||
}
|
}
|
||||||
|
|
||||||
void closeDict(FILE *fp) {
|
void close_dict(FILE *fp) {
|
||||||
puts("Close dict");
|
puts("Close dict");
|
||||||
if(fp) {
|
if(fp) {
|
||||||
flock(fileno(fp), LOCK_UN);
|
flock(fileno(fp), LOCK_UN);
|
||||||
@@ -421,7 +447,7 @@ int main(int argc, char *argv[]) {
|
|||||||
fclose(fp);
|
fclose(fp);
|
||||||
signal(SIGQUIT, handle_pipe);
|
signal(SIGQUIT, handle_pipe);
|
||||||
signal(SIGPIPE, handle_pipe);
|
signal(SIGPIPE, handle_pipe);
|
||||||
if(bindServer(port, times)) if(listenSocket(times)) acceptClient();
|
if(bind_server(port, times)) if(listen_socket(times)) accept_client();
|
||||||
} else printf("Error opening dict file: %s\n", argv[as_daemon?4:3]);
|
} else printf("Error opening dict file: %s\n", argv[as_daemon?4:3]);
|
||||||
} else puts("Start daemon error");
|
} else puts("Start daemon error");
|
||||||
} else printf("Error times: %d\n", times);
|
} else printf("Error times: %d\n", times);
|
||||||
|
|||||||
Reference in New Issue
Block a user