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

wip clientsocket -> clientsocket * #32

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
1 change: 1 addition & 0 deletions inc/Request.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include <string>
#include <map>
#include "ClientSocket.hpp"

class Request {
private:
Expand Down
48 changes: 28 additions & 20 deletions src/loop/loop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
// return oss.str();
// }

static bool setRevents(std::map<int, ServerSocket> &ssmap, std::map<int, ClientSocket> &csmap) {
static bool setRevents(std::map<int, ServerSocket> &ssmap, std::map<int, ClientSocket*> &csmap) {
std::vector<struct pollfd> pollfds;
for(std::map<int, ServerSocket>::iterator iter = ssmap.begin(); iter != ssmap.end(); ++iter) {
struct pollfd pfd;
Expand All @@ -18,7 +18,7 @@ static bool setRevents(std::map<int, ServerSocket> &ssmap, std::map<int, ClientS
pfd.events = POLLIN;
pollfds.push_back(pfd);
}
for(std::map<int, ClientSocket>::iterator iter = csmap.begin(); iter != csmap.end(); ++iter) {
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;
Expand All @@ -31,14 +31,13 @@ static bool setRevents(std::map<int, ServerSocket> &ssmap, std::map<int, ClientS
}
for(std::vector<struct pollfd>::iterator iter = pollfds.begin(); iter != pollfds.end(); ++iter) {
if (ssmap.find(iter->fd) != ssmap.end()) { ssmap[iter->fd].setRevents(iter->revents); }
else if (csmap.find(iter->fd) != csmap.end()) { csmap[iter->fd].setRevents(iter->revents); }
else if (csmap.find(iter->fd) != csmap.end()) { csmap[iter->fd]->setRevents(iter->revents); }
}
return true;
}

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

static ClientSocket::csphase detectTimedOutClientSocket(ClientSocket &cs) {
Expand All @@ -50,39 +49,48 @@ static ClientSocket::csphase detectTimedOutClientSocket(ClientSocket &cs) {
}

bool loop(std::map<int, ServerSocket> &ssmap) {
std::map<int, ClientSocket> csmap;
std::map<int, ClientSocket*> csmap;
std::map<int, Request> rqmap;
while(true) {
if (setRevents(ssmap, csmap) == false) { return false; }
for(std::map<int, ServerSocket>::iterator iter = ssmap.begin(); iter != ssmap.end(); ++iter) {
std::pair<int, sockaddr_in> socketInfo = iter->second.tryAccept();
if (socketInfo.first == -1) { continue; }
csmap.insert(std::pair<int, ClientSocket>(socketInfo.first, createCsocket(socketInfo)));
ClientSocket *newCs;
newCs = createCsocket(socketInfo);
if (newCs == NULL) {
utils::putSysError("new");
close(socketInfo.first);
continue;
}
csmap.insert(std::pair<int, ClientSocket*>(socketInfo.first, newCs));
rqmap.insert(std::pair<int, Request>(socketInfo.first, Request()));
}
for (std::map<int, ClientSocket>::iterator iter = csmap.begin(); iter != csmap.end();) {
iter->second.setPhase(detectTimedOutClientSocket(iter->second));
switch (iter->second.getPhase()) {
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->second->setPhase(iter->second->tryRecv());
++iter;
break;
case ClientSocket::SEND:
iter->second.setPhase(iter->second.trySend());
iter->second->setPhase(iter->second->trySend());
++iter;
break;
case ClientSocket::CLOSE:
std::map<int, ClientSocket>::iterator toErase = iter;
std::map<int, ClientSocket*>::iterator toErase = iter;
++iter;
iter->second.close();
toErase->second->close();
delete toErase->second;
csmap.erase(toErase);
break;
}
}
for (std::map<int, Request>::iterator iter = rqmap.begin(); iter != rqmap.end();) {
std::map<int, ClientSocket>::iterator csiter = csmap.find(iter->first);
if (csiter != csmap.end() && csiter->second.buffer.str().find("\r\n") != std::string::npos) {
ClientSocket::csphase nextcsphase = iter->second.load(csiter->second.buffer);
csiter->second.setPhase(nextcsphase);
for (std::map<int, Request>::iterator iter = rqmap.begin(); iter != rqmap.end(); ++iter) {
std::map<int, ClientSocket*>::iterator csiter = csmap.find(iter->first);
if (csiter != csmap.end() && csiter->second->buffer.str().find("\r\n") != std::string::npos) {
ClientSocket::csphase nextcsphase = iter->second.load(csiter->second->buffer);
csiter->second->setPhase(nextcsphase);
}
}
}
Expand Down
8 changes: 7 additions & 1 deletion src/request/Request.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,11 @@ std::string const &Request::getHttpVersion() const{
}

ClientSocket::csphase Request::load(std::stringstream &buffer) {

std::string line;
std::getline(buffer, line);
std::stringstream ss(line);
ss >> this->_method;
ss >> this->_path;
ss >> this->_httpVersion;
return ClientSocket::CLOSE;
}
Loading