From ad881333e03a2aa3651c0d9e6a376bc25fc5456f Mon Sep 17 00:00:00 2001 From: ubuntu201711081 <201711081@jbnu.ac.kr> Date: Mon, 14 Dec 2020 11:30:46 +0000 Subject: [PATCH] add comment --- display_bar.h | 8 ++++++-- server.c | 23 +++++++++++++++-------- socket_wrapper.h | 6 +----- 3 files changed, 22 insertions(+), 15 deletions(-) diff --git a/display_bar.h b/display_bar.h index c7fbddf..9488619 100644 --- a/display_bar.h +++ b/display_bar.h @@ -24,9 +24,13 @@ bool isatty_file(FILE * file); //After calling ready_progress_bar, you must use following functions on print. void myd_perror(const char * msg); -//argument `line` is number of newline created by argument `msg`. +/*argument `line` is number of newline created by argument `msg`. +* thread-safe +*/ int myd_vfprintf(int line,FILE * f,const char * msg,va_list arg); -//argument `line` is number of newline created by argument `msg`. +/*argument `line` is number of newline created by argument `msg`. +* thread-safe +*/ int myd_fprintf(int line,FILE * f,const char * msg, ...); diff --git a/server.c b/server.c index b7ca317..cad3836 100644 --- a/server.c +++ b/server.c @@ -250,6 +250,9 @@ int send_response(int sock,int fd, uint8_t * buf, size_t bufsize){ #endif return 0; } +/** print help message + * arg `n`: executable name + */ const char * help(const char * n){ const char * msg = "USASE : %s [Option] ...\n" "Options and arguments: \n" @@ -259,7 +262,8 @@ const char * help(const char * n){ printf(msg,n); return msg; } -/** return 0 ok. otherwise invalid format*/ +/** parse arguments. + * if return 0, success. otherwise arguments are invalid format.*/ int parse_args(int argc,const char * argv[] , in_port_t * port){ int pos = 1; const char * opt; @@ -274,7 +278,7 @@ int parse_args(int argc,const char * argv[] , in_port_t * port){ const char * value = argv[pos++]; *port = atoi(value); if (port == 0){ // either not number or zero - fprintf(stderr,"argument is either not number or zero\n"); + fprintf(stderr,"port argument is either not number or zero\n"); return 2; } } @@ -302,11 +306,10 @@ typedef struct SharedState{ //empty if less than 0 queue_struct(int,WORK_QUEUE_SIZE) socks; #ifdef USE_TRACE - queue_struct(struct timespec,WORK_QUEUE_SIZE) trace_timer; + queue_struct(struct timespec,WORK_QUEUE_SIZE) trace_timer; //sock timer for TRACE #endif - pthread_mutex_t sock_mutex; + pthread_mutex_t sock_mutex;//mutex for queue pthread_cond_t ready; - //int progress[MAX_THREAD_NUMBER]; } shared_state_t; void init_shared_state(shared_state_t * state) { @@ -437,6 +440,7 @@ int main(int argc, const char *argv[]){ int bufsize; int i = 0; in_port_t binding_port_number = SERVER_PORT; + //parse argument if (argc > 1){ int d = parse_args(argc,argv,&binding_port_number); if(d != 0 ) return d; @@ -446,6 +450,7 @@ int main(int argc, const char *argv[]){ #ifdef USE_TRACE report_resolution(); #endif + //create socket sock = socket(AF_INET,SOCK_STREAM,0); atexit(safe_exit); if(sock < 0){ @@ -460,6 +465,7 @@ int main(int argc, const char *argv[]){ } bufsize = getBufferSizeFrom(sock); #ifndef USE_NO_QUEUE + //initialize shared_state and create worker thread. init_shared_state(&globalState); for (i = 0; i < MAX_THREAD_NUMBER; i++) { worker_argument_t * args = create_worker_argument(i,bufsize); @@ -470,6 +476,7 @@ int main(int argc, const char *argv[]){ pthread_create(&worker_threads[i],NULL,worker_proc,args); } #endif + //bind socket. addr.sin_addr.s_addr = htonl(INADDR_ANY); /*0.0.0.0 모든 네트워크 인터페이스에 묶임.*/ addr.sin_family = AF_INET; addr.sin_port = htons(binding_port_number); @@ -504,11 +511,11 @@ int main(int argc, const char *argv[]){ if (queue_isfull(&globalState.socks)){ pthread_mutex_unlock(&globalState.sock_mutex); #ifdef _GNU_SOURCE - pthread_yield(); + pthread_yield();//yield for other thread and process. #else - usleep(400); + usleep(400); //alternative for `pthread_yield`, but this could occur 'busy waiting' by implementation. #endif - continue; + continue;//retry to enqueue `csock`. } else { enqueue(&globalState.socks,csock); diff --git a/socket_wrapper.h b/socket_wrapper.h index e8fa0bd..1b34570 100644 --- a/socket_wrapper.h +++ b/socket_wrapper.h @@ -25,15 +25,11 @@ struct ReadOp{ struct TransferResult{ int16_t res; int16_t error_msg_size; - int64_t file_size; /**/ + int64_t file_size; int32_t err_number; /*errno*/ }; #pragma pack(pop) -#ifdef USE_GETADDRINFO - -#endif - /** * find buffer size from sock * thread safe