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