Commit 245175ae authored by pengelstein's avatar pengelstein

Added comments

parent 5f238359
#tcp server
oupsi
\ No newline at end of file
#include "DelayThread.hpp"
#include <thread/Delay/DelayThread.hpp>
#include <unistd.h>
namespace threads
{
......@@ -26,7 +27,7 @@ namespace threads
printf("Started delay thread !\n");
while(DelayThread::Running())
{
DelayThread::Decrease_All();
DelayThread::Decrease_All(); //Decrease by one second each thread
sleep(1);
}
return;
......
#ifndef DELAYTHREAD_H
#define DELAYTHREAD_H
#include "../IThread.hpp"
#include "../Socket/SocketThread.hpp"
#include <thread/IThread.hpp>
#include <vector>
namespace threads
{
/* Class handling the data for one ip in cooldown : which ip is it and how much time does it still have in cooldown */
class ip_cooldown
{
public:
ip_cooldown(std::string ip, int cooldown);
~ip_cooldown();
int Cooldown();
/* Decreases by one the cooldown */
void Decrease();
bool Compare(std::string ip);
std::string ToString();
......
#include <thread/IThread.hpp>
#include <thread/cross.hpp>
namespace threads
{
......@@ -15,11 +16,11 @@ namespace threads
int IThread::Create()
{
IThread::_thread = std::thread([this] { Run(); });
IThread::_thread = std::thread([this] { Run(); }); //Use of lambdas to be able to register the virtual function of this class
if(!IThread::_thread.joinable())
{
fprintf(stderr, "Thread is not joinable !\n");
fprintf(stderr, "%s\n", strerror(errno));
Error();
return 1;
}
IThread::_running = true;
......@@ -35,6 +36,7 @@ namespace threads
return 0;
}
fprintf(stderr, "Thread is not joinable\n");
Error();
return 1;
}
......@@ -47,6 +49,7 @@ namespace threads
return 0;
}
fprintf(stderr, "Thread is not joinable\n");
Error();
return 1;
}
......
......@@ -7,14 +7,17 @@
namespace threads
{
/* Wrapper around the c++ thread class */
class IThread
{
public:
IThread(int id);
virtual ~IThread();
/* Main thread function, must be overriden by children classes */
virtual void Run();
bool Running();
/* Ends the execution of the thread loop */
void End();
virtual std::string ToString() = 0;
......
#include "Client.hpp"
#include <thread/Socket/Client.hpp>
#include <thread/cross.hpp>
namespace threads
{
......@@ -81,11 +82,16 @@ namespace threads
int Client::Close(int socket)
{
int res = close(socket);
int res = 0;
#ifdef __linux
res = close(socket);
#elif defined __WIN32
res = closesocket(socket);
#endif
if(res)
{
fprintf(stderr, "Socket %d close failed ! (%d)\n", socket, res);
fprintf(stderr, "%s\n", strerror(errno));
Error();
return 1;
}
return 0;
......
......@@ -33,6 +33,7 @@ namespace threads
std::string IP();
uint16_t Port();
/* Gets the socket in use for this client */
int Socket();
__socklen_t Socket_Length();
......@@ -47,6 +48,7 @@ namespace threads
std::string ToString();
private:
/* All the client description */
std::string ip;
uint16_t port;
int socket;
......
#include "ThreadPool.hpp"
#include "Socket/SocketThread.hpp"
#include "Socket/Client.hpp"
#include "Delay/DelayThread.hpp"
#include <thread/ThreadPool.hpp>
#include <thread/Socket/SocketThread.hpp>
#include <thread/Socket/Client.hpp>
#include <thread/Delay/DelayThread.hpp>
namespace threads
{
......@@ -71,16 +71,21 @@ namespace threads
return str;
}
void ThreadPool::RemoveIP(std::string ip)
int ThreadPool::RemoveIP(std::string ip)
{
fprintf(stdout, "REMOVE IP AHAHAHAHAHAH !\n");
fprintf(stdout, "Removing IP %s from pool.\n", ip.c_str());
int amount_deleted = 0;
for(auto iter = ThreadPool::pool.begin(); iter < ThreadPool::pool.end();)
{
if(!iter->get()->IP().compare(ip))
{
iter = pool.erase(iter);
amount_deleted++;
}
else
iter++;
}
return amount_deleted;
}
void ThreadPool::KillAll()
......
......@@ -11,17 +11,25 @@ namespace threads
{
class SocketThread;
class DelayThread;
/* The thread pool keeps track of all clients currently connected. The main goal is to avoid having 2 clients connected
from the same IP, and to avoid having too much client connected by making sure that we don't have more than a certain
amount of client threads registered
*/
class ThreadPool
{
public:
ThreadPool(int max);
virtual ~ThreadPool();
/* Adds a new thread to the current thread pool */
int AddThread(std::shared_ptr<threads::SocketThread> thread, threads::DelayThread *delay);
std::string ToString();
void RemoveIP(std::string ip);
/* Removes the client thread corresponding to the given ip address. Returns the amount of threads that have been deleted (0 if none). */
int RemoveIP(std::string ip);
/* Cleans and kills all the clients that are registers in the thread pool. */
void KillAll();
/* Returns the current number of clients connected in the pool. */
int Size();
/* Returns the maximum number of clients the server can handle */
int Max();
private:
std::vector<std::shared_ptr<threads::SocketThread>> pool;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment