grouped files in subdirs.
This commit is contained in:
parent
7d8585c5d9
commit
aedda9d48e
14 changed files with 281 additions and 11 deletions
38
game/network/server.hpp
Normal file
38
game/network/server.hpp
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
#pragma once
|
||||
|
||||
#include <asio.hpp>
|
||||
|
||||
#include "session.hpp"
|
||||
|
||||
using asio::ip::tcp;
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
private:
|
||||
void do_accept()
|
||||
{
|
||||
acceptor_.async_accept(socket_,
|
||||
[this](std::error_code ec)
|
||||
{
|
||||
if (!ec)
|
||||
{
|
||||
std::make_shared<Session>(std::move(socket_),state)->start();
|
||||
}
|
||||
|
||||
do_accept();
|
||||
});
|
||||
}
|
||||
|
||||
tcp::acceptor acceptor_;
|
||||
tcp::socket socket_;
|
||||
game::State* state;
|
||||
};
|
||||
80
game/network/session.cpp
Normal file
80
game/network/session.cpp
Normal file
|
|
@ -0,0 +1,80 @@
|
|||
#define ASIO_STANDALONE
|
||||
|
||||
#include "session.hpp"
|
||||
#include "game.hpp"
|
||||
#include "state/commands.hpp"
|
||||
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
bool Session::parse(string s)
|
||||
{
|
||||
s.erase(std::find_if(s.rbegin(), s.rend(),
|
||||
std::not1(std::ptr_fun<int, int>(std::isspace))).base(), s.end());
|
||||
|
||||
// TODO:
|
||||
// from knoc:
|
||||
// ALWAYS use commands here, no direct input!
|
||||
// thats important!!!!
|
||||
// TODO
|
||||
if (s.size()==1&&s[0]=='q'){
|
||||
//quit
|
||||
m_socket.close();
|
||||
m_state->quitPlayer(m_pid);
|
||||
return false;
|
||||
} else if (s.size()==1&&s[0]=='c') {
|
||||
//clear
|
||||
m_state->clear(m_pid);
|
||||
} else if (s.size()>=3&&s[0]=='n'&&s[1]==' ') {
|
||||
//set name
|
||||
m_state->setName(m_pid, s.substr(2));
|
||||
} else if (s.size()>=3&&s[0]=='v'&&s[1]==' ') {
|
||||
//set speed
|
||||
double speed = atof(s.substr(2).c_str());
|
||||
m_state->setSpeed(m_pid, speed);
|
||||
} else {
|
||||
//shoot
|
||||
double angle = atof(s.c_str());
|
||||
m_state->commandForPlayer(m_pid, new game::ShootCommand(angle));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void Session::start()
|
||||
{
|
||||
if(m_started) return;
|
||||
m_started=true;
|
||||
m_pid = m_state->addPlayer();
|
||||
char hello[] = "\nWeclome to KlassischeKeplerKriege v0.1\n Use \"n name\" to change name, \"v velocity\" to change velocity, \"c\" to clear past shots or \"q\" to close the connection.\nEverything else is interpreted as a shooting angle.\n\n> ";
|
||||
do_write(hello, strlen(hello));
|
||||
}
|
||||
|
||||
void Session::do_read()
|
||||
{
|
||||
auto self(shared_from_this());
|
||||
m_socket.async_read_some(
|
||||
asio::buffer(m_rcv_data, max_length),
|
||||
[this, self](std::error_code ec, std::size_t length)
|
||||
{
|
||||
if (!ec) {
|
||||
std::string s(m_rcv_data,length);
|
||||
if(parse(s)){
|
||||
char c[]="> ";
|
||||
do_write(c,strlen(c));
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
void Session::do_write(char m_snd_data[], std::size_t length)
|
||||
{
|
||||
auto self(shared_from_this());
|
||||
asio::async_write(m_socket, asio::buffer(m_snd_data, length),
|
||||
[this, self](std::error_code ec, std::size_t /*length*/)
|
||||
{
|
||||
if (!ec) {
|
||||
do_read();
|
||||
}
|
||||
});
|
||||
}
|
||||
37
game/network/session.hpp
Normal file
37
game/network/session.hpp
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
#pragma once
|
||||
|
||||
#define ASIO_STANDALONE
|
||||
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
#include <utility>
|
||||
#include <string>
|
||||
#include <asio.hpp>
|
||||
|
||||
#include "game.hpp"
|
||||
|
||||
using asio::ip::tcp;
|
||||
|
||||
class Session
|
||||
: public std::enable_shared_from_this<Session>
|
||||
{
|
||||
public:
|
||||
Session(tcp::socket socket, game::State* state)
|
||||
: m_socket(std::move(socket))
|
||||
{
|
||||
m_state = state;
|
||||
}
|
||||
|
||||
void start();
|
||||
|
||||
private:
|
||||
void do_read();
|
||||
void do_write(char m_snd_data[], std::size_t length);
|
||||
bool parse(std::string);
|
||||
tcp::socket m_socket;
|
||||
enum { max_length = 1024 };
|
||||
char m_rcv_data[max_length];
|
||||
game::State* m_state;
|
||||
bool m_started = false;
|
||||
int m_pid;
|
||||
};
|
||||
Loading…
Add table
Add a link
Reference in a new issue