void loop (void) { int i, n, maxfd, maxi, listenfd, clifd, nread; char buf[MAXLINE]; uid_t uid; fd_set rset, allset; FD_ZERO (&allset); /* obtain fd to listen for client requests on */ if ((listenfd = serv_listen (CS_OPEN)) < 0) { log_sys ("serv_listen error"); } FD_SET (listenfd, &allset); maxfd = listenfd; maxi = -1; for (;;) { rset = allset; /* rset gets modified each time around */ if ((n = select (maxfd + 1, &rset, NULL, NULL, NULL)) < 0) { log_sys ("select error"); } if (FD_ISSET (listenfd, &rset)) { /* accept new client request */ if ((clifd = serv_accept (listenfd, &uid)) < 0) { log_sys ("serv_accept error: %d", clifd); } i = client_add (clifd, uid); FD_SET (clifd, &allset); if (clifd > maxfd) { maxfd = clifd; /* max fd for select() */ } if (i > maxi) { maxi = i; /* max index in client[] array */ } log_msg ("new connection: uid %d, fd %d", uid, clifd); } for (i = 0; i <= maxi; i++) { /* go through client[] array */ if ((clifd = client[i].fd) < 0) { continue; } if (FD_ISSET (clifd, &rset)) { /* read argument buffer from client */ if ((nread = read (clifd, buf, MAXLINE)) < 0) { log_sys ("read error on fd %d", clifd); } else if (nread == 0) { log_msg ("closed: uid %d, fd %d", client[i].uid, clifd); client_del (clifd); /* client has closed cxn */ FD_CLR (clifd, &allset); close (clifd); } else { /* process client's request */ request (buf, nread, clifd, client[i].uid); } } } } }
void hmsg_monitor(void) { int i, maxi, listenfd, clifd, nread; char buf[MAXLINE]; uid_t uid; struct pollfd *pollfd; if ((pollfd = malloc(open_max() * sizeof(struct pollfd))) == NULL) dlog(L_ERR, "malloc error"); /* obtain fd to listen for client requests on */ if ((listenfd = msg_listen(MSG_PATH)) < 0) dlog(L_ERR, "msg_listen error"); client_add(listenfd, 0); /* we use [0] for listenfd */ pollfd[0].fd = listenfd; pollfd[0].events = POLLIN; maxi = 0; for ( ; ; ) { if (poll(pollfd, maxi + 1, -1) < 0) dlog(L_ERR, "poll error"); if (pollfd[0].revents & POLLIN) { /* accept new client request */ if ((clifd = serv_accept(listenfd, &uid)) < 0) dlog(L_ERR, "serv_accept error: %d", clifd); i = client_add(clifd, uid); pollfd[i].fd = clifd; pollfd[i].events = POLLIN; if (i > maxi) maxi = i; dlog(L_INFO, "new connection: uid %d, fd %d\n", uid, clifd); } for (i = 1; i <= maxi; i++) { if ((clifd = client[i].fd) < 0) continue; if (pollfd[i].revents & POLLHUP) { goto hungup; } else if (pollfd[i].revents & POLLIN) { /* read argument buffer from client */ if ((nread = read(clifd, buf, MAXLINE)) < 0) { dlog(L_ERR, "read error on fd %d\n", clifd); } else if (nread == 0) { hungup: dlog(L_INFO, "closed: uid %d, fd %d\n", client[i].uid, clifd); client_del(clifd); /* client has closed conn */ pollfd[i].fd = -1; close(clifd); } else { /* process client's request */ msg_parse_arg(buf, nread, clifd, client[i].uid); } } } } }
void loop(void) { int listenfd, clifd; int rval, i, nr; int maxi, maxfd; uid_t uid; fd_set rset, allset; char buf[MAXLINE]; /* obtain fd to listen for client request on */ if ((listenfd = serv_listen(CS_OPEN)) < 0) log_sys("serv_listen error"); FD_ZERO(&allset); FD_SET(listenfd, &allset); maxfd = listenfd; maxi = -1; for (;;) { rset = allset; /* rset get modified each time around */ rval = select(maxfd + 1, &rset, NULL, NULL, NULL); if (rval < 0) log_sys("select error"); if (FD_ISSET(listenfd, &rset)) { /* accept new client request */ if ((clifd = serv_accept(listenfd, &uid)) < 0) log_sys("serv_accept error"); i = client_add(clifd, uid); FD_SET(clifd, &allset); if (i > maxi) maxi = i; if (clifd > maxfd) maxfd = clifd; log_msg("new connection: uid %d, fd %d", uid, clifd); continue; } for (i = 0; i <= maxi; i++) { if (client[i].fd == -1) continue; if (FD_ISSET(client[i].fd, &rset)) { /* read argument buffer from client */ if ((nr = read(client[i].fd, buf, MAXLINE)) < 0) { log_sys("read error on fd %d", clifd); } else if (nr == 0) { log_msg("closed: uid %d, fd %d", client[i].uid, clifd); client_del(client[i].fd); /* client has closed cxn */ FD_CLR(clifd, &allset); close(clifd); } else { /* process client's request */ handler_request(buf, nr, clifd, client[i].uid); } } } } }
static int app_start(struct sec_db* sdb){ int fd,serve_fd; pthread_t tid; pthread_attr_t attr; if( (serve_fd = serv_listen(SERVICE)) < 0){ return -1; } while(1){ if( (fd = serv_accept(serve_fd,NULL)) < 0){ return -1; } if( init_pthread_attr_t(&attr)) return -1; if( pthread_create(&tid,&attr,app_do_request,(void*)&fd)) return -1; pthread_attr_destroy(&attr); }; }
void handle_control_connect(int fd, int code, void *data) { int nfd, ret, ioctl_cmd; control_state *state; nfd = serv_accept(fd); if(nfd < 0) { wack_alarm(PRINT, "error receiving wackatrl session"); return; } ioctl_cmd = 1; #ifdef WIN32 ret = ioctlsocket(nfd, FIONBIO, &ioctl_cmd); #else ret = ioctl(nfd, FIONBIO, &ioctl_cmd); #endif wack_alarm(WACK_DEBUG, "starting wackatrl session"); state = calloc(1, sizeof(control_state)); state->operation = READ_COMMAND; state->state = READ_FD; E_attach_fd(nfd, READ_FD, handle_control_session, 0, (void *)state, HIGH_PRIORITY); E_attach_fd(nfd, WRITE_FD, handle_control_session, 0, (void *)state, HIGH_PRIORITY); E_deactivate_fd(nfd, WRITE_FD); }
void *server_socket(void *a) { int n; uid_t uid; int listenfd, connfd; char buf[4096]; if((listenfd = serv_listen("/tmp/my_unix.socket")) < 0) { if(listenfd == -1) { perror("socket error"); exit(1); } if(listenfd == -2) { perror("bind error"); exit(1); } if(listenfd == -3) { perror("listen error"); exit(1); } } while(1) { conn_again: if((connfd = serv_accept(listenfd, &uid)) < 0) { if(connfd == -1) { int errsv = errno; /* EINTR为4,表示被信号中断,EAGAIN为11, 表示设置了O_NONBLOCK同时没有连接被请 求。两个宏定义在同一个文件 asm-generic/errno-base.h中 */ if((errsv == EINTR) || (errsv == ECONNABORTED)) { printf("serv_accept again...\n"); goto conn_again; } else { printf("errno = %d,", errsv); errno = errsv; perror("accept error"); exit(1); } } else if(connfd == -2) { perror("stat error"); exit(1); } else if(connfd == -3) { perror("it's not a real socket file"); exit(1); } } while(1) { if((n = read(connfd, buf, 4096)) == -1) { perror("read connfd error"); exit(1); } else if(n == 0) { //对端关闭 close(connfd); break; } else { if(((strcmp(buf,"getpic") == 0) && (strlen(buf) == 6)) || ((strcmp(buf,"flush") == 0) && (strlen(buf) == 5 ))) { ((MainForm *)a)->pic->valchanged = 1; } if((strcmp(buf,"getpic") != 0)) ((MainForm *)a)->message->setMsg(QString::fromUtf8(buf)); memset(buf, 0, sizeof(buf)); } } } }
void loop(void) { int i,n, maxfd,maxi, listenfd, clifd, nread; char buf[MAXLENLINE]; uid_t uid; fd_set rset, allset; FD_ZERO(&allset); if((listenfd = serv_listen(CS_OPEN))) { //log_sys("serv_listen error"); } FD_SET(listenfd, &allset); maxfd = listenfd; maxi = -1; for ( ; ; ) { rset = allset; if ((n=select(maxfd+1, &rset, NULL, NULL, NULL)) < 0) { //log_sys("select error"); } if (FD_ISSET(listenfd, &rset)) { if((clifd = serv_accept(listenfd, &uid)) < 0) { //log_sys("serv_accept error: %d", clifd); } i = client_add(clifd, uid); FD_SET(clifd, &allset); if (clifd > maxfd) { maxfd = clifd; } if (i > maxi) { maxi = i; } //log_msg("new connection: uid %d, fd %d", uid, clifd); } else { for (i = 0; i <= maxi; i++) { if ((clifd = client[i].fd) < 0) { continue; } if (FD_ISSET(clifd, &rset)) { if ((nread = read(clifd, buf, MAXLENLINE)) < 0) { //log_sys("read error on fd: %d",clifd); } else if(nread == 0) { //log_msg("closed: uid %d, fd %d",client[i].uid, clifd); client_del(clifd); FD_CLR(clifd,&allset); close(clifd); } else { handle_request(buf, nread, clifd, client[i].uid); } } } } } }
void loop2(void) { int i, listenfd, clifd, nread; char buf[MAXLENLINE]; uid_t uid; struct pollfd *pollfd = NULL; int numfd = 1; pollfd_wrap *pfd_wrap; struct pollfd *default_pfd; default_pfd->fd = -1; default_pfd->events = POLLIN; default_pfd->events = 0; pfd_wrap->pfd = default_pfd; pfd_wrap->maxfd = NALLOC; pollfd_init(pfd_wrap, default_pfd); pollfd = pfd_wrap->pfd; int maxfd = pfd_wrap->maxfd; if ((listenfd = serv_listen(CS_OPEN)) < 0) { //log_sys("serv_listen error"); } client_add(listenfd, 0); pollfd[0].fd = listenfd; for (;;) { if (poll(pollfd, numfd, -1) < 0) { //log_sys("poll error"); } if (pollfd[0].revents & POLLIN) { if ((clifd = serv_accept(listenfd, &uid)) < 0) { //log_sys("serv_accept error: %d", clifd); } client_add(clifd,uid); if (numfd == pfd_wrap->maxfd) { default_pfd->fd = -1; pollfd_alloc(pfd_wrap, default_pfd); } else { default_pfd->fd = clifd; pollfd_add(pfd_wrap, default_pfd); } pollfd = pfd_wrap->pfd; pollfd[numfd].fd = clifd; pollfd[numfd].events = POLLIN; pollfd[numfd].revents = 0; numfd++; //log_msg("new connection: uid %d, fd %d, uid, clifd"); } for (i = 1; i < numfd; i++) { if (pollfd[i].revents & POLLHUP) { goto hungup; } else if(pollfd[i].revents & POLLIN) { if ((nread = read(pollfd[i].fd, buf, MAXLENLINE)) < 0) { //log_sys("read error on fd %d",pollfd[i].fd); } else if(nread == 0) { hungup: //log_msg("closed: fd %d", pollfd[i].fd); client_del(pollfd[i].fd); close(pollfd[i].fd); //pack the pollfd //TODO there is a drawback, if you allocate //many pollfds, it cannot be released if you //needn't them; if (i < (numfd-1)) { pollfd[i].fd = pollfd[numfd-1].fd; pollfd[i].events = pollfd[numfd-1].events; pollfd[i].revents = pollfd[numfd-1].revents; i--; } numfd--; } else { handle_request(buf, nread, pollfd[i].fd,client[i].uid); } } } } }
/* * 侦听端口及处理请求 */ int SelectAndHandle( int listensockfd, int listenunixfd ) { unsigned long addrsize; fd_set rset,allset; int connectfd,maxfd=listensockfd>listenunixfd?listensockfd:listenunixfd; int i,n; uid_t uid; int nready; struct sockaddr cliaddr; char recvbuf[SIZERCV+1]; char *p; CLIENT client[FDSIZE]; addrsize = sizeof( struct sockaddr ); FD_ZERO( &allset ); FD_SET( listensockfd, &allset ); FD_SET( listenunixfd, &allset ); for( i=0; i<FDSIZE; i++ ) { client[i].fd = -1; client[i].i = i; } printf("%sSelectAndHandle\n", AT); /* 初始化线程池 */ thread_pool_t *pool = NULL; if( thread_pool_init(&pool, POOLSIZE) < 0 ) { err_sys( "thread pool init error" ); } n=1; while (1) { rset = allset; /* 接受新连接 */ if (( nready = select( maxfd + 1, &rset, NULL, NULL, NULL ) ) < 0 ) { fprintf(stderr,"%sselect err %d %s",AT, nready, strerror(errno) ); continue; } if (FD_ISSET( listensockfd, &rset )) { if (( connectfd = accept( listensockfd, &cliaddr, ( socklen_t *)&addrsize ) ) == -1 ) { fprintf(stderr, "%saccept err", AT ); sleep(1); continue; } SOCKAGAIN: for( i = 0; i<FDSIZE; i++ ) { if( client[i].fd < 0 ) { client[i].fd = connectfd; client[i].type = SOCK; client[i].addr = cliaddr; client[i].n = n; thread_pool_add_worker(pool, ( FUNC )HandleInSide, &(client[i]) ); n++; break; } } if (i==FDSIZE) { /* 线程池已满 */ fprintf(stderr,"%s,pool is full\n", AT); sleep(1); goto SOCKAGAIN; } if ( --nready <= 0 ) continue; } if( FD_ISSET( listenunixfd, &rset ) ) { #if defined(Darwin) if (( connectfd = serv_accept( listenunixfd, NULL ) ) < 0 ) #else if (( connectfd = serv_accept( listenunixfd, &uid ) ) < 0 ) #endif { fprintf(stderr, "%sserv_accept err %d %s\n", AT, connectfd, strerror(errno) ); sleep(1); continue; } UNIXAGAIN: for( i = 0; i<FDSIZE; i++ ) { if( client[i].fd < 0 ) { client[i].fd = connectfd; #if !defined(Darwin) client[i].uid = uid; #endif client[i].type = SOCK; thread_pool_add_worker(pool, ( FUNC )HandleOutSide, &(client[i]) ); n++; break; } } if (i==FDSIZE) { fprintf(stderr,"%s,pool is full\n", AT); sleep(1); goto UNIXAGAIN; } if ( --nready <= 0 ) continue; } } thread_pool_destroy(pool); pool = NULL; return EXIT_SUCCESS; }
void loop(void) { int i, listenfd, clifd, nread; char buf[MAXLINE]; uid_t uid; struct pollfd *pollfd; int numfd = 1; int maxfd = NALLOC; if ((pollfd = malloc(NALLOC * sizeof(struct pollfd))) == NULL) err_sys("malloc error"); for (i = 0; i < NALLOC; i++) { pollfd[i].fd = -1; pollfd[i].events = POLLIN; pollfd[i].revents = 0; } /* obtain fd to listen for client requests on */ if ((listenfd = serv_listen(CS_OPEN)) < 0) log_sys("serv_listen error"); client_add(listenfd, 0); /* we use [0] for listenfd */ pollfd[0].fd = listenfd; for ( ; ; ) { if (poll(pollfd, numfd, -1) < 0) log_sys("poll error"); if (pollfd[0].revents & POLLIN) { /* accept new client request */ if ((clifd = serv_accept(listenfd, &uid)) < 0) log_sys("serv_accept error: %d", clifd); client_add(clifd, uid); /* possibly increase the size of the pollfd array */ if (numfd == maxfd) pollfd = grow_pollfd(pollfd, &maxfd); pollfd[numfd].fd = clifd; pollfd[numfd].events = POLLIN; pollfd[numfd].revents = 0; numfd++; log_msg("new connection: uid %d, fd %d", uid, clifd); } for (i = 1; i < numfd; i++) { if (pollfd[i].revents & POLLHUP) { goto hungup; } else if (pollfd[i].revents & POLLIN) { /* read argument buffer from client */ if ((nread = read(pollfd[i].fd, buf, MAXLINE)) < 0) { log_sys("read error on fd %d", pollfd[i].fd); } else if (nread == 0) { hungup: /* the client closed the connection */ log_msg("closed: uid %d, fd %d", client[i].uid, pollfd[i].fd); client_del(pollfd[i].fd); close(pollfd[i].fd); if (i < (numfd-1)) { /* pack the array */ pollfd[i].fd = pollfd[numfd-1].fd; pollfd[i].events = pollfd[numfd-1].events; pollfd[i].revents = pollfd[numfd-1].revents; i--; /* recheck this entry */ } numfd--; } else { /* process client's request */ handle_request(buf, nread, pollfd[i].fd, client[i].uid); } } } } }