add comment

This commit is contained in:
ubuntu201711081 2020-12-14 11:30:46 +00:00
parent aeb60a7537
commit ad881333e0
3 changed files with 22 additions and 15 deletions

View File

@ -24,9 +24,13 @@ bool isatty_file(FILE * file);
//After calling ready_progress_bar, you must use following functions on print. //After calling ready_progress_bar, you must use following functions on print.
void myd_perror(const char * msg); 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); 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, ...); int myd_fprintf(int line,FILE * f,const char * msg, ...);

View File

@ -250,6 +250,9 @@ int send_response(int sock,int fd, uint8_t * buf, size_t bufsize){
#endif #endif
return 0; return 0;
} }
/** print help message
* arg `n`: executable name
*/
const char * help(const char * n){ const char * help(const char * n){
const char * msg = "USASE : %s [Option] ...\n" const char * msg = "USASE : %s [Option] ...\n"
"Options and arguments: \n" "Options and arguments: \n"
@ -259,7 +262,8 @@ const char * help(const char * n){
printf(msg,n); printf(msg,n);
return msg; 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 parse_args(int argc,const char * argv[] , in_port_t * port){
int pos = 1; int pos = 1;
const char * opt; const char * opt;
@ -274,7 +278,7 @@ int parse_args(int argc,const char * argv[] , in_port_t * port){
const char * value = argv[pos++]; const char * value = argv[pos++];
*port = atoi(value); *port = atoi(value);
if (port == 0){ // either not number or zero 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; return 2;
} }
} }
@ -302,11 +306,10 @@ typedef struct SharedState{
//empty if less than 0 //empty if less than 0
queue_struct(int,WORK_QUEUE_SIZE) socks; queue_struct(int,WORK_QUEUE_SIZE) socks;
#ifdef USE_TRACE #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 #endif
pthread_mutex_t sock_mutex; pthread_mutex_t sock_mutex;//mutex for queue
pthread_cond_t ready; pthread_cond_t ready;
//int progress[MAX_THREAD_NUMBER];
} shared_state_t; } shared_state_t;
void init_shared_state(shared_state_t * state) { void init_shared_state(shared_state_t * state) {
@ -437,6 +440,7 @@ int main(int argc, const char *argv[]){
int bufsize; int bufsize;
int i = 0; int i = 0;
in_port_t binding_port_number = SERVER_PORT; in_port_t binding_port_number = SERVER_PORT;
//parse argument
if (argc > 1){ if (argc > 1){
int d = parse_args(argc,argv,&binding_port_number); int d = parse_args(argc,argv,&binding_port_number);
if(d != 0 ) return d; if(d != 0 ) return d;
@ -446,6 +450,7 @@ int main(int argc, const char *argv[]){
#ifdef USE_TRACE #ifdef USE_TRACE
report_resolution(); report_resolution();
#endif #endif
//create socket
sock = socket(AF_INET,SOCK_STREAM,0); sock = socket(AF_INET,SOCK_STREAM,0);
atexit(safe_exit); atexit(safe_exit);
if(sock < 0){ if(sock < 0){
@ -460,6 +465,7 @@ int main(int argc, const char *argv[]){
} }
bufsize = getBufferSizeFrom(sock); bufsize = getBufferSizeFrom(sock);
#ifndef USE_NO_QUEUE #ifndef USE_NO_QUEUE
//initialize shared_state and create worker thread.
init_shared_state(&globalState); init_shared_state(&globalState);
for (i = 0; i < MAX_THREAD_NUMBER; i++) { for (i = 0; i < MAX_THREAD_NUMBER; i++) {
worker_argument_t * args = create_worker_argument(i,bufsize); 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); pthread_create(&worker_threads[i],NULL,worker_proc,args);
} }
#endif #endif
//bind socket.
addr.sin_addr.s_addr = htonl(INADDR_ANY); /*0.0.0.0 모든 네트워크 인터페이스에 묶임.*/ addr.sin_addr.s_addr = htonl(INADDR_ANY); /*0.0.0.0 모든 네트워크 인터페이스에 묶임.*/
addr.sin_family = AF_INET; addr.sin_family = AF_INET;
addr.sin_port = htons(binding_port_number); addr.sin_port = htons(binding_port_number);
@ -504,11 +511,11 @@ int main(int argc, const char *argv[]){
if (queue_isfull(&globalState.socks)){ if (queue_isfull(&globalState.socks)){
pthread_mutex_unlock(&globalState.sock_mutex); pthread_mutex_unlock(&globalState.sock_mutex);
#ifdef _GNU_SOURCE #ifdef _GNU_SOURCE
pthread_yield(); pthread_yield();//yield for other thread and process.
#else #else
usleep(400); usleep(400); //alternative for `pthread_yield`, but this could occur 'busy waiting' by implementation.
#endif #endif
continue; continue;//retry to enqueue `csock`.
} }
else { else {
enqueue(&globalState.socks,csock); enqueue(&globalState.socks,csock);

View File

@ -25,15 +25,11 @@ struct ReadOp{
struct TransferResult{ struct TransferResult{
int16_t res; int16_t res;
int16_t error_msg_size; int16_t error_msg_size;
int64_t file_size; /**/ int64_t file_size;
int32_t err_number; /*errno*/ int32_t err_number; /*errno*/
}; };
#pragma pack(pop) #pragma pack(pop)
#ifdef USE_GETADDRINFO
#endif
/** /**
* find buffer size from sock * find buffer size from sock
* thread safe * thread safe