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

优化代码结构

This commit is contained in:
源文雨
2022-04-17 21:12:36 +08:00
parent 09107be7f0
commit 2496afddfb
4 changed files with 138 additions and 147 deletions

View File

@@ -17,7 +17,7 @@ link_directories("/usr/local/lib")
add_compile_options(-std=gnu99)
message(STATUS "optional:-std=gnu99")
add_executable(simple-dict-server server.c dict.c crypto.c)
add_executable(simple-dict-server server.c crypto.c)
add_executable(simple-dict-client client.c crypto.c)
add_executable(cfgwriter cfgwriter.c)
#add_executable(migrate migrate.c)

128
dict.c
View File

@@ -1,128 +0,0 @@
#include <stdio.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <string.h>
#include <simplecrypto.h>
#include <pthread.h>
#include "dict.h"
#include "server.h"
static char* filepath;
static uint8_t dict_md5[16];
static FILE* fp = NULL; //fp for EX
static FILE* fp_read = NULL; //fp for md5
static FILE* thread_fp[THREADCNT];
#ifdef CPUBIT64
#define _dict_md5_2 ((uint64_t*)&dict_md5)
#else
#define _dict_md5_4 ((uint32_t*)&dict_md5)
#endif
int fill_md5(pthread_rwlock_t* mu) {
size_t size = get_dict_size(mu);
if(!size) {
memset(dict_md5, 0, 16);
puts("Dict is empty, use all zero md5");
return 0;
}
uint8_t* dict_buff = (uint8_t*)malloc(size);
if(dict_buff) {
if(pthread_rwlock_tryrdlock(mu)) {
perror("Readlock busy: ");
return 1;
}
rewind(fp_read);
if(fread(dict_buff, size, 1, fp_read) == 1) {
pthread_rwlock_unlock(mu);
md5(dict_buff, size, dict_md5);
free(dict_buff);
return 0;
} else {
pthread_rwlock_unlock(mu);
free(dict_buff);
perror("Read dict error: ");
return 2;
}
} else {
perror("Allocate memory error: ");
return 3;
}
}
int init_dict(char* file_path, pthread_rwlock_t* mu) {
fp = fopen(file_path, "rb+");
fp_read = fopen(file_path, "rb");
if(fp) {
int err = pthread_rwlock_init(mu, NULL);
if(err) {
perror("Init lock error: ");
return 1;
}
filepath = file_path;
return fill_md5(mu);
}
perror("Open dict error: ");
return 2;
}
FILE* open_dict(uint8_t lock_type, uint32_t index, pthread_rwlock_t* mu) {
if(lock_type & DICT_LOCK_EX) {
if(pthread_rwlock_trywrlock(mu)) {
puts("Open dict: Writelock busy");
return NULL;
}
if(!fp) fp = fopen(filepath, "rb+");
else rewind(fp);
return fp;
}
if(index >= THREADCNT) {
puts("Open dict: Index out of bounds");
return NULL;
}
if(pthread_rwlock_tryrdlock(mu)) {
puts("Open dict: Readlock busy");
return NULL;
}
if(!thread_fp[index]) thread_fp[index] = fopen(filepath, "rb");
else rewind(thread_fp[index]);
return thread_fp[index];
}
FILE* get_dict_fp_wr() {
return fp;
}
FILE* get_dict_fp_rd() {
rewind(fp_read);
return fp_read;
}
void close_dict(uint8_t lock_type, uint32_t index, pthread_rwlock_t* mu) {
if(lock_type & DICT_LOCK_EX) fflush(fp);
pthread_rwlock_unlock(mu);
puts("Close dict");
}
off_t get_dict_size() {
struct stat statbuf;
if(stat(filepath, &statbuf)==0) {
return statbuf.st_size;
}
return -1;
}
int is_md5_equal(uint8_t* digest) {
#ifdef CPUBIT64
uint64_t* digest2 = (uint64_t*)digest;
return (digest2[0] == _dict_md5_2[0]) &&
(digest2[1] == _dict_md5_2[1]);
#else
uint32_t* digest4 = (uint32_t*)digest;
return (digest4[0] == _dict_md5_4[0]) &&
(digest4[1] == _dict_md5_4[1]) &&
(digest4[2] == _dict_md5_4[2]) &&
(digest4[3] == _dict_md5_4[3]);
#endif
}

