forked from teamnwah/openmw-tes3coop
[Master] Notify server about announce state
Rename structure Server to QueryData
This commit is contained in:
parent
841e6a63f8
commit
9643eb6026
11 changed files with 66 additions and 49 deletions
|
@ -39,6 +39,17 @@ void MasterServer::Thread()
|
|||
unsigned char packetId = 0;
|
||||
|
||||
auto startTime = chrono::steady_clock::now();
|
||||
|
||||
BitStream send;
|
||||
PacketMasterQuery pmq(peer);
|
||||
pmq.SetSendStream(&send);
|
||||
|
||||
PacketMasterUpdate pmu(peer);
|
||||
pmu.SetSendStream(&send);
|
||||
|
||||
PacketMasterAnnounce pma(peer);
|
||||
pma.SetSendStream(&send);
|
||||
|
||||
while (run)
|
||||
{
|
||||
Packet *packet = peer->Receive();
|
||||
|
@ -76,10 +87,7 @@ void MasterServer::Thread()
|
|||
break;
|
||||
case ID_MASTER_QUERY:
|
||||
{
|
||||
BitStream send;
|
||||
PacketMasterQuery pmq(peer);
|
||||
pmq.SetSendStream(&send);
|
||||
pmq.SetServers(reinterpret_cast<map<SystemAddress, Server> *>(&servers));
|
||||
pmq.SetServers(reinterpret_cast<map<SystemAddress, QueryData> *>(&servers));
|
||||
pmq.Send(packet->systemAddress);
|
||||
|
||||
cout << "Sent info about all " << servers.size() << " servers to "
|
||||
|
@ -95,10 +103,7 @@ void MasterServer::Thread()
|
|||
ServerIter it = servers.find(addr);
|
||||
if (it != servers.end())
|
||||
{
|
||||
mwmp::PacketMasterUpdate pmu(peer);
|
||||
BitStream send;
|
||||
pmu.SetSendStream(&send);
|
||||
pair<SystemAddress, Server> pairPtr(it->first, static_cast<Server>(it->second));
|
||||
pair<SystemAddress, QueryData> pairPtr(it->first, static_cast<QueryData>(it->second));
|
||||
pmu.SetServer(&pairPtr);
|
||||
pmu.Send(packet->systemAddress);
|
||||
cout << "Sent info about " << addr.ToString() << " to " << packet->systemAddress.ToString()
|
||||
|
@ -111,39 +116,51 @@ void MasterServer::Thread()
|
|||
{
|
||||
ServerIter iter = servers.find(packet->systemAddress);
|
||||
|
||||
PacketMasterAnnounce pma(peer);
|
||||
pma.SetReadStream(&data);
|
||||
|
||||
SServer server;
|
||||
pma.SetServer(&server);
|
||||
pma.Read();
|
||||
|
||||
switch (pma.GetFunc())
|
||||
{
|
||||
case PacketMasterAnnounce::FUNCTION_DELETE:
|
||||
{
|
||||
if (iter != servers.end())
|
||||
servers.erase(iter);
|
||||
cout << "Deleted";
|
||||
break;
|
||||
}
|
||||
case PacketMasterAnnounce::FUNCTION_ANNOUNCE:
|
||||
{
|
||||
auto keepAliveFunc = [&]() {
|
||||
iter->second.lastUpdate = now;
|
||||
pma.SetFunc(PacketMasterAnnounce::FUNCTION_KEEP);
|
||||
pma.Send(packet->systemAddress);
|
||||
};
|
||||
|
||||
if (iter == servers.end())
|
||||
cout << "Added";
|
||||
else
|
||||
cout << "Updated";
|
||||
iter = servers.insert({packet->systemAddress, server}).first;
|
||||
break;
|
||||
if (iter != servers.end())
|
||||
{
|
||||
if (pma.GetFunc() == PacketMasterAnnounce::FUNCTION_DELETE)
|
||||
{
|
||||
servers.erase(iter);
|
||||
cout << "Deleted";
|
||||
pma.Send(packet->systemAddress);
|
||||
}
|
||||
default:
|
||||
else if (pma.GetFunc() == PacketMasterAnnounce::FUNCTION_ANNOUNCE)
|
||||
{
|
||||
cout << "Updated";
|
||||
iter = servers.insert({packet->systemAddress, server}).first;
|
||||
keepAliveFunc();
|
||||
}
|
||||
else
|
||||
{
|
||||
cout << "Keeping alive";
|
||||
keepAliveFunc();
|
||||
}
|
||||
}
|
||||
else if(pma.GetFunc() == PacketMasterAnnounce::FUNCTION_ANNOUNCE)
|
||||
{
|
||||
cout << "Added";
|
||||
iter = servers.insert({packet->systemAddress, server}).first;
|
||||
keepAliveFunc();
|
||||
}
|
||||
else
|
||||
{
|
||||
cout << "Unknown";
|
||||
pma.SetFunc(PacketMasterAnnounce::FUNCTION_DELETE);
|
||||
pma.Send(packet->systemAddress);
|
||||
}
|
||||
cout << " server " << packet->systemAddress.ToString() << endl;
|
||||
|
||||
if (pma.GetFunc() != PacketMasterAnnounce::FUNCTION_DELETE)
|
||||
iter->second.lastUpdate = now;
|
||||
peer->CloseConnection(packet->systemAddress, true);
|
||||
break;
|
||||
}
|
||||
|
|
|
@ -21,7 +21,7 @@ public:
|
|||
bool isRunning();
|
||||
void Wait();
|
||||
|
||||
struct SServer : Server
|
||||
struct SServer : QueryData
|
||||
{
|
||||
std::chrono::steady_clock::time_point lastUpdate;
|
||||
};
|
||||
|
|
|
@ -59,7 +59,7 @@ int main()
|
|||
else if (strcmp(message, "send") == 0)
|
||||
{
|
||||
puts("Sending data about server");
|
||||
Server server;
|
||||
QueryData server;
|
||||
server.SetName("Super Server");
|
||||
server.SetPlayers(0);
|
||||
server.SetMaxPlayers(0);
|
||||
|
@ -152,7 +152,7 @@ int main()
|
|||
break;
|
||||
case ID_MASTER_QUERY:
|
||||
{
|
||||
map<SystemAddress, Server> servers;
|
||||
map<SystemAddress, QueryData> servers;
|
||||
|
||||
pmq.SetReadStream(&data);
|
||||
pmq.SetServers(&servers);
|
||||
|
@ -167,7 +167,7 @@ int main()
|
|||
}
|
||||
case ID_MASTER_UPDATE:
|
||||
{
|
||||
pair<SystemAddress, Server> serverPair;
|
||||
pair<SystemAddress, QueryData> serverPair;
|
||||
PacketMasterUpdate pmu(peer);
|
||||
pmu.SetReadStream(&data);
|
||||
pmu.SetServer(&serverPair);
|
||||
|
|
|
@ -32,9 +32,9 @@ struct Plugin
|
|||
unsigned long hash;
|
||||
};
|
||||
|
||||
struct Server
|
||||
struct QueryData
|
||||
{
|
||||
Server()
|
||||
QueryData()
|
||||
{
|
||||
rules["name"].type = 's';
|
||||
rules["name"].str = "";
|
||||
|
|
|
@ -29,7 +29,7 @@ void PacketMasterAnnounce::Packet(BitStream *bs, bool send)
|
|||
ProxyMasterPacket::addServer(this, *server, send);
|
||||
}
|
||||
|
||||
void PacketMasterAnnounce::SetServer(Server *_server)
|
||||
void PacketMasterAnnounce::SetServer(QueryData *_server)
|
||||
{
|
||||
server = _server;
|
||||
}
|
||||
|
|
|
@ -19,7 +19,7 @@ namespace mwmp
|
|||
|
||||
virtual void Packet(RakNet::BitStream *bs, bool send);
|
||||
|
||||
void SetServer(Server *server);
|
||||
void SetServer(QueryData *server);
|
||||
void SetFunc(int keep);
|
||||
int GetFunc();
|
||||
|
||||
|
@ -30,7 +30,7 @@ namespace mwmp
|
|||
FUNCTION_KEEP
|
||||
};
|
||||
private:
|
||||
Server *server;
|
||||
QueryData *server;
|
||||
int func;
|
||||
};
|
||||
}
|
||||
|
|
|
@ -29,11 +29,11 @@ void PacketMasterQuery::Packet(RakNet::BitStream *bs, bool send)
|
|||
|
||||
RW(serversCount, send);
|
||||
|
||||
map<SystemAddress, Server>::iterator serverIt;
|
||||
map<SystemAddress, QueryData>::iterator serverIt;
|
||||
if(send)
|
||||
serverIt = servers->begin();
|
||||
|
||||
Server server;
|
||||
QueryData server;
|
||||
SystemAddress sa;
|
||||
while(serversCount--)
|
||||
{
|
||||
|
@ -49,12 +49,12 @@ void PacketMasterQuery::Packet(RakNet::BitStream *bs, bool send)
|
|||
if(send)
|
||||
serverIt++;
|
||||
else
|
||||
servers->insert(pair<SystemAddress, Server>(sa, server));
|
||||
servers->insert(pair<SystemAddress, QueryData>(sa, server));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void PacketMasterQuery::SetServers(map<SystemAddress, Server> *serverMap)
|
||||
void PacketMasterQuery::SetServers(map<SystemAddress, QueryData> *serverMap)
|
||||
{
|
||||
servers = serverMap;
|
||||
}
|
||||
|
|
|
@ -19,9 +19,9 @@ namespace mwmp
|
|||
|
||||
virtual void Packet(RakNet::BitStream *bs, bool send);
|
||||
|
||||
void SetServers(std::map<RakNet::SystemAddress, Server> *serverMap);
|
||||
void SetServers(std::map<RakNet::SystemAddress, QueryData> *serverMap);
|
||||
private:
|
||||
std::map<RakNet::SystemAddress, Server> *servers;
|
||||
std::map<RakNet::SystemAddress, QueryData> *servers;
|
||||
};
|
||||
}
|
||||
|
||||
|
|
|
@ -28,7 +28,7 @@ void PacketMasterUpdate::Packet(RakNet::BitStream *bs, bool send)
|
|||
|
||||
}
|
||||
|
||||
void PacketMasterUpdate::SetServer(std::pair<RakNet::SystemAddress, Server> *serverPair)
|
||||
void PacketMasterUpdate::SetServer(std::pair<RakNet::SystemAddress, QueryData> *serverPair)
|
||||
{
|
||||
server = serverPair;
|
||||
}
|
||||
|
|
|
@ -19,9 +19,9 @@ namespace mwmp
|
|||
|
||||
virtual void Packet(RakNet::BitStream *bs, bool send);
|
||||
|
||||
void SetServer(std::pair<RakNet::SystemAddress, Server> *serverPair);
|
||||
void SetServer(std::pair<RakNet::SystemAddress, QueryData> *serverPair);
|
||||
private:
|
||||
std::pair<RakNet::SystemAddress, Server> *server;
|
||||
std::pair<RakNet::SystemAddress, QueryData> *server;
|
||||
};
|
||||
}
|
||||
|
||||
|
|
|
@ -21,7 +21,7 @@ namespace mwmp
|
|||
|
||||
public:
|
||||
template<class Packet>
|
||||
static void addServer(Packet *packet, Server &server, bool send)
|
||||
static void addServer(Packet *packet, QueryData &server, bool send)
|
||||
{
|
||||
using namespace std;
|
||||
|
||||
|
|
Loading…
Reference in a new issue