KlassischeKeplerKriege/game/server.hpp

39 lines
642 B
C++
Raw Normal View History

2016-09-27 18:51:03 +00:00
#pragma once
2016-09-27 22:26:36 +00:00
#include <asio.hpp>
2016-09-27 18:51:03 +00:00
2016-09-27 22:26:36 +00:00
#include "session.hpp"
2016-09-27 18:51:03 +00:00
2016-09-27 22:26:36 +00:00
using asio::ip::tcp;
2016-09-27 18:51:03 +00:00
2016-09-27 22:26:36 +00:00
class Server
{
public:
2016-09-27 23:48:34 +00:00
Server(asio::io_service& io_service,game::State* s, short port)
2016-09-27 22:26:36 +00:00
: acceptor_(io_service, tcp::endpoint(tcp::v4(), port)),
socket_(io_service)
{
2016-09-27 23:48:34 +00:00
state=s;
2016-09-27 22:26:36 +00:00
do_accept();
}
2016-09-27 18:51:03 +00:00
private:
2016-09-27 22:26:36 +00:00
void do_accept()
{
acceptor_.async_accept(socket_,
[this](std::error_code ec)
{
if (!ec)
{
2016-09-27 23:48:34 +00:00
std::make_shared<Session>(std::move(socket_),state)->start();
2016-09-27 22:26:36 +00:00
}
do_accept();
});
}
2016-09-27 18:51:03 +00:00
2016-09-27 22:26:36 +00:00
tcp::acceptor acceptor_;
tcp::socket socket_;
2016-09-27 23:48:34 +00:00
game::State* state;
2016-09-27 18:51:03 +00:00
};