KlassischeKeplerKriege/game/network/server.hpp

39 lines
810 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:
Server(asio::io_service& io_service,game::State* s, short port)
: acceptor_(io_service, tcp::endpoint(tcp::v4(), port)),
socket_(io_service)
{
state=s;
do_accept();
}
2016-09-27 18:51:03 +00:00
private:
void do_accept()
2016-09-27 22:26:36 +00:00
{
acceptor_.async_accept(socket_,
[this](std::error_code ec)
{
if (!ec)
{
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
tcp::acceptor acceptor_;
tcp::socket socket_;
game::State* state;
2016-09-27 18:51:03 +00:00
};