135
dict.h
View File

@@ -2,6 +2,14 @@
#define _DICT_H_
#include <stdint.h>
#include <stdio.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <string.h>
#include <simplecrypto.h>
#include <pthread.h>
#include "dict.h"
#include "server.h"
#define DICTKEYSZ 127
#define DICTDATSZ 127
@@ -17,13 +25,124 @@ typedef struct DICT DICT;
#define DICT_LOCK_EX 0x02
#define DICT_LOCKING_EX 0x04
void close_dict(uint8_t lock_type, uint32_t index, pthread_rwlock_t* mu);
int fill_md5(pthread_rwlock_t* mu);
int init_dict(char* file_path, pthread_rwlock_t* mu);
int is_md5_equal(uint8_t* digest);
FILE* get_dict_fp_rd();
FILE* get_dict_fp_wr();
off_t get_dict_size();
FILE* open_dict(uint8_t lock_type, uint32_t index, pthread_rwlock_t* mu);
static char* dict_filepath;
static uint8_t dict_md5[16];
static FILE* dict_fp = NULL; //fp for EX
static FILE* dict_fp_read = NULL; //fp for md5
static FILE* dict_thread_fp[THREADCNT];
#ifdef CPUBIT64
#define _dict_md5_2 ((uint64_t*)&dict_md5)
#else
#define _dict_md5_4 ((uint32_t*)&dict_md5)
#endif
static off_t get_dict_size() {
struct stat statbuf;
if(stat(dict_filepath, &statbuf)==0) {
return statbuf.st_size;
}
return -1;
}
static int fill_md5(pthread_rwlock_t* mu) {
size_t size = get_dict_size();
if(!size) {
memset(dict_md5, 0, 16);
puts("Dict is empty, use all zero md5");
return 0;
}
uint8_t* dict_buff = (uint8_t*)malloc(size);
if(dict_buff) {
if(pthread_rwlock_tryrdlock(mu)) {
perror("Readlock busy: ");
return 1;
}
rewind(dict_fp_read);
if(fread(dict_buff, size, 1, dict_fp_read) == 1) {
pthread_rwlock_unlock(mu);
md5(dict_buff, size, dict_md5);
free(dict_buff);
return 0;
} else {
pthread_rwlock_unlock(mu);
free(dict_buff);
perror("Read dict error: ");
return 2;
}
} else {
perror("Allocate memory error: ");
return 3;
}
}
static int init_dict(char* file_path, pthread_rwlock_t* mu) {
dict_fp = fopen(file_path, "rb+");
dict_fp_read = fopen(file_path, "rb");
if(dict_fp) {
int err = pthread_rwlock_init(mu, NULL);
if(err) {
perror("Init lock error: ");
return 1;
}
dict_filepath = file_path;
return fill_md5(mu);
}
perror("Open dict error: ");
return 2;
}
static FILE* open_dict(uint8_t lock_type, uint32_t index, pthread_rwlock_t* mu) {
if(lock_type & DICT_LOCK_EX) {
if(pthread_rwlock_trywrlock(mu)) {
puts("Open dict: Writelock busy");
return NULL;
}
if(!dict_fp) dict_fp = fopen(dict_filepath, "rb+");
else rewind(dict_fp);
return dict_fp;
}
if(index >= THREADCNT) {
puts("Open dict: Index out of bounds");
return NULL;
}
if(pthread_rwlock_tryrdlock(mu)) {
puts("Open dict: Readlock busy");
return NULL;
}
if(!dict_thread_fp[index]) dict_thread_fp[index] = fopen(dict_filepath, "rb");
else rewind(dict_thread_fp[index]);
return dict_thread_fp[index];
}
static FILE* get_dict_fp_wr() {
return dict_fp;
}
static FILE* get_dict_fp_rd() {
rewind(dict_fp_read);
return dict_fp_read;
}
static void close_dict(uint8_t lock_type, uint32_t index, pthread_rwlock_t* mu) {
if(lock_type & DICT_LOCK_EX) fflush(dict_fp);
pthread_rwlock_unlock(mu);
puts("Close dict");
}
static inline int is_md5_equal(uint8_t* digest) {
#ifdef CPUBIT64
uint64_t* digest2 = (uint64_t*)digest;
return (digest2[0] == _dict_md5_2[0]) &&
(digest2[1] == _dict_md5_2[1]);
#else
uint32_t* digest4 = (uint32_t*)digest;
return (digest4[0] == _dict_md5_4[0]) &&
(digest4[1] == _dict_md5_4[1]) &&
(digest4[2] == _dict_md5_4[2]) &&
(digest4[3] == _dict_md5_4[3]);
#endif
}
#endif

