Skip to content

Commit

Permalink
Update listserver packet handling
Browse files Browse the repository at this point in the history
  • Loading branch information
xx-shitai-xx committed Feb 19, 2023
1 parent f14e81d commit 467da0f
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 72 deletions.
2 changes: 1 addition & 1 deletion dependencies/gs2lib
Submodule gs2lib updated from e77215 to 0ce4d3
10 changes: 7 additions & 3 deletions server/include/TPacket.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,24 @@
#define GS2EMU_TPACKET_H

#include <utility>

#include "IEnums.h"
#include "CString.h"


template <class T>
struct TPacket {
T Id;
CString Data;
};

/* Player packets */
typedef TPacket<ServerToPlayer> PlayerOutPacket;
typedef std::vector<PlayerOutPacket> PlayerOutPackets;
typedef TPacket<PlayerToServer> PlayerInPacket;
typedef std::vector<PlayerInPacket> PlayerInPackets;

/* ListServer packets */
typedef TPacket<ServerToListServer> ListServerOutPacket;
typedef std::vector<ListServerOutPacket> ListServerOutPackets;
typedef TPacket<ListServerToServer> ListServerInPacket;
typedef std::vector<ListServerInPacket> ListServerInPackets;

#endif //GS2EMU_TPACKET_H
11 changes: 6 additions & 5 deletions server/include/TServerList.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@
#define TSERVERLIST_H

#include <map>
#include <time.h>
#include <ctime>
#include <cassert>
#include "CFileQueue.h"
#include "CString.h"
#include "CSocket.h"
#include <assert.h>
#include "TPacket.h"

