2019-01-09 20:44:58 +01:00
|
|
|
/*
|
|
|
|
* =====================================================================================
|
|
|
|
*
|
2020-02-08 18:34:26 +09:00
|
|
|
* OpenMiner
|
2020-02-25 01:42:10 +09:00
|
|
|
*
|
2020-02-08 18:34:26 +09:00
|
|
|
* Copyright (C) 2018-2020 Unarelith, Quentin Bazin <openminer@unarelith.net>
|
2020-02-25 01:42:10 +09:00
|
|
|
* Copyright (C) 2019-2020 the OpenMiner contributors (see CONTRIBUTORS.md)
|
|
|
|
*
|
|
|
|
* This file is part of OpenMiner.
|
2019-01-09 20:44:58 +01:00
|
|
|
*
|
2020-02-25 01:42:10 +09:00
|
|
|
* OpenMiner is free software; you can redistribute it and/or
|
2020-02-08 18:34:26 +09:00
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
2019-01-09 20:44:58 +01:00
|
|
|
*
|
2020-02-25 01:42:10 +09:00
|
|
|
* OpenMiner is distributed in the hope that it will be useful,
|
2020-02-08 18:34:26 +09:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
2019-01-09 20:44:58 +01:00
|
|
|
*
|
2020-02-08 18:34:26 +09:00
|
|
|
* You should have received a copy of the GNU Lesser General Public License
|
2020-02-25 01:42:10 +09:00
|
|
|
* along with OpenMiner; if not, write to the Free Software Foundation, Inc.,
|
2020-02-08 18:34:26 +09:00
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
2019-01-09 20:44:58 +01:00
|
|
|
*
|
|
|
|
* =====================================================================================
|
|
|
|
*/
|
|
|
|
#ifndef SERVER_HPP_
|
|
|
|
#define SERVER_HPP_
|
|
|
|
|
2019-01-12 13:52:30 +01:00
|
|
|
#include <functional>
|
2019-01-20 01:31:44 +01:00
|
|
|
#include <unordered_map>
|
2019-01-12 13:52:30 +01:00
|
|
|
|
2019-01-09 20:44:58 +01:00
|
|
|
#include <SFML/Network/SocketSelector.hpp>
|
|
|
|
#include <SFML/Network/TcpListener.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 {
|
2020-06-26 01:31:41 +02:00
|
|
|
using ConnectionCallback = std::function<void(ClientInfo &, Network::Packet &packet)>;
|
2020-05-10 19:36:39 +02:00
|
|
|
using CommandCallback = std::function<void(ClientInfo &, Network::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
|
|
|
|
2020-06-23 17:36:06 +02:00
|
|
|
void disconnectAllClients();
|
|
|
|
|
2019-01-09 20:44:58 +01:00
|
|
|
void handleGameEvents();
|
|
|
|
|
2020-05-10 19:36:39 +02:00
|
|
|
void sendToAllClients(Network::Packet &packet) const;
|
2019-01-16 22:24:57 +01:00
|
|
|
|
2019-01-09 20:44:58 +01:00
|
|
|
bool isRunning() const { return m_isRunning; }
|
2020-06-26 05:53:03 +02:00
|
|
|
bool isSingleplayer() const { return m_isSingleplayer; }
|
2019-01-09 20:44:58 +01:00
|
|
|
|
2020-03-18 02:02:47 +01:00
|
|
|
u16 port() const { return m_port; }
|
|
|
|
|
2020-02-11 15:00:03 +09:00
|
|
|
const ServerInfo &info() const { return m_info; }
|
2019-01-09 20:44:58 +01:00
|
|
|
|
|
|
|
void setRunning(bool isRunning) { m_isRunning = isRunning; }
|
2020-03-04 14:38:31 +01:00
|
|
|
void setSingleplayer(bool isSingleplayer) { m_isSingleplayer = isSingleplayer; }
|
2019-01-09 20:44:58 +01:00
|
|
|
|
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();
|
|
|
|
|
2020-03-06 14:14:11 +01:00
|
|
|
void disconnectClient(ClientInfo &client);
|
|
|
|
|
2020-02-14 18:36:45 +09:00
|
|
|
bool m_isRunning = false;
|
2020-03-04 14:38:31 +01:00
|
|
|
bool m_isSingleplayer = false;
|
2019-01-09 20:44:58 +01:00
|
|
|
|
2020-03-18 02:02:47 +01:00
|
|
|
u16 m_port = 0;
|
|
|
|
|
2019-01-09 20:44:58 +01:00
|
|
|
ServerInfo m_info;
|
|
|
|
|
|
|
|
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_
|