Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

25 refactor refactor loop function #26

Merged
merged 7 commits into from
Jan 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ CXXFLAGS = -Wall -Werror -Wextra -std=c++98
RM = rm -rf
AR = ar rcs
NAME = webserv
UNAME_OS = $(shell uname -s)

INC = -I inc/

Expand All @@ -27,6 +28,12 @@ OBJ_NAME += $(addprefix $(SOCK_DIR), $(SOCK_NAME:.cpp=.o))

OBJ = $(addprefix $(OBJ_DIR), $(OBJ_NAME))

ifeq ($(UNAME_OS), Linux)
CXXFLAGS += -D_LINUX
else ifeq ($(UNAME_OS), Darwin)
CXXFLAGS += -D_DARWIN
endif

all: mkdir $(NAME)

$(NAME): $(OBJ)
Expand Down
4 changes: 3 additions & 1 deletion inc/ClientSocket.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef CLIENTSOCKET_HPP
#define CLIENTSOCKET_HPP

#include <unistd.h>
#include <poll.h>
#include <sys/socket.h>
#include "utils.hpp"
Expand Down Expand Up @@ -30,10 +31,11 @@ class ClientSocket {
short getRevents() const;
ClientSocket::csphase tryRecv();
ClientSocket::csphase trySend();
void close();
void setPhase(ClientSocket::csphase const phase);
ClientSocket::csphase getPhase() const;
std::time_t getLastSendTimestamp() const;
void setLastSendTimestamp(std::time_t const lastSendTimestamp);
};

#endif
#endif
27 changes: 12 additions & 15 deletions src/loop/loop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ static bool setRevents(std::map<int, ServerSocket> &ssmap, std::map<int, ClientS
struct pollfd pfd;
std::memset(&pfd, 0, sizeof(struct pollfd));
pfd.fd = iter->first;
pfd.events = POLLIN | POLLHUP;
pfd.events = POLLIN;
pollfds.push_back(pfd);
}
for(std::map<int, ClientSocket>::iterator iter = csmap.begin(); iter != csmap.end(); ++iter) {
struct pollfd pfd;
std::memset(&pfd, 0, sizeof(struct pollfd));
pfd.fd = iter->first;
pfd.events = POLLIN | POLLOUT | POLLHUP;
pfd.events = POLLIN | POLLOUT;
pollfds.push_back(pfd);
}
if (poll(pollfds.data(), pollfds.size(), 0) == -1) {
Expand All @@ -36,21 +36,18 @@ static bool setRevents(std::map<int, ServerSocket> &ssmap, std::map<int, ClientS
return true;
}

static void checkClosedAndTimedOutClientSockets(std::map<int, ClientSocket> &csmap) {
for (std::map<int, ClientSocket>::iterator iter = csmap.begin(); iter != csmap.end(); ++iter) {
if ((iter->second.getRevents() & POLLHUP) == POLLHUP) {iter->second.setPhase(ClientSocket::CLOSE); }
else if (std::difftime(std::time(NULL), iter->second.getLastSendTimestamp()) > 5) {
close(iter->second.getFd());
iter->second.setPhase(ClientSocket::CLOSE);
}
}
}

static ClientSocket createCsocket(std::pair<int, sockaddr_in> socketInfo) {
ClientSocket cs(socketInfo.first);
return cs;
}

static ClientSocket::csphase detectTimedOutClientSocket(ClientSocket &cs) {
if (std::difftime(std::time(NULL), cs.getLastSendTimestamp()) > 5) {
return ClientSocket::CLOSE;
}
return cs.getPhase();
}

bool loop(std::map<int, ServerSocket> &ssmap) {
std::map<int, ClientSocket> csmap;
while(true) {
Expand All @@ -60,24 +57,24 @@ bool loop(std::map<int, ServerSocket> &ssmap) {
if (socketInfo.first == -1) { continue; }
csmap.insert(std::pair<int, ClientSocket>(socketInfo.first, createCsocket(socketInfo)));
}
checkClosedAndTimedOutClientSockets(csmap);
for (std::map<int, ClientSocket>::iterator iter = csmap.begin(); iter != csmap.end();) {
iter->second.setPhase(detectTimedOutClientSocket(iter->second));
switch (iter->second.getPhase()) {
case ClientSocket::RECV:
iter->second.setPhase(iter->second.tryRecv());
++iter;
break;
case ClientSocket::SEND:
iter->second.setPhase(iter->second.trySend());
++iter;
break;
case ClientSocket::CLOSE:
std::map<int, ClientSocket>::iterator toErase = iter;
++iter;
iter->second.close();
csmap.erase(toErase);
break;
}
}
}
return true;
}
}
24 changes: 20 additions & 4 deletions src/sockets/ClientSocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

