From 6956b4fbd0d5a1a88d20f3e4a9b8fc115695bc30 Mon Sep 17 00:00:00 2001 From: M4iKZ <8481797+M4iKZ@users.noreply.github.com> Date: Thu, 20 Jul 2023 09:29:43 +0200 Subject: [PATCH] v 2.1 - improved logic - bug fixes - remove template folder, not used --- source/Common/Common.hpp | 2 +- source/Pool/Synchronization.hpp | 7 +++ source/Sockets/Sockets.cpp | 95 ++++++++++++++++++++------------- source/Sockets/Sockets.hpp | 5 +- source/Template/CMakeLists.txt | 21 -------- source/Template/Template.cpp | 12 ----- source/Template/Template.hpp | 6 --- 7 files changed, 69 insertions(+), 79 deletions(-) delete mode 100644 source/Template/CMakeLists.txt delete mode 100644 source/Template/Template.cpp delete mode 100644 source/Template/Template.hpp diff --git a/source/Common/Common.hpp b/source/Common/Common.hpp index a52ab38..e44024f 100644 --- a/source/Common/Common.hpp +++ b/source/Common/Common.hpp @@ -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() {} diff --git a/source/Pool/Synchronization.hpp b/source/Pool/Synchronization.hpp index 81799a5..d630c22 100644 --- a/source/Pool/Synchronization.hpp +++ b/source/Pool/Synchronization.hpp @@ -51,6 +51,13 @@ namespace Orpy return ptr; } + int getPoolSize() + { + std::lock_guard lock(mutex); + + return q.size(); + } + void push(T ptr) { { diff --git a/source/Sockets/Sockets.cpp b/source/Sockets/Sockets.cpp index 02c85bf..e28bce2 100644 --- a/source/Sockets/Sockets.cpp +++ b/source/Sockets/Sockets.cpp @@ -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); } @@ -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); } @@ -310,7 +312,7 @@ namespace Orpy int byte_count = 0; if (!Receive(data, byte_count)) return; - + data->phase = 2; //Elaborate! elaborate(data); } @@ -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) @@ -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; } @@ -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; } @@ -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 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().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 diff --git a/source/Sockets/Sockets.hpp b/source/Sockets/Sockets.hpp index 2567a77..301c147 100644 --- a/source/Sockets/Sockets.hpp +++ b/source/Sockets/Sockets.hpp @@ -100,6 +100,9 @@ namespace Orpy void clear(std::string); + void addClient(HTTPData*); + void clearClient(HTTPData*); + // Sends a file void sendFile(HTTPData*); @@ -109,7 +112,7 @@ namespace Orpy int _activeworkers = 0; ThreadSynchronization _sync; - std::unordered_map _clients; + std::unordered_map _clients; std::mutex mutex; diff --git a/source/Template/CMakeLists.txt b/source/Template/CMakeLists.txt deleted file mode 100644 index 8763fdd..0000000 --- a/source/Template/CMakeLists.txt +++ /dev/null @@ -1,21 +0,0 @@ -cmake_minimum_required(VERSION 3.10) -project(TemplateManager) - -# Get the name of the library -set(LIBRARY_NAME ${PROJECT_NAME}) - -# Concatenate the path and name to get the full path of the library binary -set(LIBRARY_BINARY ${CMAKE_BINARY_DIR}/Template/${CMAKE_SHARED_LIBRARY_PREFIX}${LIBRARY_NAME}${CMAKE_SHARED_LIBRARY_SUFFIX}) - -add_library(TemplateManager SHARED ${PROJECT_SOURCE_DIR}/Template.cpp) - -# Add the exported symbols -if(WIN32) - set_target_properties(TemplateManager PROPERTIES WINDOWS_EXPORT_ALL_SYMBOLS ON) -endif() - -if(WIN32) - install(TARGETS TemplateManager RUNTIME DESTINATION "${CMAKE_SOURCE_DIR}/_install/windows/Core") -else() - install(FILES ${LIBRARY_BINARY} DESTINATION "${CMAKE_SOURCE_DIR}/_install/linux/Core") -endif() \ No newline at end of file diff --git a/source/Template/Template.cpp b/source/Template/Template.cpp deleted file mode 100644 index 00efcc6..0000000 --- a/source/Template/Template.cpp +++ /dev/null @@ -1,12 +0,0 @@ - -#include "Request.hpp" - -#include "Template.hpp" - -namespace Orpy -{ - void setPage() - { - - } -} \ No newline at end of file diff --git a/source/Template/Template.hpp b/source/Template/Template.hpp deleted file mode 100644 index 8acb9d3..0000000 --- a/source/Template/Template.hpp +++ /dev/null @@ -1,6 +0,0 @@ -#pragma once - -namespace Orpy -{ - void setPage(); -} \ No newline at end of file