Skip to content

Commit

Permalink
Merge pull request #24 from Ashes110/22-add-manage-condition-of-clien…
Browse files Browse the repository at this point in the history
…tsocket-instances-and-what-should-be-done-for-it

22 add manage condition of clientsocket instances and what should be done for it
  • Loading branch information
ilovenoah authored Jan 29, 2024
2 parents 0fee08d + fe32052 commit 3cf1424
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 26 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/clang-formatter.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@
# ::: :::::::: #
# clang-formatter.yml :+: :+: :+: #
# +:+ +:+ +:+ #
# By: shtanemu <shtanemu@student.42.fr> +#+ +:+ +#+ #
# By: taiphyyy <taiphyyy@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2023/09/02 15:36:37 by shtanemu #+# #+# #
# Updated: 2024/01/29 15:42:11 by shtanemu ### ########.fr #
# Updated: 2024/01/29 17:08:55 by taiphyyy ### ########.fr #
# #
# **************************************************************************** #

on:
push:
branches:
- 'main'
- 'master'
workflow_dispatch:

name: Clang-format Code Formatter
Expand Down
20 changes: 18 additions & 2 deletions inc/ClientSocket.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,36 @@
#include <poll.h>
#include <sys/socket.h>
#include "utils.hpp"
#include <ctime>

#define BUFFERSIZE 4096

class ClientSocket {
public:
enum csphase {
RECV,
SEND,
CLOSE
};

private:
int _fd;
short _revents;
ClientSocket::csphase _phase;
std::time_t _lastSendTimestamp;

public:
ClientSocket();
ClientSocket(int const fd);
int getFd() const;
void setRevents(short revents);
bool tryRecv();
bool trySend();
short getRevents() const;
ClientSocket::csphase tryRecv();
ClientSocket::csphase trySend();
void setPhase(ClientSocket::csphase const phase);
ClientSocket::csphase getPhase() const;
std::time_t getLastSendTimestamp() const;
void setLastSendTimestamp(std::time_t const lastSendTimestamp);
};

#endif
33 changes: 27 additions & 6 deletions src/loop/loop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,16 @@ 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;
Expand All @@ -49,13 +59,24 @@ bool loop(std::map<int, ServerSocket> &ssmap) {
std::pair<int, sockaddr_in> socketInfo = iter->second.tryAccept();
if (socketInfo.first == -1) { continue; }
csmap.insert(std::pair<int, ClientSocket>(socketInfo.first, createCsocket(socketInfo)));
std::cout << csmap[socketInfo.first].getFd() << std::endl;
}
for (std::map<int, ClientSocket>::iterator iter = csmap.begin(); iter != csmap.end(); ++iter) {
if (iter->second.tryRecv() == false) { continue; }
}
for (std::map<int, ClientSocket>::iterator iter = csmap.begin(); iter != csmap.end(); ++iter) {
if (iter->second.trySend() == false) { continue; }
checkClosedAndTimedOutClientSockets(csmap);
for (std::map<int, ClientSocket>::iterator iter = csmap.begin(); iter != csmap.end();) {
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;
csmap.erase(toErase);
break;
}
}
}
return true;
Expand Down
47 changes: 32 additions & 15 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) {}
ClientSocket::ClientSocket(int const fd) : _fd(fd), _phase(ClientSocket::RECV), _lastSendTimestamp(std::time(NULL)) {}

int ClientSocket::getFd() const {
return this->_fd;
Expand All @@ -12,31 +12,48 @@ void ClientSocket::setRevents(short revents) {
this->_revents = revents;
}

bool ClientSocket::tryRecv() {
short ClientSocket::getRevents() const {
return this->_revents;
}

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

bool ClientSocket::trySend() {
const char *msg = "HTTP/1.1 200 Ok\nContent-Length: 12\n\nHelloworld!";
ClientSocket::csphase ClientSocket::trySend() {
const char *msg = "HTTP/1.1 200 Ok\nContent-Length: 11\n\nHelloworld!";
if ((this->_revents & POLLOUT) != POLLOUT) {
return false;
return this->_phase;
}
if (send(this->_fd, msg, std::strlen(msg), MSG_DONTWAIT | MSG_NOSIGNAL) == -1) {
utils::putSysError("send");
this->_revents = 0;
return false;
return ClientSocket::CLOSE;
}
this->_revents = 0;
return true;
return ClientSocket::RECV;
}

void ClientSocket::setPhase(ClientSocket::csphase const phase) {
this->_phase = phase;
}

ClientSocket::csphase ClientSocket::getPhase() const {
return this->_phase;
}

std::time_t ClientSocket::getLastSendTimestamp() const {
return this->_lastSendTimestamp;
}

void ClientSocket::setLastSendTimestamp(std::time_t const lastSendTimestamp) {
this->_lastSendTimestamp = lastSendTimestamp;
}

0 comments on commit 3cf1424

Please sign in to comment.