Compare commits

..

6 Commits

Author SHA1 Message Date
ubuntu201711081
53f8f003d7 add comment 2020-12-14 11:33:56 +00:00
ubuntu201711081
ad881333e0 add comment 2020-12-14 11:30:46 +00:00
ubuntu201711081
aeb60a7537 add bar fprintf 2020-12-14 11:09:12 +00:00
ubuntu201711081
7ffcbafaa2 add comment 2020-12-14 10:57:12 +00:00
ubuntu201711081
fb7646fac7 add tty_fd 2020-12-14 10:20:16 +00:00
ubuntu201711081
f2e8bf2376 add comment 2020-12-14 10:19:49 +00:00
10 changed files with 152 additions and 188 deletions

View File

@ -16,7 +16,7 @@ display_bar.o: display_bar.c display_bar.h
client: client.c $(ClientObject)
$(CC) -o client client.c $(ClientObject) $(CFLAGS)
p-client: p-client.c $(ClientObject)
$(CC) -o p-client p-client.c socket_wrapper.o $(CFLAGS)
$(CC) -o p-client p-client.c $(ClientObject) $(CFLAGS)
p-mulclient: p-client.c $(ClientObject)
$(CC) -o p-slowclient p-client.c $(ClientObject) $(CFLAGS) -D MUL_CLIENT=10
slowclient: client.c $(ClientObject)

View File

