mirror of
https://github.com/fumiama/simple-kanban.git
synced 2026-06-11 03:50:29 +08:00
优化代码架构
This commit is contained in:
12
cfgwriter.c
12
cfgwriter.c
@@ -1,13 +1,8 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include "config.h"
|
||||
#include "simple-protobuf/simple_protobuf.h"
|
||||
|
||||
struct CONFIG {
|
||||
char pwd[64]; //password
|
||||
char sps[64]; //set password
|
||||
};
|
||||
typedef struct CONFIG CONFIG;
|
||||
|
||||
CONFIG cfg;
|
||||
|
||||
int main() {
|
||||
@@ -19,13 +14,14 @@ int main() {
|
||||
FILE* fp = fopen("cfg.sp", "wb");
|
||||
if(fp) {
|
||||
set_pb(fp, types_len, sizeof(CONFIG), &cfg);
|
||||
memset(&cfg, 0, sizeof(CONFIG));
|
||||
fclose(fp);
|
||||
puts("Write file succeed.");
|
||||
puts("Config is saved to cfg.sp.");
|
||||
fp = NULL;
|
||||
puts("Check config...");
|
||||
fp = fopen("cfg.sp", "rb");
|
||||
if(fp) {
|
||||
SIMPLE_PB* spb = get_pb(fp);
|
||||
memset(&cfg, 0, sizeof(CONFIG));
|
||||
memcpy(&cfg, spb->target, sizeof(CONFIG));
|
||||
printf("set pwd: %s, sps: %s\n", cfg.pwd, cfg.sps);
|
||||
} else perror("[SPB]");
|
||||
|
||||
10
config.h
Normal file
10
config.h
Normal file
@@ -0,0 +1,10 @@
|
||||
#ifndef _CONFIG_H_
|
||||
#define _CONFIG_H_
|
||||
|
||||
struct CONFIG {
|
||||
char pwd[64]; //password
|
||||
char sps[64]; //set password
|
||||
};
|
||||
typedef struct CONFIG CONFIG;
|
||||
|
||||
#endif
|
||||
18
server.c
18
server.c
@@ -13,18 +13,13 @@
|
||||
#include <unistd.h>
|
||||
#include <pthread.h>
|
||||
#include <time.h>
|
||||
#include "config.h"
|
||||
#include "simple-protobuf/simple_protobuf.h"
|
||||
|
||||
#if !__APPLE__
|
||||
#include <sys/sendfile.h>
|
||||
#endif
|
||||
|
||||
struct CONFIG {
|
||||
char pwd[64]; //password
|
||||
char sps[64]; //set password
|
||||
};
|
||||
typedef struct CONFIG CONFIG;
|
||||
|
||||
CONFIG* cfg;
|
||||
|
||||
int fd;
|
||||
@@ -250,16 +245,17 @@ int s3_set_data(THREADTIMER *timer) {
|
||||
printf("Get data size: %tu\n", timer->numbytes);
|
||||
}
|
||||
}
|
||||
if(file_size <= BUFSIZ - (is_first_data?0:sizeof(uint32_t))) {
|
||||
while(timer->numbytes != file_size - (is_first_data?0:sizeof(uint32_t))) {
|
||||
timer->numbytes += recv(timer->accept_fd, timer->data + timer->numbytes + (is_first_data?0:sizeof(uint32_t)), BUFSIZ - timer->numbytes - (is_first_data?0:sizeof(uint32_t)), 0);
|
||||
size_t offset = (is_first_data?0:sizeof(uint32_t));
|
||||
if(file_size <= BUFSIZ - offset) {
|
||||
while(timer->numbytes != file_size - offset) {
|
||||
timer->numbytes += recv(timer->accept_fd, timer->data + timer->numbytes + offset, BUFSIZ - timer->numbytes - offset, 0);
|
||||
}
|
||||
if(fwrite(timer->data + (is_first_data?0:sizeof(uint32_t)), file_size, 1, timer->fp) != 1) {
|
||||
if(fwrite(timer->data + offset, file_size, 1, timer->fp) != 1) {
|
||||
puts("Set data error.");
|
||||
return close_file_and_send(timer, "erro", 4);
|
||||
} else return close_file_and_send(timer, "succ", 4);
|
||||
} else {
|
||||
if(fwrite(timer->data + (is_first_data?0:sizeof(uint32_t)), timer->numbytes - (is_first_data?0:sizeof(uint32_t)), 1, timer->fp) != 1) {
|
||||
if(fwrite(timer->data + offset, timer->numbytes - offset, 1, timer->fp) != 1) {
|
||||
puts("Set data error.");
|
||||
return close_file_and_send(timer, "erro", 4);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user