2016-01-12 03:41:44 +00:00
|
|
|
#include "Player.hpp"
|
2017-07-03 17:13:10 +00:00
|
|
|
#include "processors/ProcessorInitializer.hpp"
|
2016-01-12 03:41:44 +00:00
|
|
|
#include <RakPeer.h>
|
|
|
|
#include <Kbhit.h>
|
2017-07-28 03:57:15 +00:00
|
|
|
|
|
|
|
#include <components/misc/stringops.hpp>
|
2016-01-12 03:41:44 +00:00
|
|
|
#include <components/openmw-mp/NetworkMessages.hpp>
|
2019-08-19 18:39:33 +00:00
|
|
|
#include <components/openmw-mp/TimedLog.hpp>
|
2017-07-28 03:57:15 +00:00
|
|
|
#include <components/openmw-mp/Version.hpp>
|
|
|
|
#include <components/openmw-mp/Packets/PacketPreInit.hpp>
|
|
|
|
|
2016-01-12 03:41:44 +00:00
|
|
|
#include <iostream>
|
|
|
|
#include <Script/Script.hpp>
|
|
|
|
#include <Script/API/TimerAPI.hpp>
|
2016-08-05 06:15:00 +00:00
|
|
|
#include <chrono>
|
|
|
|
#include <thread>
|
2019-07-06 11:03:50 +00:00
|
|
|
#include <csignal>
|
2016-01-12 03:41:44 +00:00
|
|
|
|
|
|
|
#include "Networking.hpp"
|
2017-01-10 11:21:31 +00:00
|
|
|
#include "MasterClient.hpp"
|
2017-02-19 05:27:39 +00:00
|
|
|
#include "Cell.hpp"
|
2017-04-29 20:05:12 +00:00
|
|
|
#include "CellController.hpp"
|
2017-07-28 03:57:15 +00:00
|
|
|
#include "processors/PlayerProcessor.hpp"
|
|
|
|
#include "processors/ActorProcessor.hpp"
|
2018-05-15 19:07:06 +00:00
|
|
|
#include "processors/ObjectProcessor.hpp"
|
2018-05-18 03:40:28 +00:00
|
|
|
#include "processors/WorldstateProcessor.hpp"
|
2016-01-12 03:41:44 +00:00
|
|
|
|
2020-03-15 16:18:00 +00:00
|
|
|
#include "handleInput.cpp"
|
2020-03-01 17:30:00 +00:00
|
|
|
|
2016-01-12 03:41:44 +00:00
|
|
|
using namespace mwmp;
|
|
|
|
|
|
|
|
Networking *Networking::sThis = 0;
|
2017-01-29 08:24:12 +00:00
|
|
|
|
2017-04-04 06:23:34 +00:00
|
|
|
static int currentMpNum = 0;
|
2019-04-23 19:45:25 +00:00
|
|
|
static bool dataFileEnforcementState = true;
|
2018-12-01 01:03:39 +00:00
|
|
|
static bool scriptErrorIgnoringState = false;
|
2019-07-06 11:03:50 +00:00
|
|
|
bool killLoop = false;
|
2017-04-04 06:23:34 +00:00
|
|
|
|
2017-05-06 18:14:08 +00:00
|
|
|
Networking::Networking(RakNet::RakPeerInterface *peer) : mclient(nullptr)
|
2016-01-12 03:41:44 +00:00
|
|
|
{
|
|
|
|
sThis = this;
|
|
|
|
this->peer = peer;
|
2016-11-16 00:05:14 +00:00
|
|
|
players = Players::getPlayers();
|
2016-01-12 03:41:44 +00:00
|
|
|
|
2017-02-19 08:07:44 +00:00
|
|
|
CellController::create();
|
2017-02-19 05:27:39 +00:00
|
|
|
|
2019-11-09 03:12:00 +00:00
|
|
|
systemPacketController = new SystemPacketController(peer);
|
2017-04-09 05:51:28 +00:00
|
|
|
playerPacketController = new PlayerPacketController(peer);
|
|
|
|
actorPacketController = new ActorPacketController(peer);
|
2018-05-12 16:40:00 +00:00
|
|
|
objectPacketController = new ObjectPacketController(peer);
|
2018-05-18 03:40:28 +00:00
|
|
|
worldstatePacketController = new WorldstatePacketController(peer);
|
2016-01-12 03:41:44 +00:00
|
|
|
|
2016-10-19 17:51:51 +00:00
|
|
|
// Set send stream
|
2019-11-09 03:12:00 +00:00
|
|
|
systemPacketController->SetStream(0, &bsOut);
|
2017-04-09 05:51:28 +00:00
|
|
|
playerPacketController->SetStream(0, &bsOut);
|
|
|
|
actorPacketController->SetStream(0, &bsOut);
|
2018-05-12 16:40:00 +00:00
|
|
|
objectPacketController->SetStream(0, &bsOut);
|
2018-05-21 04:12:55 +00:00
|
|
|
worldstatePacketController->SetStream(0, &bsOut);
|
2016-01-12 03:41:44 +00:00
|
|
|
|
|
|
|
running = true;
|
|
|
|
exitCode = 0;
|
2016-09-30 10:13:50 +00:00
|
|
|
|
2016-11-16 17:27:46 +00:00
|
|
|
Script::Call<Script::CallbackIdentity("OnServerInit")>();
|
2017-02-20 13:15:04 +00:00
|
|
|
|
|
|
|
serverPassword = TES3MP_DEFAULT_PASSW;
|
2017-03-31 18:36:24 +00:00
|
|
|
|
2017-04-01 00:14:26 +00:00
|
|
|
ProcessorInitializer();
|
2016-01-12 03:41:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Networking::~Networking()
|
|
|
|
{
|
2016-11-16 17:27:46 +00:00
|
|
|
Script::Call<Script::CallbackIdentity("OnServerExit")>(false);
|
2016-01-12 03:41:44 +00:00
|
|
|
|
2017-02-19 08:07:44 +00:00
|
|
|
CellController::destroy();
|
2017-02-19 05:27:39 +00:00
|
|
|
|
2016-01-12 03:41:44 +00:00
|
|
|
sThis = 0;
|
2019-11-09 03:12:00 +00:00
|
|
|
delete systemPacketController;
|
2017-04-09 05:51:28 +00:00
|
|
|
delete playerPacketController;
|
2017-04-09 07:50:35 +00:00
|
|
|
delete actorPacketController;
|
2018-05-12 16:40:00 +00:00
|
|
|
delete objectPacketController;
|
2018-05-21 04:12:55 +00:00
|
|
|
delete worldstatePacketController;
|
2016-01-12 03:41:44 +00:00
|
|
|
}
|
|
|
|
|
2018-12-30 15:23:12 +00:00
|
|
|
void Networking::setServerPassword(std::string password) noexcept
|
2017-02-20 13:15:04 +00:00
|
|
|
{
|
2018-12-30 15:23:12 +00:00
|
|
|
serverPassword = password.empty() ? TES3MP_DEFAULT_PASSW : password;
|
2017-02-20 13:15:04 +00:00
|
|
|
}
|
|
|
|
|
2017-02-20 15:14:59 +00:00
|
|
|
bool Networking::isPassworded() const
|
|
|
|
{
|
|
|
|
return serverPassword != TES3MP_DEFAULT_PASSW;
|
|
|
|
}
|
|
|
|
|
2019-11-09 03:12:00 +00:00
|
|
|
void Networking::processSystemPacket(RakNet::Packet *packet)
|
2016-01-12 03:41:44 +00:00
|
|
|
{
|
2016-11-16 00:05:14 +00:00
|
|
|
Player *player = Players::getPlayer(packet->guid);
|
2016-01-12 03:41:44 +00:00
|
|
|
|
2019-11-09 03:12:00 +00:00
|
|
|
SystemPacket *myPacket = systemPacketController->GetPacket(packet->data[0]);
|
2016-01-12 03:41:44 +00:00
|
|
|
|
2019-11-09 03:12:00 +00:00
|
|
|
if (packet->data[0] == ID_SYSTEM_HANDSHAKE)
|
2016-01-12 03:41:44 +00:00
|
|
|
{
|
2019-11-09 03:12:00 +00:00
|
|
|
myPacket->setSystem(&baseSystem);
|
2017-03-06 09:44:08 +00:00
|
|
|
myPacket->Read();
|
2016-01-12 03:41:44 +00:00
|
|
|
|
2018-07-03 10:40:58 +00:00
|
|
|
if (!myPacket->isPacketValid())
|
|
|
|
{
|
2019-08-19 18:39:33 +00:00
|
|
|
LOG_MESSAGE_SIMPLE(TimedLog::LOG_ERROR, "Invalid handshake packet from client at %s", packet->systemAddress.ToString());
|
2018-07-03 10:40:58 +00:00
|
|
|
kickPlayer(player->guid);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-08-17 15:04:35 +00:00
|
|
|
if (player->isHandshaked())
|
2016-01-12 03:41:44 +00:00
|
|
|
{
|
2019-08-19 18:39:33 +00:00
|
|
|
LOG_MESSAGE_SIMPLE(TimedLog::LOG_WARN, "Wrong handshake with client at %s", packet->systemAddress.ToString());
|
2016-11-16 00:05:14 +00:00
|
|
|
kickPlayer(player->guid);
|
2016-01-12 03:41:44 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-11-09 03:12:00 +00:00
|
|
|
if (baseSystem.serverPassword != serverPassword)
|
2016-01-12 03:41:44 +00:00
|
|
|
{
|
2018-12-31 01:52:25 +00:00
|
|
|
if (isPassworded())
|
|
|
|
{
|
2019-08-19 18:39:33 +00:00
|
|
|
LOG_MESSAGE_SIMPLE(TimedLog::LOG_WARN, "Wrong server password %s used by client at %s",
|
2019-11-09 06:46:56 +00:00
|
|
|
baseSystem.serverPassword.c_str(), packet->systemAddress.ToString());
|
2018-12-31 01:52:25 +00:00
|
|
|
kickPlayer(player->guid);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-08-19 18:39:33 +00:00
|
|
|
LOG_MESSAGE_SIMPLE(TimedLog::LOG_INFO, "Client at %s tried to join using password, despite the server not being passworded",
|
2018-12-31 01:52:25 +00:00
|
|
|
packet->systemAddress.ToString());
|
|
|
|
}
|
2016-01-12 03:41:44 +00:00
|
|
|
}
|
2016-11-16 00:05:14 +00:00
|
|
|
player->setHandshake();
|
2016-09-18 03:54:45 +00:00
|
|
|
return;
|
|
|
|
}
|
2019-11-09 03:12:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Networking::processPlayerPacket(RakNet::Packet *packet)
|
|
|
|
{
|
|
|
|
Player *player = Players::getPlayer(packet->guid);
|
|
|
|
|
|
|
|
PlayerPacket *myPacket = playerPacketController->GetPacket(packet->data[0]);
|
2016-09-18 03:54:45 +00:00
|
|
|
|
|
|
|
if (!player->isHandshaked())
|
|
|
|
{
|
2018-04-09 12:25:50 +00:00
|
|
|
player->incrementHandshakeAttempts();
|
2019-08-19 18:39:33 +00:00
|
|
|
LOG_MESSAGE_SIMPLE(TimedLog::LOG_WARN, "Have not completed handshake with client at %s", packet->systemAddress.ToString());
|
|
|
|
LOG_APPEND(TimedLog::LOG_WARN, "- Attempts so far: %i", player->getHandshakeAttempts());
|
2018-04-09 12:25:50 +00:00
|
|
|
|
2018-04-17 13:36:48 +00:00
|
|
|
if (player->getHandshakeAttempts() > 20)
|
|
|
|
kickPlayer(player->guid, false);
|
|
|
|
else if (player->getHandshakeAttempts() > 5)
|
|
|
|
kickPlayer(player->guid, true);
|
|
|
|
|
2016-09-18 03:54:45 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (packet->data[0] == ID_LOADED)
|
|
|
|
{
|
2016-11-16 00:05:14 +00:00
|
|
|
player->setLoadState(Player::LOADED);
|
2016-01-12 03:41:44 +00:00
|
|
|
|
2018-10-30 05:18:32 +00:00
|
|
|
unsigned short pid = Players::getPlayer(packet->guid)->getId();
|
|
|
|
Script::Call<Script::CallbackIdentity("OnPlayerConnect")>(pid);
|
2016-01-12 03:41:44 +00:00
|
|
|
|
2018-10-30 05:18:32 +00:00
|
|
|
if (player->getLoadState() == Player::KICKED) // kicked inside in OnPlayerConnect
|
2016-01-12 03:41:44 +00:00
|
|
|
{
|
2017-04-09 05:51:28 +00:00
|
|
|
playerPacketController->GetPacket(ID_USER_DISCONNECTED)->setPlayer(Players::getPlayer(packet->guid));
|
|
|
|
playerPacketController->GetPacket(ID_USER_DISCONNECTED)->Send(false);
|
2016-11-16 00:05:14 +00:00
|
|
|
Players::deletePlayer(packet->guid);
|
2016-01-12 03:41:44 +00:00
|
|
|
return;
|
|
|
|
}
|
2016-09-18 03:54:45 +00:00
|
|
|
}
|
2017-02-05 07:01:33 +00:00
|
|
|
else if (packet->data[0] == ID_PLAYER_BASEINFO)
|
2016-09-18 03:54:45 +00:00
|
|
|
{
|
2019-08-19 18:39:33 +00:00
|
|
|
LOG_MESSAGE_SIMPLE(TimedLog::LOG_INFO, "Received ID_PLAYER_BASEINFO about %s", player->npc.mName.c_str());
|
2016-01-12 03:41:44 +00:00
|
|
|
|
2017-03-06 09:44:08 +00:00
|
|
|
myPacket->setPlayer(player);
|
|
|
|
myPacket->Read();
|
|
|
|
myPacket->Send(true);
|
2016-01-12 03:41:44 +00:00
|
|
|
}
|
|
|
|
|
2016-11-16 00:05:14 +00:00
|
|
|
if (player->getLoadState() == Player::NOTLOADED)
|
2016-01-12 03:41:44 +00:00
|
|
|
return;
|
2016-11-16 00:05:14 +00:00
|
|
|
else if (player->getLoadState() == Player::LOADED)
|
2016-09-18 03:54:45 +00:00
|
|
|
{
|
2016-11-16 00:05:14 +00:00
|
|
|
player->setLoadState(Player::POSTLOADED);
|
|
|
|
newPlayer(packet->guid);
|
2016-09-18 07:52:13 +00:00
|
|
|
return;
|
2016-01-12 03:41:44 +00:00
|
|
|
}
|
|
|
|
|
2016-07-23 14:02:06 +00:00
|
|
|
|
2017-04-04 00:05:37 +00:00
|
|
|
if (!PlayerProcessor::Process(*packet))
|
2019-08-19 18:39:33 +00:00
|
|
|
LOG_MESSAGE_SIMPLE(TimedLog::LOG_WARN, "Unhandled PlayerPacket with identifier %i has arrived", packet->data[0]);
|
2017-03-31 18:36:24 +00:00
|
|
|
|
2016-10-19 18:26:42 +00:00
|
|
|
}
|
|
|
|
|
2017-04-09 05:51:28 +00:00
|
|
|
void Networking::processActorPacket(RakNet::Packet *packet)
|
|
|
|
{
|
|
|
|
Player *player = Players::getPlayer(packet->guid);
|
|
|
|
|
|
|
|
if (!player->isHandshaked() || player->getLoadState() != Player::POSTLOADED)
|
|
|
|
return;
|
|
|
|
|
2017-04-09 13:32:44 +00:00
|
|
|
if (!ActorProcessor::Process(*packet, baseActorList))
|
2019-08-19 18:39:33 +00:00
|
|
|
LOG_MESSAGE_SIMPLE(TimedLog::LOG_WARN, "Unhandled ActorPacket with identifier %i has arrived", packet->data[0]);
|
2017-04-09 05:51:28 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-05-12 16:40:00 +00:00
|
|
|
void Networking::processObjectPacket(RakNet::Packet *packet)
|
2016-10-19 18:44:17 +00:00
|
|
|
{
|
2016-11-16 00:05:14 +00:00
|
|
|
Player *player = Players::getPlayer(packet->guid);
|
2016-10-19 18:44:17 +00:00
|
|
|
|
2016-11-16 00:05:14 +00:00
|
|
|
if (!player->isHandshaked() || player->getLoadState() != Player::POSTLOADED)
|
2016-10-19 18:44:17 +00:00
|
|
|
return;
|
|
|
|
|
2018-05-15 19:07:06 +00:00
|
|
|
if (!ObjectProcessor::Process(*packet, baseObjectList))
|
2019-08-19 18:39:33 +00:00
|
|
|
LOG_MESSAGE_SIMPLE(TimedLog::LOG_WARN, "Unhandled ObjectPacket with identifier %i has arrived", packet->data[0]);
|
2016-10-19 18:44:17 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-05-18 03:40:28 +00:00
|
|
|
void Networking::processWorldstatePacket(RakNet::Packet *packet)
|
|
|
|
{
|
|
|
|
Player *player = Players::getPlayer(packet->guid);
|
|
|
|
|
|
|
|
if (!player->isHandshaked() || player->getLoadState() != Player::POSTLOADED)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!WorldstateProcessor::Process(*packet, baseWorldstate))
|
2019-08-19 18:39:33 +00:00
|
|
|
LOG_MESSAGE_SIMPLE(TimedLog::LOG_WARN, "Unhandled WorldstatePacket with identifier %i has arrived", packet->data[0]);
|
2018-05-18 03:40:28 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-07-16 16:58:37 +00:00
|
|
|
bool Networking::preInit(RakNet::Packet *packet, RakNet::BitStream &bsIn)
|
2016-10-19 18:26:42 +00:00
|
|
|
{
|
2018-07-16 16:58:37 +00:00
|
|
|
if (packet->data[0] != ID_GAME_PREINIT)
|
2016-10-19 18:26:42 +00:00
|
|
|
{
|
2019-08-19 18:39:33 +00:00
|
|
|
LOG_MESSAGE_SIMPLE(TimedLog::LOG_WARN, "%s sent wrong first packet (ID_GAME_PREINIT was expected)",
|
2018-07-16 16:58:37 +00:00
|
|
|
packet->systemAddress.ToString());
|
|
|
|
peer->CloseConnection(packet->systemAddress, true);
|
|
|
|
}
|
2017-03-06 14:51:17 +00:00
|
|
|
|
2020-06-18 15:18:22 +00:00
|
|
|
LOG_MESSAGE_SIMPLE(TimedLog::LOG_INFO, "Received ID_GAME_PREINIT from %s", packet->systemAddress.ToString());
|
2019-04-23 19:45:25 +00:00
|
|
|
PacketPreInit::PluginContainer dataFiles;
|
2017-03-06 14:51:17 +00:00
|
|
|
|
2018-07-16 16:58:37 +00:00
|
|
|
PacketPreInit packetPreInit(peer);
|
|
|
|
packetPreInit.SetReadStream(&bsIn);
|
2019-04-23 19:45:25 +00:00
|
|
|
packetPreInit.setChecksums(&dataFiles);
|
2018-07-16 16:58:37 +00:00
|
|
|
packetPreInit.Read();
|
2018-07-02 18:06:52 +00:00
|
|
|
|
2019-04-23 19:45:25 +00:00
|
|
|
if (!packetPreInit.isPacketValid() || dataFiles.empty())
|
2018-07-16 16:58:37 +00:00
|
|
|
{
|
2020-06-18 15:18:22 +00:00
|
|
|
LOG_APPEND(TimedLog::LOG_ERROR, "- Packet was invalid");
|
2018-07-16 16:58:37 +00:00
|
|
|
peer->CloseConnection(packet->systemAddress, false); // close connection without notification
|
|
|
|
return false;
|
|
|
|
}
|
2017-07-28 03:57:15 +00:00
|
|
|
|
2019-04-23 19:45:25 +00:00
|
|
|
auto dataFile = dataFiles.begin();
|
|
|
|
if (samples.size() == dataFiles.size())
|
2018-07-16 16:58:37 +00:00
|
|
|
{
|
2019-04-23 19:45:25 +00:00
|
|
|
for (int i = 0; dataFile != dataFiles.end(); dataFile++, i++)
|
2018-07-16 16:58:37 +00:00
|
|
|
{
|
2020-06-18 15:18:22 +00:00
|
|
|
LOG_APPEND(TimedLog::LOG_INFO, "- idx: %i\tchecksum: %X\tfile: %s", i, dataFile->second[0], dataFile->first.c_str());
|
2018-07-16 16:58:37 +00:00
|
|
|
// Check if the filenames match, ignoring case
|
2019-04-23 19:45:25 +00:00
|
|
|
if (Misc::StringUtils::ciEqual(samples[i].first, dataFile->first))
|
2017-05-02 15:33:50 +00:00
|
|
|
{
|
2018-07-16 16:58:37 +00:00
|
|
|
auto &hashList = samples[i].second;
|
2019-04-23 19:45:25 +00:00
|
|
|
// Proceed if no checksums have been listed for this dataFile on the server
|
2018-07-16 16:58:37 +00:00
|
|
|
if (hashList.empty())
|
|
|
|
continue;
|
2019-04-23 19:45:25 +00:00
|
|
|
auto it = find(hashList.begin(), hashList.end(), dataFile->second[0]);
|
2018-07-16 16:58:37 +00:00
|
|
|
// Break the loop if the client's checksum isn't among those accepted by
|
|
|
|
// the server
|
|
|
|
if (it == hashList.end())
|
|
|
|
break;
|
2017-03-06 14:51:17 +00:00
|
|
|
}
|
2018-07-16 16:58:37 +00:00
|
|
|
else // name is incorrect
|
|
|
|
break;
|
2017-03-06 14:51:17 +00:00
|
|
|
}
|
2018-07-16 16:58:37 +00:00
|
|
|
}
|
|
|
|
RakNet::BitStream bs;
|
|
|
|
packetPreInit.SetSendStream(&bs);
|
2017-03-06 14:51:17 +00:00
|
|
|
|
2019-04-23 19:45:25 +00:00
|
|
|
// If the loop above was broken, then the client's data files do not match the server's
|
|
|
|
if (dataFileEnforcementState && dataFile != dataFiles.end())
|
2018-07-16 16:58:37 +00:00
|
|
|
{
|
2020-06-18 15:18:22 +00:00
|
|
|
LOG_APPEND(TimedLog::LOG_INFO, "- Client was not allowed to connect due to incompatible data files");
|
2018-07-16 16:58:37 +00:00
|
|
|
packetPreInit.setChecksums(&samples);
|
|
|
|
packetPreInit.Send(packet->systemAddress);
|
|
|
|
peer->CloseConnection(packet->systemAddress, true);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-06-18 15:18:22 +00:00
|
|
|
LOG_APPEND(TimedLog::LOG_INFO, "- Client was allowed to connect");
|
2018-07-16 16:58:37 +00:00
|
|
|
PacketPreInit::PluginContainer tmp;
|
|
|
|
packetPreInit.setChecksums(&tmp);
|
|
|
|
packetPreInit.Send(packet->systemAddress);
|
|
|
|
Players::newPlayer(packet->guid); // create player if connection allowed
|
2019-11-09 03:12:00 +00:00
|
|
|
systemPacketController->SetStream(&bsIn, nullptr); // and request handshake
|
|
|
|
systemPacketController->GetPacket(ID_SYSTEM_HANDSHAKE)->RequestData(packet->guid);
|
2018-07-16 16:58:37 +00:00
|
|
|
return true;
|
2016-10-19 18:26:42 +00:00
|
|
|
}
|
2018-07-16 16:58:37 +00:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Networking::update(RakNet::Packet *packet, RakNet::BitStream &bsIn)
|
|
|
|
{
|
2019-11-09 03:12:00 +00:00
|
|
|
if (systemPacketController->ContainsPacket(packet->data[0]))
|
|
|
|
{
|
|
|
|
systemPacketController->SetStream(&bsIn, nullptr);
|
|
|
|
processSystemPacket(packet);
|
|
|
|
}
|
|
|
|
else if (playerPacketController->ContainsPacket(packet->data[0]))
|
2016-10-19 18:26:42 +00:00
|
|
|
{
|
2018-07-16 16:58:37 +00:00
|
|
|
playerPacketController->SetStream(&bsIn, nullptr);
|
2016-11-16 00:05:14 +00:00
|
|
|
processPlayerPacket(packet);
|
2016-10-19 18:26:42 +00:00
|
|
|
}
|
2017-04-09 05:51:28 +00:00
|
|
|
else if (actorPacketController->ContainsPacket(packet->data[0]))
|
|
|
|
{
|
|
|
|
actorPacketController->SetStream(&bsIn, 0);
|
|
|
|
processActorPacket(packet);
|
|
|
|
}
|
2018-05-12 16:40:00 +00:00
|
|
|
else if (objectPacketController->ContainsPacket(packet->data[0]))
|
2016-10-19 18:26:42 +00:00
|
|
|
{
|
2018-05-12 16:40:00 +00:00
|
|
|
objectPacketController->SetStream(&bsIn, 0);
|
|
|
|
processObjectPacket(packet);
|
2016-10-19 18:44:17 +00:00
|
|
|
}
|
2018-05-18 03:40:28 +00:00
|
|
|
else if (worldstatePacketController->ContainsPacket(packet->data[0]))
|
|
|
|
{
|
|
|
|
worldstatePacketController->SetStream(&bsIn, 0);
|
|
|
|
processWorldstatePacket(packet);
|
|
|
|
}
|
2016-10-19 18:44:17 +00:00
|
|
|
else
|
2019-08-19 18:39:33 +00:00
|
|
|
LOG_MESSAGE_SIMPLE(TimedLog::LOG_WARN, "Unhandled RakNet packet with identifier %i has arrived", packet->data[0]);
|
2016-01-12 03:41:44 +00:00
|
|
|
}
|
|
|
|
|
2016-11-16 00:05:14 +00:00
|
|
|
void Networking::newPlayer(RakNet::RakNetGUID guid)
|
2016-01-12 03:41:44 +00:00
|
|
|
{
|
2017-04-09 05:51:28 +00:00
|
|
|
playerPacketController->GetPacket(ID_PLAYER_BASEINFO)->RequestData(guid);
|
2017-04-16 06:00:18 +00:00
|
|
|
playerPacketController->GetPacket(ID_PLAYER_STATS_DYNAMIC)->RequestData(guid);
|
2017-04-25 19:11:33 +00:00
|
|
|
playerPacketController->GetPacket(ID_PLAYER_POSITION)->RequestData(guid);
|
2017-04-09 05:51:28 +00:00
|
|
|
playerPacketController->GetPacket(ID_PLAYER_CELL_CHANGE)->RequestData(guid);
|
|
|
|
playerPacketController->GetPacket(ID_PLAYER_EQUIPMENT)->RequestData(guid);
|
2016-01-12 03:41:44 +00:00
|
|
|
|
2019-08-19 18:39:33 +00:00
|
|
|
LOG_MESSAGE_SIMPLE(TimedLog::LOG_WARN, "Sending info about other players to %lu", guid.g);
|
2016-10-24 18:17:53 +00:00
|
|
|
|
2016-09-18 03:54:45 +00:00
|
|
|
for (TPlayers::iterator pl = players->begin(); pl != players->end(); pl++) //sending other players to new player
|
2016-01-12 03:41:44 +00:00
|
|
|
{
|
2016-10-24 18:17:53 +00:00
|
|
|
// If we are iterating over the new player, don't send the packets below
|
2016-11-17 05:11:42 +00:00
|
|
|
if (pl->first == guid) continue;
|
2016-01-12 03:41:44 +00:00
|
|
|
|
2016-10-29 13:41:29 +00:00
|
|
|
// If an invalid key makes it into the Players map, ignore it
|
2018-06-18 21:37:52 +00:00
|
|
|
else if (pl->first == RakNet::UNASSIGNED_CRABNET_GUID) continue;
|
2016-10-29 13:41:29 +00:00
|
|
|
|
2016-12-16 09:53:56 +00:00
|
|
|
// if player not fully connected
|
|
|
|
else if (pl->second == nullptr) continue;
|
|
|
|
|
2016-10-30 23:14:17 +00:00
|
|
|
// If we are iterating over a player who has inputted their name, proceed
|
2016-11-16 00:05:14 +00:00
|
|
|
else if (pl->second->getLoadState() == Player::POSTLOADED)
|
2016-10-30 23:14:17 +00:00
|
|
|
{
|
2017-04-09 05:51:28 +00:00
|
|
|
playerPacketController->GetPacket(ID_PLAYER_BASEINFO)->setPlayer(pl->second);
|
2017-04-16 06:00:18 +00:00
|
|
|
playerPacketController->GetPacket(ID_PLAYER_STATS_DYNAMIC)->setPlayer(pl->second);
|
2017-04-09 05:51:28 +00:00
|
|
|
playerPacketController->GetPacket(ID_PLAYER_ATTRIBUTE)->setPlayer(pl->second);
|
|
|
|
playerPacketController->GetPacket(ID_PLAYER_SKILL)->setPlayer(pl->second);
|
2017-04-25 19:11:33 +00:00
|
|
|
playerPacketController->GetPacket(ID_PLAYER_POSITION)->setPlayer(pl->second);
|
2017-04-09 05:51:28 +00:00
|
|
|
playerPacketController->GetPacket(ID_PLAYER_CELL_CHANGE)->setPlayer(pl->second);
|
|
|
|
playerPacketController->GetPacket(ID_PLAYER_EQUIPMENT)->setPlayer(pl->second);
|
|
|
|
|
|
|
|
playerPacketController->GetPacket(ID_PLAYER_BASEINFO)->Send(guid);
|
2017-04-16 06:00:18 +00:00
|
|
|
playerPacketController->GetPacket(ID_PLAYER_STATS_DYNAMIC)->Send(guid);
|
2017-04-09 05:51:28 +00:00
|
|
|
playerPacketController->GetPacket(ID_PLAYER_ATTRIBUTE)->Send(guid);
|
|
|
|
playerPacketController->GetPacket(ID_PLAYER_SKILL)->Send(guid);
|
2017-04-25 19:11:33 +00:00
|
|
|
playerPacketController->GetPacket(ID_PLAYER_POSITION)->Send(guid);
|
2017-04-09 05:51:28 +00:00
|
|
|
playerPacketController->GetPacket(ID_PLAYER_CELL_CHANGE)->Send(guid);
|
|
|
|
playerPacketController->GetPacket(ID_PLAYER_EQUIPMENT)->Send(guid);
|
2016-10-30 23:14:17 +00:00
|
|
|
}
|
2016-01-12 03:41:44 +00:00
|
|
|
}
|
|
|
|
|
2019-08-19 18:39:33 +00:00
|
|
|
LOG_APPEND(TimedLog::LOG_WARN, "- Done");
|
2016-10-24 18:17:53 +00:00
|
|
|
|
2016-01-12 03:41:44 +00:00
|
|
|
}
|
|
|
|
|
2016-11-16 00:05:14 +00:00
|
|
|
void Networking::disconnectPlayer(RakNet::RakNetGUID guid)
|
2016-01-12 03:41:44 +00:00
|
|
|
{
|
2016-11-16 00:05:14 +00:00
|
|
|
Player *player = Players::getPlayer(guid);
|
2016-10-08 07:35:38 +00:00
|
|
|
if (!player)
|
2016-10-08 07:30:52 +00:00
|
|
|
return;
|
2016-11-16 17:27:46 +00:00
|
|
|
Script::Call<Script::CallbackIdentity("OnPlayerDisconnect")>(player->getId());
|
2017-03-06 09:44:08 +00:00
|
|
|
|
2017-04-09 05:51:28 +00:00
|
|
|
playerPacketController->GetPacket(ID_USER_DISCONNECTED)->setPlayer(player);
|
|
|
|
playerPacketController->GetPacket(ID_USER_DISCONNECTED)->Send(true);
|
2016-11-16 00:05:14 +00:00
|
|
|
Players::deletePlayer(guid);
|
2016-01-12 03:41:44 +00:00
|
|
|
}
|
|
|
|
|
2017-04-09 05:51:28 +00:00
|
|
|
PlayerPacketController *Networking::getPlayerPacketController() const
|
|
|
|
{
|
|
|
|
return playerPacketController;
|
|
|
|
}
|
|
|
|
|
|
|
|
ActorPacketController *Networking::getActorPacketController() const
|
2016-01-12 03:41:44 +00:00
|
|
|
{
|
2017-04-09 05:51:28 +00:00
|
|
|
return actorPacketController;
|
2016-01-12 03:41:44 +00:00
|
|
|
}
|
|
|
|
|
2018-05-12 16:40:00 +00:00
|
|
|
ObjectPacketController *Networking::getObjectPacketController() const
|
2016-01-12 03:41:44 +00:00
|
|
|
{
|
2018-05-12 16:40:00 +00:00
|
|
|
return objectPacketController;
|
2016-01-12 03:41:44 +00:00
|
|
|
}
|
|
|
|
|
2018-05-18 03:40:28 +00:00
|
|
|
WorldstatePacketController *Networking::getWorldstatePacketController() const
|
|
|
|
{
|
|
|
|
return worldstatePacketController;
|
|
|
|
}
|
|
|
|
|
2018-07-12 23:40:24 +00:00
|
|
|
BaseActorList *Networking::getReceivedActorList()
|
2017-04-09 13:32:44 +00:00
|
|
|
{
|
|
|
|
return &baseActorList;
|
|
|
|
}
|
|
|
|
|
2018-07-12 23:40:24 +00:00
|
|
|
BaseObjectList *Networking::getReceivedObjectList()
|
2017-01-29 08:24:12 +00:00
|
|
|
{
|
2018-05-12 21:42:24 +00:00
|
|
|
return &baseObjectList;
|
2017-01-29 08:24:12 +00:00
|
|
|
}
|
|
|
|
|
2018-07-12 23:40:24 +00:00
|
|
|
BaseWorldstate *Networking::getReceivedWorldstate()
|
2018-05-18 03:40:28 +00:00
|
|
|
{
|
|
|
|
return &baseWorldstate;
|
|
|
|
}
|
|
|
|
|
2017-04-04 06:23:34 +00:00
|
|
|
int Networking::getCurrentMpNum()
|
|
|
|
{
|
|
|
|
return currentMpNum;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Networking::setCurrentMpNum(int value)
|
|
|
|
{
|
|
|
|
currentMpNum = value;
|
|
|
|
}
|
|
|
|
|
2017-04-04 22:30:15 +00:00
|
|
|
int Networking::incrementMpNum()
|
2017-04-04 06:23:34 +00:00
|
|
|
{
|
|
|
|
currentMpNum++;
|
2017-04-04 22:18:42 +00:00
|
|
|
Script::Call<Script::CallbackIdentity("OnMpNumIncrement")>(currentMpNum);
|
2017-04-04 06:23:34 +00:00
|
|
|
return currentMpNum;
|
|
|
|
}
|
|
|
|
|
2019-04-23 19:45:25 +00:00
|
|
|
bool Networking::getDataFileEnforcementState()
|
2018-01-02 14:32:38 +00:00
|
|
|
{
|
2019-04-23 19:45:25 +00:00
|
|
|
return dataFileEnforcementState;
|
2018-01-02 14:32:38 +00:00
|
|
|
}
|
|
|
|
|
2019-04-23 19:45:25 +00:00
|
|
|
void Networking::setDataFileEnforcementState(bool state)
|
2018-01-02 14:32:38 +00:00
|
|
|
{
|
2019-04-23 19:45:25 +00:00
|
|
|
dataFileEnforcementState = state;
|
2018-01-02 14:32:38 +00:00
|
|
|
}
|
|
|
|
|
2018-12-01 01:03:39 +00:00
|
|
|
bool Networking::getScriptErrorIgnoringState()
|
|
|
|
{
|
|
|
|
return scriptErrorIgnoringState;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Networking::setScriptErrorIgnoringState(bool state)
|
|
|
|
{
|
|
|
|
scriptErrorIgnoringState = state;
|
|
|
|
}
|
|
|
|
|
2016-11-16 00:05:14 +00:00
|
|
|
const Networking &Networking::get()
|
2016-01-12 03:41:44 +00:00
|
|
|
{
|
|
|
|
return *sThis;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-11-16 00:05:14 +00:00
|
|
|
Networking *Networking::getPtr()
|
2016-01-12 03:41:44 +00:00
|
|
|
{
|
|
|
|
return sThis;
|
|
|
|
}
|
|
|
|
|
2017-07-04 07:57:16 +00:00
|
|
|
RakNet::SystemAddress Networking::getSystemAddress(RakNet::RakNetGUID guid)
|
|
|
|
{
|
|
|
|
return peer->GetSystemAddressFromGuid(guid);
|
|
|
|
}
|
|
|
|
|
2016-11-16 00:05:14 +00:00
|
|
|
void Networking::stopServer(int code)
|
2016-01-12 03:41:44 +00:00
|
|
|
{
|
|
|
|
running = false;
|
|
|
|
exitCode = code;
|
|
|
|
}
|
|
|
|
|
2019-07-06 11:03:50 +00:00
|
|
|
void signalHandler(int signum)
|
|
|
|
{
|
2021-03-21 13:45:01 +00:00
|
|
|
std::cout << "Interrupt signal (" << signum << ") received.\n";
|
2019-07-06 11:03:50 +00:00
|
|
|
//15 is SIGTERM(Normal OS stop call), 2 is SIGINT(Ctrl+C)
|
|
|
|
if(signum == 15 or signum == 2)
|
|
|
|
{
|
|
|
|
killLoop = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-01 17:30:00 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
BOOL WINAPI sigIntHandler(_In_ DWORD dwCtrlType) {
|
|
|
|
switch (dwCtrlType)
|
|
|
|
{
|
|
|
|
case CTRL_C_EVENT:
|
|
|
|
signalHandler(15);
|
|
|
|
return TRUE;
|
|
|
|
default:
|
|
|
|
// Pass signal on to the next handler
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2016-11-16 00:05:14 +00:00
|
|
|
int Networking::mainLoop()
|
2016-01-12 03:41:44 +00:00
|
|
|
{
|
|
|
|
RakNet::Packet *packet;
|
|
|
|
|
2020-02-11 17:16:26 +00:00
|
|
|
#ifndef _WIN32
|
2019-07-06 11:03:50 +00:00
|
|
|
struct sigaction sigIntHandler;
|
|
|
|
|
|
|
|
sigIntHandler.sa_handler = signalHandler;
|
|
|
|
sigemptyset(&sigIntHandler.sa_mask);
|
|
|
|
sigIntHandler.sa_flags = 0;
|
2020-03-01 17:30:00 +00:00
|
|
|
sigaction(SIGTERM, &sigIntHandler, NULL);
|
|
|
|
sigaction(SIGINT, &sigIntHandler, NULL);
|
|
|
|
#else
|
|
|
|
SetConsoleCtrlHandler(sigIntHandler, TRUE);
|
2020-02-11 17:16:26 +00:00
|
|
|
#endif
|
2019-07-06 11:03:50 +00:00
|
|
|
|
|
|
|
while (running and !killLoop)
|
2016-01-12 03:41:44 +00:00
|
|
|
{
|
2020-03-01 17:30:00 +00:00
|
|
|
mwmp_input::handler();
|
2016-01-12 03:41:44 +00:00
|
|
|
for (packet=peer->Receive(); packet; peer->DeallocatePacket(packet), packet=peer->Receive())
|
|
|
|
{
|
2017-04-24 12:01:05 +00:00
|
|
|
if (getMasterClient()->Process(packet))
|
|
|
|
continue;
|
|
|
|
|
2016-01-12 03:41:44 +00:00
|
|
|
switch (packet->data[0])
|
|
|
|
{
|
|
|
|
case ID_REMOTE_DISCONNECTION_NOTIFICATION:
|
2019-08-19 18:39:33 +00:00
|
|
|
LOG_MESSAGE_SIMPLE(TimedLog::LOG_WARN, "Client at %s has disconnected", packet->systemAddress.ToString());
|
2016-01-12 03:41:44 +00:00
|
|
|
break;
|
|
|
|
case ID_REMOTE_CONNECTION_LOST:
|
2019-08-19 18:39:33 +00:00
|
|
|
LOG_MESSAGE_SIMPLE(TimedLog::LOG_WARN, "Client at %s has lost connection", packet->systemAddress.ToString());
|
2016-01-12 03:41:44 +00:00
|
|
|
break;
|
|
|
|
case ID_REMOTE_NEW_INCOMING_CONNECTION:
|
2019-08-19 18:39:33 +00:00
|
|
|
LOG_MESSAGE_SIMPLE(TimedLog::LOG_WARN, "Client at %s has connected", packet->systemAddress.ToString());
|
2016-01-12 03:41:44 +00:00
|
|
|
break;
|
|
|
|
case ID_CONNECTION_REQUEST_ACCEPTED: // client to server
|
|
|
|
{
|
2019-08-19 18:39:33 +00:00
|
|
|
LOG_MESSAGE_SIMPLE(TimedLog::LOG_WARN, "Our connection request has been accepted");
|
2016-01-12 03:41:44 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case ID_NEW_INCOMING_CONNECTION:
|
2019-08-19 18:39:33 +00:00
|
|
|
LOG_MESSAGE_SIMPLE(TimedLog::LOG_WARN, "A connection is incoming from %s", packet->systemAddress.ToString());
|
2016-01-12 03:41:44 +00:00
|
|
|
break;
|
|
|
|
case ID_NO_FREE_INCOMING_CONNECTIONS:
|
2019-08-19 18:39:33 +00:00
|
|
|
LOG_MESSAGE_SIMPLE(TimedLog::LOG_WARN, "The server is full");
|
2016-01-12 03:41:44 +00:00
|
|
|
break;
|
|
|
|
case ID_DISCONNECTION_NOTIFICATION:
|
2019-08-19 18:39:33 +00:00
|
|
|
LOG_MESSAGE_SIMPLE(TimedLog::LOG_WARN, "Client at %s has disconnected", packet->systemAddress.ToString());
|
2016-11-16 00:05:14 +00:00
|
|
|
disconnectPlayer(packet->guid);
|
2016-01-12 03:41:44 +00:00
|
|
|
break;
|
|
|
|
case ID_CONNECTION_LOST:
|
2019-08-19 18:39:33 +00:00
|
|
|
LOG_MESSAGE_SIMPLE(TimedLog::LOG_WARN, "Client at %s has lost connection", packet->systemAddress.ToString());
|
2016-11-16 00:05:14 +00:00
|
|
|
disconnectPlayer(packet->guid);
|
2016-01-12 03:41:44 +00:00
|
|
|
break;
|
2017-09-02 06:01:01 +00:00
|
|
|
case ID_SND_RECEIPT_ACKED:
|
2017-01-07 15:05:22 +00:00
|
|
|
case ID_CONNECTED_PING:
|
|
|
|
case ID_UNCONNECTED_PING:
|
|
|
|
break;
|
2016-01-12 03:41:44 +00:00
|
|
|
default:
|
2018-07-16 16:58:37 +00:00
|
|
|
{
|
|
|
|
RakNet::BitStream bsIn(&packet->data[1], packet->length, false);
|
|
|
|
bsIn.IgnoreBytes((unsigned int) RakNet::RakNetGUID::size()); // Ignore GUID from received packet
|
|
|
|
|
|
|
|
|
2018-07-21 04:34:45 +00:00
|
|
|
if (Players::doesPlayerExist(packet->guid))
|
2018-07-16 16:58:37 +00:00
|
|
|
update(packet, bsIn);
|
|
|
|
else
|
|
|
|
preInit(packet, bsIn);
|
2016-01-12 03:41:44 +00:00
|
|
|
break;
|
2018-07-16 16:58:37 +00:00
|
|
|
}
|
2016-01-12 03:41:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
TimerAPI::Tick();
|
2021-03-21 13:45:01 +00:00
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(1));
|
2016-01-12 03:41:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TimerAPI::Terminate();
|
|
|
|
return exitCode;
|
|
|
|
}
|
|
|
|
|
2018-04-17 13:36:48 +00:00
|
|
|
void Networking::kickPlayer(RakNet::RakNetGUID guid, bool sendNotification)
|
2016-01-12 03:41:44 +00:00
|
|
|
{
|
2018-04-17 13:36:48 +00:00
|
|
|
peer->CloseConnection(guid, sendNotification);
|
2016-01-12 03:41:44 +00:00
|
|
|
}
|
2016-08-14 23:48:25 +00:00
|
|
|
|
2017-07-27 16:29:17 +00:00
|
|
|
void Networking::banAddress(const char *ipAddress)
|
|
|
|
{
|
|
|
|
peer->AddToBanList(ipAddress);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Networking::unbanAddress(const char *ipAddress)
|
|
|
|
{
|
|
|
|
peer->RemoveFromBanList(ipAddress);
|
|
|
|
}
|
|
|
|
|
2016-11-16 00:05:14 +00:00
|
|
|
unsigned short Networking::numberOfConnections() const
|
2016-08-14 23:48:25 +00:00
|
|
|
{
|
|
|
|
return peer->NumberOfConnections();
|
|
|
|
}
|
2016-08-19 00:18:25 +00:00
|
|
|
|
2016-11-16 00:05:14 +00:00
|
|
|
unsigned int Networking::maxConnections() const
|
2016-08-19 00:18:25 +00:00
|
|
|
{
|
|
|
|
return peer->GetMaximumIncomingConnections();
|
2016-11-04 12:15:14 +00:00
|
|
|
}
|
|
|
|
|
2016-11-16 00:05:14 +00:00
|
|
|
int Networking::getAvgPing(RakNet::AddressOrGUID addr) const
|
2016-11-04 12:15:14 +00:00
|
|
|
{
|
|
|
|
return peer->GetAveragePing(addr);
|
|
|
|
}
|
2017-01-10 11:21:31 +00:00
|
|
|
|
2018-09-05 10:19:34 +00:00
|
|
|
unsigned short Networking::getPort() const
|
|
|
|
{
|
|
|
|
return peer->GetMyBoundAddress().GetPort();
|
|
|
|
}
|
|
|
|
|
2017-01-10 11:21:31 +00:00
|
|
|
MasterClient *Networking::getMasterClient()
|
|
|
|
{
|
|
|
|
return mclient;
|
|
|
|
}
|
|
|
|
|
2017-04-24 12:01:05 +00:00
|
|
|
void Networking::InitQuery(std::string queryAddr, unsigned short queryPort)
|
2017-01-10 11:21:31 +00:00
|
|
|
{
|
2017-04-24 12:01:05 +00:00
|
|
|
mclient = new MasterClient(peer, queryAddr, queryPort);
|
2017-01-10 11:21:31 +00:00
|
|
|
}
|
2017-05-06 18:49:58 +00:00
|
|
|
|
|
|
|
void Networking::postInit()
|
|
|
|
{
|
2019-03-12 01:18:57 +00:00
|
|
|
Script::Call<Script::CallbackIdentity("OnRequestDataFileList")>();
|
2019-03-12 03:36:33 +00:00
|
|
|
Script::Call<Script::CallbackIdentity("OnServerPostInit")>();
|
2018-10-30 06:02:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
PacketPreInit::PluginContainer &Networking::getSamples()
|
|
|
|
{
|
|
|
|
return samples;
|
2017-05-06 18:49:58 +00:00
|
|
|
}
|