View File

@@ -65,7 +65,7 @@ static void handle_int(int signo);
static void handle_pipe(int signo);
static void handle_quit(int signo);
static void init_dict_pool(FILE *fp);
static void kill_thread(THREADTIMER* timer);
static void cleanup_thread(THREADTIMER* timer);
static uint32_t last_nonnull(char* p, uint32_t max_size);
static int listen_socket();
static int send_all(THREADTIMER *timer);
@@ -304,10 +304,9 @@ static int s3_set_data(THREADTIMER *timer) {
if(!set_pb(get_dict_fp_wr(), items_len, sizeof(DICT), setdict)) {
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);
return close_and_send(timer, ACKSUCC, "succ", 4);
}
printf("Set data: dict[%s]=%s\n", setdict->key, timer->dat);
return close_and_send(timer, ACKSUCC, "succ", 4);
}
static enum SERVERACK del(FILE *fp, char* key, int len, char ret[4]) {
@@ -413,8 +412,8 @@ static void accept_timer(void *p) {
}
}
static void kill_thread(THREADTIMER* timer) {
puts("Start killing");
static void cleanup_thread(THREADTIMER* timer) {
puts("Start cleaning");
accept_threads[timer->index] = 0;
if(timer->accept_fd) {
close(timer->accept_fd);
@@ -427,7 +426,7 @@ static void kill_thread(THREADTIMER* timer) {
puts("Free data");
}
if(timer->lock_type) close_dict(timer->lock_type, timer->index, &mu);
puts("Finish killing");
puts("Finish cleaning");
}
static void handle_int(int signo) {
@@ -445,11 +444,11 @@ static void handle_accept(void *p) {
pthread_t thread;
if (pthread_create(&thread, &attr, (void *)&accept_timer, p)) {
perror("Error creating timer thread: ");
kill_thread(timer_pointer_of(p));
cleanup_thread(timer_pointer_of(p));
return;
}
puts("Creating timer thread succeeded");
pthread_cleanup_push((void*)&kill_thread, p);
pthread_cleanup_push((void*)&cleanup_thread, p);
int accept_fd = timer_pointer_of(p)->accept_fd;
uint32_t index = timer_pointer_of(p)->index;
char *buff = malloc(BUFSIZ*sizeof(char));
@@ -569,6 +568,7 @@ static void accept_client() {
}
signal(SIGINT, handle_int);
signal(SIGQUIT, handle_quit);
signal(SIGKILL, exit);
signal(SIGPIPE, handle_pipe);
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
@@ -612,7 +612,7 @@ static void accept_client() {
reset_seq(p);
if (pthread_create(accept_threads + p, &attr, (void *)&handle_accept, timer)) {
perror("Error creating thread: ");
kill_thread(timer);
cleanup_thread(timer);
continue;
}
puts("Creating thread succeeded");