-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathServer.hpp
83 lines (70 loc) · 2.5 KB
/
Server.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Server.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ychen2 <ychen2@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/02 22:00:02 by ychen2 #+# #+# */
/* Updated: 2024/09/09 18:55:12 by ychen2 ### ########.fr */
/* */
/* ************************************************************************** */
#pragma once
#include "MiddleStages.hpp"
#include "Settings.hpp"
#include "State.hpp"
#include <algorithm>
#include <cstdio>
#include <exception>
#include <iostream>
#include <poll.h>
#include <unistd.h>
#include <utility>
#include <vector>
#include <list>
#ifdef __APPLE__
#include <fcntl.h>
#endif
#define BUFFER_SIZE 2
#define BACK_LOG 32
#define MAX_EVENTS 16
#define CGI_TIMEOUT 2
#define EVENT_TIMEOUT 100
#define SERVER_TIMEOUT 500
class Server {
public:
// Constructer, it creates the non-blocking socket connection, listen to the
// ip/port from settings calling socket, setsockopt, bind, listen
// put socket fds into pfs
Server(std::vector<Settings> &servers, char **env);
~Server();
// getters
std::vector<struct pollfd>::iterator getNextPfdsEnd();
char **get_env();
// member methods
void close_conn(std::list<State>::iterator &cur_state);
void new_conns(int sock_fd);
void add_to_poll_in(int fd);
void add_to_poll_out(int fd);
std::vector<struct pollfd>::iterator find_it_in_nxt(int fd);
void getServerConfig(State &);
void remove_from_poll(int fd);
// Start waiting for events
// calling poll, accept, recv, send
void run();
private:
char **_env;
static bool _constructed;
std::vector<int> _socks_fd;
std::vector<struct pollfd> _cur_poll_fds;
std::vector<struct pollfd> _next_poll_fds;
std::vector<Settings> &_settings;
std::list<State> _states;
// funcs
bool is_socket(int fd);
std::list<State>::iterator getState(int fd);
void run_a_server(
std::vector<Settings>::iterator &); // socket, setsockopt, bind, listen
void checkTimeoutCGI();
void checkTimeoutConn();
};