// set signal to np static void net_set_sighandler(struct net_pool* np) { // add signal handler int status = socketpair(PF_UNIX, SOCK_STREAM, 0, sigpipefd); assert(status != -1); poll_setnonblocking(sigpipefd[1]); struct net_socket* nstmp = net_socket_new(np, sigpipefd[0], true); nstmp->status = SOCKET_STATUS_SIGNAL; addsig(SIGTERM); addsig(SIGINT); }
int run_child(int idx , client_data* users , char* share_mem) { epoll_event events[MAX_EVENT_NUMBER]; int child_epollfd = epoll_create(5); assert(child_epollfd != -1); int connfd = users[idx].connfd; addfd(child_epollfd , connfd); int pipefd = users[idx].pipefd[1]; addfd(child_epollfd , pipefd); int ret; addsig(SIGTERM , child_term_handler , false); while(!stop_child){ int number = epoll_wait(child_epollfd , events , MAX_EVENT_NUMBER , -1); if((number < 0) &&(errno != EINTR)){ printf("epoll failure\n"); break; } for(int i = 0 ; i < number ; i++){ int sockfd = events[i].data.fd; if((sockfd == connfd) &&(events[i].events & EPOLLIN)){ memset(share_mem + idx*BUFFER_SIZE , '\0' , BUFFER_SIZE); ret = recv(connfd , share_mem + idx*BUFFER_SIZE , BUFFER_SIZE-1 , 0); if(ret < 0){ if(errno != EAGAIN){ stop_child = true; } }else if(ret == 0){ stop_child = true; }else{ send(pipefd , (char*)&idx , sizeof(idx) , 0); } } //主进程通过管道将第client个客户的数据发送到本进程负责的客户端 else if((sockfd == pipefd) && (events[i].events & EPOLLIN)){ int client = 0; ret = recv(sockfd , (char*)&client , sizeof(client) , 0); if(ret < 0){ if(errno != EAGAIN){ stop_child = true; } }else if(ret == 0){ stop_child = true; }else{ send(connfd , share_mem + client*BUFFER_SIZE , BUFFER_SIZE , 0); } } } close(connfd); close(pipefd); close(child_epollfd); return 0; } }
int main( int argc, char* argv[] ) { if( argc <= 2 ) { printf( "usage: %s ip_address port_number\n", basename( argv[0] ) ); return 1; } const char* ip = argv[1]; int port = atoi( argv[2] ); struct sockaddr_in address; bzero( &address, sizeof( address ) ); address.sin_family = AF_INET; inet_pton( AF_INET, ip, &address.sin_addr ); address.sin_port = htons( port ); int sock = socket( PF_INET, SOCK_STREAM, 0 ); assert( sock >= 0 ); int ret = bind( sock, ( struct sockaddr* )&address, sizeof( address ) ); assert( ret != -1 ); ret = listen( sock, 5 ); assert( ret != -1 ); struct sockaddr_in client; socklen_t client_addrlength = sizeof( client ); connfd = accept( sock, ( struct sockaddr* )&client, &client_addrlength ); if ( connfd < 0 ) { printf( "errno is: %d\n", errno ); } else { addsig( SIGURG, sig_urg ); fcntl( connfd, F_SETOWN, getpid() ); char buffer[ BUF_SIZE ]; while( 1 ) { memset( buffer, '\0', BUF_SIZE ); ret = recv( connfd, buffer, BUF_SIZE-1, 0 ); if( ret <= 0 ) { break; } printf( "got %d bytes of normal data '%s'\n", ret, buffer ); } close( connfd ); } close( sock ); return 0; }
event_handler_t* create_signal_accepter(reactor_t* r,int sig) { event_handler_t* eh = malloc(sizeof(event_handler_t)); int ret = socketpair( PF_UNIX, SOCK_STREAM, 0, pipefd ); assert( ret != -1 ); eh->fd = pipefd[0]; eh->reactor = r; eh->handle_event = &handle_signal_event; addsig( sig ); return eh; }
int main( int argc, char* argv[] ) { if( argc <= 2 ) { printf( "usage: %s ip_address port_number\n", basename( argv[0] ) ); return 1; } const char* ip = argv[1]; int port = atoi( argv[2] ); int ret = 0; struct sockaddr_in address; bzero( &address, sizeof( address ) ); address.sin_family = AF_INET; inet_pton( AF_INET, ip, &address.sin_addr ); address.sin_port = htons( port ); listenfd = socket( PF_INET, SOCK_STREAM, 0 ); assert( listenfd >= 0 ); ret = bind( listenfd, ( struct sockaddr* )&address, sizeof( address ) ); assert( ret != -1 ); ret = listen( listenfd, 5 ); assert( ret != -1 ); for( int i = 0; i < PROCESS_COUNT; ++i ) { ret = socketpair( PF_UNIX, SOCK_STREAM, 0, sub_process[i].pipefd ); assert( ret != -1 ); sub_process[i].pid = fork(); if( sub_process[i].pid < 0 ) { continue; } else if( sub_process[i].pid > 0 ) { close( sub_process[i].pipefd[1] ); setnonblocking( sub_process[i].pipefd[0] ); continue; } else { close( sub_process[i].pipefd[0] ); setnonblocking( sub_process[i].pipefd[1] ); run_child( i ); exit( 0 ); } } epoll_event events[ MAX_EVENT_NUMBER ]; epollfd = epoll_create( 5 ); assert( epollfd != -1 ); addfd( epollfd, listenfd ); ret = socketpair( PF_UNIX, SOCK_STREAM, 0, sig_pipefd ); assert( ret != -1 ); setnonblocking( sig_pipefd[1] ); addfd( epollfd, sig_pipefd[0] ); addsig( SIGCHLD, sig_handler ); addsig( SIGTERM, sig_handler ); addsig( SIGINT, sig_handler ); addsig( SIGPIPE, SIG_IGN ); bool stop_server = false; int sub_process_counter = 0; while( !stop_server ) { int number = epoll_wait( epollfd, events, MAX_EVENT_NUMBER, -1 ); if ( ( number < 0 ) && ( errno != EINTR ) ) { printf( "epoll failure\n" ); break; } for ( int i = 0; i < number; i++ ) { int sockfd = events[i].data.fd; if( sockfd == listenfd ) { int new_conn = 1; send( sub_process[sub_process_counter++].pipefd[0], ( char* )&new_conn, sizeof( new_conn ), 0 ); printf( "send request to child %d\n", sub_process_counter-1 ); sub_process_counter %= PROCESS_COUNT; } else if( ( sockfd == sig_pipefd[0] ) && ( events[i].events & EPOLLIN ) ) { int sig; char signals[1024]; ret = recv( sig_pipefd[0], signals, sizeof( signals ), 0 ); if( ret == -1 ) { continue; } else if( ret == 0 ) { continue; } else { for( int i = 0; i < ret; ++i ) { switch( signals[i] ) { case SIGCHLD: { pid_t pid; int stat; while ( ( pid = waitpid( -1, &stat, WNOHANG ) ) > 0 ) { for( int i = 0; i < PROCESS_COUNT; ++i ) { if( sub_process[i].pid == pid ) { close( sub_process[i].pipefd[0] ); sub_process[i].pid = -1; } } } stop_server = true; for( int i = 0; i < PROCESS_COUNT; ++i ) { if( sub_process[i].pid != -1 ) { stop_server = false; } } break; } case SIGTERM: case SIGINT: { printf( "kill all the clild now\n" ); for( int i = 0; i < PROCESS_COUNT; ++i ) { int pid = sub_process[i].pid; kill( pid, SIGTERM ); } break; } default: { break; } } } } } else { continue; } } } del_resource(); return 0; }
int run_child( int idx ) { epoll_event events[ MAX_EVENT_NUMBER ]; int child_epollfd = epoll_create( 5 ); assert( child_epollfd != -1 ); int pipefd = sub_process[idx].pipefd[1]; addfd( child_epollfd, pipefd ); int ret; addsig( SIGTERM, child_term_handler, false ); addsig( SIGCHLD, child_child_handler ); client_data* users = new client_data[ USER_PER_PROCESS ]; while( !stop_child ) { int number = epoll_wait( child_epollfd, events, MAX_EVENT_NUMBER, -1 ); if ( ( number < 0 ) && ( errno != EINTR ) ) { printf( "epoll failure\n" ); break; } for ( int i = 0; i < number; i++ ) { int sockfd = events[i].data.fd; if( ( sockfd == pipefd ) && ( events[i].events & EPOLLIN ) ) { int client = 0; ret = recv( sockfd, ( char* )&client, sizeof( client ), 0 ); if( ret < 0 ) { if( errno != EAGAIN ) { stop_child = true; } } else if( ret == 0 ) { stop_child = true; } else { struct sockaddr_in client_address; socklen_t client_addrlength = sizeof( client_address ); int connfd = accept( listenfd, ( struct sockaddr* )&client_address, &client_addrlength ); if ( connfd < 0 ) { printf( "errno is: %d\n", errno ); continue; } memset( users[connfd].buf, '\0', BUFFER_SIZE ); users[connfd].address = client_address; users[connfd].read_idx = 0; addfd( child_epollfd, connfd ); } } else if( events[i].events & EPOLLIN ) { int idx = 0; while( true ) { idx = users[sockfd].read_idx; ret = recv( sockfd, users[sockfd].buf + idx, BUFFER_SIZE-1-idx, 0 ); if( ret < 0 ) { if( errno != EAGAIN ) { epoll_ctl( child_epollfd, EPOLL_CTL_DEL, sockfd, 0 ); close( sockfd ); } break; } else if( ret == 0 ) { epoll_ctl( child_epollfd, EPOLL_CTL_DEL, sockfd, 0 ); close( sockfd ); break; } else { users[sockfd].read_idx += ret; printf( "user content is: %s\n", users[sockfd].buf ); idx = users[sockfd].read_idx; if( ( idx < 2 ) || ( users[sockfd].buf[idx-2] != '\r' ) || ( users[sockfd].buf[idx-1] != '\n' ) ) { continue; } users[sockfd].buf[users[sockfd].read_idx-2] = '\0'; char* file_name = users[sockfd].buf; if( access( file_name, F_OK ) == -1 ) { epoll_ctl( child_epollfd, EPOLL_CTL_DEL, sockfd, 0 ); close( sockfd ); break; } ret = fork(); if( ret == -1 ) { epoll_ctl( child_epollfd, EPOLL_CTL_DEL, sockfd, 0 ); close( sockfd ); break; } else if( ret > 0 ) { epoll_ctl( child_epollfd, EPOLL_CTL_DEL, sockfd, 0 ); close( sockfd ); break; } else { close( STDOUT_FILENO ); dup( sockfd ); execl( users[sockfd].buf, users[sockfd].buf, 0 ); exit( 0 ); } } } } else { continue; } } } delete [] users; close( pipefd ); close( child_epollfd ); return 0; }
/*Here we could deal with some signal handler*/ void hand_sig() { addsig( SIGPIPE, SIG_IGN ,false); addsig(SIGINT,cpu_time,false); }
int main(int argc, char *argv[]){ if(argc <= 2){ printf("usage: %s ip_address port_number\n", basename(argv[0])); return 1; } const char* ip = argv[1]; int port = atoi(argv[2]); struct sockaddr_in address; bzero(&address, sizeof(address)); address.sin_family = AF_INET; inet_pton(AF_INET, ip, &address.sin_addr); address.sin_port = htons(port); int listenfd = socket(PF_INET, SOCK_STREAM, 0); assert(listenfd >= 0); int ret = bind(listenfd, (struct sockaddr*)&address, sizeof(address)); assert(ret != -1); ret = listen(listenfd, 5); assert(ret != -1); epoll_event events[MAX_EVENT_NUMBER]; int epollfd = epoll_create(5); assert(epollfd != 0); addfd(epollfd, listenfd); ret = socketpair(PF_UNIX, SOCK_STREAM, 0, pipefd); assert(ret != -1); setnonblocking(pipefd[1]); addfd(epollfd, pipefd[0]); /*设置信号处理函数*/ addsig(SIGALARM); addsig(SIGTERM); bool stop_server = false; client_data* users = new client_data[FD_LIMIT]; bool timeout = false; alarm(TIMESLOT); /*定时*/ while(!stop_server){ int number = epoll_wait(epollfd, events, MAX_EVENT_NUMBER, -1); if(number < 0 && errno != EINTR){ printf("epoll failed\n"); break; } for(int i = 0; i < number; i++){ int sockfd = events[i].data.fd; if(sockfd == listenfd){ struct sockaddr_in client_address; socklen_t client_addrlength = sizeof(client_address); int connfd = accept(sockfd, (struct sockaddr*)&client_address, &client_addrlength); addfd(epollfd, connfd); users[connfd].address = client_address; users[connfd].sockfd = connfd; util_timer* timer = new util_timer; timer->client_data = users[connfd]; timer->cb_func = cb_func; time_t cur = time(NULL); timer->expire = cur + 3*TIMESLOT; users[connfd].timer = timer; timer_lst.add_timer(timer); } else if((sockfd == pipefd[0]) && (events[i].events & EPOLLIN)){ char signals[1024]; memset(signals, '\0', sizeof(signals)); ret = recv(pipefd[0], signals, sizeof(signals), 0); if(ret < 0){ //handle the error continue; } else if(ret == 0){ continue; } else { for(int i = 0; i < ret; i++){ switch (signals[i]) case SIGALARM: /*关闭长时间不响应的连接*/ timeout = true;//不立即处理定时任务 break; case SIGTERM: stop_server = true; } } } else if(events[i].events & EPOLLIN){ memset(users[sockfd].buf, '\0', BUFFER_SIZE); ret = recv(sockfd, users[sockfd].buf, BUFFER_SIZE - 1, 0); util_timer* timer = users[sockfd].timer; if(ret < 0){ if(errno != EAGAIN){ /*发生错误,则关闭连接,并移除定时器*/ cb_func(&users[sockfd]); if(timer){ timer_lst.del_timer(timer); } } } else if(ret == 0){ /*对方已关闭连接,则关闭连接,并移除定时器*/ cb_func(&users[sockfd]); if(timer){ timer_lst.del_timer(timer); } } else { printf("get %d bytes of client data %s from %d\n", ret, users[sockfd].buf, sockfd); /*有数据,则需要调整定时器,以延迟该连接被关闭的时间*/ if(timer){ time_t cur = time(NULL); timer->expire = cur + 3*TIMESLOT; printf("adjust timer once\n"); timer_lst.adjust_timer(timer); } } } else { //others } } /*最后处理定时事件*/ if(timeout){ timer_handler(); timeout = false; } } close(listenfd); close(pipefd[0]); close(pipefd[1]); delete[] users; return 0; }
int main(int argc ,char* argv[]) { if(argc <= 2) { printf("usage : %s ip_address port_number \n",argv[0]); return 1; } const char* ip = argv[1]; int port = atoi(argv[2]); int ret =0; struct sockaddr_in address; bzero(&address,sizeof(address)); address.sin_family = AF_INET; inet_pton(AF_INET,ip,&address.sin_addr); address.sin_port = htons(port); listenfd = socket(PF_INET,SOCK_STREAM,0); assert(listenfd >= 0); ret = bind(listenfd,(struct sockaddr*) &address,sizeof(address)); assert(ret != -1); ret = listen(listenfd,5); assert(ret != -1); user_count = 0; users = (client_data *) malloc(sizeof(client_data)*(USER_LIMIT+1)); sub_process = (int *)malloc(sizeof(int)*(PROCESS_LIMIT+1)); int i = 0; for(; i<PROCESS_LIMIT;++i) { sub_process[i] = -1; } struct epoll_event events[MAX_EVENT_NUMBER]; epollfd = epoll_create(5); assert(epollfd != -1); addfd(epollfd,listenfd); ret = socketpair(PF_UNIX,SOCK_STREAM,0,sig_pipefd); assert(ret != -1); setnonblocking(sig_pipefd[1]); addfd(epollfd,sig_pipefd[0]); addsig(SIGCHLD,sig_handler,true); addsig(SIGTERM,sig_handler,true); addsig(SIGINT,sig_handler,true); addsig(SIGPIPE,SIG_IGN,true); bool stop_server = false; bool terminate = false; shmfd = shm_open(shm_name,O_CREAT|O_RDWR,0666); assert(shmfd != -1); ret = ftruncate(shmfd,USER_LIMIT*BUFFER_SIZE); assert(ret != -1); share_mem = (char*) mmap(NULL,USER_LIMIT*BUFFER_SIZE,PROT_READ|PROT_WRITE,MAP_SHARED,shmfd,0); assert(share_mem != MAP_FAILED); close(shmfd); while(!stop_server) { int number = epoll_wait(epollfd,events,MAX_EVENT_NUMBER,-1); if((number <0) && (errno != EINTR)) { printf("epoll failure\n"); break; } int i = 0; for(;i<number;i++) { int sockfd = events[i].data.fd; // 处理客户端链接请求 if(sockfd == listenfd) { struct sockaddr_in client_address; socklen_t client_addrlength = sizeof(client_address); int connfd = accept(listenfd,(struct sockaddr*)&client_address,&client_addrlength); if(connfd < 0) { printf("errno is :%d\n",errno); continue; } if(user_count >= USER_LIMIT) { const char* info ="too many users\n"; printf("%s",info); send(connfd,info,strlen(info),0); close(connfd); continue; } users[user_count].address = client_address; users[user_count].connfd = connfd; ret = socketpair(PF_UNIX,SOCK_STREAM,0,users[user_count].pipefd); assert(ret != -1); pid_t pid = fork(); if(pid <0) { close(connfd); continue; } else if(pid == 0) { close(epollfd); close(listenfd); close(users[user_count].pipefd[0]); close(sig_pipefd[0]); close(sig_pipefd[1]); run_child(user_count,users,share_mem); munmap((void*)share_mem,USER_LIMIT*BUFFER_SIZE); exit(0); } else { close(connfd); close(users[user_count].pipefd[1]); addfd(epollfd,users[user_count].pipefd[0]); users[user_count].pid = pid; sub_process[pid]= user_count; user_count ++; } } // 处理信号事件 else if( sockfd == sig_pipefd[0] && (events[i].events & EPOLLIN)) { int sig; char signals[1024]; ret = recv(sig_pipefd[0],signals,sizeof(signals),0); if( ret == -1) { continue; } else if ( ret == 0) { continue; } else { int i=0; for ( ;i< ret; ++i) { switch(signals[i]) { case SIGCHLD: { pid_t pid; int stat; while(( pid = waitpid(-1,&stat,WNOHANG)) > 0) { /* 用子进程pid获取关闭的客户端的编号 * */ int del_user = sub_process[pid]; sub_process[pid] = -1; if((del_user < 0) || (del_user> USER_LIMIT)) { continue; } //清除此客户链接使用的相关数据,更新user和sub_process数组 epoll_ctl(epollfd,EPOLL_CTL_DEL,users[del_user].pipefd[0],0); close(users[del_user].pipefd[0]); users[del_user]= users[--user_count]; sub_process[users[del_user].pid] = del_user; } if(terminate && user_count == 0) { stop_server = true; } break; } case SIGTERM: case SIGINT: { printf("kill all the child now \n"); if(user_count ==0) { stop_server = true; break; } int i=0; for(;i<user_count;++i) { int pid = users[i].pid; kill(pid,SIGTERM); } terminate = true; break; } default: { break; } } } } } else if(events[i].events & EPOLLIN) { int child = 0; ret = recv(sockfd,(char*)&child,sizeof(child),0); printf("read data from child accross pipe\n"); if(ret == -1) { continue; } else if (ret ==0) { continue; } else { int j =0; for( ;j<user_count;++j) { if(users[j].pipefd[0] != sockfd) { printf("send data to child accross pipe\n"); send(users[j].pipefd[0],(char*)&child,sizeof(child),0); } } } } } } del_resource(); return 0; }
int main(int argc,char* argv[]){ const char* ip="127.0.0.1"; int port=atoi("13000"); //init read data char* file="./ml-1m/rating.dat"; //rat.dat为5万小样本 rating.dat为中等样本,ratings.dat为大样本,大样本运行载入太慢 //忽略SIGPIPE信号 addsig(SIGPIPE,SIG_IGN); int M=8; if(!readData(file)){ printf("file error\n"); return 1; } int rands=random(M); SplitData(M,rands); UserSimilarity(); //创建线程池 threadpool<UserIIF>* pool; try{ pool=new threadpool<UserIIF>(); }catch(...){ return 1; } /*预先为每个可能的客户连接分配一个http_conn对象*/ UserIIF* users=new UserIIF[MAX_FD]; users->testRes(10); //上一句会输出准确率,召回率,覆盖率,新颖度 assert(users); int user_count=0; int listenfd=socket(AF_INET,SOCK_STREAM,0); assert(listenfd>=0); struct linger tmp={1,0}; setsockopt(listenfd,SOL_SOCKET,SO_LINGER,&tmp,sizeof(tmp)); int ret=0; struct sockaddr_in address; bzero(&address,sizeof(address)); address.sin_family=AF_INET; inet_pton(AF_INET,ip,&address.sin_addr); address.sin_port=htons(port); ret=bind(listenfd,(struct sockaddr*)&address,sizeof(address)); assert(ret>=0); ret=listen(listenfd,5); assert(ret>=0); epoll_event events[MAX_EVENT_NUMBER]; int epollfd=epoll_create(5); assert(epollfd!=-1); UserIIF::m_epollfd=epollfd; addfd(epollfd,listenfd,false); while(true){ int number=epoll_wait(epollfd,events,MAX_EVENT_NUMBER,-1); if ((number<0)&&(errno!=EINTR)) { printf("epoll failure\n"); break; } for (int i = 0; i < number; i++) { int sockfd=events[i].data.fd; if (sockfd==listenfd) { struct sockaddr_in client_address; socklen_t client_addrlen=sizeof(client_address); int connfd=accept(listenfd,(struct sockaddr*)&client_address,&client_addrlen); if (connfd<0) { printf("errno is:%d\n",errno ); continue; } users[connfd].init(connfd,true);//debug }else if((events[i].events&EPOLLRDHUP)||(events[i].events&EPOLLERR)||(events[i].events&EPOLLRDHUP)){ //有一场,直接关闭客户连接 printf("%d stop\n",sockfd); users[sockfd].close(); }else if(events[i].events&EPOLLIN){ //根据读的结果,决定时将任务添加到线程池,还是关闭连接 if (users[sockfd].readfrom()==true) { pool->append(users+sockfd); }else{ users[sockfd].close(); } }else if(events[i].events&EPOLLOUT){ //根据写的结果,决定是否关闭连接 if (!users[sockfd].writeto()) { users[sockfd].close(); } }else{} } } close(epollfd); close(listenfd); delete [] users; delete pool; return 0; }
int main(int argc, char *argv[]) { bool stop_server = false; client_data *users; bool timeout = false; int epollfd; epoll_event events[MAX_EVENT_NUMBER]; int listenfd; int ret = 0; struct sockaddr_in address; const char *ip; int port; if (argc < 2) { printf("usage: ./main ip_address port_number"); return 1; } ip = argv[1]; port = atoi(argv[2]); bzero(&address, sizeof(address)); address.sin_family = AF_INET; inet_pton(AF_INET, ip, &address.sin_addr); address.sin_port = htons(port); listenfd = socket(PF_INET, SOCK_STREAM, 0); assert(listenfd >= 0); ret = bind(listenfd, (struct sockaddr *)&address, sizeof(address)); assert(ret != -1); ret = listen(listenfd, 5); assert(ret != -1); epollfd = epoll_create(5); assert(epollfd != -1); addfd(epollfd, listenfd); ret = socketpair(PF_UNIX, SOCK_STREAM, 0, pipefd); assert(ret != -1); setnonblocking(pipefd[1]); addfd(epollfd, pipefd[0]); addsig(SIGALRM); addsig(SIGTERM); users = new client_data[FD_LIMIT]; alarm(TIMESLOT); while (!stop_server) { int number = epoll_wait(epollfd, events, MAX_EVENT_NUMBER, -1); if ((number < 0) && (errno != EINTR)) { printf("epoll failure\n"); break; } for (int i=0; i<number; ++i) { int sockfd = events[i].data.fd; if (sockfd == listenfd) { struct sockaddr_in client_address; socklen_t client_addlength = sizeof(client_address); int connfd = accept(listenfd, (struct sockaddr *)&client_address, &client_addlength); addfd(epollfd, connfd); users[connfd].address = client_address; users[connfd].sockfd = connfd; util_timer *timer = new util_timer; timer->user_data = &users[connfd]; timer->cb_func = cb_func; time_t cur = time(NULL); timer->expire = cur + TIME_INACTIVE; users[connfd].timer = timer; timer_lst.add_timer(timer); } else if ((sockfd == pipefd[0]) && (events[i]).events & EPOLLIN) { char signals[1024]; ret = recv(pipefd[0], signals, sizeof(signals), 0); if (ret == -1) continue; else if (ret == 0) continue; else { for (int i=0; i<ret; ++i) { switch (signals[i]) { case SIGALRM: timeout = true; break; case SIGTERM: stop_server = true; } } } } else if (events[i].events & EPOLLIN) { util_timer *timer = users[sockfd].timer; memset(users[sockfd].buf, '\0', BUFFER_SIZE); ret = recv(sockfd, users[sockfd].buf, BUFFER_SIZE, 0); if (ret < 0) { if (errno != EAGAIN) { cb_func(&users[sockfd]); if (timer) timer_lst.del_timer(timer); } } else { users[sockfd].buf[ret-1] = '\0'; printf("get %d bytes of client data %s from %d\n", ret, users[sockfd].buf, sockfd); if (timer) { time_t cur = time(NULL); timer->expire = cur + TIME_INACTIVE; timer_lst.adjust_timer(timer); } } } } if (timeout) { timer_handler(); timeout = false; } } close(listenfd); close(pipefd[0]); close(pipefd[1]); delete []users; return 0; }
int main( int argc, char* argv[] ) { if( argc <= 2 ) { printf( "usage: %s ip_address port_number\n", basename( argv[0] ) ); return 1; } const char* ip = argv[1]; int port = atoi( argv[2] ); addsig( SIGPIPE, SIG_IGN ); threadpool< http_conn >* pool = NULL; try { pool = new threadpool< http_conn >; } catch( ... ) { return 1; } http_conn* users = new http_conn[ MAX_FD ]; assert( users ); int user_count = 0; int listenfd = socket( PF_INET, SOCK_STREAM, 0 ); assert( listenfd >= 0 ); struct linger tmp = { 1, 0 }; setsockopt( listenfd, SOL_SOCKET, SO_LINGER, &tmp, sizeof( tmp ) ); int ret = 0; struct sockaddr_in address; bzero( &address, sizeof( address ) ); address.sin_family = AF_INET; inet_pton( AF_INET, ip, &address.sin_addr ); address.sin_port = htons( port ); ret = bind( listenfd, ( struct sockaddr* )&address, sizeof( address ) ); assert( ret >= 0 ); ret = listen( listenfd, 5 ); assert( ret >= 0 ); epoll_event events[ MAX_EVENT_NUMBER ]; int epollfd = epoll_create( 5 ); assert( epollfd != -1 ); addfd( epollfd, listenfd, false ); http_conn::m_epollfd = epollfd; while( true ) { int number = epoll_wait( epollfd, events, MAX_EVENT_NUMBER, -1 ); if ( ( number < 0 ) && ( errno != EINTR ) ) { printf( "epoll failure\n" ); break; } for ( int i = 0; i < number; i++ ) { int sockfd = events[i].data.fd; if( sockfd == listenfd ) { struct sockaddr_in client_address; socklen_t client_addrlength = sizeof( client_address ); int connfd = accept( listenfd, ( struct sockaddr* )&client_address, &client_addrlength ); if ( connfd < 0 ) { printf( "errno is: %d\n", errno ); continue; } if( http_conn::m_user_count >= MAX_FD ) { show_error( connfd, "Internal server busy" ); continue; } users[connfd].init( connfd, client_address ); } else if( events[i].events & ( EPOLLRDHUP | EPOLLHUP | EPOLLERR ) ) { users[sockfd].close_conn(); } else if( events[i].events & EPOLLIN ) { if( users[sockfd].read() ) { pool->append( users + sockfd ); } else { users[sockfd].close_conn(); } } else if( events[i].events & EPOLLOUT ) { if( !users[sockfd].write() ) { users[sockfd].close_conn(); } } else {} } } close( epollfd ); close( listenfd ); delete [] users; delete pool; return 0; }
int main() { int port = atoi("2500"); int ret = 0; struct sockaddr_in address; //bzero(&address, sizeof(address)); memset(&address, 0, sizeof(address)); address.sin_family = AF_INET; address.sin_port = htons(port); address.sin_addr.s_addr = htonl(INADDR_ANY); int listenfd = socket(AF_INET, SOCK_STREAM, 0); assert(listenfd>=0); ret = bind( listenfd, (struct sockaddr*)&address, sizeof(address)); assert( ret != -1 ); ret = listen( listenfd, 5 ); assert( ret != -1 ); epoll_events events[MAX_EVENT_NUMER]; int epollfd = epoll_create(5); assert(epollfd != -1); addfd( epollfd, listenfd); /**/ ret = socketpair(AF_UNIX, SOCK_STREAM, 0, pipefd); assert( ret != -1 ); setnonblocking( pipefd[1]); addfd( epollfd, pipefd[0]); addsig( SIGALRM ); addsig( SIGTERM ); bool stop_server = false; client_data* users = new client_data[FD_LIMIT]; bool timeout = false; alarm(TIMESLOT); while( !stop_server ) { int number = epoll_wait( epollfd, events, MAX_EVENT_NUMER, -1 ); if( (number<0) && (errno != EINTR)) { printf("epoll failure\n"); break; } for( int i=0; i<number; i++) { int sockfd = events[i].data.fd; if( sockfd == listenfd ) { struct sockaddr_in client_address; socklen_t client_addrlen = sizeof( client_addrlen); int connfd = accept( listenfd, (struct sockaddr*) &client_address, &client_addrlen); addfd( epollfd, connfd ); users[connfd].address = client_address; users[connfd].sockfd = connfd; util_timer* timer = new util_timer; timer->user_data = &users[connfd]; timer->cb_func = cb_func; time_t cur = time(NULL); timer->expire = cur + 3*TIMESLOT; users[connfd].timer = timer; timer_list.add_timer(timer); } else if( (sockfd == pipefd[0]) && (events[i].events & EPOLLIN)) { int sig; char signals[1024]; ret = recv( pipefd[0], signals, sizeof(signals), 0); if( ret == -1 || ret == 0 ) { continue; } else { for(int i=0; i<ret; i++) { switch( signals[i]) { case SIGALRM: { timeout = true; break; } case SIGHUP: { continue; } case SIGTERM: { stop_server = true; } case SIGINT: { printf("recv SIGINT\n"); stop_server = true; } } } } } else if( events[i].events & EPOLLIN ) { memset( users[sockfd].buf, '\0', BUFFER_SIZE ); ret = recv( sockfd, users[sockfd].buf, BUFFER_SIZE-1, 0); printf("get %d bytes of client data %s from %d\n", ret, users[sockfd].buf, sockfd); util_timer* timer = users[sockfd].timer; if( ret<0 ) { if( errno != EAGAIN ) { cb_func( &users[sockfd]); if( timer ) { timer_list.del_timer( timer ); } } } else if( ret == 0 ) { cb_func( &users[sockfd]); if(timer) { timer_list.del_timer( timer ); } } else { if( timer ) { time_t cur = time(NULL); timer->expire = cur + 3*TIMESLOT; printf("adjust timer once\n"); timer_list.adjust_timer( timer ); } } if( timeout ) { timer_handler(); timeout = false; } } } } close(listenfd); close(epollfd); close(pipefd[0]); close(pipefd[1]); delete [] users; return 0; }
int main(int argc,char * argv[]){ if (argc <= 2){ printf("usage : ip port\n"); // return 1; } const char * ip = argv[1]; int port = atoi(argv[2]); struct sockaddr_in address; memset(&address,0,sizeof(address)); //address.sin_family = AF_INET; //inet_pton(AF_INET,ip,&address.sin_addr); //address.sin_port = htons(port); address.sin_family=AF_INET; address.sin_addr.s_addr=INADDR_ANY; address.sin_port=htons(8000); int listenfd = socket(PF_INET,SOCK_STREAM,0); assert(listenfd >= 0); int ret = bind(listenfd,(struct sockaddr * )&address,sizeof(address)); if (ret == -1){ perrmsg(errno); return 1; } ret = listen(listenfd,5); assert(ret != -1); struct epoll_event events[MAX_EVENT_NUMBER]; int epollfd = epoll_create(5); assert (epollfd != -1); addfd(epollfd,listenfd); ret = socketpair(PF_UNIX,SOCK_STREAM,0,pipefd); assert (ret != -1); setnoblocking(pipefd[1]); addfd(epollfd,pipefd[0]); addsig(SIGHUP); addsig(SIGCHLD); addsig(SIGTERM); addsig(SIGINT); int stop_server = 0; while(!stop_server){ int number = epoll_wait(epollfd,events,MAX_EVENT_NUMBER,-1); if ((number != 0) && ( errno != EINTR)){ printf ("epoll failed\n"); strerror(errno); } int i=0; for ( i; i< number; i++){ int sockfd = events[i].data.fd; if (sockfd = listenfd){ struct sockaddr_in client_address; socklen_t client_lenth = sizeof(client_address); int connfd = accept(listenfd,(struct sockaddr *)&client_address,&client_lenth); addfd(epollfd,connfd); } else if ((sockfd == pipefd[0]) && (events[i].events & EPOLLIN)){ int sig; char signals[1024]; ret = recv(pipefd[0],signals,sizeof(signals),0); if (ret == -1){ continue; } else if(ret == 0){ continue; } else{ int i; for ( i=0;i<ret;++i){ printf("sig no occured is %d",signals[i]); if (signals[i] == SIGINT){ stop_server = 1; } } } } } } printf("close fds\n"); close(listenfd); close(pipefd[0]); close(pipefd[1]); return 0; }