-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServer.cpp
More file actions
54 lines (45 loc) · 1.23 KB
/
Server.cpp
File metadata and controls
54 lines (45 loc) · 1.23 KB
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
#include "Server.h"
#include <boost/bind.hpp>
#include <utility>
#include "Config.h"
#include <iostream>
Server::Server(boost::asio::io_context& ioContext) :
ioContext(ioContext),
acceptor(ioContext, tcp::endpoint(tcp::v4(), Config::serverPort)),
isRunning(false)
{
startAccepting();
mainLoopThread = std::thread(&Server::mainLoop, this);
}
Server::~Server()
{
mainLoopThread.join();
}
void Server::mainLoop()
{
std::cout << "Starting loop" << std::endl;
isRunning = true;
while (isRunning) {
std::for_each(playersSet.begin(), playersSet.end(), [](Player player)
{
player.processPlayer();
});
std::this_thread::sleep_for(std::chrono::milliseconds(500));
}
}
void Server::startAccepting()
{
std::cout << "Check" << std::endl;
Connection::handler newConnection = Connection::createConnection(ioContext);
acceptor.async_accept(newConnection->getSocket(),
boost::bind(&Server::handleNewConnection, this, newConnection,
boost::asio::placeholders::error));
}
void Server::handleNewConnection(Connection::handler newConnection, const boost::system::error_code& error)
{
if (!error) {
const Player newPlayer(std::move(newConnection));
playersSet.push_back(newPlayer);
}
startAccepting();
}