Compare commits
5 Commits
80a03a8610
...
bf3cc14c4d
Author | SHA1 | Date | |
---|---|---|---|
|
bf3cc14c4d | ||
|
c23db73d71 | ||
|
355411acaf | ||
|
5183083888 | ||
|
80f3226421 |
3
.vscode/settings.json
vendored
3
.vscode/settings.json
vendored
@ -5,6 +5,7 @@
|
||||
"files.associations": {
|
||||
"socket_wrapper.h": "c",
|
||||
"stdalign.h": "c",
|
||||
"timerhelper.h": "c"
|
||||
"timerhelper.h": "c",
|
||||
"termio.h": "c"
|
||||
}
|
||||
}
|
6
Makefile
6
Makefile
@ -9,11 +9,13 @@ all:
|
||||
|
||||
socket_wrapper.o: socket_wrapper.c socket_wrapper.h
|
||||
$(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
|
||||
$(CC) -o client client.c socket_wrapper.o $(CFLAGS)
|
||||
server: socket_wrapper.o server.c
|
||||
$(CC) -o server server.c socket_wrapper.o $(CFLAGS)
|
||||
server: socket_wrapper.o server.c display_bar.o
|
||||
$(CC) -o server server.c socket_wrapper.o display_bar.o $(CFLAGS)
|
||||
p-server: socket_wrapper.o p-server.c
|
||||
$(CC) -o p-server p-server.c socket_wrapper.o $(CFLAGS)
|
||||
p-client: socket_wrapper.o p-client.c
|
||||
|
@ -33,11 +33,13 @@ For server
|
||||
- DEFAULT_RESPONSE_REQUEST(p-server only): 3(-1 is INF)
|
||||
- USE_TRACE
|
||||
- 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
|
||||
- MUL_CLIENT(second unit)
|
||||
- SLOW_CLIENT(microsecond unit, (buf_size/SLOW_CLIENT) bytes/usec)
|
||||
- DEFAULT_PROGRESS_BAR_WIDTH: 30
|
||||
|
||||
For both
|
||||
- TIMEOUT: 5(second unit)
|
||||
- MAX_TERMINAL_ROW
|
||||
- DEFAULT_PROGRESS_BAR_WIDTH: 30
|
2
client.c
2
client.c
@ -238,7 +238,7 @@ static bool init_queue_from_chararray(simple_queue_t * restrict q,const char **
|
||||
// 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){
|
||||
q->method = From_CharArray;
|
||||
q->method = From_FileStream;
|
||||
q->fs = f;
|
||||
return true;
|
||||
}
|
||||
|
118
display_bar.c
Normal file
118
display_bar.c
Normal file
@ -0,0 +1,118 @@
|
||||
#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);
|
||||
}
|
37
display_bar.h
Normal file
37
display_bar.h
Normal file
@ -0,0 +1,37 @@
|
||||
#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
|
36
p-server.c
36
p-server.c
@ -52,15 +52,25 @@ static const int RESPONSE_REQUEST = 3;
|
||||
static const int RESPONSE_REQUEST = DEFAULT_RESPONSE_REQUEST;
|
||||
#endif
|
||||
|
||||
#define USE_TRACE
|
||||
|
||||
#ifdef USE_TRACE
|
||||
#include <sys/time.h>
|
||||
#include <time.h>
|
||||
#include "timerhelper.h"
|
||||
|
||||
enum{
|
||||
Trace_Timer_ID = CLOCK_REALTIME
|
||||
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
|
||||
|
||||
/*========
|
||||
*Operation
|
||||
*========*/
|
||||
@ -243,6 +253,9 @@ int main(int argc, const char *argv[]){
|
||||
int d = parse_args(argc,argv,&binding_port_number);
|
||||
if(d != 0 ) return d;
|
||||
}
|
||||
#ifdef USE_TRACE
|
||||
report_resolution();
|
||||
#endif
|
||||
sock = socket(AF_INET,SOCK_STREAM,0);
|
||||
atexit(safe_exit);
|
||||
if(sock < 0){
|
||||
@ -286,7 +299,7 @@ int main(int argc, const char *argv[]){
|
||||
char ip_buf[INET_ADDRSTRLEN];
|
||||
const char * msg;
|
||||
int retval = 0;
|
||||
struct timespec ts,tsmiddle,tsend;
|
||||
struct timespec ts_top_begin,ts_top_end, ts_bottom_begin, ts_bottom_end;
|
||||
if((csock = accept(sock, (struct sockaddr *)&client_addr,&client_addr_len)) < 0){
|
||||
free(buf);
|
||||
perror("accept error");
|
||||
@ -294,13 +307,14 @@ int main(int argc, const char *argv[]){
|
||||
}
|
||||
msg = inet_ntop(AF_INET,&client_addr.sin_addr,ip_buf,sizeof(ip_buf));
|
||||
#ifdef USE_TRACE
|
||||
clock_gettime(Trace_Timer_ID,&ts);
|
||||
clock_gettime(Top_Trace_Timer_ID,&ts_top_begin);
|
||||
#endif
|
||||
fprintf(stderr,"Connected on : %s:%d\n",msg == NULL ? "(null)" : msg , ntohs(client_addr.sin_port));
|
||||
pid = fork();
|
||||
if(pid == 0){
|
||||
#ifdef USE_TRACE
|
||||
clock_gettime(Trace_Timer_ID,&tsmiddle);
|
||||
clock_gettime(Top_Trace_Timer_ID,&ts_top_end);
|
||||
clock_gettime(Bottom_Trace_Timer_ID,&ts_bottom_begin);
|
||||
#endif
|
||||
if((fd = read_request(csock,buf,bufsize)) > 0){
|
||||
retval = send_response(csock,fd,buf,bufsize);
|
||||
@ -310,12 +324,10 @@ int main(int argc, const char *argv[]){
|
||||
if(close(csock) < 0)
|
||||
perror("csock close error");
|
||||
#ifdef USE_TRACE
|
||||
clock_gettime(Trace_Timer_ID,&tsend);
|
||||
struct timespec tophalf = timespec_sub(tsmiddle,ts);
|
||||
struct timespec bottomhelf = timespec_sub(tsend,tsmiddle);
|
||||
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);
|
||||
clock_gettime(Bottom_Trace_Timer_ID,&ts_bottom_end);
|
||||
struct timespec tophalf = timespec_sub(ts_top_end,ts_top_begin);
|
||||
struct timespec bottomhelf = timespec_sub(ts_bottom_end,ts_bottom_end);
|
||||
fprintf(stderr,"top: %ld ns, bottom: %ld ns\n",tophalf.tv_nsec,bottomhelf.tv_nsec);
|
||||
#endif
|
||||
free(buf);
|
||||
return retval;
|
||||
|
180
server.c
180
server.c
@ -16,7 +16,7 @@
|
||||
|
||||
#include "socket_wrapper.h"
|
||||
#include "simple_circular_buffer.h"
|
||||
#include "timerhelper.h"
|
||||
#include "display_bar.h"
|
||||
|
||||
#ifndef DEFAULT_MAX_LISTEN_SOCKET
|
||||
static const int MAX_LISTEN_SOCKET = 16;
|
||||
@ -60,8 +60,35 @@ enum{
|
||||
MAX_THREAD_NUMBER = DEFAULT_MAX_THREAD_NUMBER
|
||||
#endif
|
||||
};
|
||||
#define USE_TRACE
|
||||
#define USE_NO_QUEUE
|
||||
#ifdef SLOW_SERVER
|
||||
//micro second unit
|
||||
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
|
||||
@ -80,11 +107,11 @@ int send_fail(int sock,const char * msg){
|
||||
res.error_msg_size = strlen(msg);
|
||||
//os will be combining if tcp_autocorking emabled.
|
||||
if(send(sock,&res,sizeof(res),0) < 0){
|
||||
perror("error msg send");
|
||||
server_perror("error msg send");
|
||||
return -1;
|
||||
}
|
||||
if (send(sock,msg,res.error_msg_size,0) < 0){
|
||||
perror("error msg send");
|
||||
server_perror("error msg send");
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
@ -100,7 +127,7 @@ int send_errno(int sock){
|
||||
r.file_size = 0;
|
||||
r.error_msg_size = 0;
|
||||
if(send(sock,&r,sizeof(r),0)){
|
||||
perror("errno send");
|
||||
server_perror("errno send");
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
@ -115,7 +142,7 @@ int read_request(int sock,uint8_t * buf,size_t bufsize){
|
||||
ssize_t n = recv_until_byte(sock,&p,sizeof(p),TIMEOUT);
|
||||
if (n < 0){
|
||||
if (n == -2) fprintf(stderr,"timeout!");
|
||||
else perror("receive fail");
|
||||
else server_perror("receive fail");
|
||||
return -1;
|
||||
}
|
||||
if(bufsize <= ((size_t)p.file_url_size) + sizeof(p) + 1){
|
||||
@ -132,7 +159,13 @@ 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
|
||||
fprintf(stdout,"str size: %d, request %s\n",p.file_url_size,buf);
|
||||
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);
|
||||
if(strchr((char *)buf,'/') != NULL){
|
||||
send_fail(sock,"Illegal character /");
|
||||
return -1;
|
||||
@ -154,6 +187,7 @@ int send_response(int sock,int fd, uint8_t * buf, size_t bufsize){
|
||||
struct stat st;
|
||||
off_t offset = 0;
|
||||
ssize_t readed = 0;
|
||||
progress_bar_t pbar;
|
||||
r.res = RES_OK;
|
||||
r.err_number = 0;
|
||||
r.error_msg_size = 0;
|
||||
@ -165,33 +199,46 @@ int send_response(int sock,int fd, uint8_t * buf, size_t bufsize){
|
||||
}
|
||||
r.file_size = st.st_size;
|
||||
if(send(sock,&r,sizeof(r),0)<0){
|
||||
perror("send fail");
|
||||
server_perror("send fail");
|
||||
return -1;
|
||||
}
|
||||
if(use_gui)
|
||||
init_progress_bar(&pbar,10);
|
||||
#ifdef USE_SENDFILE
|
||||
while (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){
|
||||
perror("send file fail");
|
||||
server_perror("send file fail");
|
||||
return -1;
|
||||
}
|
||||
if(use_gui)
|
||||
DisplayProgressBar(&pbar,offset,r.file_size,"",false);
|
||||
#ifdef SLOW_SERVER
|
||||
usleep(SLOW_SERVER_TIME);
|
||||
#endif
|
||||
}
|
||||
#else
|
||||
while (offset < r.file_size)
|
||||
{
|
||||
readed = bufsize < (r.file_size - offset) ? bufsize : r.file_size - offset;
|
||||
if(read(fd,buf,readed)<0){
|
||||
perror("send response read fail");
|
||||
server_perror("send response read fail");
|
||||
return -1;
|
||||
}
|
||||
if(send(sock,buf,readed,0)<0){
|
||||
perror("send response send fail");
|
||||
server_perror("send response send fail");
|
||||
return -1;
|
||||
}
|
||||
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
|
||||
return 0;
|
||||
}
|
||||
@ -199,7 +246,8 @@ const char * help(const char * n){
|
||||
const char * msg = "USASE : %s [Option] ...\n"
|
||||
"Options and arguments: \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);
|
||||
return msg;
|
||||
}
|
||||
@ -207,11 +255,9 @@ const char * help(const char * n){
|
||||
int parse_args(int argc,const char * argv[] , in_port_t * port){
|
||||
int pos = 1;
|
||||
const char * opt;
|
||||
while (pos < argc)
|
||||
{
|
||||
while (pos < argc){
|
||||
opt = argv[pos++];
|
||||
if (strcmp(opt,"-h") == 0 || strcmp(opt,"--help") == 0)
|
||||
{
|
||||
if (strcmp(opt,"-h") == 0 || strcmp(opt,"--help") == 0){
|
||||
help(argv[0]);
|
||||
return 0;
|
||||
}
|
||||
@ -229,18 +275,20 @@ int parse_args(int argc,const char * argv[] , in_port_t * port){
|
||||
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;
|
||||
}
|
||||
//============
|
||||
//Simple Thread Pool
|
||||
//============
|
||||
#ifdef USE_TRACE
|
||||
enum{
|
||||
Trace_Timer_ID = CLOCK_REALTIME
|
||||
};
|
||||
#endif
|
||||
|
||||
#ifndef USE_NO_QUEUE
|
||||
typedef struct SharedState{
|
||||
//empty if less than 0
|
||||
@ -305,34 +353,39 @@ void * worker_proc(void * data){
|
||||
worker_argument_t * args = (worker_argument_t *)data;
|
||||
int fd, csock;
|
||||
#ifdef USE_TRACE
|
||||
struct timespec ts,ts_middle,ts_end;
|
||||
struct timespec ts_top_begin,ts_top_end, ts_bottom_begin, ts_bottom_end;
|
||||
#endif
|
||||
for(;;){
|
||||
|
||||
pthread_mutex_lock(&globalState.sock_mutex);
|
||||
while (queue_isempty(&globalState.socks)){
|
||||
pthread_cond_wait(&globalState.ready,&globalState.sock_mutex);
|
||||
}
|
||||
csock = dequeue(&globalState.socks);
|
||||
#ifdef USE_TRACE
|
||||
ts = dequeue(&globalState.trace_timer);
|
||||
ts_top_begin = dequeue(&globalState.trace_timer);
|
||||
#endif
|
||||
pthread_mutex_unlock(&globalState.sock_mutex);
|
||||
#ifdef USE_TRACE
|
||||
clock_gettime(Trace_Timer_ID,&ts_middle);
|
||||
clock_gettime(Top_Trace_Timer_ID,&ts_top_end);
|
||||
clock_gettime(Bottom_Trace_Timer_ID,&ts_bottom_begin);
|
||||
#endif
|
||||
if((fd = read_request(csock,args->buf,args->bufsize)) > 0){
|
||||
send_response(csock,fd,args->buf,args->bufsize);
|
||||
close(fd);
|
||||
}
|
||||
#ifdef USE_TRACE
|
||||
clock_gettime(Trace_Timer_ID,&ts_end);
|
||||
struct timespec tophalf = timespec_sub(ts_middle,ts);
|
||||
struct timespec bottomhelf = timespec_sub(ts_end,ts_middle);
|
||||
fprintf(stderr,"top : %ld ns, bottom : %ld ns\n",tophalf.tv_nsec,bottomhelf.tv_nsec);
|
||||
clock_gettime(Bottom_Trace_Timer_ID,&ts_bottom_end);
|
||||
struct timespec tophalf = timespec_sub(ts_top_end,ts_top_end);
|
||||
struct timespec bottomhelf = 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,bottomhelf.tv_nsec);
|
||||
unlock_scrolled();
|
||||
}
|
||||
#endif
|
||||
if(close(csock) < 0)
|
||||
perror("csock close error");
|
||||
server_perror("csock close error");
|
||||
}
|
||||
destory_worker_argument(args);
|
||||
return NULL;
|
||||
@ -344,22 +397,29 @@ void * worker_proc(void * data){
|
||||
int fd, csock;
|
||||
csock = args->csock;
|
||||
#ifdef USE_TRACE
|
||||
struct timespec ts,ts_middle,ts_end;
|
||||
ts = args->ts;
|
||||
clock_gettime(Trace_Timer_ID,&ts_middle);
|
||||
struct timespec ts_top_begin,ts_top_end, ts_bottom_begin, ts_bottom_end;
|
||||
ts_top_begin = args->ts;
|
||||
clock_gettime(Top_Trace_Timer_ID,&ts_top_end);
|
||||
clock_gettime(Bottom_Trace_Timer_ID,&ts_bottom_begin);
|
||||
#endif
|
||||
if((fd = read_request(csock,args->buf,args->bufsize)) > 0){
|
||||
send_response(csock,fd,args->buf,args->bufsize);
|
||||
close(fd);
|
||||
}
|
||||
#ifdef USE_TRACE
|
||||
clock_gettime(Trace_Timer_ID,&ts_end);
|
||||
struct timespec tophalf = timespec_sub(ts_middle,ts);
|
||||
struct timespec bottomhelf = timespec_sub(ts_end,ts_middle);
|
||||
fprintf(stderr,"top : %ld ns, bottom : %ld ns\n",tophalf.tv_nsec,bottomhelf.tv_nsec);
|
||||
clock_gettime(Bottom_Trace_Timer_ID,&ts_bottom_end);
|
||||
struct timespec tophalf = timespec_sub(ts_top_end,ts_top_begin);
|
||||
struct timespec bottomhelf = 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,bottomhelf.tv_nsec);
|
||||
unlock_scrolled();
|
||||
}
|
||||
else fprintf(stderr,"top : %ld ns, bottom : %ld ns\n",tophalf.tv_nsec,bottomhelf.tv_nsec);
|
||||
#endif
|
||||
if(close(csock) < 0)
|
||||
perror("csock close error");
|
||||
server_perror("csock close error");
|
||||
destory_worker_argument(args);
|
||||
return NULL;
|
||||
}
|
||||
@ -382,17 +442,21 @@ int main(int argc, const char *argv[]){
|
||||
int d = parse_args(argc,argv,&binding_port_number);
|
||||
if(d != 0 ) return d;
|
||||
}
|
||||
|
||||
if(use_gui)
|
||||
ready_progress_bar();
|
||||
#ifdef USE_TRACE
|
||||
report_resolution();
|
||||
#endif
|
||||
sock = socket(AF_INET,SOCK_STREAM,0);
|
||||
atexit(safe_exit);
|
||||
if(sock < 0){
|
||||
perror("sock create fail");
|
||||
server_perror("sock create fail");
|
||||
return 1;
|
||||
}
|
||||
else {
|
||||
int option = 1;
|
||||
if(setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,&option,sizeof(option)) < 0){
|
||||
perror("setsockopt");
|
||||
server_perror("setsockopt");
|
||||
}
|
||||
}
|
||||
bufsize = getBufferSizeFrom(sock);
|
||||
@ -412,17 +476,23 @@ int main(int argc, const char *argv[]){
|
||||
addr.sin_port = htons(binding_port_number);
|
||||
|
||||
if(bind(sock, (struct sockaddr *)&addr,sizeof(addr)) < 0){
|
||||
perror("bind failed");
|
||||
server_perror("bind failed");
|
||||
return 1;
|
||||
} else {
|
||||
char ip_buf[INET_ADDRSTRLEN];
|
||||
const char * msg = inet_ntop(AF_INET,&addr.sin_addr,ip_buf,sizeof(ip_buf));
|
||||
assert(msg != NULL);
|
||||
fprintf(stdout,"server bind on %s:%d\n",msg ,binding_port_number);
|
||||
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);
|
||||
}
|
||||
|
||||
if(listen(sock,MAX_LISTEN_SOCKET) < 0){
|
||||
perror("listen failed");
|
||||
server_perror("listen failed");
|
||||
return 1;
|
||||
}
|
||||
|
||||
@ -430,10 +500,16 @@ 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));
|
||||
fprintf(stdout,"Connected on : %s:%d\n",msg == NULL ? "(null)" : msg , ntohs(addr.sin_port));
|
||||
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));
|
||||
#ifdef USE_TRACE
|
||||
struct timespec ts;
|
||||
clock_gettime(Trace_Timer_ID, &ts);
|
||||
struct timespec ts_top_begin;
|
||||
clock_gettime(Top_Trace_Timer_ID, &ts_top_begin);
|
||||
#endif
|
||||
#ifndef USE_NO_QUEUE
|
||||
for(;;){
|
||||
@ -450,7 +526,7 @@ int main(int argc, const char *argv[]){
|
||||
else {
|
||||
enqueue(&globalState.socks,csock);
|
||||
#ifdef USE_TRACE
|
||||
enqueue(&globalState.trace_timer,ts);
|
||||
enqueue(&globalState.trace_timer,ts_top_begin);
|
||||
#endif
|
||||
}
|
||||
break;
|
||||
@ -461,7 +537,7 @@ int main(int argc, const char *argv[]){
|
||||
pthread_t thread_a;
|
||||
worker_argument_t * args = create_worker_argument(i++,bufsize,csock);
|
||||
#ifdef USE_TRACE
|
||||
args->ts = ts;
|
||||
args->ts = ts_top_begin;
|
||||
#endif
|
||||
pthread_create(&thread_a,NULL,worker_proc,args);
|
||||
pthread_detach(thread_a);
|
||||
|
Loading…
Reference in New Issue
Block a user