2021-12-05 17:54:36 +00:00
|
|
|
#include <iostream>
|
|
|
|
#include <string>
|
|
|
|
#include <chrono>
|
|
|
|
#include <thread>
|
|
|
|
|
|
|
|
//DLU Includes:
|
|
|
|
#include "dCommonVars.h"
|
|
|
|
#include "dServer.h"
|
2023-10-21 23:31:55 +00:00
|
|
|
#include "Logger.h"
|
2021-12-05 17:54:36 +00:00
|
|
|
#include "Database.h"
|
|
|
|
#include "dConfig.h"
|
|
|
|
#include "dChatFilter.h"
|
|
|
|
#include "Diagnostics.h"
|
2022-11-01 18:21:26 +00:00
|
|
|
#include "AssetManager.h"
|
2022-11-27 11:59:59 +00:00
|
|
|
#include "BinaryPathFinder.h"
|
2023-05-03 21:38:32 +00:00
|
|
|
#include "eConnectionType.h"
|
2021-12-05 17:54:36 +00:00
|
|
|
#include "PlayerContainer.h"
|
|
|
|
#include "ChatPacketHandler.h"
|
2023-05-03 21:38:32 +00:00
|
|
|
#include "eChatMessageType.h"
|
|
|
|
#include "eChatInternalMessageType.h"
|
|
|
|
#include "eWorldMessageType.h"
|
2023-11-18 11:55:12 +00:00
|
|
|
#include "ChatIgnoreList.h"
|
2021-12-05 17:54:36 +00:00
|
|
|
|
|
|
|
#include "Game.h"
|
2022-12-16 10:24:02 +00:00
|
|
|
|
|
|
|
//RakNet includes:
|
|
|
|
#include "RakNetDefines.h"
|
2024-01-05 12:33:52 +00:00
|
|
|
#include "MessageIdentifiers.h"
|
2023-05-03 21:38:32 +00:00
|
|
|
|
2021-12-05 17:54:36 +00:00
|
|
|
namespace Game {
|
2023-10-21 23:31:55 +00:00
|
|
|
Logger* logger = nullptr;
|
2022-12-15 14:13:49 +00:00
|
|
|
dServer* server = nullptr;
|
|
|
|
dConfig* config = nullptr;
|
|
|
|
dChatFilter* chatFilter = nullptr;
|
|
|
|
AssetManager* assetManager = nullptr;
|
2024-01-02 03:50:00 +00:00
|
|
|
Game::signal_t lastSignal = 0;
|
2023-08-04 02:38:19 +00:00
|
|
|
std::mt19937 randomEngine;
|
2023-11-19 01:33:52 +00:00
|
|
|
PlayerContainer playerContainer;
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
|
2023-10-21 23:31:55 +00:00
|
|
|
Logger* SetupLogger();
|
2021-12-05 17:54:36 +00:00
|
|
|
void HandlePacket(Packet* packet);
|
|
|
|
|
|
|
|
int main(int argc, char** argv) {
|
2022-12-16 10:24:02 +00:00
|
|
|
constexpr uint32_t chatFramerate = mediumFramerate;
|
|
|
|
constexpr uint32_t chatFrameDelta = mediumFrameDelta;
|
2021-12-05 17:54:36 +00:00
|
|
|
Diagnostics::SetProcessName("Chat");
|
|
|
|
Diagnostics::SetProcessFileName(argv[0]);
|
|
|
|
Diagnostics::Initialize();
|
|
|
|
|
2024-01-02 03:50:00 +00:00
|
|
|
std::signal(SIGINT, Game::OnSignal);
|
|
|
|
std::signal(SIGTERM, Game::OnSignal);
|
|
|
|
|
2021-12-05 17:54:36 +00:00
|
|
|
//Create all the objects we need to run our service:
|
|
|
|
Game::logger = SetupLogger();
|
2022-12-15 14:13:49 +00:00
|
|
|
if (!Game::logger) return EXIT_FAILURE;
|
|
|
|
|
|
|
|
//Read our config:
|
2023-12-30 13:07:49 +00:00
|
|
|
Game::config = new dConfig("chatconfig.ini");
|
2022-12-15 14:13:49 +00:00
|
|
|
Game::logger->SetLogToConsole(Game::config->GetValue("log_to_console") != "0");
|
|
|
|
Game::logger->SetLogDebugStatements(Game::config->GetValue("log_debug_statements") == "1");
|
|
|
|
|
2023-10-21 23:31:55 +00:00
|
|
|
LOG("Starting Chat server...");
|
2023-12-31 06:26:49 +00:00
|
|
|
LOG("Version: %s", PROJECT_VERSION);
|
2023-10-21 23:31:55 +00:00
|
|
|
LOG("Compiled on: %s", __TIMESTAMP__);
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-11-01 18:21:26 +00:00
|
|
|
try {
|
2022-12-15 14:13:49 +00:00
|
|
|
std::string clientPathStr = Game::config->GetValue("client_location");
|
2022-11-27 11:59:59 +00:00
|
|
|
if (clientPathStr.empty()) clientPathStr = "./res";
|
|
|
|
std::filesystem::path clientPath = std::filesystem::path(clientPathStr);
|
|
|
|
if (clientPath.is_relative()) {
|
|
|
|
clientPath = BinaryPathFinder::GetBinaryDir() / clientPath;
|
|
|
|
}
|
|
|
|
|
|
|
|
Game::assetManager = new AssetManager(clientPath);
|
2022-11-01 18:21:26 +00:00
|
|
|
} catch (std::runtime_error& ex) {
|
2023-10-21 23:31:55 +00:00
|
|
|
LOG("Got an error while setting up assets: %s", ex.what());
|
2023-05-03 21:38:32 +00:00
|
|
|
|
2022-11-01 18:21:26 +00:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
2021-12-05 17:54:36 +00:00
|
|
|
//Connect to the MySQL Database
|
|
|
|
try {
|
2023-11-18 00:47:18 +00:00
|
|
|
Database::Connect();
|
2022-07-28 13:39:57 +00:00
|
|
|
} catch (sql::SQLException& ex) {
|
2023-10-21 23:31:55 +00:00
|
|
|
LOG("Got an error while connecting to the database: %s", ex.what());
|
2022-04-09 22:35:01 +00:00
|
|
|
Database::Destroy("ChatServer");
|
2021-12-05 17:54:36 +00:00
|
|
|
delete Game::server;
|
|
|
|
delete Game::logger;
|
2022-12-15 14:13:49 +00:00
|
|
|
return EXIT_FAILURE;
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//Find out the master's IP:
|
|
|
|
std::string masterIP;
|
2022-12-16 10:24:02 +00:00
|
|
|
uint32_t masterPort = 1000;
|
2023-11-18 00:47:18 +00:00
|
|
|
auto masterInfo = Database::Get()->GetMasterInfo();
|
|
|
|
if (masterInfo) {
|
|
|
|
masterIP = masterInfo->ip;
|
|
|
|
masterPort = masterInfo->port;
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
//It's safe to pass 'localhost' here, as the IP is only used as the external IP.
|
2024-01-05 00:21:03 +00:00
|
|
|
uint32_t maxClients = 999;
|
2022-12-16 10:24:02 +00:00
|
|
|
uint32_t ourPort = 1501;
|
2024-01-05 00:21:03 +00:00
|
|
|
std::string ourIP = "localhost";
|
|
|
|
GeneralUtils::TryParse(Game::config->GetValue("max_clients"), maxClients);
|
|
|
|
GeneralUtils::TryParse(Game::config->GetValue("chat_server_port"), ourPort);
|
|
|
|
const auto externalIPString = Game::config->GetValue("external_ip");
|
|
|
|
if (!externalIPString.empty()) ourIP = externalIPString;
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2024-01-05 00:21:03 +00:00
|
|
|
Game::server = new dServer(ourIP, ourPort, 0, maxClients, false, true, Game::logger, masterIP, masterPort, ServerType::Chat, Game::config, &Game::lastSignal);
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2024-01-05 00:21:03 +00:00
|
|
|
bool dontGenerateDCF = false;
|
|
|
|
GeneralUtils::TryParse(Game::config->GetValue("dont_generate_dcf"), dontGenerateDCF);
|
|
|
|
Game::chatFilter = new dChatFilter(Game::assetManager->GetResPath().string() + "/chatplus_en_us", dontGenerateDCF);
|
2023-08-04 02:38:19 +00:00
|
|
|
|
|
|
|
Game::randomEngine = std::mt19937(time(0));
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2023-11-19 01:33:52 +00:00
|
|
|
Game::playerContainer.Initialize();
|
2023-11-18 07:15:31 +00:00
|
|
|
|
2021-12-05 17:54:36 +00:00
|
|
|
//Run it until server gets a kill message from Master:
|
|
|
|
auto t = std::chrono::high_resolution_clock::now();
|
|
|
|
Packet* packet = nullptr;
|
2022-12-16 10:24:02 +00:00
|
|
|
constexpr uint32_t logFlushTime = 30 * chatFramerate; // 30 seconds in frames
|
|
|
|
constexpr uint32_t sqlPingTime = 10 * 60 * chatFramerate; // 10 minutes in frames
|
|
|
|
uint32_t framesSinceLastFlush = 0;
|
|
|
|
uint32_t framesSinceMasterDisconnect = 0;
|
|
|
|
uint32_t framesSinceLastSQLPing = 0;
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2024-01-02 03:50:00 +00:00
|
|
|
Game::logger->Flush(); // once immediately before main loop
|
|
|
|
while (!Game::ShouldShutdown()) {
|
2021-12-05 17:54:36 +00:00
|
|
|
//Check if we're still connected to master:
|
|
|
|
if (!Game::server->GetIsConnectedToMaster()) {
|
|
|
|
framesSinceMasterDisconnect++;
|
|
|
|
|
2022-12-16 10:24:02 +00:00
|
|
|
if (framesSinceMasterDisconnect >= chatFramerate)
|
2021-12-05 17:54:36 +00:00
|
|
|
break; //Exit our loop, shut down.
|
2022-07-28 13:39:57 +00:00
|
|
|
} else framesSinceMasterDisconnect = 0;
|
2021-12-05 17:54:36 +00:00
|
|
|
|
|
|
|
//In world we'd update our other systems here.
|
|
|
|
|
|
|
|
//Check for packets here:
|
|
|
|
Game::server->ReceiveFromMaster(); //ReceiveFromMaster also handles the master packets if needed.
|
|
|
|
packet = Game::server->Receive();
|
|
|
|
if (packet) {
|
|
|
|
HandlePacket(packet);
|
|
|
|
Game::server->DeallocatePacket(packet);
|
|
|
|
packet = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Push our log every 30s:
|
2022-12-16 10:24:02 +00:00
|
|
|
if (framesSinceLastFlush >= logFlushTime) {
|
2021-12-05 17:54:36 +00:00
|
|
|
Game::logger->Flush();
|
|
|
|
framesSinceLastFlush = 0;
|
2022-07-28 13:39:57 +00:00
|
|
|
} else framesSinceLastFlush++;
|
2021-12-05 17:54:36 +00:00
|
|
|
|
|
|
|
//Every 10 min we ping our sql server to keep it alive hopefully:
|
2022-12-16 10:24:02 +00:00
|
|
|
if (framesSinceLastSQLPing >= sqlPingTime) {
|
2021-12-05 17:54:36 +00:00
|
|
|
//Find out the master's IP for absolutely no reason:
|
|
|
|
std::string masterIP;
|
2022-12-16 10:24:02 +00:00
|
|
|
uint32_t masterPort;
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2023-11-18 00:47:18 +00:00
|
|
|
auto masterInfo = Database::Get()->GetMasterInfo();
|
|
|
|
if (masterInfo) {
|
|
|
|
masterIP = masterInfo->ip;
|
|
|
|
masterPort = masterInfo->port;
|
|
|
|
}
|
2021-12-05 17:54:36 +00:00
|
|
|
|
|
|
|
framesSinceLastSQLPing = 0;
|
2022-07-28 13:39:57 +00:00
|
|
|
} else framesSinceLastSQLPing++;
|
2021-12-05 17:54:36 +00:00
|
|
|
|
|
|
|
//Sleep our thread since auth can afford to.
|
2022-12-16 10:24:02 +00:00
|
|
|
t += std::chrono::milliseconds(chatFrameDelta); //Chat can run at a lower "fps"
|
2021-12-05 17:54:36 +00:00
|
|
|
std::this_thread::sleep_until(t);
|
|
|
|
}
|
|
|
|
|
|
|
|
//Delete our objects here:
|
2022-04-09 22:35:01 +00:00
|
|
|
Database::Destroy("ChatServer");
|
2021-12-05 17:54:36 +00:00
|
|
|
delete Game::server;
|
|
|
|
delete Game::logger;
|
2022-12-15 14:13:49 +00:00
|
|
|
delete Game::config;
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-04-09 22:57:18 +00:00
|
|
|
return EXIT_SUCCESS;
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
|
2023-10-21 23:31:55 +00:00
|
|
|
Logger* SetupLogger() {
|
2022-11-27 11:59:59 +00:00
|
|
|
std::string logPath = (BinaryPathFinder::GetBinaryDir() / ("logs/ChatServer_" + std::to_string(time(nullptr)) + ".log")).string();
|
2021-12-05 17:54:36 +00:00
|
|
|
bool logToConsole = false;
|
2021-12-11 12:29:34 +00:00
|
|
|
bool logDebugStatements = false;
|
2021-12-05 17:54:36 +00:00
|
|
|
#ifdef _DEBUG
|
|
|
|
logToConsole = true;
|
2021-12-11 12:29:34 +00:00
|
|
|
logDebugStatements = true;
|
2021-12-05 17:54:36 +00:00
|
|
|
#endif
|
|
|
|
|
2023-10-21 23:31:55 +00:00
|
|
|
return new Logger(logPath, logToConsole, logDebugStatements);
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void HandlePacket(Packet* packet) {
|
|
|
|
if (packet->data[0] == ID_DISCONNECTION_NOTIFICATION || packet->data[0] == ID_CONNECTION_LOST) {
|
2023-10-21 23:31:55 +00:00
|
|
|
LOG("A server has disconnected, erasing their connected players from the list.");
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (packet->data[0] == ID_NEW_INCOMING_CONNECTION) {
|
2023-10-21 23:31:55 +00:00
|
|
|
LOG("A server is connecting, awaiting user list.");
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
|
2023-05-06 18:32:53 +00:00
|
|
|
if (packet->length < 4) return; // Nothing left to process. Need 4 bytes to continue.
|
|
|
|
|
2023-05-03 21:38:32 +00:00
|
|
|
if (static_cast<eConnectionType>(packet->data[1]) == eConnectionType::CHAT_INTERNAL) {
|
|
|
|
switch (static_cast<eChatInternalMessageType>(packet->data[3])) {
|
|
|
|
case eChatInternalMessageType::PLAYER_ADDED_NOTIFICATION:
|
2023-11-19 01:33:52 +00:00
|
|
|
Game::playerContainer.InsertPlayer(packet);
|
2021-12-05 17:54:36 +00:00
|
|
|
break;
|
|
|
|
|
2023-05-03 21:38:32 +00:00
|
|
|
case eChatInternalMessageType::PLAYER_REMOVED_NOTIFICATION:
|
2023-11-19 01:33:52 +00:00
|
|
|
Game::playerContainer.RemovePlayer(packet);
|
2021-12-05 17:54:36 +00:00
|
|
|
break;
|
|
|
|
|
2023-05-03 21:38:32 +00:00
|
|
|
case eChatInternalMessageType::MUTE_UPDATE:
|
2023-11-19 01:33:52 +00:00
|
|
|
Game::playerContainer.MuteUpdate(packet);
|
2021-12-05 17:54:36 +00:00
|
|
|
break;
|
|
|
|
|
2023-05-03 21:38:32 +00:00
|
|
|
case eChatInternalMessageType::CREATE_TEAM:
|
2023-11-19 01:33:52 +00:00
|
|
|
Game::playerContainer.CreateTeamServer(packet);
|
2021-12-05 17:54:36 +00:00
|
|
|
break;
|
|
|
|
|
2023-05-03 21:38:32 +00:00
|
|
|
case eChatInternalMessageType::ANNOUNCEMENT: {
|
2021-12-05 17:54:36 +00:00
|
|
|
//we just forward this packet to every connected server
|
|
|
|
CINSTREAM;
|
|
|
|
Game::server->Send(&inStream, packet->systemAddress, true); //send to everyone except origin
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
2023-10-21 23:31:55 +00:00
|
|
|
LOG("Unknown CHAT_INTERNAL id: %i", int(packet->data[3]));
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-03 21:38:32 +00:00
|
|
|
if (static_cast<eConnectionType>(packet->data[1]) == eConnectionType::CHAT) {
|
|
|
|
switch (static_cast<eChatMessageType>(packet->data[3])) {
|
|
|
|
case eChatMessageType::GET_FRIENDS_LIST:
|
2021-12-05 17:54:36 +00:00
|
|
|
ChatPacketHandler::HandleFriendlistRequest(packet);
|
|
|
|
break;
|
|
|
|
|
2023-05-03 21:38:32 +00:00
|
|
|
case eChatMessageType::GET_IGNORE_LIST:
|
2023-11-18 11:55:12 +00:00
|
|
|
ChatIgnoreList::GetIgnoreList(packet);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case eChatMessageType::ADD_IGNORE:
|
|
|
|
ChatIgnoreList::AddIgnore(packet);
|
2021-12-05 17:54:36 +00:00
|
|
|
break;
|
|
|
|
|
2023-11-19 01:33:52 +00:00
|
|
|
case eChatMessageType::REMOVE_IGNORE:
|
|
|
|
ChatIgnoreList::RemoveIgnore(packet);
|
|
|
|
break;
|
|
|
|
|
2023-05-03 21:38:32 +00:00
|
|
|
case eChatMessageType::TEAM_GET_STATUS:
|
2021-12-05 17:54:36 +00:00
|
|
|
ChatPacketHandler::HandleTeamStatusRequest(packet);
|
|
|
|
break;
|
|
|
|
|
2023-05-03 21:38:32 +00:00
|
|
|
case eChatMessageType::ADD_FRIEND_REQUEST:
|
2021-12-05 17:54:36 +00:00
|
|
|
//this involves someone sending the initial request, the response is below, response as in from the other player.
|
|
|
|
//We basically just check to see if this player is online or not and route the packet.
|
|
|
|
ChatPacketHandler::HandleFriendRequest(packet);
|
|
|
|
break;
|
|
|
|
|
2023-05-03 21:38:32 +00:00
|
|
|
case eChatMessageType::ADD_FRIEND_RESPONSE:
|
2021-12-05 17:54:36 +00:00
|
|
|
//This isn't the response a server sent, rather it is a player's response to a received request.
|
|
|
|
//Here, we'll actually have to add them to eachother's friend lists depending on the response code.
|
|
|
|
ChatPacketHandler::HandleFriendResponse(packet);
|
|
|
|
break;
|
|
|
|
|
2023-05-03 21:38:32 +00:00
|
|
|
case eChatMessageType::REMOVE_FRIEND:
|
2021-12-05 17:54:36 +00:00
|
|
|
ChatPacketHandler::HandleRemoveFriend(packet);
|
|
|
|
break;
|
|
|
|
|
2023-05-03 21:38:32 +00:00
|
|
|
case eChatMessageType::GENERAL_CHAT_MESSAGE:
|
2021-12-05 17:54:36 +00:00
|
|
|
ChatPacketHandler::HandleChatMessage(packet);
|
|
|
|
break;
|
|
|
|
|
2023-05-03 21:38:32 +00:00
|
|
|
case eChatMessageType::PRIVATE_CHAT_MESSAGE:
|
2021-12-05 17:54:36 +00:00
|
|
|
//This message is supposed to be echo'd to both the sender and the receiver
|
|
|
|
//BUT: they have to have different responseCodes, so we'll do some of the ol hacky wacky to fix that right up.
|
|
|
|
ChatPacketHandler::HandlePrivateChatMessage(packet);
|
|
|
|
break;
|
|
|
|
|
2023-05-03 21:38:32 +00:00
|
|
|
case eChatMessageType::TEAM_INVITE:
|
2021-12-05 17:54:36 +00:00
|
|
|
ChatPacketHandler::HandleTeamInvite(packet);
|
|
|
|
break;
|
|
|
|
|
2023-05-03 21:38:32 +00:00
|
|
|
case eChatMessageType::TEAM_INVITE_RESPONSE:
|
2021-12-05 17:54:36 +00:00
|
|
|
ChatPacketHandler::HandleTeamInviteResponse(packet);
|
|
|
|
break;
|
|
|
|
|
2023-05-03 21:38:32 +00:00
|
|
|
case eChatMessageType::TEAM_LEAVE:
|
2021-12-05 17:54:36 +00:00
|
|
|
ChatPacketHandler::HandleTeamLeave(packet);
|
|
|
|
break;
|
|
|
|
|
2023-05-03 21:38:32 +00:00
|
|
|
case eChatMessageType::TEAM_SET_LEADER:
|
2021-12-05 17:54:36 +00:00
|
|
|
ChatPacketHandler::HandleTeamPromote(packet);
|
|
|
|
break;
|
|
|
|
|
2023-05-03 21:38:32 +00:00
|
|
|
case eChatMessageType::TEAM_KICK:
|
2021-12-05 17:54:36 +00:00
|
|
|
ChatPacketHandler::HandleTeamKick(packet);
|
|
|
|
break;
|
|
|
|
|
2023-05-03 21:38:32 +00:00
|
|
|
case eChatMessageType::TEAM_SET_LOOT:
|
2021-12-05 17:54:36 +00:00
|
|
|
ChatPacketHandler::HandleTeamLootOption(packet);
|
|
|
|
break;
|
2022-07-25 02:26:51 +00:00
|
|
|
|
2021-12-05 17:54:36 +00:00
|
|
|
default:
|
2023-10-21 23:31:55 +00:00
|
|
|
LOG("Unknown CHAT id: %i", int(packet->data[3]));
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-03 21:38:32 +00:00
|
|
|
if (static_cast<eConnectionType>(packet->data[1]) == eConnectionType::WORLD) {
|
|
|
|
switch (static_cast<eWorldMessageType>(packet->data[3])) {
|
|
|
|
case eWorldMessageType::ROUTE_PACKET: {
|
2023-10-21 23:31:55 +00:00
|
|
|
LOG("Routing packet from world");
|
2021-12-05 17:54:36 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
2023-10-21 23:31:55 +00:00
|
|
|
LOG("Unknown World id: %i", int(packet->data[3]));
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|