Skip to content

Commit

Permalink
v 2.1
Browse files Browse the repository at this point in the history
- improved logic
- bug fixes
- remove template folder, not used
  • Loading branch information
M4iKZ committed Jul 20, 2023
1 parent 2c4fbeb commit 6956b4f
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 79 deletions.
2 changes: 1 addition & 1 deletion source/Common/Common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ namespace Orpy

struct HTTPData
{
HTTPData(int p = 0, int clientfd = -1, std::string k = "") : fd(clientfd), key(k), phase(p), length(0), buffer({}) {}
HTTPData(int p = 0, int clientfd = -1, std::string k = "", std::string ip = "") : fd(clientfd), key(k), phase(p), IP(ip), length(0), buffer({}) {}

~HTTPData() {}

Expand Down
7 changes: 7 additions & 0 deletions source/Pool/Synchronization.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,13 @@ namespace Orpy
return ptr;
}

int getPoolSize()
{
std::lock_guard<std::mutex> lock(mutex);

return q.size();
}

void push(T ptr)
{
{
Expand Down
95 changes: 57 additions & 38 deletions source/Sockets/Sockets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -250,15 +250,16 @@ namespace Orpy

std::string key = std::to_string(client_fd);

_clients[key].key = key;
_clients[key].fd = client_fd;
HTTPData* client = new HTTPData(0, client_fd, key);

char client_ip[INET_ADDRSTRLEN];
struct in_addr ipAddr = client_address.sin_addr;
inet_ntop(AF_INET, &ipAddr, client_ip, INET_ADDRSTRLEN);

std::string ip(client_ip);
_clients[key].IP = ip;
client->IP = ip;

addClient(client);

_sync.push(key);
}
Expand All @@ -278,16 +279,17 @@ namespace Orpy
auto data = _clients.find(key);
if (data != _clients.end())
{
if (data->second.startTime < std::time(nullptr))
int phase = data->second->phase;
if (data->second->startTime < std::time(nullptr))
clear(key);
else if (data->second.phase == 0)
receiveData(&data->second);
else if (data->second.phase == 1)
sendData(&data->second);
else if (data->second.phase == 2)
elaborate(&data->second);
else if (data->second.phase == 3)
sendFile(&data->second);
else if (phase == 0)
receiveData(data->second);
else if (phase == 1)
sendData(data->second);
else if (phase == 2)
elaborate(data->second);
else if (phase == 3)
sendFile(data->second);
else
clear(key);
}
Expand All @@ -310,7 +312,7 @@ namespace Orpy
int byte_count = 0;
if (!Receive(data, byte_count))
return;

data->phase = 2; //Elaborate!
elaborate(data);
}
Expand All @@ -324,10 +326,11 @@ namespace Orpy
return;
}

HTTPData newData(0, data->fd, data->key);
_clients[data->key] = newData;
std::string key = data->key;
_clients[key] = new HTTPData(0, data->fd, data->key, data->IP);
delete data;

_sync.push(data->key);
_sync.push(key);
}

void Sockets::sendFile(HTTPData* data)
Expand Down Expand Up @@ -367,10 +370,11 @@ namespace Orpy

if (data->sent >= data->fileSize)
{
HTTPData newData(0, data->fd, data->key);
_clients[data->key] = newData;
std::string key = data->key;
_clients[key] = new HTTPData(0, data->fd, data->key, data->IP);
delete data;

_sync.push(data->key);
_sync.push(key);
return;
}

Expand Down Expand Up @@ -417,30 +421,22 @@ namespace Orpy
{
data->buffer.insert(data->buffer.end(), chunk.begin(), chunk.begin() + bytes_received);
total_bytes += bytes_received;

if (total_bytes > MaxUploadSize)
return true;
}
else
{
if (total_bytes > 0)
return true;
else if (total_bytes == 0)
if (bytes_received != 0)
{
return true;
#ifdef _WIN32
if (WSAGetLastError() == WSAEWOULDBLOCK)
if (WSAGetLastError() == WSAEWOULDBLOCK)
#else
if (errno == EAGAIN || errno == EWOULDBLOCK)
#endif
{
_sync.push(data->key);
break;
}

}

clear(data->key);
if (errno == EAGAIN || errno == EWOULDBLOCK)
#endif
_sync.push(data->key);
else
clear(data->key);

break;
}
Expand Down Expand Up @@ -475,14 +471,37 @@ namespace Orpy
{
auto data = _clients.find(key);
if (data != _clients.end())
clearClient(data->second);
}
}

void Sockets::addClient(HTTPData* data)
{
std::lock_guard<std::mutex> lock(mutex);
{
if (_sync.getPoolSize() == 0 && _clients.size() > 0)
{
close(data->second.fd, false);
debug(key + " with fd " + std::to_string(data->second.fd) + " session from " + data->second.IP + " closed, open sessions " + std::to_string(_clients.size() - 1));
_clients.erase(key);
for (auto& c : _clients)
if (c.second->startTime < std::time(nullptr))
clearClient(c.second);

if (_clients.size() == 0)
std::unordered_map<std::string, HTTPData*>().swap(_clients);
}

_clients[data->key] = data;
}
}

void Sockets::clearClient(HTTPData* data)
{
close(data->fd, false);
debug(data->key + " with fd " + std::to_string(data->fd) + " session from " + data->IP + " closed, open sessions " + std::to_string(_clients.size() - 1));

_clients.erase(data->key);
delete data;
}

void Sockets::close(int fd, bool main)
{
#ifdef _WIN32
Expand Down
5 changes: 4 additions & 1 deletion source/Sockets/Sockets.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@ namespace Orpy

void clear(std::string);

void addClient(HTTPData*);
void clearClient(HTTPData*);

// Sends a file
void sendFile(HTTPData*);

Expand All @@ -109,7 +112,7 @@ namespace Orpy
int _activeworkers = 0;

ThreadSynchronization<std::string> _sync;
std::unordered_map<std::string, HTTPData> _clients;
std::unordered_map<std::string, HTTPData*> _clients;

std::mutex mutex;

Expand Down
21 changes: 0 additions & 21 deletions source/Template/CMakeLists.txt

This file was deleted.

12 changes: 0 additions & 12 deletions source/Template/Template.cpp

This file was deleted.

6 changes: 0 additions & 6 deletions source/Template/Template.hpp

This file was deleted.

0 comments on commit 6956b4f

Please sign in to comment.