Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
T
TCPServer
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Packages
Packages
Container Registry
Analytics
Analytics
Repository
Value Stream
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Pierre,Engelstein
TCPServer
Commits
245175ae
Commit
245175ae
authored
Jun 20, 2019
by
pengelstein
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added comments
parent
5f238359
Changes
9
Hide whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
47 additions
and
19 deletions
+47
-19
README.md
README.md
+0
-1
src/thread/Delay/DelayThread.cpp
src/thread/Delay/DelayThread.cpp
+3
-2
src/thread/Delay/DelayThread.hpp
src/thread/Delay/DelayThread.hpp
+3
-2
src/thread/IThread.cpp
src/thread/IThread.cpp
+5
-2
src/thread/IThread.hpp
src/thread/IThread.hpp
+4
-1
src/thread/Socket/Client.cpp
src/thread/Socket/Client.cpp
+9
-3
src/thread/Socket/Client.hpp
src/thread/Socket/Client.hpp
+2
-0
src/thread/ThreadPool.cpp
src/thread/ThreadPool.cpp
+11
-6
src/thread/ThreadPool.hpp
src/thread/ThreadPool.hpp
+10
-2
No files found.
README.md
View file @
245175ae
#tcp server
oupsi
\ No newline at end of file
src/thread/Delay/DelayThread.cpp
View file @
245175ae
#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
;
...
...
src/thread/Delay/DelayThread.hpp
View file @
245175ae
#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
();
...
...
src/thread/IThread.cpp
View file @
245175ae
#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
;
}
...
...
src/thread/IThread.hpp
View file @
245175ae
...
...
@@ -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
;
...
...
src/thread/Socket/Client.cpp
View file @
245175ae
#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
;
...
...
src/thread/Socket/Client.hpp
View file @
245175ae
...
...
@@ -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
;
...
...
src/thread/ThreadPool.cpp
View file @
245175ae
#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
()
...
...
src/thread/ThreadPool.hpp
View file @
245175ae
...
...
@@ -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
;
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment