Compare commits

..

No commits in common. "bf3cc14c4dd9553d7f5b390060966485ff20c7a9" and "80a03a8610d13b9b9e8278db900e9716f4ad3c13" have entirely different histories.

8 changed files with 70 additions and 318 deletions

View File

@ -5,7 +5,6 @@
"files.associations": { "files.associations": {
"socket_wrapper.h": "c", "socket_wrapper.h": "c",
"stdalign.h": "c", "stdalign.h": "c",
"timerhelper.h": "c", "timerhelper.h": "c"
"termio.h": "c"
} }
} }

View File

@ -9,13 +9,11 @@ all:
socket_wrapper.o: socket_wrapper.c socket_wrapper.h socket_wrapper.o: socket_wrapper.c socket_wrapper.h
$(CC) -c socket_wrapper.c -o socket_wrapper.o $(CFLAGS) $(CC) -c socket_wrapper.c -o socket_wrapper.o $(CFLAGS)
display_bar.o: display_bar.c display_bar.h
$(CC) -c display_bar.c -o display_bar.o $(CFLAGS)
client: socket_wrapper.o client.c client: socket_wrapper.o client.c
$(CC) -o client client.c socket_wrapper.o $(CFLAGS) $(CC) -o client client.c socket_wrapper.o $(CFLAGS)
server: socket_wrapper.o server.c display_bar.o server: socket_wrapper.o server.c
$(CC) -o server server.c socket_wrapper.o display_bar.o $(CFLAGS) $(CC) -o server server.c socket_wrapper.o $(CFLAGS)
p-server: socket_wrapper.o p-server.c p-server: socket_wrapper.o p-server.c
$(CC) -o p-server p-server.c socket_wrapper.o $(CFLAGS) $(CC) -o p-server p-server.c socket_wrapper.o $(CFLAGS)
p-client: socket_wrapper.o p-client.c p-client: socket_wrapper.o p-client.c

View File

@ -33,13 +33,11 @@ For server
- DEFAULT_RESPONSE_REQUEST(p-server only): 3(-1 is INF) - DEFAULT_RESPONSE_REQUEST(p-server only): 3(-1 is INF)
- USE_TRACE - USE_TRACE
- USE_NO_QUEUE - USE_NO_QUEUE
- SLOW_SERVER(microsecond unit): if turn on sendfile option, DEFAULT_SEND_FILE_CHUNK_SIZE/SLOW bytes/usec, otherwise (buf_size/SLOW_CLIENT) bytes/usec
For client For client
- MUL_CLIENT(second unit) - MUL_CLIENT(second unit)
- SLOW_CLIENT(microsecond unit, (buf_size/SLOW_CLIENT) bytes/usec) - SLOW_CLIENT(microsecond unit, (buf_size/SLOW_CLIENT) bytes/usec)
- DEFAULT_PROGRESS_BAR_WIDTH: 30
For both For both
- TIMEOUT: 5(second unit) - TIMEOUT: 5(second unit)
- MAX_TERMINAL_ROW
- DEFAULT_PROGRESS_BAR_WIDTH: 30

View File

