2023-11-11 16:43:58 +02:00
|
|
|
/*
|
|
|
|
* LobbyServer.h, part of VCMI engine
|
|
|
|
*
|
|
|
|
* Authors: listed in file AUTHORS in main folder
|
|
|
|
*
|
|
|
|
* License: GNU General Public License v2.0 or later
|
|
|
|
* Full text of license available in license.txt file, in main folder
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
|
2024-01-12 01:10:41 +02:00
|
|
|
#include "../lib/network/NetworkInterface.h"
|
2024-01-11 22:51:36 +02:00
|
|
|
#include "LobbyDefines.h"
|
2023-11-11 16:43:58 +02:00
|
|
|
|
2023-11-12 16:35:53 +02:00
|
|
|
VCMI_LIB_NAMESPACE_BEGIN
|
|
|
|
class JsonNode;
|
|
|
|
VCMI_LIB_NAMESPACE_END
|
|
|
|
|
2023-12-29 13:09:56 +02:00
|
|
|
class LobbyDatabase;
|
2023-11-12 15:32:54 +02:00
|
|
|
|
2024-02-02 00:29:15 +02:00
|
|
|
class LobbyServer final : public INetworkServerListener
|
2023-11-12 15:32:54 +02:00
|
|
|
{
|
2023-12-30 00:41:16 +02:00
|
|
|
struct AwaitingProxyState
|
|
|
|
{
|
|
|
|
std::string accountID;
|
|
|
|
std::string roomID;
|
2024-02-02 00:29:15 +02:00
|
|
|
NetworkConnectionWeakPtr accountConnection;
|
|
|
|
NetworkConnectionWeakPtr roomConnection;
|
2023-12-30 00:41:16 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/// list of connected proxies. All messages received from (key) will be redirected to (value) connection
|
2024-02-02 15:32:06 +02:00
|
|
|
std::map<NetworkConnectionPtr, NetworkConnectionPtr> activeProxies;
|
2023-12-30 00:41:16 +02:00
|
|
|
|
|
|
|
/// list of half-established proxies from server that are still waiting for client to connect
|
|
|
|
std::vector<AwaitingProxyState> awaitingProxies;
|
2023-11-12 21:23:42 +02:00
|
|
|
|
2023-12-30 00:41:16 +02:00
|
|
|
/// list of logged in accounts (vcmiclient's)
|
2024-02-02 00:29:15 +02:00
|
|
|
std::map<NetworkConnectionPtr, std::string> activeAccounts;
|
2023-12-30 00:41:16 +02:00
|
|
|
|
|
|
|
/// list of currently logged in game rooms (vcmiserver's)
|
2024-02-02 00:29:15 +02:00
|
|
|
std::map<NetworkConnectionPtr, std::string> activeGameRooms;
|
2023-11-12 21:23:42 +02:00
|
|
|
|
2023-11-12 15:32:54 +02:00
|
|
|
std::unique_ptr<LobbyDatabase> database;
|
2024-01-12 01:10:41 +02:00
|
|
|
std::unique_ptr<INetworkHandler> networkHandler;
|
|
|
|
std::unique_ptr<INetworkServer> networkServer;
|
2023-11-11 16:43:58 +02:00
|
|
|
|
2024-03-27 13:08:41 +02:00
|
|
|
/// removes any "weird" symbols from chat message that might break UI
|
2023-12-30 00:41:16 +02:00
|
|
|
std::string sanitizeChatMessage(const std::string & inputString) const;
|
2024-03-27 13:08:41 +02:00
|
|
|
|
2024-01-26 17:40:31 +02:00
|
|
|
bool isAccountNameValid(const std::string & accountName) const;
|
2023-12-29 13:09:56 +02:00
|
|
|
|
2024-01-26 17:40:31 +02:00
|
|
|
NetworkConnectionPtr findAccount(const std::string & accountID) const;
|
|
|
|
NetworkConnectionPtr findGameRoom(const std::string & gameRoomID) const;
|
2023-12-30 00:41:16 +02:00
|
|
|
|
|
|
|
void onNewConnection(const NetworkConnectionPtr & connection) override;
|
2024-02-02 01:27:19 +02:00
|
|
|
void onDisconnected(const NetworkConnectionPtr & connection, const std::string & errorMessage) override;
|
|
|
|
void onPacketReceived(const NetworkConnectionPtr & connection, const std::vector<std::byte> & message) override;
|
2023-11-12 16:35:53 +02:00
|
|
|
|
2023-12-30 00:41:16 +02:00
|
|
|
void sendMessage(const NetworkConnectionPtr & target, const JsonNode & json);
|
|
|
|
|
|
|
|
void broadcastActiveAccounts();
|
|
|
|
void broadcastActiveGameRooms();
|
|
|
|
|
2024-01-29 22:05:11 +02:00
|
|
|
JsonNode prepareActiveGameRooms();
|
|
|
|
|
2024-03-04 14:12:15 +02:00
|
|
|
/// Attempts to load json from incoming byte stream and validate it
|
|
|
|
/// Returns parsed json on success or empty json node on failure
|
|
|
|
JsonNode parseAndValidateMessage(const std::vector<std::byte> & message) const;
|
|
|
|
|
2024-03-13 23:02:53 +02:00
|
|
|
void sendChatMessage(const NetworkConnectionPtr & target, const std::string & channelType, const std::string & channelName, const std::string & accountID, const std::string & displayName, const std::string & messageText);
|
2023-12-30 00:41:16 +02:00
|
|
|
void sendAccountCreated(const NetworkConnectionPtr & target, const std::string & accountID, const std::string & accountCookie);
|
2024-02-02 00:12:30 +02:00
|
|
|
void sendOperationFailed(const NetworkConnectionPtr & target, const std::string & reason);
|
2024-03-11 17:48:56 +02:00
|
|
|
void sendServerLoginSuccess(const NetworkConnectionPtr & target, const std::string & accountCookie);
|
|
|
|
void sendClientLoginSuccess(const NetworkConnectionPtr & target, const std::string & accountCookie, const std::string & displayName);
|
2024-03-13 23:02:53 +02:00
|
|
|
void sendFullChatHistory(const NetworkConnectionPtr & target, const std::string & channelType, const std::string & channelName, const std::string & channelNameForClient);
|
|
|
|
void sendRecentChatHistory(const NetworkConnectionPtr & target, const std::string & channelType, const std::string & channelName);
|
|
|
|
void sendChatHistory(const NetworkConnectionPtr & target, const std::string & channelType, const std::string & channelName, const std::vector<LobbyChatMessage> & history);
|
2023-12-30 00:41:16 +02:00
|
|
|
void sendAccountJoinsRoom(const NetworkConnectionPtr & target, const std::string & accountID);
|
2024-01-29 22:05:11 +02:00
|
|
|
void sendJoinRoomSuccess(const NetworkConnectionPtr & target, const std::string & gameRoomID, bool proxyMode);
|
2023-12-30 00:41:16 +02:00
|
|
|
void sendInviteReceived(const NetworkConnectionPtr & target, const std::string & accountID, const std::string & gameRoomID);
|
2024-03-20 18:40:37 +02:00
|
|
|
void sendMatchesHistory(const NetworkConnectionPtr & target);
|
2023-12-30 00:41:16 +02:00
|
|
|
|
|
|
|
void receiveClientRegister(const NetworkConnectionPtr & connection, const JsonNode & json);
|
|
|
|
void receiveClientLogin(const NetworkConnectionPtr & connection, const JsonNode & json);
|
|
|
|
void receiveServerLogin(const NetworkConnectionPtr & connection, const JsonNode & json);
|
|
|
|
void receiveClientProxyLogin(const NetworkConnectionPtr & connection, const JsonNode & json);
|
|
|
|
void receiveServerProxyLogin(const NetworkConnectionPtr & connection, const JsonNode & json);
|
2023-12-27 14:24:49 +02:00
|
|
|
|
2023-12-30 00:41:16 +02:00
|
|
|
void receiveSendChatMessage(const NetworkConnectionPtr & connection, const JsonNode & json);
|
2024-03-13 23:02:53 +02:00
|
|
|
void receiveRequestChatHistory(const NetworkConnectionPtr & connection, const JsonNode & json);
|
2024-03-04 14:11:45 +02:00
|
|
|
void receiveActivateGameRoom(const NetworkConnectionPtr & connection, const JsonNode & json);
|
2023-12-30 00:41:16 +02:00
|
|
|
void receiveJoinGameRoom(const NetworkConnectionPtr & connection, const JsonNode & json);
|
|
|
|
void receiveLeaveGameRoom(const NetworkConnectionPtr & connection, const JsonNode & json);
|
2024-03-11 19:47:35 +02:00
|
|
|
void receiveChangeRoomDescription(const NetworkConnectionPtr & connection, const JsonNode & json);
|
2024-03-12 20:53:39 +02:00
|
|
|
void receiveGameStarted(const NetworkConnectionPtr & connection, const JsonNode & json);
|
2023-12-30 00:41:16 +02:00
|
|
|
void receiveSendInvite(const NetworkConnectionPtr & connection, const JsonNode & json);
|
2023-12-29 13:19:04 +02:00
|
|
|
|
2023-11-11 16:43:58 +02:00
|
|
|
public:
|
2023-12-30 00:41:16 +02:00
|
|
|
explicit LobbyServer(const boost::filesystem::path & databasePath);
|
2023-12-29 13:09:56 +02:00
|
|
|
~LobbyServer();
|
2023-11-18 16:34:18 +02:00
|
|
|
|
|
|
|
void start(uint16_t port);
|
|
|
|
void run();
|
2023-11-11 16:43:58 +02:00
|
|
|
};
|