mirror of
https://github.com/fumiama/simple-kanban.git
synced 2026-07-01 00:20:27 +08:00
使用内建字节序反转,修复linux下send_all后连接中断
This commit is contained in:
2
.gitignore
vendored
2
.gitignore
vendored
@@ -56,3 +56,5 @@ build
|
|||||||
.vscode/settings.json
|
.vscode/settings.json
|
||||||
|
|
||||||
.DS_Store
|
.DS_Store
|
||||||
|
test.c
|
||||||
|
test
|
||||||
26
server.c
26
server.c
@@ -18,6 +18,9 @@
|
|||||||
|
|
||||||
#if !__APPLE__
|
#if !__APPLE__
|
||||||
#include <sys/sendfile.h>
|
#include <sys/sendfile.h>
|
||||||
|
#include <endian.h>
|
||||||
|
#else
|
||||||
|
#include <machine/endian.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
CONFIG* cfg;
|
CONFIG* cfg;
|
||||||
@@ -133,7 +136,9 @@ int send_all(char* file_path, THREADTIMER *timer) {
|
|||||||
printf("Get file size: %u bytes.\n", file_size);
|
printf("Get file size: %u bytes.\n", file_size);
|
||||||
off_t len = 0;
|
off_t len = 0;
|
||||||
#if __APPLE__
|
#if __APPLE__
|
||||||
//苹果基本没有大端,不作处理
|
#ifdef WORDS_BIGENDIAN
|
||||||
|
file_size = __DARWIN_OSSwapInt32(file_size);
|
||||||
|
#endif
|
||||||
struct sf_hdtr hdtr;
|
struct sf_hdtr hdtr;
|
||||||
struct iovec headers;
|
struct iovec headers;
|
||||||
headers.iov_base = &file_size;
|
headers.iov_base = &file_size;
|
||||||
@@ -146,21 +151,15 @@ int send_all(char* file_path, THREADTIMER *timer) {
|
|||||||
if(!re) perror("Sendfile");
|
if(!re) perror("Sendfile");
|
||||||
#else
|
#else
|
||||||
#ifdef WORDS_BIGENDIAN
|
#ifdef WORDS_BIGENDIAN
|
||||||
uint32_t little_fs;
|
uint32_t little_fs = __builtin_bswap32(file_size);
|
||||||
char* q = (char*)(&little_fs);
|
|
||||||
char* p = (char*)(&file_size);
|
|
||||||
q[0] = p[3];
|
|
||||||
q[1] = p[2];
|
|
||||||
q[2] = p[1];
|
|
||||||
q[3] = p[0];
|
|
||||||
send(timer->accept_fd, &little_fs, sizeof(uint32_t), 0);
|
send(timer->accept_fd, &little_fs, sizeof(uint32_t), 0);
|
||||||
#else
|
#else
|
||||||
send(timer->accept_fd, &file_size, sizeof(uint32_t), 0);
|
send(timer->accept_fd, &file_size, sizeof(uint32_t), 0);
|
||||||
#endif
|
#endif
|
||||||
re = sendfile(timer->accept_fd, fileno(fp), &len, file_size) >= 0;
|
re = !sendfile(timer->accept_fd, fileno(fp), &len, file_size) >= 0;
|
||||||
if(!re) perror("Sendfile");
|
if(!re) perror("Sendfile");
|
||||||
#endif
|
#endif
|
||||||
printf("Send %uF bytes.\n", len);
|
printf("Send %u bytes.\n", len);
|
||||||
close_file(fp);
|
close_file(fp);
|
||||||
timer->is_open = 0;
|
timer->is_open = 0;
|
||||||
}
|
}
|
||||||
@@ -226,12 +225,7 @@ int s2_set(THREADTIMER *timer) {
|
|||||||
int s3_set_data(THREADTIMER *timer) {
|
int s3_set_data(THREADTIMER *timer) {
|
||||||
timer->status = 0;
|
timer->status = 0;
|
||||||
#ifdef WORDS_BIGENDIAN
|
#ifdef WORDS_BIGENDIAN
|
||||||
uint32_t file_size;
|
uint32_t file_size = __builtin_bswap32(*(uint32_t*)(timer->data));
|
||||||
char* p = (char*)(&file_size);
|
|
||||||
p[0] = timer->data[3];
|
|
||||||
p[1] = timer->data[2];
|
|
||||||
p[2] = timer->data[1];
|
|
||||||
p[3] = timer->data[0];
|
|
||||||
#else
|
#else
|
||||||
uint32_t file_size = *(uint32_t*)(timer->data);
|
uint32_t file_size = *(uint32_t*)(timer->data);
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
Reference in New Issue
Block a user