void wcb_recv(EV_P_ ev_io *w, int tev) { client_t *c = aux_memberof(client_t, wev_recv, w); char buf [4096]; int nb = aux_unix_recv(w->fd, buf, 4096); double time = ev_now(loop) - c->ts; times_sum += time; times_count++; if (times_max < time) times_max = time; if (times_min > time) times_min = time; /* fprintf(stderr, "recv fd=%d %f (%d bytes) %.*s\n", w->fd, ev_now(loop) - c->ts, nb, nb, buf); */ /* if (nb != 57) fprintf(stderr, "recv error %d bytes\n", nb); */ if (0 > nb) { if (errno == EAGAIN) { return; } fprintf(stderr, "recv error fd=%d (%d: %s)\n", w->fd, errno, strerror(errno)); client_del(c); return; } }
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); } } } } }
int client_send(client_t *c) { int nb = aux_unix_send(c->wev_recv.fd, REQUEST, sizeof(REQUEST) - 1); c->ts = ev_now(loop); /* fprintf(stderr, "send fd=%d\n", c->wev_recv.fd); */ if (0 > nb) { fprintf(stderr, "send error fd=%d (%d: %s)\n", c->wev_recv.fd, errno, strerror(errno)); client_del(c); return -1; } return 0; }
void client_disconnect(struct TClient* client) { close( client->m_sock ); if ( client->m_remain != 0 ) { --g_clients.m_blocked; } if ( client->m_ipid != -1 ) { kill( client->m_ipid, p_chldterm ); } if ( client->m_opid != -1 ) { kill( client->m_opid, p_chldterm ); } client_del( client ); }
void wcb_connect(EV_P_ ev_io *w, int tev) { client_t *c = aux_memberof(client_t, wev_connect, w); if (EV_READ & tev) { fprintf(stderr, "conn error fd=%d\n", w->fd); client_del(c); return; } #if 0 int rc = client_send(c); if (0 > rc) return; #endif c->to_send = 1; ev_io_stop(loop, &c->wev_connect); ev_io_start(loop, &c->wev_send); ev_io_start(loop, &c->wev_recv); }
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); } } } } }
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); } } } } }