@ -238,7 +238,7 @@ static bool init_queue_from_chararray(simple_queue_t * restrict q,const char **
// q have lifetime of arg f. // q have lifetime of arg f.
// do not close file f before queue closed. // do not close file f before queue closed.
static bool init_queue_from_file(simple_queue_t * restrict q, FILE * f){ static bool init_queue_from_file(simple_queue_t * restrict q, FILE * f){
q->method = From_FileStream; q->method = From_CharArray;
q->fs = f; q->fs = f;
return true; return true;
} }

View File

@ -1,118 +0,0 @@
#include<stdio.h>
#include<stdbool.h>
#include<stdarg.h>
#include<string.h>
#include<errno.h>
#include<unistd.h>
#include<termio.h>
#include<pthread.h>
#include "display_bar.h"
#include "timerhelper.h"
enum{
#ifndef DEFAULT_PROGRESS_BAR_WIDTH
PROGRESS_BAR_WIDTH = 30,
#else
PROGRESS_BAR_WIDTH = DEFAULT_PROGRESS_BAR_WIDTH;
#endif
#ifndef DEFAULT_MAX_TERMINAL_ROW
MAX_TERMINAL_ROW = 1000,
#else
MAX_TERMINAL_ROW = MAX_TERMINAL_ROW;
#endif
};
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
static raw_progress_bar_t scrolled = 1;
void ready_progress_bar(){
fflush(stdout);
}
void lock_scrolled(){
pthread_mutex_lock(&mutex);
}
void unlock_scrolled(){
pthread_mutex_unlock(&mutex);
}
void add_scrolled_unlocked(unsigned int i){
scrolled += i;
}
raw_progress_bar_t create_raw_progress_bar(){
int ret;
lock_scrolled();
ret = scrolled;
//make space for progress bar
fprintf(stdout,"\n");
scrolled++;
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;
double cur_progress = ((double)offset)/(double)total *100.f;
size_t cur_pos = (cur_progress / 100.0 * PROGRESS_BAR_WIDTH); //must be less than SIZE_MAX. other value is undefined behavior.
int pos;
struct winsize wnd_size;
if (offset == total)
cur_pos = PROGRESS_BAR_WIDTH;
for (i = 0; i < PROGRESS_BAR_WIDTH; i++){
if (i < cur_pos)
buf[i] = '=';
else if(i == cur_pos)
buf[i] = '>';
else buf[i] = '.';
}
lock_scrolled();
pos = scrolled - bar;
if (pos > MAX_TERMINAL_ROW) {
unlock_scrolled();
return;
}//optimization.
//if ioctl failed? what should i do...
ioctl(STDIN_FILENO,TIOCGWINSZ,(char *)&wnd_size);
if (wnd_size.ws_row < pos) return;
fprintf(stdout,"\x1b[%dA[%s]: %.2f%% %s bytes: %ld/%ld \x1b[%dB\r",pos,buf,cur_progress,filename,total,offset,pos);
fflush(stdout);
unlock_scrolled();
}
bool isatty_file(FILE * file){
return isatty(fileno(file));
}
void myd_perror(const char * msg){
char buf[1024];
int err = errno;
strerror_r(errno,buf,sizeof(buf));
errno = err;
if (isatty(STDERR_FILENO)){
lock_scrolled();
scrolled++;
fprintf(stderr,"%s: %s\n",msg,buf);
unlock_scrolled();
}
}
void init_progress_bar(progress_bar_t * bar,int update_rate){
memset(bar,0,sizeof(*bar));
bar->bar = create_raw_progress_bar();
bar->update_rate = update_rate;
}
void DisplayProgressBar(progress_bar_t * bar,size_t offset,size_t total,const char * filename, bool sync){
unsigned diff;
struct timespec ts,dif;
clock_gettime(CLOCK_REALTIME,&ts);
if (!sync){
dif = timespec_sub(ts,bar->last_update);
diff = dif.tv_nsec / 1000000 + dif.tv_sec;
if (diff < bar->update_rate){
return;
}
}
bar->last_update = ts;
RawDisplayProgressBar(bar->bar,offset,total,filename);
}

View File

@ -1,37 +0,0 @@
#include<stdio.h>
#include<time.h>
#include<stdbool.h>
#ifndef _DISPLAY_BAR_H_
#define _DISPLAY_BAR_H_
typedef unsigned raw_progress_bar_t;
void lock_scrolled();
void unlock_scrolled();
void ready_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);
bool isatty_file(FILE * file);
void myd_perror(const char * msg);
typedef struct {
raw_progress_bar_t bar;
//millisecond unit
int update_rate;
struct timespec last_update;
} progress_bar_t;
void init_progress_bar(progress_bar_t * bar,int update_rate);
void DisplayProgressBar(progress_bar_t * bar,size_t offset,size_t total,const char * filename, bool sync);
#endif

View File

