Rewrite how zone finds ports

This commit is contained in:
KimLS 2020-03-21 17:05:28 -07:00
parent e384cf6149
commit 52d2469da2
6 changed files with 47 additions and 38 deletions

View File

@ -25,6 +25,10 @@ namespace EQ
uv_run(&m_loop, UV_RUN_DEFAULT); uv_run(&m_loop, UV_RUN_DEFAULT);
} }
void Shutdown() {
uv_stop(&m_loop);
}
uv_loop_t* Handle() { return &m_loop; } uv_loop_t* Handle() { return &m_loop; }
private: private:

View File

@ -415,6 +415,7 @@ int main(int argc, char** argv) {
RegisterConsoleFunctions(console); RegisterConsoleFunctions(console);
} }
zoneserver_list.Init();
std::unique_ptr<EQ::Net::ServertalkServer> server_connection; std::unique_ptr<EQ::Net::ServertalkServer> server_connection;
server_connection.reset(new EQ::Net::ServertalkServer()); server_connection.reset(new EQ::Net::ServertalkServer());

View File

@ -39,7 +39,6 @@ ZSList::ZSList()
{ {
NextID = 1; NextID = 1;
CurGroupID = 1; CurGroupID = 1;
LastAllocatedPort = 0;
memset(pLockedZones, 0, sizeof(pLockedZones)); memset(pLockedZones, 0, sizeof(pLockedZones));
m_tick.reset(new EQ::Timer(5000, true, std::bind(&ZSList::OnTick, this, std::placeholders::_1))); m_tick.reset(new EQ::Timer(5000, true, std::bind(&ZSList::OnTick, this, std::placeholders::_1)));
@ -76,7 +75,12 @@ void ZSList::Remove(const std::string &uuid)
auto iter = zone_server_list.begin(); auto iter = zone_server_list.begin();
while (iter != zone_server_list.end()) { while (iter != zone_server_list.end()) {
if ((*iter)->GetUUID().compare(uuid) == 0) { if ((*iter)->GetUUID().compare(uuid) == 0) {
auto port = (*iter)->GetCPort();
zone_server_list.erase(iter); zone_server_list.erase(iter);
if (port != 0) {
m_ports_free.push_back(port);
}
return; return;
} }
iter++; iter++;
@ -239,6 +243,14 @@ bool ZSList::SetLockedZone(uint16 iZoneID, bool iLock) {
return false; return false;
} }
void ZSList::Init()
{
const WorldConfig* Config = WorldConfig::get();
for (uint16 i = Config->ZonePortLow; i <= Config->ZonePortHigh; ++i) {
m_ports_free.push_back(i);
}
}
bool ZSList::IsZoneLocked(uint16 iZoneID) { bool ZSList::IsZoneLocked(uint16 iZoneID) {
for (auto &zone : pLockedZones) { for (auto &zone : pLockedZones) {
if (zone == iZoneID) if (zone == iZoneID)
@ -577,30 +589,15 @@ void ZSList::RebootZone(const char* ip1, uint16 port, const char* ip2, uint32 sk
safe_delete_array(tmp); safe_delete_array(tmp);
} }
uint16 ZSList::GetAvailableZonePort() uint16 ZSList::GetAvailableZonePort()
{ {
const WorldConfig *Config = WorldConfig::get(); if (m_ports_free.empty()) {
int i; return 0;
uint16 port = 0;
if (LastAllocatedPort == 0)
i = Config->ZonePortLow;
else
i = LastAllocatedPort + 1;
while (i != LastAllocatedPort && port == 0) {
if (i>Config->ZonePortHigh)
i = Config->ZonePortLow;
if (!FindByPort(i)) {
port = i;
break;
}
i++;
} }
LastAllocatedPort = port;
return port; auto first = m_ports_free.front();
m_ports_free.pop_front();
return first;
} }
uint32 ZSList::TriggerBootup(uint32 iZoneID, uint32 iInstanceID) { uint32 ZSList::TriggerBootup(uint32 iZoneID, uint32 iInstanceID) {

View File

@ -7,6 +7,7 @@
#include "../common/event/timer.h" #include "../common/event/timer.h"
#include <vector> #include <vector>
#include <memory> #include <memory>
#include <deque>
class WorldTCPConnection; class WorldTCPConnection;
class ServerPacket; class ServerPacket;
@ -22,6 +23,7 @@ public:
ZSList(); ZSList();
~ZSList(); ~ZSList();
void Init();
bool IsZoneLocked(uint16 iZoneID); bool IsZoneLocked(uint16 iZoneID);
bool SendPacket(ServerPacket *pack); bool SendPacket(ServerPacket *pack);
bool SendPacket(uint32 zoneid, ServerPacket *pack); bool SendPacket(uint32 zoneid, ServerPacket *pack);
@ -73,8 +75,7 @@ private:
uint32 NextID; uint32 NextID;
uint16 pLockedZones[MaxLockedZones]; uint16 pLockedZones[MaxLockedZones];
uint32 CurGroupID; uint32 CurGroupID;
uint16 LastAllocatedPort; std::deque<uint16> m_ports_free;
std::unique_ptr<EQ::Timer> m_tick; std::unique_ptr<EQ::Timer> m_tick;
std::unique_ptr<EQ::Timer> m_keepalive; std::unique_ptr<EQ::Timer> m_keepalive;

View File

@ -92,7 +92,6 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#include "../common/unix.h" #include "../common/unix.h"
#endif #endif
volatile bool RunLoops = true;
extern volatile bool is_zone_loaded; extern volatile bool is_zone_loaded;
EntityList entity_list; EntityList entity_list;
@ -577,19 +576,19 @@ int main(int argc, char** argv) {
return 0; return 0;
} }
void Shutdown()
{
Zone::Shutdown(true);
LogInfo("Shutting down...");
LogSys.CloseFileLogs();
EQ::EventLoop::Get().Shutdown();
}
void CatchSignal(int sig_num) { void CatchSignal(int sig_num) {
#ifdef _WINDOWS #ifdef _WINDOWS
LogInfo("Recieved signal: [{}]", sig_num); LogInfo("Recieved signal: [{}]", sig_num);
#endif #endif
RunLoops = false; Shutdown();
}
void Shutdown()
{
Zone::Shutdown(true);
RunLoops = false;
LogInfo("Shutting down...");
LogSys.CloseFileLogs();
} }
/* Update Window Title with relevant information */ /* Update Window Title with relevant information */

View File

@ -57,7 +57,7 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
extern EntityList entity_list; extern EntityList entity_list;
extern Zone* zone; extern Zone* zone;
extern volatile bool is_zone_loaded; extern volatile bool is_zone_loaded;
extern void CatchSignal(int); extern void Shutdown();
extern WorldServer worldserver; extern WorldServer worldserver;
extern PetitionList petition_list; extern PetitionList petition_list;
extern uint32 numclients; extern uint32 numclients;
@ -192,8 +192,15 @@ void WorldServer::HandleMessage(uint16 opcode, const EQ::Net::Packet &p)
if (pack->size != sizeof(ServerConnectInfo)) if (pack->size != sizeof(ServerConnectInfo))
break; break;
ServerConnectInfo* sci = (ServerConnectInfo*)pack->pBuffer; ServerConnectInfo* sci = (ServerConnectInfo*)pack->pBuffer;
LogInfo("World assigned Port: [{}] for this zone", sci->port);
ZoneConfig::SetZonePort(sci->port); if (sci->port == 0) {
LogCritical("World did not have a port to assign from this server, the port range was not large enough.");
Shutdown();
}
else {
LogInfo("World assigned Port: [{}] for this zone", sci->port);
ZoneConfig::SetZonePort(sci->port);
}
break; break;
} }
case ServerOP_ChannelMessage: { case ServerOP_ChannelMessage: {
@ -482,7 +489,7 @@ void WorldServer::HandleMessage(uint16 opcode, const EQ::Net::Packet &p)
} }
case ServerOP_ShutdownAll: { case ServerOP_ShutdownAll: {
entity_list.Save(); entity_list.Save();
CatchSignal(2); Shutdown();
break; break;
} }
case ServerOP_ZoneShutdown: { case ServerOP_ZoneShutdown: {