ClientSocket::ClientSocket() {}

ClientSocket::ClientSocket(int const fd) : _fd(fd), _phase(ClientSocket::RECV), _lastSendTimestamp(std::time(NULL)) {}
ClientSocket::ClientSocket(int const fd) : _fd(fd), _revents(0), _phase(ClientSocket::RECV), _lastSendTimestamp(std::time(NULL)) {}

int ClientSocket::getFd() const {
return this->_fd;
Expand All @@ -18,30 +18,46 @@ short ClientSocket::getRevents() const {

ClientSocket::csphase ClientSocket::tryRecv() {
char buf[BUFFERSIZE];
ssize_t recvlen;

if ((this->_revents & POLLIN) != POLLIN) {
return this->_phase;
}
std::memset(&buf, 0, sizeof(buf));
if (recv(this->_fd, buf, BUFFERSIZE - 1, 0) == -1) {
recvlen = recv(this->_fd, buf, BUFFERSIZE - 1, 0);
if (recvlen == -1) {
utils::putSysError("recv");
return ClientSocket::CLOSE;
}
if (recvlen == 0) { return ClientSocket::CLOSE; }
this->_lastSendTimestamp = std::time(NULL);
return ClientSocket::SEND;
}

ClientSocket::csphase ClientSocket::trySend() {
#if defined(_LINUX)
const int flags = MSG_DONTWAIT | MSG_NOSIGNAL;
#elif defined(_DARWIN)
const int flags = MSG_DONTWAIT | SO_NOSIGPIPE;
#else
const int flags = MSG_DONTWAIT | MSG_NOSIGNAL;
#endif
const char *msg = "HTTP/1.1 200 Ok\nContent-Length: 11\n\nHelloworld!";
if ((this->_revents & POLLOUT) != POLLOUT) {
return this->_phase;
}
if (send(this->_fd, msg, std::strlen(msg), MSG_DONTWAIT | MSG_NOSIGNAL) == -1) {

if (send(this->_fd, msg, std::strlen(msg), flags) == -1) {
utils::putSysError("send");
return ClientSocket::CLOSE;
}
return ClientSocket::RECV;
}

void ClientSocket::close() {
if (::close(this->_fd) == -1) { utils::putSysError("close"); }
}

void ClientSocket::setPhase(ClientSocket::csphase const phase) {
this->_phase = phase;
}
Expand All @@ -56,4 +72,4 @@ std::time_t ClientSocket::getLastSendTimestamp() const {

void ClientSocket::setLastSendTimestamp(std::time_t const lastSendTimestamp) {
this->_lastSendTimestamp = lastSendTimestamp;
}
}
4 changes: 2 additions & 2 deletions src/sockets/ServerSocket.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "ServerSocket.hpp"

ServerSocket::ServerSocket() {}
ServerSocket::ServerSocket(std::string ipaddress, std::string port) : _ipaddress(ipaddress), _port(port) {}
ServerSocket::ServerSocket(std::string ipaddress, std::string port) : _ipaddress(ipaddress), _port(port), _revents(0) {}

static u_int32_t convertIpStrToUint(std::string const &ipaddr) {
u_int32_t s_addr(0);
Expand Down Expand Up @@ -85,4 +85,4 @@ int ServerSocket::getFd() const {
// int main() {
// ServerSocket ss("8.8.8.8", "8080");
// ss.init();
// }
// }
Loading