-
Notifications
You must be signed in to change notification settings - Fork 0
/
server_ClientManager.cpp
55 lines (49 loc) · 1.51 KB
/
server_ClientManager.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include "server_ClientManager.h"
#include "common_OSError.h"
#include <list>
#include <utility>
ClientManager::ClientManager(Socket&& server_socket,CircleVector&& vector,
Stadistics&& stadistics,char* port) : server_socket(std::move(server_socket)),
vector(std::move(vector)), stadistics(std::move(stadistics)),
thread(std::ref(*this)),list(), keep_accepting(true) {
server_socket.Bind(0,port);
server_socket.Listen(1);
}
void ClientManager::run(){
try{
while (keep_accepting){
std::list< std::unique_ptr<ClientProxy> >::iterator it;
for (it = list.begin(); it != list.end(); it++){
if ((*(*it)).isDead()){
(*(*it)).join();
list.erase(it);
}
}
Socket client_socket;
server_socket.Accept(client_socket);
std::unique_ptr<ClientProxy> client
(new ClientProxy
(std::move(client_socket),vector.next(),std::move(stadistics)));
list.push_back(std::move(client));
}
} catch(OSError& e){
if (keep_accepting == true){
throw OSError(e.what());
}
}
}
void ClientManager::operator()(){
this->run();
}
void ClientManager::stop(){
keep_accepting = false;
server_socket.Close();
std::list< std::unique_ptr<ClientProxy> >::iterator it;
for (it = list.begin(); it != list.end(); it++){
(*(*it)).join();
}
}
ClientManager::~ClientManager(){
this->stop();
thread.join();
}