Lines Matching refs:srvst

32 	bool (*cb)(struct server_state *srvst, size_t idx);
36 static bool server_io_cb(struct server_state *srvst, size_t idx) in server_io_cb() argument
38 short revents = srvst->fds[idx].revents; in server_io_cb()
39 short *events = &srvst->fds[idx].events; in server_io_cb()
40 struct sock_io_cb *cb = srvst->cb; in server_io_cb()
43 fd = srvst->fds[idx].fd; in server_io_cb()
65 srvst->fds[idx].fd = -1; in server_io_cb()
69 static bool server_add_state(struct server_state *srvst, in server_add_state() argument
70 bool (*cb)(struct server_state *srvst, size_t idx), in server_add_state() argument
77 for (n = 0; n < srvst->nfds; n++) { in server_add_state()
78 if (srvst->fds[n].fd == -1) { in server_add_state()
79 srvst->socks[n].cb = cb; in server_add_state()
80 srvst->socks[n].serv = serv; in server_add_state()
81 srvst->fds[n].fd = fd; in server_add_state()
82 srvst->fds[n].events = poll_events; in server_add_state()
83 srvst->fds[n].revents = 0; in server_add_state()
88 p = realloc(srvst->socks, sizeof(*srvst->socks) * (srvst->nfds + 1)); in server_add_state()
91 srvst->socks = p; in server_add_state()
92 srvst->socks[srvst->nfds].cb = cb; in server_add_state()
93 srvst->socks[srvst->nfds].serv = serv; in server_add_state()
95 p = realloc(srvst->fds, sizeof(*srvst->fds) * (srvst->nfds + 1)); in server_add_state()
98 srvst->fds = p; in server_add_state()
99 srvst->fds[srvst->nfds].fd = fd; in server_add_state()
100 srvst->fds[srvst->nfds].events = poll_events; in server_add_state()
101 srvst->fds[srvst->nfds].revents = 0; in server_add_state()
103 srvst->nfds++; in server_add_state()
107 static bool tcp_server_accept_cb(struct server_state *srvst, size_t idx) in tcp_server_accept_cb() argument
109 short revents = srvst->fds[idx].revents; in tcp_server_accept_cb()
119 fd = accept(srvst->fds[idx].fd, sa, &len); in tcp_server_accept_cb()
127 if (srvst->cb->accept && in tcp_server_accept_cb()
128 !srvst->cb->accept(srvst->cb->ptr, fd, &io_events)) { in tcp_server_accept_cb()
134 return server_add_state(srvst, server_io_cb, srvst->socks[idx].serv, in tcp_server_accept_cb()
138 static bool udp_server_cb(struct server_state *srvst, size_t idx) in udp_server_cb() argument
140 short revents = srvst->fds[idx].revents; in udp_server_cb()
145 return srvst->cb->accept(srvst->cb->ptr, srvst->fds[idx].fd, NULL); in udp_server_cb()
148 static bool server_quit_cb(struct server_state *srvst, size_t idx) in server_quit_cb() argument
151 srvst->got_quit = true; in server_quit_cb()
156 bool (*cb)(struct server_state *srvst, size_t idx)) in sock_server() argument
158 struct server_state srvst = { .cb = ts->cb }; in sock_server() local
166 if (!server_add_state(&srvst, cb, ts->bind + n, in sock_server()
171 if (!server_add_state(&srvst, server_quit_cb, NULL, in sock_server()
182 pres = poll(srvst.fds, srvst.nfds, -1); in sock_server()
187 for (n = 0; pres && n < srvst.nfds; n++) { in sock_server()
188 if (srvst.fds[n].revents) { in sock_server()
190 if (!srvst.socks[n].cb(&srvst, n)) in sock_server()
195 if (srvst.got_quit) in sock_server()
202 for (n = 0; n < srvst.nfds; n++) { in sock_server()
204 if (srvst.fds[n].fd != -1 && srvst.socks[n].serv && in sock_server()
205 srvst.fds[n].fd != srvst.socks[n].serv->fd) { in sock_server()
206 if (close(srvst.fds[n].fd)) in sock_server()
207 warn("sock_server: close(%d)", srvst.fds[n].fd); in sock_server()
210 free(srvst.socks); in sock_server()
211 free(srvst.fds); in sock_server()