2016-01-12 03:41:44 +00:00
|
|
|
//
|
|
|
|
// Created by koncord on 12.01.16.
|
|
|
|
//
|
|
|
|
|
|
|
|
#include "Player.hpp"
|
|
|
|
#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"
|
|
|
|
|
|
|
|
using namespace mwmp;
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
Networking *Networking::sThis = 0;
|
|
|
|
|
|
|
|
Networking::Networking(RakNet::RakPeerInterface *peer)
|
|
|
|
{
|
|
|
|
sThis = this;
|
|
|
|
this->peer = peer;
|
|
|
|
players = Players::GetPlayers();
|
|
|
|
|
2016-10-19 13:54:39 +00:00
|
|
|
playerController = new PlayerPacketController(peer);
|
2016-10-19 17:51:51 +00:00
|
|
|
worldController = new WorldPacketController(peer);
|
2016-01-12 03:41:44 +00:00
|
|
|
|
2016-10-19 17:51:51 +00:00
|
|
|
// Set send stream
|
|
|
|
playerController->SetStream(0, &bsOut);
|
|
|
|
worldController->SetStream(0, &bsOut);
|
2016-01-12 03:41:44 +00:00
|
|
|
|
|
|
|
running = true;
|
|
|
|
exitCode = 0;
|
2016-09-30 10:13:50 +00:00
|
|
|
|
|
|
|
Script::Call<Script::CallbackIdentity("OnServerInit")>();
|
2016-01-12 03:41:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Networking::~Networking()
|
|
|
|
{
|
|
|
|
Script::Call<Script::CallbackIdentity("OnServerExit")>(false);
|
|
|
|
|
|
|
|
sThis = 0;
|
2016-10-19 13:54:39 +00:00
|
|
|
delete playerController;
|
2016-09-30 10:13:50 +00:00
|
|
|
LOG_QUIT();
|
2016-01-12 03:41:44 +00:00
|
|
|
}
|
|
|
|
|
2016-10-19 18:26:42 +00:00
|
|
|
void Networking::ProcessPlayerPacket(RakNet::Packet *packet)
|
2016-01-12 03:41:44 +00:00
|
|
|
{
|
|
|
|
Player *player = Players::GetPlayer(packet->guid);
|
|
|
|
|
2016-10-19 13:54:39 +00:00
|
|
|
PlayerPacket *myPacket = playerController->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
|
|
|
{
|
|
|
|
string passw = "SuperPassword";
|
|
|
|
|
|
|
|
myPacket->Read(player);
|
|
|
|
|
2016-08-17 15:04:35 +00:00
|
|
|
if (player->isHandshaked())
|
2016-01-12 03:41:44 +00:00
|
|
|
{
|
2016-08-19 04:54:10 +00:00
|
|
|
LOG_MESSAGE_SIMPLE(Log::LOG_WARN, "Wrong handshake with player %d, name: %s",
|
2016-08-18 17:20:17 +00:00
|
|
|
player->GetID(),
|
|
|
|
player->Npc()->mName.c_str());
|
2016-01-12 03:41:44 +00:00
|
|
|
KickPlayer(player->guid);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-08-17 15:04:35 +00:00
|
|
|
if (*player->GetPassw() != passw)
|
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)",
|
2016-08-18 17:20:17 +00:00
|
|
|
player->GetID(),
|
|
|
|
player->Npc()->mName.c_str(),
|
|
|
|
player->GetPassw()->c_str());
|
2016-01-12 03:41:44 +00:00
|
|
|
KickPlayer(player->guid);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
player->Handshake();
|
2016-09-18 03:54:45 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!player->isHandshaked())
|
|
|
|
{
|
|
|
|
LOG_MESSAGE_SIMPLE(Log::LOG_WARN, "Have not completed handshake with player %d",
|
|
|
|
player->GetID());
|
|
|
|
//KickPlayer(player->guid);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (packet->data[0] == ID_LOADED)
|
|
|
|
{
|
2016-09-18 07:52:13 +00:00
|
|
|
player->Loaded(Player::LOADED);
|
2016-01-12 03:41:44 +00:00
|
|
|
|
|
|
|
static constexpr unsigned int ident = Script::CallbackIdentity("OnPlayerConnect");
|
|
|
|
Script::CallBackReturn<ident> result = true;
|
|
|
|
Script::Call<ident>(result, Players::GetPlayer(packet->guid)->GetID());
|
|
|
|
|
2016-08-17 15:04:35 +00:00
|
|
|
if (!result)
|
2016-01-12 03:41:44 +00:00
|
|
|
{
|
2016-10-19 13:54:39 +00:00
|
|
|
playerController->GetPacket(ID_USER_DISCONNECTED)->Send(Players::GetPlayer(packet->guid), false);
|
2016-01-12 03:41:44 +00:00
|
|
|
Players::DeletePlayer(packet->guid);
|
|
|
|
return;
|
|
|
|
}
|
2016-09-18 03:54:45 +00:00
|
|
|
}
|
2016-09-19 08:06:04 +00:00
|
|
|
else if (packet->data[0] == ID_GAME_BASE_INFO)
|
2016-09-18 03:54:45 +00:00
|
|
|
{
|
|
|
|
LOG_MESSAGE_SIMPLE(Log::LOG_INFO, "Received ID_GAME_BASE_INFO about %s",
|
2016-10-19 18:26:42 +00:00
|
|
|
player->Npc()->mName.c_str());
|
2016-01-12 03:41:44 +00:00
|
|
|
|
2016-09-18 03:54:45 +00:00
|
|
|
myPacket->Read(player);
|
|
|
|
myPacket->Send(player, true);
|
2016-01-12 03:41:44 +00:00
|
|
|
}
|
|
|
|
|
2016-09-18 07:52:13 +00:00
|
|
|
if (player->LoadedState() == Player::NOTLOADED)
|
2016-01-12 03:41:44 +00:00
|
|
|
return;
|
2016-09-18 07:52:13 +00:00
|
|
|
else if (player->LoadedState() == Player::LOADED)
|
2016-09-18 03:54:45 +00:00
|
|
|
{
|
2016-09-18 07:52:13 +00:00
|
|
|
player->Loaded(Player::POSTLOADED);
|
2016-09-18 03:54:45 +00:00
|
|
|
NewPlayer(packet->guid);
|
2016-09-18 07:52:13 +00:00
|
|
|
return;
|
2016-01-12 03:41:44 +00:00
|
|
|
}
|
|
|
|
|
2016-08-17 15:04:35 +00:00
|
|
|
switch (packet->data[0])
|
2016-01-12 03:41:44 +00:00
|
|
|
{
|
2016-09-18 03:54:45 +00:00
|
|
|
/*case ID_GAME_BASE_INFO:
|
2016-01-12 03:41:44 +00:00
|
|
|
{
|
2016-10-19 18:26:42 +00:00
|
|
|
LOG_MESSAGE_SIMPLE(Log::LOG_INFO, "Received ID_GAME_BASE_INFO about %s",
|
|
|
|
player->Npc()->mName.c_str());
|
2016-01-12 03:41:44 +00:00
|
|
|
|
2016-10-19 18:26:42 +00:00
|
|
|
myPacket->Read(player);
|
|
|
|
myPacket->Send(player, true);
|
2016-01-12 03:41:44 +00:00
|
|
|
|
2016-10-19 18:26:42 +00:00
|
|
|
break;
|
2016-09-18 03:54:45 +00:00
|
|
|
}*/
|
2016-10-19 18:26:42 +00:00
|
|
|
case ID_GAME_POS:
|
|
|
|
{
|
|
|
|
//DEBUG_PRINTF("ID_GAME_POS \n");
|
2016-01-12 03:41:44 +00:00
|
|
|
|
2016-10-19 18:26:42 +00:00
|
|
|
if (!player->CreatureStats()->mDead)
|
2016-01-12 03:41:44 +00:00
|
|
|
{
|
2016-10-19 18:26:42 +00:00
|
|
|
myPacket->Read(player);
|
|
|
|
myPacket->Send(player, true); //send to other clients
|
|
|
|
}
|
2016-01-12 03:41:44 +00:00
|
|
|
|
2016-10-19 18:26:42 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case ID_GAME_CELL:
|
|
|
|
{
|
|
|
|
LOG_MESSAGE_SIMPLE(Log::LOG_INFO, "Received ID_GAME_CELL from %s",
|
|
|
|
player->Npc()->mName.c_str());
|
2016-08-18 19:29:54 +00:00
|
|
|
|
2016-10-19 18:26:42 +00:00
|
|
|
if (!player->CreatureStats()->mDead)
|
|
|
|
{
|
|
|
|
myPacket->Read(player);
|
2016-08-18 19:29:54 +00:00
|
|
|
|
2016-10-19 18:26:42 +00:00
|
|
|
LOG_APPEND(Log::LOG_INFO, "- Moved to %s",
|
|
|
|
player->GetCell()->getDescription().c_str());
|
2016-01-12 03:41:44 +00:00
|
|
|
|
2016-10-19 18:26:42 +00:00
|
|
|
myPacket->Send(player, true); //send to other clients
|
|
|
|
Script::Call<Script::CallbackIdentity("OnPlayerChangeCell")>(player->GetID());
|
2016-01-12 03:41:44 +00:00
|
|
|
}
|
2016-10-19 18:26:42 +00:00
|
|
|
else
|
2016-08-22 23:24:10 +00:00
|
|
|
{
|
2016-10-19 18:26:42 +00:00
|
|
|
LOG_APPEND(Log::LOG_INFO, "- Ignored because %s is dead",
|
|
|
|
player->Npc()->mName.c_str());
|
|
|
|
}
|
2016-08-22 23:24:10 +00:00
|
|
|
|
2016-10-19 18:26:42 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case ID_GAME_ATTRIBUTE:
|
|
|
|
{
|
2016-08-22 23:24:10 +00:00
|
|
|
|
2016-10-19 18:26:42 +00:00
|
|
|
if (!player->CreatureStats()->mDead)
|
2016-01-12 03:41:44 +00:00
|
|
|
{
|
2016-10-19 18:26:42 +00:00
|
|
|
myPacket->Read(player);
|
|
|
|
myPacket->Send(player, true);
|
2016-01-12 03:41:44 +00:00
|
|
|
|
2016-10-19 18:26:42 +00:00
|
|
|
Script::Call<Script::CallbackIdentity("OnPlayerChangeAttributes")>(player->GetID());
|
|
|
|
}
|
2016-08-23 00:04:22 +00:00
|
|
|
|
2016-10-19 18:26:42 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case ID_GAME_SKILL:
|
|
|
|
{
|
2016-01-12 03:41:44 +00:00
|
|
|
|
2016-10-19 18:26:42 +00:00
|
|
|
if (!player->CreatureStats()->mDead)
|
2016-09-25 11:28:25 +00:00
|
|
|
{
|
2016-10-19 18:26:42 +00:00
|
|
|
myPacket->Read(player);
|
|
|
|
myPacket->Send(player, true);
|
2016-09-25 11:28:25 +00:00
|
|
|
|
2016-10-19 18:26:42 +00:00
|
|
|
Script::Call<Script::CallbackIdentity("OnPlayerChangeSkills")>(player->GetID());
|
|
|
|
}
|
2016-09-25 11:28:25 +00:00
|
|
|
|
2016-10-19 18:26:42 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case ID_GAME_LEVEL:
|
|
|
|
{
|
2016-09-25 11:28:25 +00:00
|
|
|
|
2016-10-19 18:26:42 +00:00
|
|
|
if (!player->CreatureStats()->mDead)
|
2016-01-12 03:41:44 +00:00
|
|
|
{
|
|
|
|
myPacket->Read(player);
|
|
|
|
myPacket->Send(player, true);
|
|
|
|
|
2016-10-19 18:26:42 +00:00
|
|
|
Script::Call<Script::CallbackIdentity("OnPlayerChangeLevel")>(player->GetID());
|
2016-01-12 03:41:44 +00:00
|
|
|
}
|
|
|
|
|
2016-10-19 18:26:42 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case ID_GAME_EQUIPMENT:
|
|
|
|
{
|
|
|
|
DEBUG_PRINTF("ID_GAME_EQUIPMENT\n");
|
2016-01-12 03:41:44 +00:00
|
|
|
|
2016-10-19 18:26:42 +00:00
|
|
|
myPacket->Read(player);
|
|
|
|
myPacket->Send(player, true);
|
|
|
|
|
|
|
|
Script::Call<Script::CallbackIdentity("OnPlayerChangeEquipment")>(player->GetID());
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
2016-01-12 03:41:44 +00:00
|
|
|
|
2016-10-19 18:26:42 +00:00
|
|
|
case ID_GAME_ATTACK:
|
|
|
|
{
|
|
|
|
DEBUG_PRINTF("ID_GAME_ATTACK\n");
|
2016-10-08 07:15:43 +00:00
|
|
|
|
2016-10-19 18:26:42 +00:00
|
|
|
if (!player->CreatureStats()->mDead)
|
|
|
|
{
|
|
|
|
myPacket->Read(player);
|
2016-10-08 07:15:43 +00:00
|
|
|
|
2016-10-19 18:26:42 +00:00
|
|
|
Player *target = Players::GetPlayer(player->GetAttack()->target);
|
2016-10-08 07:15:43 +00:00
|
|
|
|
2016-10-19 18:26:42 +00:00
|
|
|
if (target == nullptr)
|
|
|
|
target = player;
|
|
|
|
|
|
|
|
LOG_MESSAGE_SIMPLE(Log::LOG_VERBOSE, "Player: %s attaked %s state: %d", player->Npc()->mName.c_str(),
|
|
|
|
target->Npc()->mName.c_str(), player->GetAttack()->pressed == 1);
|
|
|
|
if (player->GetAttack()->pressed == 0)
|
|
|
|
{
|
|
|
|
LOG_APPEND(Log::LOG_VERBOSE, "success: %d", player->GetAttack()->success == 1);
|
|
|
|
if (player->GetAttack()->success == 1)
|
2016-01-12 03:41:44 +00:00
|
|
|
{
|
2016-10-19 18:26:42 +00:00
|
|
|
LOG_APPEND(Log::LOG_VERBOSE, "damage: %d", player->GetAttack()->damage == 1);
|
|
|
|
player->setLastAttackerID(target->GetID());
|
2016-01-12 03:41:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
myPacket->Send(player, true);
|
2016-10-19 18:26:42 +00:00
|
|
|
playerController->GetPacket(ID_GAME_DYNAMICSTATS)->RequestData(player->GetAttack()->target);
|
2016-01-12 03:41:44 +00:00
|
|
|
}
|
2016-10-19 18:26:42 +00:00
|
|
|
break;
|
|
|
|
}
|
2016-01-12 03:41:44 +00:00
|
|
|
|
2016-10-19 18:26:42 +00:00
|
|
|
case ID_GAME_DYNAMICSTATS:
|
|
|
|
{
|
|
|
|
DEBUG_PRINTF("ID_GAME_DYNAMICSTATS\n");
|
|
|
|
myPacket->Read(player);
|
|
|
|
myPacket->Send(player, true);
|
|
|
|
break;
|
|
|
|
}
|
2016-08-18 22:32:39 +00:00
|
|
|
|
2016-10-19 18:26:42 +00:00
|
|
|
case ID_GAME_DIE:
|
|
|
|
{
|
|
|
|
LOG_MESSAGE_SIMPLE(Log::LOG_INFO, "Received ID_GAME_DIE from %s",
|
|
|
|
player->Npc()->mName.c_str());
|
2016-10-08 07:15:43 +00:00
|
|
|
|
2016-10-19 18:26:42 +00:00
|
|
|
Player *killer = Players::GetPlayer(player->getLastAttackerID());
|
2016-10-08 07:15:43 +00:00
|
|
|
|
2016-10-19 18:26:42 +00:00
|
|
|
short reason = 0; // unknown;
|
2016-10-08 07:15:43 +00:00
|
|
|
|
2016-10-19 18:26:42 +00:00
|
|
|
if (!killer)
|
|
|
|
killer = player;
|
|
|
|
else if (killer->GetID() == player->GetID())
|
|
|
|
reason = 2; //suicide
|
|
|
|
else
|
|
|
|
reason = 1; //killed
|
2016-10-08 07:15:43 +00:00
|
|
|
|
2016-10-19 18:26:42 +00:00
|
|
|
player->resetLastAttacker();
|
2016-01-12 03:41:44 +00:00
|
|
|
|
2016-10-19 18:26:42 +00:00
|
|
|
player->CreatureStats()->mDead = true;
|
|
|
|
myPacket->Send(player, true);
|
2016-01-12 03:41:44 +00:00
|
|
|
|
2016-10-19 18:26:42 +00:00
|
|
|
Script::Call<Script::CallbackIdentity("OnPlayerDeath")>(player->GetID(), reason, killer->GetID());
|
2016-01-12 03:41:44 +00:00
|
|
|
|
2016-10-19 18:26:42 +00:00
|
|
|
break;
|
|
|
|
}
|
2016-01-12 03:41:44 +00:00
|
|
|
|
2016-10-19 18:26:42 +00:00
|
|
|
case ID_GAME_RESURRECT:
|
|
|
|
{
|
|
|
|
DEBUG_PRINTF("ID_GAME_RESURRECT\n");
|
|
|
|
//packetResurrect.Read(player);
|
|
|
|
player->CreatureStats()->mDead = false;
|
|
|
|
myPacket->Send(player, true);
|
|
|
|
playerController->GetPacket(ID_GAME_POS)->RequestData(player->guid);
|
|
|
|
playerController->GetPacket(ID_GAME_CELL)->RequestData(player->guid);
|
2016-01-12 03:41:44 +00:00
|
|
|
|
2016-10-19 18:26:42 +00:00
|
|
|
Script::Call<Script::CallbackIdentity("OnPlayerResurrect")>(player->GetID());
|
2016-01-12 03:41:44 +00:00
|
|
|
|
2016-10-19 18:26:42 +00:00
|
|
|
break;
|
|
|
|
}
|
2016-01-12 03:41:44 +00:00
|
|
|
|
2016-10-19 18:26:42 +00:00
|
|
|
case ID_GAME_DRAWSTATE:
|
|
|
|
{
|
|
|
|
DEBUG_PRINTF("ID_GAME_DRAWSTATE\n");
|
|
|
|
myPacket->Read(player);
|
|
|
|
myPacket->Send(player, true);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case ID_CHAT_MESSAGE:
|
|
|
|
{
|
|
|
|
DEBUG_PRINTF("ID_CHAT_MESSAGE\n");
|
|
|
|
myPacket->Read(player);
|
|
|
|
Script::CallBackReturn<Script::CallbackIdentity("OnPlayerSendMessage")> result = true;
|
|
|
|
Script::Call<Script::CallbackIdentity("OnPlayerSendMessage")>(result, player->GetID(), player->ChatMessage()->c_str());
|
|
|
|
|
|
|
|
if (result)
|
2016-01-12 03:41:44 +00:00
|
|
|
{
|
2016-10-19 18:26:42 +00:00
|
|
|
*player->ChatMessage() = player->Npc()->mName + " (" + std::to_string(player->GetID()) + "): "
|
|
|
|
+ *player->ChatMessage() + "\n";
|
|
|
|
myPacket->Send(player, false);
|
2016-01-12 03:41:44 +00:00
|
|
|
myPacket->Send(player, true);
|
|
|
|
}
|
2016-10-19 18:26:42 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case ID_GAME_CHARGEN:
|
|
|
|
{
|
|
|
|
DEBUG_PRINTF("ID_GAME_CHARGEN\n");
|
|
|
|
myPacket->Read(player);
|
2016-01-12 03:41:44 +00:00
|
|
|
|
2016-10-19 18:26:42 +00:00
|
|
|
if (player->CharGenStage()->current == player->CharGenStage()->end && player->CharGenStage()->current != 0)
|
2016-01-12 03:41:44 +00:00
|
|
|
{
|
2016-10-19 18:26:42 +00:00
|
|
|
Script::Call<Script::CallbackIdentity("OnPlayerEndCharGen")>(player->GetID());
|
|
|
|
cout << "RACE: " << player->Npc()->mRace << endl;
|
2016-01-12 03:41:44 +00:00
|
|
|
}
|
2016-10-19 18:26:42 +00:00
|
|
|
break;
|
|
|
|
}
|
2016-01-12 03:41:44 +00:00
|
|
|
|
2016-10-19 18:26:42 +00:00
|
|
|
case ID_GUI_MESSAGEBOX:
|
|
|
|
{
|
|
|
|
DEBUG_PRINTF("ID_GUI_MESSAGEBOX\n");
|
|
|
|
myPacket->Read(player);
|
2016-01-12 03:41:44 +00:00
|
|
|
|
2016-10-19 18:26:42 +00:00
|
|
|
Script::Call<Script::CallbackIdentity("OnGUIAction")>(player->GetID(), (int)player->guiMessageBox.id,
|
|
|
|
player->guiMessageBox.data.c_str());
|
|
|
|
break;
|
|
|
|
}
|
2016-07-23 14:02:06 +00:00
|
|
|
|
2016-10-19 18:26:42 +00:00
|
|
|
case ID_GAME_CHARCLASS:
|
|
|
|
{
|
|
|
|
DEBUG_PRINTF("ID_GAME_CHARCLASS\n");
|
|
|
|
myPacket->Read(player);
|
|
|
|
break;
|
|
|
|
}
|
2016-07-23 14:02:06 +00:00
|
|
|
|
2016-10-23 14:57:38 +00:00
|
|
|
case ID_GAME_INVENTORY:
|
|
|
|
{
|
|
|
|
DEBUG_PRINTF("ID_GAME_INVENTORY\n");
|
|
|
|
myPacket->Read(player);
|
|
|
|
break;
|
|
|
|
}
|
2016-08-30 03:17:06 +00:00
|
|
|
|
2016-10-19 18:26:42 +00:00
|
|
|
default:
|
2016-10-19 18:44:17 +00:00
|
|
|
LOG_MESSAGE_SIMPLE(Log::LOG_WARN, "Unhandled PlayerPacket with identifier %i has arrived",
|
|
|
|
packet->data[0]);
|
2016-10-19 18:26:42 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-19 18:44:17 +00:00
|
|
|
void Networking::ProcessWorldPacket(RakNet::Packet *packet)
|
|
|
|
{
|
|
|
|
Player *player = Players::GetPlayer(packet->guid);
|
|
|
|
|
|
|
|
if (!player->isHandshaked() || player->LoadedState() != Player::POSTLOADED)
|
|
|
|
return;
|
|
|
|
|
2016-10-20 19:15:47 +00:00
|
|
|
WorldPacket *myPacket = worldController->GetPacket(packet->data[0]);
|
|
|
|
WorldEvent *event = new WorldEvent(player->guid);
|
2016-10-22 11:13:16 +00:00
|
|
|
event->cellRef.blank();
|
2016-10-20 19:15:47 +00:00
|
|
|
|
2016-10-19 18:44:17 +00:00
|
|
|
switch (packet->data[0])
|
|
|
|
{
|
|
|
|
|
2016-10-22 15:05:32 +00:00
|
|
|
case ID_WORLD_OBJECT_PLACE:
|
2016-10-21 18:57:05 +00:00
|
|
|
{
|
2016-10-22 15:05:32 +00:00
|
|
|
LOG_MESSAGE_SIMPLE(Log::LOG_WARN, "Received ID_WORLD_OBJECT_PLACE from %s",
|
2016-10-21 18:57:05 +00:00
|
|
|
player->Npc()->mName.c_str());
|
|
|
|
|
|
|
|
myPacket->Read(event);
|
|
|
|
myPacket->Send(event, true);
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2016-10-22 15:05:32 +00:00
|
|
|
case ID_WORLD_OBJECT_DELETE:
|
2016-10-19 18:44:17 +00:00
|
|
|
{
|
2016-10-22 15:05:32 +00:00
|
|
|
LOG_MESSAGE_SIMPLE(Log::LOG_WARN, "Received ID_WORLD_OBJECT_DELETE from %s",
|
2016-10-19 18:44:17 +00:00
|
|
|
player->Npc()->mName.c_str());
|
2016-10-20 19:15:47 +00:00
|
|
|
|
|
|
|
myPacket->Read(event);
|
2016-10-24 08:26:31 +00:00
|
|
|
|
|
|
|
LOG_APPEND(Log::LOG_WARN, "- cellRef: %s, %i\n- cell: %s",
|
|
|
|
event->cellRef.mRefID.c_str(),
|
|
|
|
event->cellRef.mRefNum.mIndex,
|
|
|
|
event->cell.getDescription().c_str());
|
|
|
|
|
|
|
|
myPacket->Send(event, true);
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2016-10-24 10:20:04 +00:00
|
|
|
case ID_WORLD_OBJECT_LOCK:
|
|
|
|
{
|
|
|
|
LOG_MESSAGE_SIMPLE(Log::LOG_WARN, "Received ID_WORLD_OBJECT_LOCK from %s",
|
|
|
|
player->Npc()->mName.c_str());
|
|
|
|
|
|
|
|
myPacket->Read(event);
|
|
|
|
|
|
|
|
LOG_APPEND(Log::LOG_WARN, "- cellRef: %s, %i\n- cell: %s",
|
|
|
|
event->cellRef.mRefID.c_str(),
|
|
|
|
event->cellRef.mRefNum.mIndex,
|
|
|
|
event->cell.getDescription().c_str());
|
|
|
|
|
|
|
|
myPacket->Send(event, true);
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2016-10-24 08:26:31 +00:00
|
|
|
case ID_WORLD_OBJECT_UNLOCK:
|
|
|
|
{
|
|
|
|
LOG_MESSAGE_SIMPLE(Log::LOG_WARN, "Received ID_WORLD_OBJECT_UNLOCK from %s",
|
|
|
|
player->Npc()->mName.c_str());
|
|
|
|
|
|
|
|
myPacket->Read(event);
|
2016-10-22 09:45:19 +00:00
|
|
|
|
2016-10-23 13:55:30 +00:00
|
|
|
LOG_APPEND(Log::LOG_WARN, "- cellRef: %s, %i\n- cell: %s",
|
2016-10-23 13:33:53 +00:00
|
|
|
event->cellRef.mRefID.c_str(),
|
|
|
|
event->cellRef.mRefNum.mIndex,
|
2016-10-23 13:55:30 +00:00
|
|
|
event->cell.getDescription().c_str());
|
2016-10-22 09:45:19 +00:00
|
|
|
|
2016-10-20 19:15:47 +00:00
|
|
|
myPacket->Send(event, true);
|
2016-10-19 18:44:17 +00:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
LOG_MESSAGE_SIMPLE(Log::LOG_WARN, "Unhandled WorldPacket with identifier %i has arrived",
|
|
|
|
packet->data[0]);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-10-19 18:26:42 +00:00
|
|
|
void Networking::Update(RakNet::Packet *packet)
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
|
|
|
|
{
|
|
|
|
RakNet::RakNetGUID ignoredGUID;
|
|
|
|
bsIn.Read(ignoredGUID);
|
|
|
|
(void)ignoredGUID;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (player == 0)
|
|
|
|
{
|
|
|
|
playerController->SetStream(&bsIn, 0);
|
|
|
|
|
|
|
|
playerController->GetPacket(ID_HANDSHAKE)->RequestData(packet->guid);
|
|
|
|
Players::NewPlayer(packet->guid);
|
|
|
|
player = Players::GetPlayer(packet->guid);
|
|
|
|
playerController->GetPacket(ID_USER_MYID)->Send(Players::GetPlayer(packet->guid), false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else if (playerController->ContainsPacket(packet->data[0]))
|
|
|
|
{
|
|
|
|
playerController->SetStream(&bsIn, 0);
|
|
|
|
ProcessPlayerPacket(packet);
|
|
|
|
}
|
|
|
|
else if (worldController->ContainsPacket(packet->data[0]))
|
|
|
|
{
|
|
|
|
worldController->SetStream(&bsIn, 0);
|
2016-10-19 18:44:17 +00:00
|
|
|
ProcessWorldPacket(packet);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Networking::NewPlayer(RakNet::RakNetGUID guid)
|
|
|
|
{
|
2016-10-19 13:54:39 +00:00
|
|
|
playerController->GetPacket(ID_GAME_BASE_INFO)->RequestData(guid);
|
|
|
|
playerController->GetPacket(ID_GAME_DYNAMICSTATS)->RequestData(guid);
|
|
|
|
playerController->GetPacket(ID_GAME_POS)->RequestData(guid);
|
|
|
|
playerController->GetPacket(ID_GAME_CELL)->RequestData(guid);
|
|
|
|
playerController->GetPacket(ID_GAME_EQUIPMENT)->RequestData(guid);
|
2016-01-12 03:41:44 +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-08-17 15:04:35 +00:00
|
|
|
if (pl->first == guid) continue;
|
2016-01-12 03:41:44 +00:00
|
|
|
|
2016-10-19 13:54:39 +00:00
|
|
|
playerController->GetPacket(ID_GAME_BASE_INFO)->Send(pl->second, guid);
|
|
|
|
playerController->GetPacket(ID_GAME_DYNAMICSTATS)->Send(pl->second, guid);
|
|
|
|
playerController->GetPacket(ID_GAME_ATTRIBUTE)->Send(pl->second, guid);
|
|
|
|
playerController->GetPacket(ID_GAME_SKILL)->Send(pl->second, guid);
|
|
|
|
playerController->GetPacket(ID_GAME_POS)->Send(pl->second, guid);
|
|
|
|
playerController->GetPacket(ID_GAME_CELL)->Send(pl->second, guid);
|
|
|
|
playerController->GetPacket(ID_GAME_EQUIPMENT)->Send(pl->second, guid);
|
2016-01-12 03:41:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-08-18 02:21:25 +00:00
|
|
|
|
|
|
|
|
2016-01-12 03:41:44 +00:00
|
|
|
void Networking::DisconnectPlayer(RakNet::RakNetGUID guid)
|
|
|
|
{
|
2016-10-08 07:30:52 +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;
|
|
|
|
Script::Call<Script::CallbackIdentity("OnPlayerDisconnect")>(player->GetID());
|
2016-10-19 13:54:39 +00:00
|
|
|
playerController->GetPacket(ID_USER_DISCONNECTED)->Send(player, true);
|
2016-01-12 03:41:44 +00:00
|
|
|
Players::DeletePlayer(guid);
|
|
|
|
}
|
|
|
|
|
2016-10-19 13:54:39 +00:00
|
|
|
PlayerPacketController *Networking::GetPlayerController() const
|
2016-01-12 03:41:44 +00:00
|
|
|
{
|
2016-10-19 13:54:39 +00:00
|
|
|
return playerController;
|
2016-01-12 03:41:44 +00:00
|
|
|
}
|
|
|
|
|
2016-10-19 17:51:51 +00:00
|
|
|
WorldPacketController *Networking::GetWorldController() const
|
2016-01-12 03:41:44 +00:00
|
|
|
{
|
2016-10-19 17:51:51 +00:00
|
|
|
return worldController;
|
2016-01-12 03:41:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const Networking &Networking::Get()
|
|
|
|
{
|
|
|
|
return *sThis;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Networking *Networking::GetPtr()
|
|
|
|
{
|
|
|
|
return sThis;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Networking::StopServer(int code)
|
|
|
|
{
|
|
|
|
running = false;
|
|
|
|
exitCode = code;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Networking::MainLoop()
|
|
|
|
{
|
|
|
|
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())
|
|
|
|
{
|
|
|
|
switch (packet->data[0])
|
|
|
|
{
|
|
|
|
case ID_REMOTE_DISCONNECTION_NOTIFICATION:
|
2016-10-19 10:00:33 +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:
|
2016-10-19 10:00:33 +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:
|
2016-10-19 10:00:33 +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-10-19 10:00:33 +00:00
|
|
|
LOG_MESSAGE_SIMPLE(Log::LOG_WARN, "%s", "Our connection request has been accepted");
|
2016-01-12 03:41:44 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case ID_NEW_INCOMING_CONNECTION:
|
2016-10-19 10:00:33 +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-10-19 10:00:33 +00:00
|
|
|
LOG_MESSAGE_SIMPLE(Log::LOG_WARN, "%s", "The server is full");
|
2016-01-12 03:41:44 +00:00
|
|
|
break;
|
|
|
|
case ID_DISCONNECTION_NOTIFICATION:
|
2016-10-19 10:00:33 +00:00
|
|
|
LOG_MESSAGE_SIMPLE(Log::LOG_WARN, "Client at %s has disconnected",
|
|
|
|
packet->systemAddress.ToString());
|
2016-01-12 03:41:44 +00:00
|
|
|
DisconnectPlayer(packet->guid);
|
|
|
|
break;
|
|
|
|
case ID_CONNECTION_LOST:
|
2016-10-19 10:00:33 +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
|
|
|
DisconnectPlayer(packet->guid);
|
|
|
|
break;
|
|
|
|
default:
|
2016-08-05 06:21:09 +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;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Networking::KickPlayer(RakNet::RakNetGUID guid)
|
|
|
|
{
|
|
|
|
peer->CloseConnection(guid, true);
|
|
|
|
}
|
2016-08-14 23:48:25 +00:00
|
|
|
|
|
|
|
unsigned short Networking::NumberOfConnections() const
|
|
|
|
{
|
|
|
|
return peer->NumberOfConnections();
|
|
|
|
}
|
2016-08-19 00:18:25 +00:00
|
|
|
|
|
|
|
unsigned int Networking::MaxConnections() const
|
|
|
|
{
|
|
|
|
return peer->GetMaximumIncomingConnections();
|
|
|
|
}
|