int read_files(t_file *files, char *dirname, t_waiting **waiting, t_command *command) { t_file *tmp; t_column *clength; tmp = files; if (!(clength = set_columns_length(files, command, 0))) return (0); while (tmp) { if (!read_file(tmp, command, dirname, clength)) { delete_waiting(waiting); delete_files(&files); return (-1); } push_waiting(&tmp, dirname, waiting, command); tmp = tmp->next; } ft_memdel((void**)&clength); return (1); }
void run() { long time, delta; int ret, cli_idx, cli; nfds_t nfds; if (allocate_mem() < 0) { free_mem(); return; } cli_cnt = 0; nfds = 2; fd_all[0].fd = pcap_fd; fd_all[0].events = POLLIN; fd_all[1].fd = listenfd; fd_all[1].events = POLLIN; for (cli = 0; cli < max_client; ++cli) avai_no[cli] = max_client - cli - 1; avai_cnt = max_client; result.item[max_query - 1].next = -1; for (ret = max_query - 2; ret >= 0; --ret) result.item[ret].next = ret + 1; result.avai = 0; waiting.item[max_query - 1].next = -1; for (ret = max_query - 2; ret >= 0; --ret) waiting.item[ret].next = ret + 1; waiting.avai = 0; waiting.head = -1; for (cli = 0; cli < max_client; ++cli) { in_buffer[cli].len = 0; in_buffer[cli].newline = in_buffer[cli].buf; out_buffer[cli].head = 0; out_buffer[cli].tail = 0; out_buffer[cli].tot = 0; pending[cli].in = -1; pending[cli].out = -1; } fprintf(stderr, "max_client: %d, max_query: %d. Exceeded will be rejected.\n", max_client, max_query); time = -1; while (running) { pop_waiting(); if (time == -1) delta = 1000; else delta = time - gettime(); while (delta >= 0) { for (cli_idx = 0; cli_idx < cli_cnt; ++cli_idx) { cli = cli_no[cli_idx]; push_waiting(cli); if ((fd_cli[cli_idx].events & POLLIN) == 0 && in_buffer[cli].len != GUK_MAX_QUERY_LEN) fd_cli[cli_idx].events |= POLLIN; } if ((ret = poll(fd_all, nfds, delta + 1)) > 0) { if (fd_all[0].revents == POLLIN) gk_cm_read_cap(); ret = (fd_all[1].revents == POLLIN); // ret here stand for new connection available nfds -= 2; for (cli_idx = 0; cli_idx < cli_cnt; ++cli_idx) { if (fd_cli[cli_idx].revents & (POLLERR | POLLNVAL | POLLHUP)) { fprintf(stderr, "Connection closed or broken."); close_client(cli_idx); --nfds; continue; } cli = cli_no[cli_idx]; if (fd_cli[cli_idx].revents & POLLOUT) { do { pop_result(cli); } while (try_write(cli)); if (all_written(cli)) fd_cli[cli_idx_of[cli]].events &= ~POLLOUT; last_act[cli] = gettime(); } if (fd_cli[cli_idx].revents & POLLIN) { while (try_read(cli)) { push_waiting(cli); } if (in_buffer[cli].len == GUK_MAX_QUERY_LEN) fd_cli[cli_idx].events &= ~POLLIN; last_act[cli] = gettime(); } else if (ret && is_idle(cli) && gettime() - last_act[cli] >= (GUK_SERV_TIMEOUT * 1000)) { fprintf(stderr, "Client timeout. "); close_client(cli_idx); --nfds; } } /* remove closed clients */ for (cli_idx = 0; cli_cnt > (int)nfds; ) { while (cli_idx < (int)nfds && fd_cli[cli_idx].fd >= 0) ++cli_idx; if (cli_idx == (int)nfds) { cli_cnt = cli_idx; break; } else { while (fd_cli[--cli_cnt].fd < 0); memcpy(fd_cli + cli_idx, fd_cli + cli_cnt, sizeof(struct pollfd)); cli_idx_of[(cli_no[cli_idx] = cli_no[cli_cnt])] = cli_idx; } } nfds += 2; if (ret) while (cli_cnt < max_client && accept_client() == 0) ++nfds; } else if (ret < 0) perror("poll"); time = gk_cm_conn_next_time(); if (time == -1) delta = 1000; else delta = time - gettime(); } time = gk_cm_conn_step(); } gk_cm_finalize(); for (cli_idx = 0; cli_idx < cli_cnt; ++cli_idx) { cli = cli_no[cli_idx]; do { pop_result(cli); } while (try_write(cli)); } for (cli_idx = 0; cli_idx < cli_cnt; ++cli_idx) close_client(cli_idx); free_mem(); }