@ -52,25 +52,15 @@ static const int RESPONSE_REQUEST = 3;
static const int RESPONSE_REQUEST = DEFAULT_RESPONSE_REQUEST; static const int RESPONSE_REQUEST = DEFAULT_RESPONSE_REQUEST;
#endif #endif
#ifdef USE_TRACE #define USE_TRACE
#include <sys/time.h> #include <sys/time.h>
#include <time.h> #include <time.h>
#include "timerhelper.h" #include "timerhelper.h"
enum{ enum{
Top_Trace_Timer_ID = CLOCK_REALTIME, Trace_Timer_ID = CLOCK_REALTIME
Bottom_Trace_Timer_ID = CLOCK_THREAD_CPUTIME_ID
}; };
static inline void report_resolution()
{
struct timespec top_res,bottom_res;
clock_getres(Top_Trace_Timer_ID,&top_res);
clock_getres(Bottom_Trace_Timer_ID,&bottom_res);
fprintf(stderr,"top res: %ld, bottom res: %ld\n",top_res.tv_nsec,bottom_res.tv_nsec);
}
#endif
/*======== /*========
*Operation *Operation
*========*/ *========*/
@ -253,9 +243,6 @@ int main(int argc, const char *argv[]){
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;
} }
#ifdef USE_TRACE
report_resolution();
#endif
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){
@ -299,7 +286,7 @@ int main(int argc, const char *argv[]){
char ip_buf[INET_ADDRSTRLEN]; char ip_buf[INET_ADDRSTRLEN];
const char * msg; const char * msg;
int retval = 0; int retval = 0;
struct timespec ts_top_begin,ts_top_end, ts_bottom_begin, ts_bottom_end; struct timespec ts,tsmiddle,tsend;
if((csock = accept(sock, (struct sockaddr *)&client_addr,&client_addr_len)) < 0){ if((csock = accept(sock, (struct sockaddr *)&client_addr,&client_addr_len)) < 0){
free(buf); free(buf);
perror("accept error"); perror("accept error");
@ -307,14 +294,13 @@ int main(int argc, const char *argv[]){
} }
msg = inet_ntop(AF_INET,&client_addr.sin_addr,ip_buf,sizeof(ip_buf)); msg = inet_ntop(AF_INET,&client_addr.sin_addr,ip_buf,sizeof(ip_buf));
#ifdef USE_TRACE #ifdef USE_TRACE
clock_gettime(Top_Trace_Timer_ID,&ts_top_begin); clock_gettime(Trace_Timer_ID,&ts);
#endif #endif
fprintf(stderr,"Connected on : %s:%d\n",msg == NULL ? "(null)" : msg , ntohs(client_addr.sin_port)); fprintf(stderr,"Connected on : %s:%d\n",msg == NULL ? "(null)" : msg , ntohs(client_addr.sin_port));
pid = fork(); pid = fork();
if(pid == 0){ if(pid == 0){
#ifdef USE_TRACE #ifdef USE_TRACE
clock_gettime(Top_Trace_Timer_ID,&ts_top_end); clock_gettime(Trace_Timer_ID,&tsmiddle);
clock_gettime(Bottom_Trace_Timer_ID,&ts_bottom_begin);
#endif #endif
if((fd = read_request(csock,buf,bufsize)) > 0){ if((fd = read_request(csock,buf,bufsize)) > 0){
retval = send_response(csock,fd,buf,bufsize); retval = send_response(csock,fd,buf,bufsize);
@ -324,10 +310,12 @@ int main(int argc, const char *argv[]){
if(close(csock) < 0) if(close(csock) < 0)
perror("csock close error"); perror("csock close error");
#ifdef USE_TRACE #ifdef USE_TRACE
clock_gettime(Bottom_Trace_Timer_ID,&ts_bottom_end); clock_gettime(Trace_Timer_ID,&tsend);
struct timespec tophalf = timespec_sub(ts_top_end,ts_top_begin); struct timespec tophalf = timespec_sub(tsmiddle,ts);
struct timespec bottomhelf = timespec_sub(ts_bottom_end,ts_bottom_end); struct timespec bottomhelf = timespec_sub(tsend,tsmiddle);
fprintf(stderr,"top: %ld ns, bottom: %ld ns\n",tophalf.tv_nsec,bottomhelf.tv_nsec); struct timespec resolution;
clock_getres(Trace_Timer_ID,&resolution);
fprintf(stderr,"top: %ld ns, bottom: %ld ns, res: %ld\n",tophalf.tv_nsec,bottomhelf.tv_nsec,resolution.tv_nsec);
#endif #endif
free(buf); free(buf);
return retval; return retval;

170
server.c
View File

@ -16,7 +16,7 @@
#include "socket_wrapper.h" #include "socket_wrapper.h"
#include "simple_circular_buffer.h" #include "simple_circular_buffer.h"
#include "display_bar.h" #include "timerhelper.h"
#ifndef DEFAULT_MAX_LISTEN_SOCKET #ifndef DEFAULT_MAX_LISTEN_SOCKET
static const int MAX_LISTEN_SOCKET = 16; static const int MAX_LISTEN_SOCKET = 16;
@ -60,35 +60,8 @@ enum{
MAX_THREAD_NUMBER = DEFAULT_MAX_THREAD_NUMBER MAX_THREAD_NUMBER = DEFAULT_MAX_THREAD_NUMBER
#endif #endif
}; };
#ifdef SLOW_SERVER #define USE_TRACE
//micro second unit #define USE_NO_QUEUE
static const int SLOW_SERVER_TIME = SLOW_SERVER;
#endif
//#define USE_TRACE
#ifdef USE_TRACE
#include "timerhelper.h"
enum{
Top_Trace_Timer_ID = CLOCK_REALTIME,
Bottom_Trace_Timer_ID = CLOCK_THREAD_CPUTIME_ID
};
static inline void report_resolution()
{
struct timespec top_res,bottom_res;
clock_getres(Top_Trace_Timer_ID,&top_res);
clock_getres(Bottom_Trace_Timer_ID,&bottom_res);
fprintf(stderr,"top res: %ld, bottom res: %ld\n",top_res.tv_nsec,bottom_res.tv_nsec);
}
#endif
static bool use_gui = false;
static inline void server_perror(const char * msg){
if(use_gui)
myd_perror(msg);
else
perror(msg);
}
/*======== /*========
*Operation *Operation
@ -107,11 +80,11 @@ int send_fail(int sock,const char * msg){
res.error_msg_size = strlen(msg); res.error_msg_size = strlen(msg);
//os will be combining if tcp_autocorking emabled. //os will be combining if tcp_autocorking emabled.
if(send(sock,&res,sizeof(res),0) < 0){ if(send(sock,&res,sizeof(res),0) < 0){
server_perror("error msg send"); perror("error msg send");
return -1; return -1;
} }
if (send(sock,msg,res.error_msg_size,0) < 0){ if (send(sock,msg,res.error_msg_size,0) < 0){
server_perror("error msg send"); perror("error msg send");
return -1; return -1;
} }
return 0; return 0;
@ -127,7 +100,7 @@ int send_errno(int sock){
r.file_size = 0; r.file_size = 0;
r.error_msg_size = 0; r.error_msg_size = 0;
if(send(sock,&r,sizeof(r),0)){ if(send(sock,&r,sizeof(r),0)){
server_perror("errno send"); perror("errno send");
return -1; return -1;
} }
return 0; return 0;
@ -142,7 +115,7 @@ int read_request(int sock,uint8_t * buf,size_t bufsize){
ssize_t n = recv_until_byte(sock,&p,sizeof(p),TIMEOUT); ssize_t n = recv_until_byte(sock,&p,sizeof(p),TIMEOUT);
if (n < 0){ if (n < 0){
if (n == -2) fprintf(stderr,"timeout!"); if (n == -2) fprintf(stderr,"timeout!");
else server_perror("receive fail"); else perror("receive fail");
return -1; return -1;
} }
if(bufsize <= ((size_t)p.file_url_size) + sizeof(p) + 1){ if(bufsize <= ((size_t)p.file_url_size) + sizeof(p) + 1){
@ -159,13 +132,7 @@ int read_request(int sock,uint8_t * buf,size_t bufsize){
} }
n = recv_until_byte(sock,buf,p.file_url_size,TIMEOUT); n = recv_until_byte(sock,buf,p.file_url_size,TIMEOUT);
buf[p.file_url_size] = '\0'; //truncate 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); 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);
if(strchr((char *)buf,'/') != NULL){ if(strchr((char *)buf,'/') != NULL){
send_fail(sock,"Illegal character /"); send_fail(sock,"Illegal character /");
return -1; return -1;
@ -187,7 +154,6 @@ int send_response(int sock,int fd, uint8_t * buf, size_t bufsize){
struct stat st; struct stat st;
off_t offset = 0; off_t offset = 0;
ssize_t readed = 0; ssize_t readed = 0;
progress_bar_t pbar;
r.res = RES_OK; r.res = RES_OK;
r.err_number = 0; r.err_number = 0;
r.error_msg_size = 0; r.error_msg_size = 0;
@ -199,46 +165,33 @@ int send_response(int sock,int fd, uint8_t * buf, size_t bufsize){
} }
r.file_size = st.st_size; r.file_size = st.st_size;
if(send(sock,&r,sizeof(r),0)<0){ if(send(sock,&r,sizeof(r),0)<0){
server_perror("send fail"); perror("send fail");
return -1; return -1;
} }
if(use_gui)
init_progress_bar(&pbar,10);
#ifdef USE_SENDFILE #ifdef USE_SENDFILE
while (r.file_size != offset) while (r.file_size != offset)
{ {
size_t count = SEND_FILE_CHUNK_SIZE < (r.file_size - offset) ? SEND_FILE_CHUNK_SIZE : (r.file_size - offset); size_t count = SEND_FILE_CHUNK_SIZE < (r.file_size - offset) ? SEND_FILE_CHUNK_SIZE : (r.file_size - offset);
if((readed = sendfile(sock,fd,&offset,count)) < 0){ if((readed = sendfile(sock,fd,&offset,count)) < 0){
server_perror("send file fail"); perror("send file fail");
return -1; return -1;
} }
if(use_gui)
DisplayProgressBar(&pbar,offset,r.file_size,"",false);
#ifdef SLOW_SERVER
usleep(SLOW_SERVER_TIME);
#endif
} }
#else #else
while (offset < r.file_size) while (offset < r.file_size)
{ {
readed = bufsize < (r.file_size - offset) ? bufsize : r.file_size - offset; readed = bufsize < (r.file_size - offset) ? bufsize : r.file_size - offset;
if(read(fd,buf,readed)<0){ if(read(fd,buf,readed)<0){
server_perror("send response read fail"); perror("send response read fail");
return -1; return -1;
} }
if(send(sock,buf,readed,0)<0){ if(send(sock,buf,readed,0)<0){
server_perror("send response send fail"); perror("send response send fail");
return -1; return -1;
} }
offset += readed; offset += readed;
if(use_gui)
DisplayProgressBar(&pbar,offset,r.file_size,"",false);
#ifdef SLOW_SERVER
usleep(SLOW_SERVER_TIME);
#endif
} }
if(use_gui)
DisplayProgressBar(&pbar,offset,r.file_size,"",true);
#endif #endif
return 0; return 0;
} }
@ -246,8 +199,7 @@ 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"
"-p port\t:set to port binding. couldn't set to 0\n" "-p port\t:set to port binding. couldn't set to 0\n"
"-h\t:print help message.\n" "-h\t:print help message.\n";
"--progress_bar\t: show pretty progress bar";
printf(msg,n); printf(msg,n);
return msg; return msg;
} }
@ -255,9 +207,11 @@ const char * help(const char * n){
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;
while (pos < argc){ while (pos < argc)
{
opt = argv[pos++]; opt = argv[pos++];
if (strcmp(opt,"-h") == 0 || strcmp(opt,"--help") == 0){ if (strcmp(opt,"-h") == 0 || strcmp(opt,"--help") == 0)
{
help(argv[0]); help(argv[0]);
return 0; return 0;
} }
@ -275,20 +229,18 @@ int parse_args(int argc,const char * argv[] , in_port_t * port){
return 2; //failed to find argument. return 2; //failed to find argument.
} }
} }
else if(strcmp(opt,"--progress_bar") == 0){
use_gui = true;
}
else{
fprintf(stderr,"unknown option\n");
help(argv[0]);
return 2;
}
} }
return 0; return 0;
} }
//============ //============
//Simple Thread Pool //Simple Thread Pool
//============ //============
#ifdef USE_TRACE
enum{
Trace_Timer_ID = CLOCK_REALTIME
};
#endif
#ifndef USE_NO_QUEUE #ifndef USE_NO_QUEUE
typedef struct SharedState{ typedef struct SharedState{
//empty if less than 0 //empty if less than 0
@ -353,39 +305,34 @@ void * worker_proc(void * data){
worker_argument_t * args = (worker_argument_t *)data; worker_argument_t * args = (worker_argument_t *)data;
int fd, csock; int fd, csock;
#ifdef USE_TRACE #ifdef USE_TRACE
struct timespec ts_top_begin,ts_top_end, ts_bottom_begin, ts_bottom_end; struct timespec ts,ts_middle,ts_end;
#endif #endif
for(;;){ for(;;){
pthread_mutex_lock(&globalState.sock_mutex); pthread_mutex_lock(&globalState.sock_mutex);
while (queue_isempty(&globalState.socks)){ while (queue_isempty(&globalState.socks)){
pthread_cond_wait(&globalState.ready,&globalState.sock_mutex); pthread_cond_wait(&globalState.ready,&globalState.sock_mutex);
} }
csock = dequeue(&globalState.socks); csock = dequeue(&globalState.socks);
#ifdef USE_TRACE #ifdef USE_TRACE
ts_top_begin = dequeue(&globalState.trace_timer); ts = dequeue(&globalState.trace_timer);
#endif #endif
pthread_mutex_unlock(&globalState.sock_mutex); pthread_mutex_unlock(&globalState.sock_mutex);
#ifdef USE_TRACE #ifdef USE_TRACE
clock_gettime(Top_Trace_Timer_ID,&ts_top_end); clock_gettime(Trace_Timer_ID,&ts_middle);
clock_gettime(Bottom_Trace_Timer_ID,&ts_bottom_begin);
#endif #endif
if((fd = read_request(csock,args->buf,args->bufsize)) > 0){ if((fd = read_request(csock,args->buf,args->bufsize)) > 0){
send_response(csock,fd,args->buf,args->bufsize); send_response(csock,fd,args->buf,args->bufsize);
close(fd); close(fd);
} }
#ifdef USE_TRACE #ifdef USE_TRACE
clock_gettime(Bottom_Trace_Timer_ID,&ts_bottom_end); clock_gettime(Trace_Timer_ID,&ts_end);
struct timespec tophalf = timespec_sub(ts_top_end,ts_top_end); struct timespec tophalf = timespec_sub(ts_middle,ts);
struct timespec bottomhelf = timespec_sub(ts_bottom_end,ts_bottom_begin); struct timespec bottomhelf = timespec_sub(ts_end,ts_middle);
if(use_gui&&isatty_file(stderr)){
lock_scrolled();
add_scrolled_unlocked(1);
fprintf(stderr,"top : %ld ns, bottom : %ld ns\n",tophalf.tv_nsec,bottomhelf.tv_nsec); fprintf(stderr,"top : %ld ns, bottom : %ld ns\n",tophalf.tv_nsec,bottomhelf.tv_nsec);
unlock_scrolled();
}
#endif #endif
if(close(csock) < 0) if(close(csock) < 0)
server_perror("csock close error"); perror("csock close error");
} }
destory_worker_argument(args); destory_worker_argument(args);
return NULL; return NULL;
@ -397,29 +344,22 @@ void * worker_proc(void * data){
int fd, csock; int fd, csock;
csock = args->csock; csock = args->csock;
#ifdef USE_TRACE #ifdef USE_TRACE
struct timespec ts_top_begin,ts_top_end, ts_bottom_begin, ts_bottom_end; struct timespec ts,ts_middle,ts_end;
ts_top_begin = args->ts; ts = args->ts;
clock_gettime(Top_Trace_Timer_ID,&ts_top_end); clock_gettime(Trace_Timer_ID,&ts_middle);
clock_gettime(Bottom_Trace_Timer_ID,&ts_bottom_begin);
#endif #endif
if((fd = read_request(csock,args->buf,args->bufsize)) > 0){ if((fd = read_request(csock,args->buf,args->bufsize)) > 0){
send_response(csock,fd,args->buf,args->bufsize); send_response(csock,fd,args->buf,args->bufsize);
close(fd); close(fd);
} }
#ifdef USE_TRACE #ifdef USE_TRACE
clock_gettime(Bottom_Trace_Timer_ID,&ts_bottom_end); clock_gettime(Trace_Timer_ID,&ts_end);
struct timespec tophalf = timespec_sub(ts_top_end,ts_top_begin); struct timespec tophalf = timespec_sub(ts_middle,ts);
struct timespec bottomhelf = timespec_sub(ts_bottom_end,ts_bottom_begin); struct timespec bottomhelf = timespec_sub(ts_end,ts_middle);
if(use_gui&&isatty_file(stderr)){
lock_scrolled();
add_scrolled_unlocked(1);
fprintf(stderr,"top : %ld ns, bottom : %ld ns\n",tophalf.tv_nsec,bottomhelf.tv_nsec); fprintf(stderr,"top : %ld ns, bottom : %ld ns\n",tophalf.tv_nsec,bottomhelf.tv_nsec);
unlock_scrolled();
}
else fprintf(stderr,"top : %ld ns, bottom : %ld ns\n",tophalf.tv_nsec,bottomhelf.tv_nsec);
#endif #endif
if(close(csock) < 0) if(close(csock) < 0)
server_perror("csock close error"); perror("csock close error");
destory_worker_argument(args); destory_worker_argument(args);
return NULL; return NULL;
} }
@ -442,21 +382,17 @@ int main(int argc, const char *argv[]){
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;
} }
if(use_gui)
ready_progress_bar();
#ifdef USE_TRACE
report_resolution();
#endif
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){
server_perror("sock create fail"); perror("sock create fail");
return 1; return 1;
} }
else { else {
int option = 1; int option = 1;
if(setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,&option,sizeof(option)) < 0){ if(setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,&option,sizeof(option)) < 0){
server_perror("setsockopt"); perror("setsockopt");
} }
} }
bufsize = getBufferSizeFrom(sock); bufsize = getBufferSizeFrom(sock);
@ -476,23 +412,17 @@ int main(int argc, const char *argv[]){
addr.sin_port = htons(binding_port_number); addr.sin_port = htons(binding_port_number);
if(bind(sock, (struct sockaddr *)&addr,sizeof(addr)) < 0){ if(bind(sock, (struct sockaddr *)&addr,sizeof(addr)) < 0){
server_perror("bind failed"); perror("bind failed");
return 1; return 1;
} else { } else {
char ip_buf[INET_ADDRSTRLEN]; char ip_buf[INET_ADDRSTRLEN];
const char * msg = inet_ntop(AF_INET,&addr.sin_addr,ip_buf,sizeof(ip_buf)); const char * msg = inet_ntop(AF_INET,&addr.sin_addr,ip_buf,sizeof(ip_buf));
assert(msg != NULL); 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); 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);
} }
if(listen(sock,MAX_LISTEN_SOCKET) < 0){ if(listen(sock,MAX_LISTEN_SOCKET) < 0){
server_perror("listen failed"); perror("listen failed");
return 1; return 1;
} }
@ -500,16 +430,10 @@ int main(int argc, const char *argv[]){
{ {
char ip_buf[INET_ADDRSTRLEN]; char ip_buf[INET_ADDRSTRLEN];
const char * msg = inet_ntop(AF_INET,&client_addr.sin_addr,ip_buf,sizeof(ip_buf)); 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)); 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));
#ifdef USE_TRACE #ifdef USE_TRACE
struct timespec ts_top_begin; struct timespec ts;
clock_gettime(Top_Trace_Timer_ID, &ts_top_begin); clock_gettime(Trace_Timer_ID, &ts);
#endif #endif
#ifndef USE_NO_QUEUE #ifndef USE_NO_QUEUE
for(;;){ for(;;){
@ -526,7 +450,7 @@ int main(int argc, const char *argv[]){
else { else {
enqueue(&globalState.socks,csock); enqueue(&globalState.socks,csock);
#ifdef USE_TRACE #ifdef USE_TRACE
enqueue(&globalState.trace_timer,ts_top_begin); enqueue(&globalState.trace_timer,ts);
#endif #endif
} }
break; break;
@ -537,7 +461,7 @@ int main(int argc, const char *argv[]){
pthread_t thread_a; pthread_t thread_a;
worker_argument_t * args = create_worker_argument(i++,bufsize,csock); worker_argument_t * args = create_worker_argument(i++,bufsize,csock);
#ifdef USE_TRACE #ifdef USE_TRACE
args->ts = ts_top_begin; args->ts = ts;
#endif #endif
pthread_create(&thread_a,NULL,worker_proc,args); pthread_create(&thread_a,NULL,worker_proc,args);
pthread_detach(thread_a); pthread_detach(thread_a);