2019-01-09 20:44:58 +01:00
|
|
|
/*
|
|
|
|
* =====================================================================================
|
|
|
|
*
|
|
|
|
* Filename: Server.hpp
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
*
|
|
|
|
* Created: 23/01/2018 14:47:14
|
|
|
|
*
|
|
|
|
* Author: Quentin Bazin, <quent42340@gmail.com>
|
|
|
|
*
|
|
|
|
* =====================================================================================
|
|
|
|
*/
|
|
|
|
#ifndef SERVER_HPP_
|
|
|
|
#define SERVER_HPP_
|
|
|
|
|
2019-01-12 13:52:30 +01:00
|
|
|
#include <functional>
|
|
|
|
|
2019-01-09 20:44:58 +01:00
|
|
|
#include <SFML/Network/SocketSelector.hpp>
|
|
|
|
#include <SFML/Network/TcpListener.hpp>
|
|
|
|
#include <SFML/Network/UdpSocket.hpp>
|
|
|
|
|
2019-01-16 22:24:57 +01:00
|
|
|
#include "Network.hpp"
|
2019-01-09 20:44:58 +01:00
|
|
|
#include "ServerInfo.hpp"
|
|
|
|
|
|
|
|
class Server {
|
2019-01-16 22:24:57 +01:00
|
|
|
using ConnectionCallback = std::function<void(Client&)>;
|
2019-01-18 01:59:26 +01:00
|
|
|
using CommandCallback = std::function<void(Client &, sf::Packet &packet)>;
|
2019-01-12 13:52:30 +01:00
|
|
|
|
2019-01-09 20:44:58 +01:00
|
|
|
public:
|
2019-01-11 23:54:33 +01:00
|
|
|
void init(u16 port = 4242);
|
2019-01-09 20:44:58 +01:00
|
|
|
|
|
|
|
void handleKeyState();
|
|
|
|
|
|
|
|
void handleGameEvents();
|
|
|
|
|
2019-01-16 22:24:57 +01:00
|
|
|
void sendToAllClients(sf::Packet &packet);
|
|
|
|
|
2019-01-09 20:44:58 +01:00
|
|
|
bool isRunning() const { return m_isRunning; }
|
|
|
|
|
|
|
|
ServerInfo &info() { return m_info; }
|
|
|
|
|
|
|
|
sf::UdpSocket &udpSocket() { return m_udpSocket; }
|
|
|
|
|
|
|
|
void setRunning(bool isRunning) { m_isRunning = isRunning; }
|
|
|
|
|
2019-01-16 22:24:57 +01:00
|
|
|
void setConnectionCallback(const ConnectionCallback &callback) { m_connectionCallback = callback; }
|
|
|
|
void setCommandCallback(Network::Command command, const CommandCallback &callback) { m_commands[command] = callback; }
|
2019-01-12 13:52:30 +01:00
|
|
|
|
2019-01-09 20:44:58 +01:00
|
|
|
private:
|
|
|
|
void handleNewConnections();
|
|
|
|
void handleClientMessages();
|
|
|
|
|
|
|
|
bool m_isRunning = true;
|
|
|
|
|
|
|
|
ServerInfo m_info;
|
|
|
|
|
|
|
|
sf::UdpSocket m_udpSocket;
|
|
|
|
|
|
|
|
sf::TcpListener m_tcpListener;
|
|
|
|
sf::SocketSelector m_selector;
|
2019-01-12 13:52:30 +01:00
|
|
|
|
2019-01-16 22:24:57 +01:00
|
|
|
ConnectionCallback m_connectionCallback;
|
|
|
|
|
|
|
|
std::unordered_map<Network::Command, CommandCallback> m_commands;
|
2019-01-09 20:44:58 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif // SERVER_HPP_
|