2016-01-12 03:41:44 +00:00
|
|
|
//
|
|
|
|
// Created by koncord on 12.01.16.
|
|
|
|
//
|
|
|
|
|
|
|
|
#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>
|
|
|
|
#include <components/openmw-mp/NetworkMessages.hpp>
|
2016-08-17 18:18:04 +00:00
|
|
|
#include <components/openmw-mp/Log.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>
|
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-03 17:13:10 +00:00
|
|
|
#include "apps/openmw-mp/processors/PlayerProcessor.hpp"
|
|
|
|
#include "apps/openmw-mp/processors/ActorProcessor.hpp"
|
|
|
|
#include "apps/openmw-mp/processors/WorldProcessor.hpp"
|
2017-02-20 13:15:04 +00:00
|
|
|
#include <components/openmw-mp/Version.hpp>
|
2017-03-06 14:51:17 +00:00
|
|
|
#include <components/openmw-mp/Packets/PacketPreInit.hpp>
|
2016-01-12 03:41:44 +00:00
|
|
|
|
|
|
|
using namespace mwmp;
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
Networking *Networking::sThis = 0;
|
2017-01-29 08:24:12 +00:00
|
|
|
|
2017-04-04 06:23:34 +00:00
|
|
|
static int currentMpNum = 0;
|
|
|
|
|
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
|
|
|
|
2017-04-09 05:51:28 +00:00
|
|
|
playerPacketController = new PlayerPacketController(peer);
|
|
|
|
actorPacketController = new ActorPacketController(peer);
|
|
|
|
worldPacketController = new WorldPacketController(peer);
|
2016-01-12 03:41:44 +00:00
|
|
|
|
2016-10-19 17:51:51 +00:00
|
|
|
// Set send stream
|
2017-04-09 05:51:28 +00:00
|
|
|
playerPacketController->SetStream(0, &bsOut);
|
|
|
|
actorPacketController->SetStream(0, &bsOut);
|
|
|
|
worldPacketController->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;
|
2017-04-09 05:51:28 +00:00
|
|
|
delete playerPacketController;
|
2017-04-09 07:50:35 +00:00
|
|
|
delete actorPacketController;
|
2017-04-09 05:51:28 +00:00
|
|
|
delete worldPacketController;
|
2016-09-30 10:13:50 +00:00
|
|
|
LOG_QUIT();
|
2016-01-12 03:41:44 +00:00
|
|
|
}
|
|
|
|
|
2017-02-20 13:15:04 +00:00
|
|
|
void Networking::setServerPassword(std::string passw) noexcept
|
|
|
|
{
|
|
|
|
serverPassword = passw.empty() ? TES3MP_DEFAULT_PASSW : passw;
|
|
|
|
}
|
|
|
|
|
2017-02-20 15:14:59 +00:00
|
|
|
bool Networking::isPassworded() const
|
|
|
|
{
|
|
|
|
return serverPassword != TES3MP_DEFAULT_PASSW;
|
|
|
|
}
|
|
|
|
|
2016-11-16 00:05:14 +00:00
|
|
|
void Networking::processPlayerPacket(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
|
|
|
|
2017-04-09 05:51:28 +00:00
|
|
|
PlayerPacket *myPacket = playerPacketController->GetPacket(packet->data[0]);
|
2016-01-12 03:41:44 +00:00
|
|
|
|
2016-08-17 15:04:35 +00:00
|
|
|
if (packet->data[0] == ID_HANDSHAKE)
|
2016-01-12 03:41:44 +00:00
|
|
|
{
|
2017-03-06 09:44:08 +00:00
|
|
|
myPacket->setPlayer(player);
|
|
|
|
myPacket->Read();
|
2016-01-12 03:41:44 +00:00
|
|
|
|
2016-08-17 15:04:35 +00:00
|
|
|
if (player->isHandshaked())
|
2016-01-12 03:41:44 +00:00
|
|
|
{
|
2017-02-27 09:13:07 +00:00
|
|
|
LOG_MESSAGE_SIMPLE(Log::LOG_WARN, "Wrong handshake with player %d, name: %s", player->getId(),
|
|
|
|
player->npc.mName.c_str());
|
2016-11-16 00:05:14 +00:00
|
|
|
kickPlayer(player->guid);
|
2016-01-12 03:41:44 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-02-20 13:15:04 +00:00
|
|
|
if (player->passw != serverPassword)
|
2016-01-12 03:41:44 +00:00
|
|
|
{
|
2016-08-19 04:54:10 +00:00
|
|
|
LOG_MESSAGE_SIMPLE(Log::LOG_WARN, "Wrong server password for player %d, name: %s (pass: %s)",
|
2017-02-27 09:13:07 +00:00
|
|
|
player->getId(), player->npc.mName.c_str(), player->passw.c_str());
|
2016-11-16 00:05:14 +00:00
|
|
|
kickPlayer(player->guid);
|
2016-01-12 03:41:44 +00:00
|
|
|
return;
|
|
|
|
}
|
2016-11-16 00:05:14 +00:00
|
|
|
player->setHandshake();
|
2016-09-18 03:54:45 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!player->isHandshaked())
|
|
|
|
{
|
2017-02-27 09:13:07 +00:00
|
|
|
LOG_MESSAGE_SIMPLE(Log::LOG_WARN, "Have not completed handshake with player %d", player->getId());
|
2016-11-16 17:27:46 +00:00
|
|
|
//KickPlayer(player->guid);
|
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
|
|
|
|
2016-11-16 17:27:46 +00:00
|
|
|
static constexpr unsigned int ident = Script::CallbackIdentity("OnPlayerConnect");
|
2016-01-12 03:41:44 +00:00
|
|
|
Script::CallBackReturn<ident> result = true;
|
2016-11-16 00:05:14 +00:00
|
|
|
Script::Call<ident>(result, Players::getPlayer(packet->guid)->getId());
|
2016-01-12 03:41:44 +00:00
|
|
|
|
2016-08-17 15:04:35 +00:00
|
|
|
if (!result)
|
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
|
|
|
{
|
2017-02-27 09:13:07 +00:00
|
|
|
LOG_MESSAGE_SIMPLE(Log::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))
|
2017-02-27 09:13:07 +00:00
|
|
|
LOG_MESSAGE_SIMPLE(Log::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))
|
|
|
|
LOG_MESSAGE_SIMPLE(Log::LOG_WARN, "Unhandled ActorPacket with identifier %i has arrived", packet->data[0]);
|
2017-04-09 05:51:28 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-11-16 00:05:14 +00:00
|
|
|
void Networking::processWorldPacket(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;
|
|
|
|
|
2017-04-04 02:03:14 +00:00
|
|
|
if (!WorldProcessor::Process(*packet, baseEvent))
|
2017-02-27 09:13:07 +00:00
|
|
|
LOG_MESSAGE_SIMPLE(Log::LOG_WARN, "Unhandled WorldPacket with identifier %i has arrived", packet->data[0]);
|
2016-10-19 18:44:17 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-11-16 00:05:14 +00:00
|
|
|
void Networking::update(RakNet::Packet *packet)
|
2016-10-19 18:26:42 +00:00
|
|
|
{
|
2016-11-16 00:05:14 +00:00
|
|
|
Player *player = Players::getPlayer(packet->guid);
|
2016-08-30 03:17:06 +00:00
|
|
|
|
2016-10-19 18:26:42 +00:00
|
|
|
RakNet::BitStream bsIn(&packet->data[1], packet->length, false);
|
|
|
|
|
2017-02-27 08:23:14 +00:00
|
|
|
bsIn.IgnoreBytes((unsigned int) RakNet::RakNetGUID::size()); // Ignore GUID from received packet
|
2016-10-19 18:26:42 +00:00
|
|
|
|
|
|
|
if (player == 0)
|
|
|
|
{
|
2017-04-04 00:05:37 +00:00
|
|
|
if (packet->data[0] == ID_GAME_PREINIT)
|
2017-03-06 14:51:17 +00:00
|
|
|
{
|
|
|
|
DEBUG_PRINTF("ID_GAME_PREINIT");
|
|
|
|
PacketPreInit::PluginContainer plugins;
|
|
|
|
|
|
|
|
PacketPreInit packetPreInit(peer);
|
|
|
|
packetPreInit.SetReadStream(&bsIn);
|
|
|
|
packetPreInit.setChecksums(&plugins);
|
|
|
|
packetPreInit.Read();
|
|
|
|
|
2017-05-02 12:21:00 +00:00
|
|
|
auto plugin = plugins.begin();
|
|
|
|
if (samples.size() == plugins.size())
|
|
|
|
{
|
|
|
|
for (int i = 0; plugin != plugins.end(); plugin++, i++)
|
|
|
|
{
|
2017-05-02 15:33:50 +00:00
|
|
|
LOG_APPEND(Log::LOG_VERBOSE, "- %X\t%s", plugin->second[0], plugin->first.c_str());
|
2017-05-02 12:21:00 +00:00
|
|
|
if (samples[i].first == plugin->first) // if name is correct
|
|
|
|
{
|
|
|
|
auto &hashList = samples[i].second;
|
|
|
|
if (hashList.empty()) // and server do not allow to have custom hash for plugin
|
|
|
|
continue;
|
2017-05-02 15:33:50 +00:00
|
|
|
auto it = find(hashList.begin(), hashList.end(), plugin->second[0]);
|
2017-05-02 19:47:58 +00:00
|
|
|
if (it == hashList.end()) // hash not found in sample
|
2017-05-02 12:21:00 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
else // name is incorrect
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2017-05-02 15:33:50 +00:00
|
|
|
RakNet::BitStream bs;
|
|
|
|
packetPreInit.SetSendStream(&bs);
|
2017-05-02 19:47:58 +00:00
|
|
|
if (plugin != plugins.end()) // if condition is true, then client have wrong plugin list
|
2017-03-06 14:51:17 +00:00
|
|
|
{
|
2017-05-02 15:33:50 +00:00
|
|
|
packetPreInit.setChecksums(&samples);
|
|
|
|
packetPreInit.Send(packet->systemAddress);
|
|
|
|
peer->CloseConnection(packet->systemAddress, true);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
PacketPreInit::PluginContainer tmp;
|
|
|
|
packetPreInit.setChecksums(&tmp);
|
|
|
|
packetPreInit.Send(packet->systemAddress);
|
2017-03-06 14:51:17 +00:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-04-09 05:51:28 +00:00
|
|
|
playerPacketController->SetStream(&bsIn, 0);
|
2016-10-19 18:26:42 +00:00
|
|
|
|
2017-04-09 05:51:28 +00:00
|
|
|
playerPacketController->GetPacket(ID_HANDSHAKE)->RequestData(packet->guid);
|
2016-11-16 00:05:14 +00:00
|
|
|
Players::newPlayer(packet->guid);
|
|
|
|
player = Players::getPlayer(packet->guid);
|
2016-10-19 18:26:42 +00:00
|
|
|
return;
|
|
|
|
}
|
2017-04-09 05:51:28 +00:00
|
|
|
else if (playerPacketController->ContainsPacket(packet->data[0]))
|
2016-10-19 18:26:42 +00:00
|
|
|
{
|
2017-04-09 05:51:28 +00:00
|
|
|
playerPacketController->SetStream(&bsIn, 0);
|
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);
|
|
|
|
}
|
|
|
|
else if (worldPacketController->ContainsPacket(packet->data[0]))
|
2016-10-19 18:26:42 +00:00
|
|
|
{
|
2017-04-09 05:51:28 +00:00
|
|
|
worldPacketController->SetStream(&bsIn, 0);
|
2016-11-16 00:05:14 +00:00
|
|
|
processWorldPacket(packet);
|
2016-10-19 18:44:17 +00:00
|
|
|
}
|
|
|
|
else
|
2017-02-27 09:13:07 +00:00
|
|
|
LOG_MESSAGE_SIMPLE(Log::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
|
|
|
|
2017-02-27 09:13:07 +00:00
|
|
|
LOG_MESSAGE_SIMPLE(Log::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
|
2016-11-17 05:11:42 +00:00
|
|
|
else if (pl->first == RakNet::UNASSIGNED_RAKNET_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
|
|
|
}
|
|
|
|
|
2016-11-17 04:39:35 +00:00
|
|
|
LOG_APPEND(Log::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
|
|
|
}
|
|
|
|
|
2017-04-09 05:51:28 +00:00
|
|
|
WorldPacketController *Networking::getWorldPacketController() const
|
2016-01-12 03:41:44 +00:00
|
|
|
{
|
2017-04-09 05:51:28 +00:00
|
|
|
return worldPacketController;
|
2016-01-12 03:41:44 +00:00
|
|
|
}
|
|
|
|
|
2017-04-09 13:32:44 +00:00
|
|
|
BaseActorList *Networking::getLastActorList()
|
|
|
|
{
|
|
|
|
return &baseActorList;
|
|
|
|
}
|
|
|
|
|
2017-02-06 10:53:41 +00:00
|
|
|
BaseEvent *Networking::getLastEvent()
|
2017-01-29 08:24:12 +00:00
|
|
|
{
|
2017-02-23 05:47:00 +00:00
|
|
|
return &baseEvent;
|
2017-01-29 08:24:12 +00:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2017-05-02 15:33:50 +00:00
|
|
|
PacketPreInit::PluginContainer Networking::getPluginListSample()
|
2017-05-02 12:19:51 +00:00
|
|
|
{
|
2017-05-02 15:33:50 +00:00
|
|
|
PacketPreInit::PluginContainer pls;
|
2017-05-02 12:19:51 +00:00
|
|
|
unsigned id = 0;
|
2017-05-31 05:37:11 +00:00
|
|
|
while (true)
|
2017-05-02 12:19:51 +00:00
|
|
|
{
|
|
|
|
unsigned field = 0;
|
|
|
|
auto name = "";
|
2017-05-02 15:33:50 +00:00
|
|
|
Script::Call<Script::CallbackIdentity("OnRequestPluginList")>(name, id, field++);
|
2017-05-02 19:47:58 +00:00
|
|
|
if (strlen(name) == 0)
|
2017-05-02 12:19:51 +00:00
|
|
|
break;
|
2017-05-02 15:33:50 +00:00
|
|
|
PacketPreInit::HashList hashList;
|
2017-05-31 05:37:11 +00:00
|
|
|
while (true)
|
2017-05-02 12:19:51 +00:00
|
|
|
{
|
|
|
|
auto hash = "";
|
2017-05-02 15:33:50 +00:00
|
|
|
Script::Call<Script::CallbackIdentity("OnRequestPluginList")>(hash, id, field++);
|
2017-05-02 19:47:58 +00:00
|
|
|
if (strlen(hash) == 0)
|
2017-05-02 12:19:51 +00:00
|
|
|
break;
|
|
|
|
hashList.push_back((unsigned)stoul(hash));
|
|
|
|
}
|
|
|
|
pls.push_back({name, hashList});
|
|
|
|
id++;
|
|
|
|
}
|
|
|
|
return pls;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2016-11-16 00:05:14 +00:00
|
|
|
int Networking::mainLoop()
|
2016-01-12 03:41:44 +00:00
|
|
|
{
|
|
|
|
RakNet::Packet *packet;
|
|
|
|
|
|
|
|
while (running)
|
|
|
|
{
|
2016-08-17 15:04:35 +00:00
|
|
|
if (kbhit() && getch() == '\n')
|
2016-01-12 03:41:44 +00:00
|
|
|
break;
|
|
|
|
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:
|
2017-02-27 09:13:07 +00:00
|
|
|
LOG_MESSAGE_SIMPLE(Log::LOG_WARN, "Client at %s has disconnected", packet->systemAddress.ToString());
|
2016-01-12 03:41:44 +00:00
|
|
|
break;
|
|
|
|
case ID_REMOTE_CONNECTION_LOST:
|
2017-02-27 09:13:07 +00:00
|
|
|
LOG_MESSAGE_SIMPLE(Log::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:
|
2017-02-27 09:13:07 +00:00
|
|
|
LOG_MESSAGE_SIMPLE(Log::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
|
|
|
|
{
|
2016-11-17 04:39:35 +00:00
|
|
|
LOG_MESSAGE_SIMPLE(Log::LOG_WARN, "Our connection request has been accepted");
|
2016-01-12 03:41:44 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case ID_NEW_INCOMING_CONNECTION:
|
2017-02-27 09:13:07 +00:00
|
|
|
LOG_MESSAGE_SIMPLE(Log::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:
|
2016-11-17 04:39:35 +00:00
|
|
|
LOG_MESSAGE_SIMPLE(Log::LOG_WARN, "The server is full");
|
2016-01-12 03:41:44 +00:00
|
|
|
break;
|
|
|
|
case ID_DISCONNECTION_NOTIFICATION:
|
2017-02-27 09:13:07 +00:00
|
|
|
LOG_MESSAGE_SIMPLE(Log::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:
|
2017-02-27 09:13:07 +00:00
|
|
|
LOG_MESSAGE_SIMPLE(Log::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-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:
|
2016-11-16 00:05:14 +00:00
|
|
|
update(packet);
|
2016-01-12 03:41:44 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
TimerAPI::Tick();
|
2016-08-05 06:15:00 +00:00
|
|
|
this_thread::sleep_for(chrono::milliseconds(1));
|
2016-01-12 03:41:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TimerAPI::Terminate();
|
|
|
|
return exitCode;
|
|
|
|
}
|
|
|
|
|
2016-11-16 00:05:14 +00:00
|
|
|
void Networking::kickPlayer(RakNet::RakNetGUID guid)
|
2016-01-12 03:41:44 +00:00
|
|
|
{
|
|
|
|
peer->CloseConnection(guid, true);
|
|
|
|
}
|
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
|
|
|
|
|
|
|
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()
|
|
|
|
{
|
|
|
|
Script::Call<Script::CallbackIdentity("OnServerPostInit")>();
|
|
|
|
samples = getPluginListSample();
|
2017-05-31 05:37:11 +00:00
|
|
|
if (mclient)
|
2017-05-06 18:52:28 +00:00
|
|
|
{
|
2017-05-31 05:37:11 +00:00
|
|
|
for (auto plugin : samples)
|
2017-05-06 18:52:28 +00:00
|
|
|
{
|
2017-05-31 05:37:11 +00:00
|
|
|
if (!plugin.second.empty())
|
2017-05-06 18:52:28 +00:00
|
|
|
mclient->PushPlugin({plugin.first, plugin.second[0]});
|
|
|
|
else
|
|
|
|
mclient->PushPlugin({plugin.first, 0});
|
|
|
|
}
|
|
|
|
}
|
2017-05-06 18:49:58 +00:00
|
|
|
}
|