From c1f3be104288a8000e0eb7bf9b5870d76a7d5ad0 Mon Sep 17 00:00:00 2001 From: fumiama Date: Thu, 27 May 2021 17:15:47 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BD=BF=E7=94=A8=E5=86=85=E5=BB=BA=E5=AD=97?= =?UTF-8?q?=E8=8A=82=E5=BA=8F=E5=8F=8D=E8=BD=AC=EF=BC=8C=E4=BF=AE=E5=A4=8D?= =?UTF-8?q?linux=E4=B8=8Bsend=5Fall=E5=90=8E=E8=BF=9E=E6=8E=A5=E4=B8=AD?= =?UTF-8?q?=E6=96=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 4 +++- server.c | 28 +++++++++++----------------- 2 files changed, 14 insertions(+), 18 deletions(-) diff --git a/.gitignore b/.gitignore index 60c964e..529d1e7 100644 --- a/.gitignore +++ b/.gitignore @@ -55,4 +55,6 @@ build .vscode .vscode/settings.json -.DS_Store \ No newline at end of file +.DS_Store +test.c +test \ No newline at end of file diff --git a/server.c b/server.c index a8909c7..0f9907f 100644 --- a/server.c +++ b/server.c @@ -17,7 +17,10 @@ #include "config.h" #if !__APPLE__ - #include + #include + #include +#else + #include #endif CONFIG* cfg; @@ -133,7 +136,9 @@ int send_all(char* file_path, THREADTIMER *timer) { printf("Get file size: %u bytes.\n", file_size); off_t len = 0; #if __APPLE__ - //苹果基本没有大端,不作处理 + #ifdef WORDS_BIGENDIAN + file_size = __DARWIN_OSSwapInt32(file_size); + #endif struct sf_hdtr hdtr; struct iovec headers; headers.iov_base = &file_size; @@ -146,21 +151,15 @@ int send_all(char* file_path, THREADTIMER *timer) { if(!re) perror("Sendfile"); #else #ifdef WORDS_BIGENDIAN - uint32_t little_fs; - 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]; + uint32_t little_fs = __builtin_bswap32(file_size); send(timer->accept_fd, &little_fs, sizeof(uint32_t), 0); #else send(timer->accept_fd, &file_size, sizeof(uint32_t), 0); #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"); #endif - printf("Send %uF bytes.\n", len); + printf("Send %u bytes.\n", len); close_file(fp); timer->is_open = 0; } @@ -226,12 +225,7 @@ int s2_set(THREADTIMER *timer) { int s3_set_data(THREADTIMER *timer) { timer->status = 0; #ifdef WORDS_BIGENDIAN - uint32_t file_size; - 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]; + uint32_t file_size = __builtin_bswap32(*(uint32_t*)(timer->data)); #else uint32_t file_size = *(uint32_t*)(timer->data); #endif