Skip to content

Commit

Permalink
add(server): add select for handle multiple I/O; tmp
Browse files Browse the repository at this point in the history
  • Loading branch information
ak0327 committed Aug 29, 2023
1 parent 7498736 commit 3a0a43d
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 20 deletions.
90 changes: 70 additions & 20 deletions srcs/Server/Server.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <netdb.h>
#include <errno.h>
#include <poll.h>
#include <sys/socket.h>
#include <unistd.h>
#include <cstdio>
Expand Down Expand Up @@ -29,32 +30,81 @@ Server::~Server() {

void Server::process_client_connection() {
// select
// todo
fd_set readfds, readfds_save;
int n, maxfds, next_maxfds;

// accept
Result<int, std::string> accept_result = accept_connection(this->_socket.get_socket_fd());
if (!accept_result.is_ok()) {
throw std::runtime_error("[Error] accept:" + accept_result.get_err_value());
}
this->_connect_fd = accept_result.get_ok_value();
FD_ZERO(&readfds_save);
FD_SET(this->_socket.get_socket_fd(), &readfds_save);
maxfds = next_maxfds = this->_socket.get_socket_fd() + 1;

while (true) { // todo: loop break condition
readfds = readfds_save;
errno = 0;
n = select(maxfds, &readfds, NULL, NULL, NULL); // todo
if (n <= 0) {
std::string err_str = "[Error] accept:" + std::string(strerror(errno));
throw std::runtime_error(err_str);
}
if (FD_ISSET(this->_socket.get_socket_fd(), &readfds)) {
FD_CLR(this->_socket.get_socket_fd(), &readfds);

std::cout << CYAN "[SERVER] (" << getpid() <<
") accept incoming connections (fd=" <<
this->_socket.get_socket_fd() << ")" RESET << std::endl;

// accept
Result<int, std::string> accept_result = accept_connection(this->_socket.get_socket_fd());
if (!accept_result.is_ok()) {
throw std::runtime_error("[Error] accept:" + accept_result.get_err_value());
}
this->_connect_fd = accept_result.get_ok_value();

FD_SET(this->_connect_fd, &readfds_save);
if (this->_connect_fd + 1 > maxfds) {
next_maxfds = this->_connect_fd + 1;
}
}

for (int fd = 0; fd < maxfds; ++fd) {
if (FD_ISSET(fd, &readfds)) {
// recv
Result<std::string, std::string>recv_result = recv_request(this->_connect_fd);
if (!recv_result.is_ok()) {
throw std::runtime_error("[Error] recv:" + recv_result.get_err_value());
}
this->_recv_message = recv_result.get_err_value();
// std::cout << YELLOW "recv_msg(string):[" << this->_recv_message << "]" RESET << std::endl;

// request, response
HttpRequest request = HttpRequest(this->_recv_message);
HttpResponse response = HttpResponse(request);

// send
Result<int, std::string> send_result = send_response(this->_connect_fd, response);
if (!send_result.is_ok()) {
throw std::runtime_error("[Error] send:" + send_result.get_err_value());
}

// recv
Result<std::string, std::string>recv_result = recv_request(this->_connect_fd);
if (!recv_result.is_ok()) {
throw std::runtime_error("[Error] recv:" + recv_result.get_err_value());
close_connection(fd);
FD_CLR(fd, &readfds_save);
if (maxfds == fd + 1) {
next_maxfds = find_maxfds(&readfds_save);
}
}
}
maxfds = next_maxfds;
}
this->_recv_message = recv_result.get_err_value();
// std::cout << YELLOW "recv_msg(string):[" << this->_recv_message << "]" RESET << std::endl;
}

// request, response
HttpRequest request = HttpRequest(this->_recv_message);
HttpResponse response = HttpResponse(request);
int Server::find_maxfds(fd_set *fds) {
int i;

// send
Result<int, std::string> send_result = send_response(this->_connect_fd, response);
if (!send_result.is_ok()) {
throw std::runtime_error("[Error] send:" + send_result.get_err_value());
for (i = FD_SETSIZE; i >= 0; i--) {
if (FD_ISSET(i, fds)) {
return i + 1;
}
}
return 0;
}

std::string Server::get_recv_message() const { return this->_recv_message; }
Expand Down
1 change: 1 addition & 0 deletions srcs/Server/Server.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,5 @@ class Server {
static Result<std::string, std::string> recv_request(int connect_fd);
static Result<int, std::string> send_response(int connect_fd, const HttpResponse &response);
static void close_connection(int connect_fd);
static int find_maxfds(fd_set *fds);
};

0 comments on commit 3a0a43d

Please sign in to comment.