enum
{
Expand Down Expand Up @@ -36,13 +37,13 @@ class TServerList : public CSocketStub
~TServerList();

bool doTimedEvents();

// Socket-Control Functions
bool getConnected() const;
bool main();
bool connectServer();
CSocket* getSocket() { return &sock; }
void sendPacket(CString& pPacket, bool sendNow = false);
void sendPacket(const ListServerOutPacket& pPacket, bool sendNow = false);

// Send players to the listserver
void addPlayer(TPlayer *player);
Expand Down Expand Up @@ -93,7 +94,7 @@ class TServerList : public CSocketStub
void msgSVI_SENDTEXT(CString& pPacket);
void msgSVI_PMPLAYER(CString& pPacket);
void msgSVI_ASSIGNPCID(CString& pPacket);

protected:
// Packet Functions
bool parsePacket(CString& pPacket);
Expand Down
27 changes: 15 additions & 12 deletions server/src/TPlayer/TPlayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1071,7 +1071,7 @@ bool TPlayer::processChat(CString pChat)
if (file.length() != 0)
setProps(CString() >> (char)PLPROP_HEADGIF >> (char)(chatParse[1].length() + 100) << chatParse[1], PLSETPROPS_SETBYPLAYER | PLSETPROPS_FORWARD | PLSETPROPS_FORWARDSELF);
else
server->getServerList()->sendPacket(CString() >> (char)SVO_GETFILE3 >> (short)id >> (char)0 >> (char)chatParse[1].length() << chatParse[1]);
server->getServerList()->sendPacket({SVO_GETFILE3, CString() >> (short)id >> (char)0 >> (char)chatParse[1].length() << chatParse[1]});
}
else if (chatParse[0] == "setbody" && chatParse.size() == 2)
{
Expand Down Expand Up @@ -1118,7 +1118,7 @@ bool TPlayer::processChat(CString pChat)
if (file.length() != 0)
setProps(CString() >> (char)PLPROP_BODYIMG >> (char)chatParse[1].length() << chatParse[1], PLSETPROPS_SETBYPLAYER | PLSETPROPS_FORWARD | PLSETPROPS_FORWARDSELF);
else
server->getServerList()->sendPacket(CString() >> (char)SVO_GETFILE3 >> (short)id >> (char)1 >> (char)chatParse[1].length() << chatParse[1]);
server->getServerList()->sendPacket({SVO_GETFILE3, CString() >> (short)id >> (char)1 >> (char)chatParse[1].length() << chatParse[1]});
}
else if (chatParse[0] == "setsword" && chatParse.size() == 2)
{
Expand Down Expand Up @@ -1165,7 +1165,7 @@ bool TPlayer::processChat(CString pChat)
if (file.length() != 0)
setProps(CString() >> (char)PLPROP_SWORDPOWER >> (char)(swordPower + 30) >> (char)chatParse[1].length() << chatParse[1], PLSETPROPS_SETBYPLAYER | PLSETPROPS_FORWARD | PLSETPROPS_FORWARDSELF);
else
server->getServerList()->sendPacket(CString() >> (char)SVO_GETFILE3 >> (short)id >> (char)2 >> (char)chatParse[1].length() << chatParse[1]);
server->getServerList()->sendPacket({SVO_GETFILE3, CString() >> (short)id >> (char)2 >> (char)chatParse[1].length() << chatParse[1]});
}
else if (chatParse[0] == "setshield" && chatParse.size() == 2)
{
Expand Down Expand Up @@ -1212,7 +1212,7 @@ bool TPlayer::processChat(CString pChat)
if (file.length() != 0)
setProps(CString() >> (char)PLPROP_SHIELDPOWER >> (char)(shieldPower + 10) >> (char)chatParse[1].length() << chatParse[1], PLSETPROPS_SETBYPLAYER | PLSETPROPS_FORWARD | PLSETPROPS_FORWARDSELF);
else
server->getServerList()->sendPacket(CString() >> (char)SVO_GETFILE3 >> (short)id >> (char)3 >> (char)chatParse[1].length() << chatParse[1]);
server->getServerList()->sendPacket({SVO_GETFILE3, CString() >> (short)id >> (char)3 >> (char)chatParse[1].length() << chatParse[1]});
}
else if (chatParse[0] == "setskin" && chatParse.size() == 2 && setcolorsallowed)
{
Expand Down Expand Up @@ -2078,11 +2078,14 @@ void TPlayer::setNick(CString pNickName, bool force)
if (askGlobal)
{
server->getServerList()->sendPacket(
CString() >> (char)SVO_VERIGUILD >> (short)id
>> (char)accountName.length() << accountName
>> (char)newNick.length() << newNick
>> (char)guild.length() << guild
);
{
SVO_VERIGUILD,
CString() >> (short)id
>> (char)accountName.length() << accountName
>> (char)newNick.length() << newNick
>> (char)guild.length() << guild
}
);
}
}
else
Expand Down Expand Up @@ -3928,7 +3931,7 @@ bool TPlayer::msgPLI_SERVERWARP(CString& pPacket)
{
CString servername = pPacket.readString("");
server->getServerLog().out("%s is requesting serverwarp to %s", accountName.text(), servername.text());
server->getServerList()->sendPacket(CString() >> (char)SVO_SERVERINFO >> (short)id << servername);
server->getServerList()->sendPacket({SVO_SERVERINFO, CString() >> (short)id << servername});
return true;
}

Expand Down Expand Up @@ -3959,7 +3962,7 @@ bool TPlayer::msgPLI_RAWDATA(CString& pPacket)
bool TPlayer::msgPLI_PROFILEGET(CString& pPacket)
{
// Send the packet ID for backwards compatibility.
server->getServerList()->sendPacket(CString() >> (char)SVO_GETPROF >> (short)id << pPacket);
server->getServerList()->sendPacket({SVO_GETPROF, CString() >> (short)id << pPacket});
return true;
}

Expand All @@ -3970,7 +3973,7 @@ bool TPlayer::msgPLI_PROFILESET(CString& pPacket)

// Old gserver would send the packet ID with pPacket so, for
// backwards compatibility, do that here.
server->getServerList()->sendPacket(CString() >> (char)SVO_SETPROF << pPacket);
server->getServerList()->sendPacket({SVO_SETPROF, CString() << pPacket});
return true;
}

Expand Down
8 changes: 4 additions & 4 deletions server/src/TPlayer/TPlayerExternalPlayers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ bool TPlayer::addPMServer(CString& option)
TServerList* list = server->getServerList();

