13 cout <<
"Destroyed master socket\n";
29 _fd = socket(AF_INET, SOCK_STREAM, 0);
31 throw std::runtime_error(
"socket() error" +
string(strerror(errno)));
32 if (setsockopt(
_fd, SOL_SOCKET, SO_REUSEADDR, (
char *)&x,
sizeof(x)) < 0 && close(
_fd) <= 0)
33 throw std::runtime_error(
"setsockopt() error: " +
string(strerror(errno)));
36 _address.sin_addr.s_addr = inet_addr(ip.c_str());
40 throw std::runtime_error(
"bind() error: " +
string(strerror(errno)));
41 if (listen(
_fd, 3) < 0 && close(
_fd) <= 0)
42 throw std::runtime_error(
"listen() error: " +
string(strerror(errno)));
43 cout <<
"New master socket with fd " <<
_fd <<
" which listen " << ip <<
":" << port <<
"\n";
52 for (std::vector<Client *>::iterator child =
_childs.begin(); child <
_childs.end(); child++) {
71 int new_socket = accept(
_fd, (
struct sockaddr *)&
_address, (socklen_t *)&addrlen);
73 throw std::runtime_error(
"accept() error:" +
string(strerror(errno)));
75 fcntl(new_socket, F_SETFL, O_NONBLOCK);
82 for (std::vector<Client *>::iterator it =
_childs.begin(); it <
_childs.end(); it++) {
83 child_fd = (*it)->_fd;
85 valread = read(child_fd, buffer, 128);
86 buffer[valread] =
'\0';
88 getpeername(child_fd, (
struct sockaddr *)&
_address, (socklen_t *)&addrlen);
92 }
else if ((*it)->getRequest(env, buffer))
93 (*it)->handleRequest();
114 std::vector<Server *> exact, inrange;
118 for (std::vector<Server *>::iterator server = env->
_servers.begin(); server < env->_servers.end(); server++) {
119 std::vector<ip_port_t> serv_listens = (*server)->_listens;
120 for (std::vector<ip_port_t>::iterator it = serv_listens.begin(); it < serv_listens.end(); it++) {
124 exact.push_back(*server);
127 bool is_inrange =
true;
128 ip_listen =
split((*it).ip,
".");
129 vec_string::iterator r = ip_required.begin();
130 for (vec_string::iterator l = ip_listen.end(); l >= ip_listen.begin(); --l) {
131 if (*l != *r && *l !=
"0")
135 inrange.push_back(*server);
140 if (exact.size() == 0) {
141 std::cout <<
"in range server check\n";
142 for (std::vector<Server *>::iterator server = inrange.begin(); server < inrange.end(); server++) {
143 if (host == (*server)->getName())
146 return inrange.front();
148 std::cout <<
"exact server check\n";
149 for (std::vector<Server *>::iterator server = exact.begin(); server < exact.end(); server++) {
150 if (host == (*server)->getName())
153 return exact.front();
std::vector< Server * > _servers
static int _amount
< The higher one
struct sockaddr_in _address
void post_select(Env *env)
static int _max_fd
< The lower socket fd
Server * choose_server(Env *env, string host)
static int _min_fd
< The sockets fd which will be select
std::vector< Client * > _childs
std::vector< string > vec_string