| | |
| | | socket_t sock; |
| | | int epollfd; |
| | | int connfd; |
| | | int nfds; |
| | | struct epoll_event event_array[MAX_EVENTS]; |
| | | |
| | | char buf[1024]; |
| | |
| | | |
| | | log_nrml("TLV server program start running\n"); |
| | | |
| | | /* set open file description number to max */ |
| | | set_socket_rlimit(); |
| | | |
| | | /* initial listen socket */ |
| | | if( socket_listen(&sock, DEF_LISTEN_IP, port) < 0 ) |
| | | { |
| | | log_err("create listen socket failure\n"); |
| | | return -2; |
| | | } |
| | | |
| | | /* create epoll and put listen socket into it */ |
| | | if( (epollfd=epoll_init(MAX_EVENTS, sock.fd)) < 0 ) |
| | | { |
| | | log_err("initial epoll for listen socket failure\n"); |
| | |
| | | /* g_signal.stop defined in proc.c, and will be set when catch stop signal */ |
| | | while( !g_signal.stop ) |
| | | { |
| | | /* program will blocked here */ |
| | | rv = epoll_wait(epollfd, event_array, MAX_EVENTS, -1/* never timeout */); |
| | | if( rv < 0 ) |
| | | /* epoll wailt and program will blocked here */ |
| | | nfds = epoll_wait(epollfd, event_array, MAX_EVENTS, -1 /* never timeout */); |
| | | if( nfds <= 0 ) |
| | | { |
| | | log_err("epoll failure: %s\n", strerror(errno)); |
| | | continue; |
| | | } |
| | | else if( rv == 0) |
| | | { |
| | | log_err("epoll get timeout\n"); |
| | | log_err("epoll_wait failure or timeout: %s\n", strerror(errno)); |
| | | continue; |
| | | } |
| | | |
| | | /* rv>0 is the active events count */ |
| | | for(i=0; i<rv; i++) |
| | | /* nfds>0 is the active events count */ |
| | | for(i=0; i<nfds; i++) |
| | | { |
| | | /* epoll get error */ |
| | | if ( (event_array[i].events&EPOLLERR) || (event_array[i].events&EPOLLHUP) ) |
| | | { |
| | | log_err("epoll_wait get error on fd[%d]: %s\n", event_array[i].data.fd, strerror(errno)); |
| | | epoll_del(epollfd, event_array[i].data.fd); |
| | | close(event_array[i].data.fd); |
| | | continue; |
| | | } |
| | | |
| | | /* listen socket get event means new client start connect now */ |
| | | if( event_array[i].data.fd == sock.fd ) |
| | | { |
| | | struct epoll_event event; |
| | | /* accept new client socket */ |
| | | if( (connfd=accept(sock.fd, (struct sockaddr *)NULL, NULL)) < 0) |
| | | { |
| | | log_nrml("accept new client failure: %s\n", strerror(errno)); |
| | |
| | | } |
| | | log_info("accept new client socket[%d]\n", connfd); |
| | | |
| | | event.data.fd = connfd; |
| | | event.events = EPOLLIN; |
| | | epoll_ctl(epollfd, EPOLL_CTL_ADD, connfd, &event); |
| | | |
| | | #if 0 |
| | | /* add new client socket into epoll */ |
| | | if( epoll_add(epollfd, connfd) < 0 ) |
| | | { |
| | | close(connfd); |
| | | log_err("epoll add client socket failure, rv=%d\n", rv); |
| | | } |
| | | log_dbg("epoll add new client socket[%d] ok.\n", connfd); |
| | | #endif |
| | | } |
| | | /* already connected client socket get data arrive */ |
| | | else |
| | | { |
| | | rv=read(event_array[i].data.fd, buf, sizeof(buf)); |
| | |
| | | log_dbg("socket[%d] receive %d bytes data\n", event_array[i].data.fd, rv); |
| | | logger_dump(LOG_LEVEL_INFO, buf, rv); |
| | | } |
| | | } |
| | | } |
| | | |
| | | } |
| | | } |
| | | sleep(1); |
| | | } |
| | | |
| | | logger_term(); |