bool PMSrvExist = false;
for (std::vector<CString>::const_iterator ij = PMServerList.begin(); ij != PMServerList.end(); ++ij)
for (const auto & ij : PMServerList)
{
if ((ij)->text() == option)
if (ij.text() == option)
{
PMSrvExist = true;
}
Expand All @@ -23,7 +23,7 @@ bool TPlayer::addPMServer(CString& option)
if (!PMSrvExist)
{
PMServerList.push_back(option);
list->sendPacket(CString() >> (char)SVO_REQUESTLIST >> (short)id << CString(CString() << "GraalEngine" << "\n" << "pmserverplayers" << "\n" << option << "\n").gtokenizeI());
list->sendPacket({SVO_REQUESTLIST, CString() >> (short)id << CString(CString() << "GraalEngine" << "\n" << "pmserverplayers" << "\n" << option << "\n").gtokenizeI()});
return true;
}
else
Expand Down Expand Up @@ -196,7 +196,7 @@ bool TPlayer::updatePMPlayers(CString& servername, CString& players)
bool TPlayer::pmExternalPlayer(CString servername, CString account, CString& pmMessage)
{
TServerList* list = server->getServerList();
list->sendPacket(CString() >> (char)SVO_PMPLAYER >> (short)id << CString(CString() << servername << "\n" << accountName << "\n" << nickName << "\n" << "GraalEngine" << "\n" << "pmplayer" << "\n" << account << "\n" << pmMessage).gtokenizeI());
list->sendPacket(ListServerOutPacket{SVO_PMPLAYER, CString() >> (short)id << CString(CString() << servername << "\n" << accountName << "\n" << nickName << "\n" << "GraalEngine" << "\n" << "pmplayer" << "\n" << account << "\n" << pmMessage).gtokenizeI()});
return true;
}

Expand Down
10 changes: 5 additions & 5 deletions server/src/TPlayer/TPlayerRequestText.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ bool TPlayer::msgPLI_REQUESTTEXT(CString& pPacket)
if (type == "lister")
{
if (option == "simplelist")
list->sendPacket(CString() >> (char)SVO_REQUESTLIST >> (short)id << CString(weapon << "\n" << type << "\n" << "simpleserverlist" << "\n").gtokenizeI());
list->sendPacket({SVO_REQUESTLIST, CString() >> (short)id << CString(weapon << "\n" << type << "\n" << "simpleserverlist" << "\n").gtokenizeI()});
else if (option == "rebornlist")
list->sendPacket(CString() >> (char)SVO_REQUESTLIST >> (short)id << packet);
list->sendPacket({SVO_REQUESTLIST, CString() >> (short)id << packet});
else if (option == "subscriptions") {
// some versions of the loginserver scripts expected the response of subscriptions2 rather than subscriptions
sendPacket({PLO_SERVERTEXT, CString() << CString(CString() << weapon << "\n" << type << "\n" << "subscriptions" << "\n" << CString(CString() << "unlimited" << "\n" << "Unlimited Subscription" << "\n" << "\"\"" << "\n").gtokenizeI()).gtokenizeI()});
Expand All @@ -29,12 +29,12 @@ bool TPlayer::msgPLI_REQUESTTEXT(CString& pPacket)
else if (option == "getglobalitems")
sendPacket({PLO_SERVERTEXT, CString() << CString(weapon << "\n" << type << "\n" << "globalitems" << "\n" << accountName.text() << "\n" << CString(CString(CString() << "autobill=1" << "\n" << "autobillmine=1" << "\n" << "bundle=1" << "\n" << "creationtime=1212768763" << "\n" << "currenttime=1353248504" << "\n" << "description=Gives" << "\n" << "duration=2629800" << "\n" << "flags=subscription" << "\n" << "icon=graalicon_big.png" << "\n" << "itemid=1" << "\n" << "lifetime=1" << "\n" << "owner=global" << "\n" << "ownertype=server" << "\n" << "price=100" << "\n" << "quantity=988506" << "\n" << "status=available" << "\n" << "title=Gold" << "\n" << "tradable=1" << "\n" << "typeid=62" << "\n" << "world=global" << "\n").gtokenizeI()).gtokenizeI()).gtokenizeI()});
else if (option == "serverinfo") {
list->sendPacket(CString() >> (char)SVO_REQUESTSVRINFO >> (short)id << packet);
list->sendPacket({SVO_REQUESTSVRINFO, CString() >> (short)id << packet});
}

}
else if (type == "pmservers" || type == "pmguilds") {
list->sendPacket(CString() >> (char)SVO_REQUESTLIST >> (short)id << packet);
list->sendPacket({SVO_REQUESTLIST, CString() >> (short)id << packet});
}
else if (type == "pmserverplayers")
addPMServer(option);
Expand Down Expand Up @@ -146,7 +146,7 @@ bool TPlayer::msgPLI_SENDTEXT(CString& pPacket)
else if (type == "lister")
{
if (option == "serverinfo")
list->sendPacket(CString() >> (char)SVO_REQUESTSVRINFO >> (short)id << packet);
list->sendPacket({SVO_REQUESTSVRINFO, CString() >> (short)id << packet});

if (!getGuest())
{
Expand Down
87 changes: 45 additions & 42 deletions server/src/TServerList.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -227,28 +227,33 @@ bool TServerList::connectServer()

// Use the new protocol for communicating with the listserver
_fileQueue.setCodec(ENCRYPT_GEN_1, 0);
sendPacket(CString() >> (char)SVO_REGISTERV3 << version, true);
sendPacket({SVO_REGISTERV3, CString() << version}, true);
_fileQueue.setCodec(ENCRYPT_GEN_2, 0);

// Send before SVO_NEWSERVER or else we will get an incorrect name.
CSettings* adminsettings = _server->getAdminSettings();
sendPacket(CString() >> (char)SVO_SERVERHQPASS << adminsettings->getStr("hq_password"));
sendPacket({SVO_SERVERHQPASS, CString() << adminsettings->getStr("hq_password")});

// Send server info.
sendPacket(CString() >> (char)SVO_NEWSERVER
>> (char)name.length() << name
>> (char)desc.length() << desc
>> (char)language.length() << language
>> (char)version.length() << version
>> (char)url.length() << url
>> (char)ip.length() << ip
>> (char)port.length() << port
>> (char)localip.length() << localip);
sendPacket(
{
SVO_NEWSERVER,
CString()
>> (char)name.length() << name
>> (char)desc.length() << desc
>> (char)language.length() << language
>> (char)version.length() << version
>> (char)url.length() << url
>> (char)ip.length() << ip
>> (char)port.length() << port
>> (char)localip.length() << localip
}
);

// Set the level now.
if(_server->getSettings()->getBool("onlystaff", false))
sendPacket(CString() >> (char)SVO_SERVERHQLEVEL >> (char)0);
else sendPacket(CString() >> (char)SVO_SERVERHQLEVEL >> (char)adminsettings->getInt("hq_level", 1));
sendPacket({SVO_SERVERHQLEVEL, CString() >> (char)0});
else sendPacket({SVO_SERVERHQLEVEL, CString() >> (char)adminsettings->getInt("hq_level", 1)});

sendVersionConfig();

Expand Down Expand Up @@ -278,18 +283,19 @@ void TServerList::sendVersionConfig()
sendText(fmt::format("Listserver,settings,allowedversions,{}", versionNames.text()));
}

void TServerList::sendPacket(CString& pPacket, bool sendNow)
void TServerList::sendPacket(const ListServerOutPacket& pPacket, bool sendNow)
{
// empty buffer?
if (pPacket.isEmpty())
if (pPacket.Data.isEmpty())
return;

CString packet = CString() >> (char)pPacket.Id << pPacket.Data;
// append '\n'
if (pPacket[pPacket.length()-1] != '\n')
pPacket.writeChar('\n');
if (packet[packet.length()-1] != '\n')
packet.writeChar('\n');

// append buffer
_fileQueue.addPacket(pPacket);
_fileQueue.addPacket(packet);

// send buffer now?
if (sendNow)
Expand All @@ -304,28 +310,28 @@ void TServerList::addPlayer(TPlayer *player)
assert(player != nullptr);

CString dataPacket;
dataPacket >> (char)SVO_PLYRADD >> (short)player->getId() >> (char)player->getType();
dataPacket >> (short)player->getId() >> (char)player->getType();
dataPacket >> (char)PLPROP_ACCOUNTNAME << player->getProp(PLPROP_ACCOUNTNAME);
dataPacket >> (char)PLPROP_NICKNAME << player->getProp(PLPROP_NICKNAME);
dataPacket >> (char)PLPROP_CURLEVEL << player->getProp(PLPROP_CURLEVEL);
dataPacket >> (char)PLPROP_X << player->getProp(PLPROP_X);
dataPacket >> (char)PLPROP_Y << player->getProp(PLPROP_Y);
dataPacket >> (char)PLPROP_ALIGNMENT << player->getProp(PLPROP_ALIGNMENT);
dataPacket >> (char)PLPROP_IPADDR << player->getProp(PLPROP_IPADDR);
sendPacket(dataPacket);
sendPacket({SVO_PLYRADD, dataPacket});
}

void TServerList::deletePlayer(TPlayer *player)
{
assert(player != nullptr);

sendPacket(CString() >> (char)SVO_PLYRREM >> (short)player->getId());
sendPacket({SVO_PLYRREM, CString() >> (short)player->getId()});
}

void TServerList::sendPlayers()
{
// Clears the serverlist players
sendPacket(CString() >> (char)SVO_SETPLYR);
sendPacket({SVO_SETPLYR, CString()});

// Adds the players to the serverlist
auto playerList = _server->getPlayerList();
Expand Down Expand Up @@ -405,48 +411,45 @@ void TServerList::handleText(const CString& data)

void TServerList::sendText(const CString& data)
{
CString dataPacket;
dataPacket.writeGChar(SVO_SENDTEXT);
dataPacket << data;
sendPacket(dataPacket);
sendPacket({SVO_SENDTEXT, data});
}

void TServerList::sendText(const std::vector<CString>& stringList)
{
CString dataPacket;
dataPacket.writeGChar(SVO_SENDTEXT);
for (const auto & string : stringList)
dataPacket << string.gtokenize();
sendPacket(dataPacket);
sendPacket({SVO_SENDTEXT, dataPacket});
}

void TServerList::sendTextForPlayer(TPlayer *player, const CString& data)
{
assert(player != nullptr);

CString dataPacket;
dataPacket.writeGChar(SVO_REQUESTLIST);
dataPacket >> (short)player->getId() << data;
sendPacket(dataPacket);
sendPacket({SVO_REQUESTLIST, CString() >> (short)player->getId() << data});
}

void TServerList::sendLoginPacketForPlayer(TPlayer *player, const CString& password, const CString& identity)
{
sendPacket(CString() >> (char)SVO_VERIACC2
>> (char)player->getAccountName().length() << player->getAccountName()
>> (char)password.length() << password
>> (short)player->getId() >> (char)player->getType()
>> (short)identity.length() << identity
);
sendPacket(
{
SVO_VERIACC2,
CString()
>> (char)player->getAccountName().length() << player->getAccountName()
>> (char)password.length() << password
>> (short)player->getId() >> (char)player->getType()
>> (short)identity.length() << identity
}
);
}

void TServerList::sendServerHQ()
{
CSettings* adminsettings = _server->getAdminSettings();
sendPacket(CString() >> (char)SVO_SERVERHQPASS << adminsettings->getStr("hq_password"));
sendPacket({SVO_SERVERHQPASS, CString() << adminsettings->getStr("hq_password")});
if(_server->getSettings()->getBool("onlystaff", false))
sendPacket(CString() >> (char)SVO_SERVERHQLEVEL >> (char)0);
else sendPacket(CString() >> (char)SVO_SERVERHQLEVEL >> (char)adminsettings->getInt("hq_level", 1));
sendPacket({SVO_SERVERHQLEVEL, CString() >> (char)0});
else sendPacket({SVO_SERVERHQLEVEL, CString() >> (char)adminsettings->getInt("hq_level", 1)});
}

/*
Expand Down Expand Up @@ -728,7 +731,7 @@ void TServerList::msgSVI_FILEEND2(CString& pPacket)
void TServerList::msgSVI_PING(CString& pPacket)
{
// When server pings, we pong
sendPacket(CString() >> (char)SVO_PING);
sendPacket({SVO_PING, CString()});
}

void TServerList::msgSVI_RAWDATA(CString& pPacket)
Expand Down

0 comments on commit 467da0f

Please sign in to comment.