@ -30,12 +30,17 @@ enum{
static bool DisplayProgress = true;
static inline void server_perror(const char * msg){
static inline void client_perror(const char * msg){
if(DisplayProgress)
myd_perror(msg);
else
perror(msg);
}
static inline int client_fprintf(int line,FILE * file,const char * msg,...){
va_list va;
va_start(va,msg);
return DisplayProgress ? myd_vfprintf(line,file,msg,va) : vfprintf(file,msg,va);
}
/*========
*Operation
*========*/
@ -45,14 +50,14 @@ int sendReadOp(int sock,const char * filename){
op.file_url_size = strlen(filename);
op.padding0 = 0;
if(send(sock,&op,sizeof(op),0)<0){
server_perror("readop send fail");
client_perror("readop send fail");
return -1;
}
#ifdef MUL_CLIENT
sleep(MUL_CLIENT);
#endif
if(send(sock,filename,op.file_url_size,0)<0){
server_perror("readop filename send fail");
client_perror("readop filename send fail");
return -1;
}
return 0;
@ -71,7 +76,7 @@ int recvFile(int sock, const char * filename,size_t file_size){
}
fd = open(filename,O_WRONLY|O_CREAT|O_TRUNC,S_IRWXU|S_IRWXG|S_IROTH);
if (fd < 0){
server_perror("file open fail");
client_perror("file open fail");
return -1;
}
if(DisplayProgress){
@ -86,12 +91,12 @@ int recvFile(int sock, const char * filename,size_t file_size){
if((i = recv_until_byte(sock,buf,readed,TIMEOUT)) < 0){
if(i == -2)
fprintf(stderr,"recv file failed : timeout connetion lost\n");
else server_perror("recv file failed");
else client_perror("recv file failed");
return_value = -1;
goto END;
}
if(write(fd,buf,readed)<0){
server_perror("file write failed");
client_perror("file write failed");
return_value = -1;
goto END;
}
@ -113,55 +118,31 @@ int recvData(int sock,const char * filename){
int i=0;
if((i=recv_until_byte(sock,&res,sizeof(res),TIMEOUT)) < 0){
if (i == -2) {
if(DisplayProgress && isatty_file(stderr)){
lock_scrolled();
add_scrolled_unlocked(1);
fprintf(stderr,"timeout\n");
unlock_scrolled();
}
else fprintf(stderr,"timeout\n");
client_fprintf(1,stderr,"timeout\n");
}
else server_perror("recv fail");
else client_perror("recv fail");
return -1;
}
char error_meesage_buf[256] = "";
switch(res.res){
case RES_ERR:
strerror_r(res.err_number,error_meesage_buf,sizeof(error_meesage_buf));
if(DisplayProgress && isatty_file(stderr)){
lock_scrolled();
add_scrolled_unlocked(1);
fprintf(stderr,"Server Fail: %s\n", error_meesage_buf);
unlock_scrolled();
}
else fprintf(stderr,"Server Fail: %s\n", error_meesage_buf);
client_fprintf(1,stderr,"Server Fail: %s\n", error_meesage_buf);
return -1;
case RES_USR_ERR:
assert(res.error_msg_size < 80);/*todo : fix*/
if((i=recv_until_byte(sock,error_meesage_buf,res.error_msg_size,TIMEOUT)) < 0){
if (i == -2) fprintf(stderr,"timeout\n");
else server_perror("recv fail");
else client_perror("recv fail");
return -1;
}
if(DisplayProgress && isatty_file(stderr)){
lock_scrolled();
add_scrolled_unlocked(1);
fprintf(stderr,"Error Message From Server: %s\n",error_meesage_buf);
unlock_scrolled();
}
else fprintf(stderr,"Error Message From Server: %s\n",error_meesage_buf);
client_fprintf(1,stderr,"Error Message From Server: %s\n",error_meesage_buf);
return -1;
case RES_OK:
return recvFile(sock,filename,res.file_size);
break;
default:
if(DisplayProgress && isatty_file(stderr)){
lock_scrolled();
add_scrolled_unlocked(1);
fprintf(stderr,"unknown value!\n");
unlock_scrolled();
}
else fprintf(stderr,"unknown value!\n");
client_fprintf(1,stderr,"unknown value!\n");
return -1;
}
@ -173,22 +154,16 @@ int SendOpAndReceiveFile(const char * filename, struct sockaddr const * addr){
int sock;
int ret = -1;
if(!quiet_mode){
if(DisplayProgress && isatty_file(stdout)){
lock_scrolled();
add_scrolled_unlocked(1);
fprintf(stdout,"request %s\n",filename);
unlock_scrolled();
}
else fprintf(stdout,"request %s\n",filename);
client_fprintf(1,stdout,"request %s\n",filename);
}
sock = socket(AF_INET,SOCK_STREAM,0);
if(sock < 0){
server_perror("sock create fail");
client_perror("sock create fail");
return -1;
}
if(connect(sock,(struct sockaddr *)addr,sizeof(*addr)) < 0){
server_perror("connect failed");
client_perror("connect failed");
return -1;
}
if(sendReadOp(sock,filename) == 0){
@ -233,7 +208,7 @@ typedef struct SimpleCharQueue{
const char ** filename_end;
FILE * fs;
} simple_queue_t;
//
// q have lifetime of arg b or arg e.
// do not deallocate before queue closed.
static bool init_queue_from_chararray(simple_queue_t * restrict q,const char ** b, const char ** e){
@ -242,7 +217,7 @@ static bool init_queue_from_chararray(simple_queue_t * restrict q,const char **
q->filename_end = e;
return true;
}
//
// q have lifetime of arg f.
// do not close file f before queue closed.
static bool init_queue_from_file(simple_queue_t * restrict q, FILE * f){
@ -424,13 +399,7 @@ int main(int argc, const char *argv[]){
err = getsockaddrbyname(AF_INET,SOCK_STREAM,0,cmd_args.server_name,(struct sockaddr *)&addr);
if (err != 0){
int check;
if(DisplayProgress && isatty_file(stderr)){
lock_scrolled();
add_scrolled_unlocked(1);
fprintf(stderr,"netdb fail: %s\n",gai_strerror(err));
unlock_scrolled();
}
else fprintf(stderr,"netdb fail: %s\n",gai_strerror(err));
client_fprintf(1,stderr,"netdb fail: %s\n",gai_strerror(err));
//assume that sernmae is *.*.*.* and try to parse addr
check = inet_pton(AF_INET,cmd_args.server_name,&addr.sin_addr);
assert(check != -1);
@ -470,6 +439,7 @@ int main(int argc, const char *argv[]){
destroy_worker_return(ret);
}
}
quit_progress_bar();
if (bench.benchmode){
struct timespec result;
double avg;
@ -481,6 +451,5 @@ int main(int argc, const char *argv[]){
fprintf(stdout,"operation: %lf us/op\n",avg / 1000.0);
fprintf(stdout,"resolution: %ld sec %ld ns\n",bench.resolution.tv_sec,bench.resolution.tv_nsec);
}
quit_progress_bar();
return retval;
}

View File

@ -40,6 +40,11 @@ void ready_progress_bar(){
tty_fd = open(buf,O_RDONLY);
}
}
void quit_progress_bar(){
if(tty_fd != STDIN_FILENO){
close(tty_fd);
}
}
void lock_scrolled(){
#ifdef DEBUG
@ -72,7 +77,6 @@ raw_progress_bar_t create_raw_progress_bar(){
unlock_scrolled();
return ret;
}
void RawDisplayProgressBar(raw_progress_bar_t bar,size_t offset,size_t total,const char * filename){
char buf[PROGRESS_BAR_WIDTH];
size_t i;
@ -96,8 +100,8 @@ void RawDisplayProgressBar(raw_progress_bar_t bar,size_t offset,size_t total,con
return;
}//optimization.
//if ioctl failed? what should i do...
ioctl(STDIN_FILENO,TIOCGWINSZ,(char *)&wnd_size);
if (wnd_size.ws_row < pos){
ioctl(tty_fd,TIOCGWINSZ,(char *)&wnd_size);
if (wnd_size.ws_row < pos){//render nothing if render position is out of window size.
unlock_scrolled();
return;
}
@ -105,6 +109,7 @@ void RawDisplayProgressBar(raw_progress_bar_t bar,size_t offset,size_t total,con
fflush(stdout);
unlock_scrolled();
}
bool isatty_file(FILE * file){
return isatty(fileno(file));
}
@ -121,6 +126,24 @@ void myd_perror(const char * msg){
}
}
int myd_vfprintf(int line,FILE * f,const char * msg, va_list arg){
int ret;
if(isatty_file(f)){
lock_scrolled();
add_scrolled_unlocked(line);
ret = vfprintf(f,msg,arg);
unlock_scrolled();
}
else{
ret = vfprintf(f,msg,arg);
}
return ret;
}
int myd_fprintf(int line,FILE * f,const char * msg, ...){
va_list va;
va_start(va,msg);
return myd_vfprintf(line,f,msg,va);
}
void init_progress_bar(progress_bar_t * bar,int update_rate){
memset(bar,0,sizeof(*bar));
bar->bar = create_raw_progress_bar();
@ -141,9 +164,3 @@ void DisplayProgressBar(progress_bar_t * bar,size_t offset,size_t total,const ch
bar->last_update = ts;
RawDisplayProgressBar(bar->bar,offset,total,filename);
}
void quit_progress_bar(){
if(tty_fd != STDIN_FILENO){
close(tty_fd);
}
}

View File

@ -1,29 +1,38 @@
#include<stdio.h>
#include<time.h>
#include<stdbool.h>
#include<stdarg.h>
#ifndef _DISPLAY_BAR_H_
#define _DISPLAY_BAR_H_
//declare for raw operation
typedef unsigned raw_progress_bar_t;
void lock_scrolled();
void unlock_scrolled();
void ready_progress_bar();
void quit_progress_bar();
void add_scrolled_unlocked(unsigned int i);
raw_progress_bar_t create_raw_progress_bar();
void RawDisplayProgressBar(raw_progress_bar_t bar,size_t offset,size_t total,const char * filename);
//you should call before using progrees bar.
void ready_progress_bar();
//quit progress bar system.
void quit_progress_bar();
//same as isatty
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`.
* 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`.
* thread-safe
*/
int myd_fprintf(int line,FILE * f,const char * msg, ...);
typedef struct {
raw_progress_bar_t bar;
@ -33,7 +42,8 @@ typedef struct {
} progress_bar_t;
//update rate is millsecond unit
void init_progress_bar(progress_bar_t * bar,int update_rate);
/// display progress bar
/// if `sync`, update unconditionally.
void DisplayProgressBar(progress_bar_t * bar,size_t offset,size_t total,const char * filename, bool sync);
#endif

View File

@ -30,12 +30,17 @@ enum{
static bool DisplayProgress = true;
static inline void server_perror(const char * msg){
static inline void client_perror(const char * msg){
if(DisplayProgress)
myd_perror(msg);
else
perror(msg);
}
static inline int client_fprintf(int line,FILE * file,const char * msg,...){
va_list va;
va_start(va,msg);
return DisplayProgress ? myd_vfprintf(line,file,msg,va) : vfprintf(file,msg,va);
}
/*========
*Operation
*========*/
@ -45,14 +50,14 @@ int sendReadOp(int sock,const char * filename){
op.file_url_size = strlen(filename);
op.padding0 = 0;
if(send(sock,&op,sizeof(op),0)<0){
server_perror("readop send fail");
client_perror("readop send fail");
return -1;
}
#ifdef MUL_CLIENT
sleep(MUL_CLIENT);
#endif
if(send(sock,filename,op.file_url_size,0)<0){
server_perror("readop filename send fail");
client_perror("readop filename send fail");
return -1;
}
return 0;
@ -71,7 +76,7 @@ int recvFile(int sock, const char * filename,size_t file_size){
}
fd = open(filename,O_WRONLY|O_CREAT|O_TRUNC,S_IRWXU|S_IRWXG|S_IROTH);
if (fd < 0){
server_perror("file open fail");
client_perror("file open fail");
return -1;
}
if(DisplayProgress){
@ -86,12 +91,12 @@ int recvFile(int sock, const char * filename,size_t file_size){
if((i = recv_until_byte(sock,buf,readed,TIMEOUT)) < 0){
if(i == -2)
fprintf(stderr,"recv file failed : timeout connetion lost\n");
else server_perror("recv file failed");
else client_perror("recv file failed");
return_value = -1;
goto END;
}
if(write(fd,buf,readed)<0){
server_perror("file write failed");
client_perror("file write failed");
return_value = -1;
goto END;
}
@ -113,55 +118,31 @@ int recvData(int sock,const char * filename){
int i=0;
if((i=recv_until_byte(sock,&res,sizeof(res),TIMEOUT)) < 0){
if (i == -2) {
if(DisplayProgress && isatty_file(stderr)){
lock_scrolled();
add_scrolled_unlocked(1);
fprintf(stderr,"timeout\n");
unlock_scrolled();
}
else fprintf(stderr,"timeout\n");
client_fprintf(1,stderr,"timeout\n");
}
else server_perror("recv fail");
else client_perror("recv fail");
return -1;
}
char error_meesage_buf[256] = "";
switch(res.res){
case RES_ERR:
strerror_r(res.err_number,error_meesage_buf,sizeof(error_meesage_buf));
if(DisplayProgress && isatty_file(stderr)){
lock_scrolled();
add_scrolled_unlocked(1);
fprintf(stderr,"Server Fail: %s\n", error_meesage_buf);
unlock_scrolled();
}
else fprintf(stderr,"Server Fail: %s\n", error_meesage_buf);
client_fprintf(1,stderr,"Server Fail: %s\n", error_meesage_buf);
return -1;
case RES_USR_ERR:
assert(res.error_msg_size < 80);/*todo : fix*/
if((i=recv_until_byte(sock,error_meesage_buf,res.error_msg_size,TIMEOUT)) < 0){
if (i == -2) fprintf(stderr,"timeout\n");
else server_perror("recv fail");
else client_perror("recv fail");
return -1;
}
if(DisplayProgress && isatty_file(stderr)){
lock_scrolled();
add_scrolled_unlocked(1);
fprintf(stderr,"Error Message From Server: %s\n",error_meesage_buf);
unlock_scrolled();
}
else fprintf(stderr,"Error Message From Server: %s\n",error_meesage_buf);
client_fprintf(1,stderr,"Error Message From Server: %s\n",error_meesage_buf);
return -1;
case RES_OK:
return recvFile(sock,filename,res.file_size);
break;
default:
if(DisplayProgress && isatty_file(stderr)){
lock_scrolled();
add_scrolled_unlocked(1);
fprintf(stderr,"unknown value!\n");
unlock_scrolled();
}
else fprintf(stderr,"unknown value!\n");
client_fprintf(1,stderr,"unknown value!\n");
return -1;
}
@ -173,22 +154,16 @@ int SendOpAndReceiveFile(const char * filename, struct sockaddr const * addr){
int sock;
int ret = -1;
if(!quiet_mode){
if(DisplayProgress && isatty_file(stdout)){
lock_scrolled();
add_scrolled_unlocked(1);
fprintf(stdout,"request %s\n",filename);
unlock_scrolled();
}
else fprintf(stdout,"request %s\n",filename);
client_fprintf(1,stdout,"request %s\n",filename);
}
sock = socket(AF_INET,SOCK_STREAM,0);
if(sock < 0){
server_perror("sock create fail");
client_perror("sock create fail");
return -1;
}
if(connect(sock,(struct sockaddr *)addr,sizeof(*addr)) < 0){
server_perror("connect failed");
client_perror("connect failed");
return -1;
}
if(sendReadOp(sock,filename) == 0){
@ -233,7 +208,7 @@ typedef struct SimpleCharQueue{
const char ** filename_end;
FILE * fs;
} simple_queue_t;
//
// q have lifetime of arg b or arg e.
// do not deallocate before queue closed.
static bool init_queue_from_chararray(simple_queue_t * restrict q,const char ** b, const char ** e){
@ -242,7 +217,7 @@ static bool init_queue_from_chararray(simple_queue_t * restrict q,const char **
q->filename_end = e;
return true;
}
//
// q have lifetime of arg f.
// do not close file f before queue closed.
static bool init_queue_from_file(simple_queue_t * restrict q, FILE * f){
@ -424,13 +399,7 @@ int main(int argc, const char *argv[]){
err = getsockaddrbyname(AF_INET,SOCK_STREAM,0,cmd_args.server_name,(struct sockaddr *)&addr);
if (err != 0){
int check;
if(DisplayProgress && isatty_file(stderr)){
lock_scrolled();
add_scrolled_unlocked(1);
fprintf(stderr,"netdb fail: %s\n",gai_strerror(err));
unlock_scrolled();
}
else fprintf(stderr,"netdb fail: %s\n",gai_strerror(err));
client_fprintf(1,stderr,"netdb fail: %s\n",gai_strerror(err));
//assume that sernmae is *.*.*.* and try to parse addr
check = inet_pton(AF_INET,cmd_args.server_name,&addr.sin_addr);
assert(check != -1);
@ -470,6 +439,7 @@ int main(int argc, const char *argv[]){
destroy_worker_return(ret);
}
}
quit_progress_bar();
if (bench.benchmode){
struct timespec result;
double avg;
@ -481,6 +451,5 @@ int main(int argc, const char *argv[]){
fprintf(stdout,"operation: %lf us/op\n",avg / 1000.0);
fprintf(stdout,"resolution: %ld sec %ld ns\n",bench.resolution.tv_sec,bench.resolution.tv_nsec);
}
quit_progress_bar();
return retval;
}

View File

@ -211,6 +211,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"
@ -219,7 +222,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;
@ -236,10 +240,14 @@ 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,"port argument is either not number or zero\n");
return 2;
}
}
else return 2; //failed to find argument.
else{
fprintf(stderr,"need argument\n");
return 2; //failed to find argument.
}
}
}
return 0;

View File

@ -13,6 +13,7 @@
#include <assert.h>
#include <fcntl.h>
#include <pthread.h>
#include <stdarg.h>
#include "socket_wrapper.h"
#include "simple_circular_buffer.h"
@ -97,6 +98,11 @@ static inline void server_perror(const char * msg){
else
perror(msg);
}
static inline int server_fprintf(int line,FILE * file,const char * msg,...){
va_list va;
va_start(va,msg);
return use_gui ? myd_vfprintf(line,file,msg,va) : vfprintf(file,msg,va);
}
/*========
*Operation
@ -167,13 +173,7 @@ int read_request(int sock,uint8_t * buf,size_t bufsize){
}
n = recv_until_byte(sock,buf,p.file_url_size,TIMEOUT);
buf[p.file_url_size] = '\0'; //truncate
if(use_gui&&isatty_file(stdout)){
lock_scrolled();
add_scrolled_unlocked(1);
fprintf(stdout,"str size: %d, request %s\n",p.file_url_size,buf);
unlock_scrolled();
}
else fprintf(stdout,"str size: %d, request %s\n",p.file_url_size,buf);
server_fprintf(1,stdout,"str size: %d, request %s\n",p.file_url_size,buf);
if(strchr((char *)buf,'/') != NULL){
send_fail(sock,"Illegal character /");
return -1;
@ -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;
}
}
@ -301,24 +305,23 @@ int parse_args(int argc,const char * argv[] , in_port_t * port){
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;
#endif
pthread_mutex_t sock_mutex;
#ifdef USE_TRACE
queue_struct(struct timespec,WORK_QUEUE_SIZE) trace_timer; //sock timer for TRACE
#endif
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) {
queue_init(&state->socks);
#ifdef USE_TRACE
#ifdef USE_TRACE
queue_init(&state->trace_timer);
#endif
#endif
pthread_mutex_init(&state->sock_mutex,NULL);
pthread_cond_init(&state->ready,NULL);
}
#endif
//
//argument for thread worker
typedef struct WorkerArgument
{
int id;
@ -365,10 +368,11 @@ void * worker_proc(void * data){
#endif
for(;;){
pthread_mutex_lock(&globalState.sock_mutex);
//wait until request come.
while (queue_isempty(&globalState.socks)){
pthread_cond_wait(&globalState.ready,&globalState.sock_mutex);
}
csock = dequeue(&globalState.socks);
csock = dequeue(&globalState.socks); //dequeue socket descriptor for request.
#ifdef USE_TRACE
ts_top_begin = dequeue(&globalState.trace_timer);
#endif
@ -377,6 +381,7 @@ void * worker_proc(void * data){
clock_gettime(Top_Trace_Timer_ID,&ts_top_end);
clock_gettime(Bottom_Trace_Timer_ID,&ts_bottom_begin);
#endif
//process request.
if((fd = read_request(csock,args->buf,args->bufsize)) > 0){
send_response(csock,fd,args->buf,args->bufsize);
close(fd);
@ -385,13 +390,7 @@ void * worker_proc(void * data){
clock_gettime(Bottom_Trace_Timer_ID,&ts_bottom_end);
struct timespec tophalf = timespec_sub(ts_top_end,ts_top_begin);
struct timespec bottomhalf = timespec_sub(ts_bottom_end,ts_bottom_begin);
if(use_gui&&isatty_file(stderr)){
lock_scrolled();
add_scrolled_unlocked(1);
fprintf(stderr,"top : %ld ns, bottom : %ld ns\n",tophalf.tv_nsec,bottomhalf.tv_nsec);
unlock_scrolled();
}
else fprintf(stderr,"top : %ld ns, bottom : %ld ns\n",tophalf.tv_nsec,bottomhalf.tv_nsec);
server_fprintf(1,stderr,"top : %ld ns, bottom : %ld ns\n",tophalf.tv_nsec,bottomhalf.tv_nsec);
#endif
if(close(csock) < 0)
server_perror("csock close error");
@ -419,13 +418,7 @@ void * worker_proc(void * data){
clock_gettime(Bottom_Trace_Timer_ID,&ts_bottom_end);
struct timespec tophalf = timespec_sub(ts_top_end,ts_top_begin);
struct timespec bottomhalf = timespec_sub(ts_bottom_end,ts_bottom_begin);
if(use_gui&&isatty_file(stderr)){
lock_scrolled();
add_scrolled_unlocked(1);
fprintf(stderr,"top : %ld ns, bottom : %ld ns\n",tophalf.tv_nsec,bottomhalf.tv_nsec);
unlock_scrolled();
}
else fprintf(stderr,"top : %ld ns, bottom : %ld ns\n",tophalf.tv_nsec,bottomhalf.tv_nsec);
server_fprintf(1,stderr,"top : %ld ns, bottom : %ld ns\n",tophalf.tv_nsec,bottomhalf.tv_nsec);
#endif
if(close(csock) < 0)
server_perror("csock close error");
@ -447,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;
@ -454,8 +448,9 @@ int main(int argc, const char *argv[]){
if(use_gui)
ready_progress_bar();
#ifdef USE_TRACE
report_resolution();
report_resolution();
#endif
//create socket
sock = socket(AF_INET,SOCK_STREAM,0);
atexit(safe_exit);
if(sock < 0){
@ -470,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);
@ -480,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);
@ -491,13 +488,7 @@ int main(int argc, const char *argv[]){
char ip_buf[INET_ADDRSTRLEN];
const char * msg = inet_ntop(AF_INET,&addr.sin_addr,ip_buf,sizeof(ip_buf));
assert(msg != NULL);
if(use_gui&&isatty_file(stdout)){
lock_scrolled();
add_scrolled_unlocked(1);
fprintf(stdout,"server bind on %s:%d\n",msg ,binding_port_number);
unlock_scrolled();
}
else fprintf(stdout,"server bind on %s:%d\n",msg ,binding_port_number);
server_fprintf(1,stdout,"server bind on %s:%d\n",msg ,binding_port_number);
}
if(listen(sock,MAX_LISTEN_SOCKET) < 0){
@ -509,13 +500,7 @@ int main(int argc, const char *argv[]){
{
char ip_buf[INET_ADDRSTRLEN];
const char * msg = inet_ntop(AF_INET,&client_addr.sin_addr,ip_buf,sizeof(ip_buf));
if(use_gui&&isatty_file(stdout)){
lock_scrolled();
add_scrolled_unlocked(1);
fprintf(stdout,"Connected on : %s:%d\n",msg == NULL ? "(null)" : msg , ntohs(addr.sin_port));
unlock_scrolled();
}
else fprintf(stdout,"Connected on : %s:%d\n",msg == NULL ? "(null)" : msg , ntohs(addr.sin_port));
server_fprintf(1,stdout,"Connected on : %s:%d\n",msg == NULL ? "(null)" : msg , ntohs(addr.sin_port));
#ifdef USE_TRACE
struct timespec ts_top_begin;
clock_gettime(Top_Trace_Timer_ID, &ts_top_begin);
@ -526,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);

View File

@ -3,26 +3,32 @@
#include<stddef.h>
//queue type declare
#define queue_struct(queue_type,queue_size) struct{\
queue_type data [(queue_size)+1];\
size_t begin;\
size_t end;\
}
//queue's attribute
#define queue_size(queue) ((sizeof((queue)->data)/sizeof((queue)->data[0])) - 1)
#define queue_isempty(queue) ((queue)->begin == (queue)->end)
#define queue_isfull(queue) ((queue)->begin == (((queue)->end + 1) % queue_size(queue)))
//queue's methods
#define queue_init(queue) do{\
(queue)->begin = 0;\
(queue)->end = 0;\
}while(0)
//unchecked
//this do not check that queue is full.
#define enqueue(queue,element) do{ \
(queue)->data[(queue)->end] = (element);\
(queue)->end = ((queue)->end + 1) % (queue_size(queue) + 1);\
}while(0)
//unchecked
//this do not check that queue is empty.
#define dequeue(queue) \
(((queue)->begin = ((queue)->begin + 1) % (queue_size(queue) + 1)) ,\
(queue)->data[((queue)->begin + queue_size(queue)) % (queue_size(queue) + 1)])

View File

@ -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

View File

@ -2,6 +2,10 @@
#define _TIMERHELPER_H_
#include <sys/time.h>
#ifndef __always_inline
#define __always_inline
#endif
///subtract b from a to get difference
__always_inline struct timespec timespec_sub(struct timespec a,struct timespec b){
struct timespec ret;
ret.tv_sec = a.tv_sec - b.tv_sec;