[Browser] Fix crashing of browser (I hope)

0.6.1
Koncord 8 years ago
parent 1f632d8bc9
commit d7cbfc98eb

@ -27,9 +27,11 @@ QueryHelper::QueryHelper(QAbstractItemModel *model)
void QueryHelper::refresh() void QueryHelper::refresh()
{ {
if (!queryThread->isRunning()) if (!queryThread->isRunning())
{
_model->removeRows(0, _model->rowCount()); _model->removeRows(0, _model->rowCount());
queryThread->start(); queryThread->start();
emit started(); emit started();
}
} }
void QueryHelper::terminate() void QueryHelper::terminate()

@ -43,21 +43,41 @@ QueryClient &QueryClient::Get()
map<SystemAddress, QueryData> QueryClient::Query() map<SystemAddress, QueryData> QueryClient::Query()
{ {
status = -1;
map<SystemAddress, QueryData> query; map<SystemAddress, QueryData> query;
if (Connect() == IS_NOT_CONNECTED)
return query;
BitStream bs; BitStream bs;
bs.Write((unsigned char) (ID_MASTER_QUERY)); bs.Write((unsigned char) (ID_MASTER_QUERY));
int code = peer->Send(&bs, HIGH_PRIORITY, RELIABLE_ORDERED, CHANNEL_MASTER, masterAddr, false); qDebug() << "Locking mutex in QueryClient::Query()";
mxServers.lock();
status = -1;
int attempts = 3;
do
{
if (Connect() == IS_NOT_CONNECTED)
{
qDebug() << "Unlocking mutex in QueryClient::Query()";
mxServers.unlock();
return query;
}
if (code == 0) int code = peer->Send(&bs, HIGH_PRIORITY, RELIABLE_ORDERED, CHANNEL_MASTER, masterAddr, false);
return query;
pmq->SetServers(&query); if (code == 0)
status = GetAnswer(); {
qDebug() << "Unlocking mutex in QueryClient::Query()";
mxServers.unlock();
return query;
}
pmq->SetServers(&query);
status = GetAnswer(ID_MASTER_QUERY);
RakSleep(100);
}
while(status != ID_MASTER_QUERY && attempts-- > 0);
if(status != ID_MASTER_QUERY)
qDebug() << "Getting query was failed";
qDebug() << "Unlocking mutex in QueryClient::Query()";
peer->CloseConnection(masterAddr, true);
mxServers.unlock();
qDebug() <<"Answer" << (status == ID_MASTER_QUERY ? "ok." : "wrong."); qDebug() <<"Answer" << (status == ID_MASTER_QUERY ? "ok." : "wrong.");
return query; return query;
@ -65,24 +85,40 @@ map<SystemAddress, QueryData> QueryClient::Query()
pair<SystemAddress, QueryData> QueryClient::Update(RakNet::SystemAddress addr) pair<SystemAddress, QueryData> QueryClient::Update(RakNet::SystemAddress addr)
{ {
qDebug() << "Locking mutex in QueryClient::Update(RakNet::SystemAddress addr)";
pair<SystemAddress, QueryData> server; pair<SystemAddress, QueryData> server;
if (Connect() == IS_NOT_CONNECTED)
{
status = -1;
return server;
}
BitStream bs; BitStream bs;
bs.Write((unsigned char) (ID_MASTER_UPDATE)); bs.Write((unsigned char) (ID_MASTER_UPDATE));
bs.Write(addr); bs.Write(addr);
peer->Send(&bs, HIGH_PRIORITY, RELIABLE_ORDERED, CHANNEL_MASTER, masterAddr, false);
mxServers.lock();
status = -1;
int attempts = 3;
pmu->SetServer(&server); pmu->SetServer(&server);
status = GetAnswer(); do
{
if (Connect() == IS_NOT_CONNECTED)
{
qDebug() << IS_NOT_CONNECTED;
qDebug() << "Unlocking mutex in QueryClient::Update(RakNet::SystemAddress addr)";
mxServers.unlock();
return server;
}
peer->Send(&bs, HIGH_PRIORITY, RELIABLE_ORDERED, CHANNEL_MASTER, masterAddr, false);
status = GetAnswer(ID_MASTER_UPDATE);
RakSleep(100);
}
while(status != ID_MASTER_UPDATE && attempts-- > 0);
if(status != ID_MASTER_UPDATE)
qDebug() << "Getting update was failed";
peer->CloseConnection(masterAddr, true);
qDebug() << "Unlocking mutex in QueryClient::Update(RakNet::SystemAddress addr)";
mxServers.unlock();
return server; return server;
} }
MASTER_PACKETS QueryClient::GetAnswer() MASTER_PACKETS QueryClient::GetAnswer(MASTER_PACKETS waitingPacket)
{ {
RakNet::Packet *packet; RakNet::Packet *packet;
bool update = true; bool update = true;
@ -105,20 +141,30 @@ MASTER_PACKETS QueryClient::GetAnswer()
update = false; update = false;
break; break;
case ID_MASTER_QUERY: case ID_MASTER_QUERY:
pmq->Read(); qDebug() << "ID_MASTER_QUERY";
if (waitingPacket == ID_MASTER_QUERY)
pmq->Read();
else
qDebug() << "Got wrong packet";
update = false; update = false;
id = pid; id = pid;
break; break;
case ID_MASTER_UPDATE: case ID_MASTER_UPDATE:
pmu->Read(); qDebug() << "ID_MASTER_UPDATE";
if (waitingPacket == ID_MASTER_UPDATE)
pmu->Read();
else
qDebug() << "Got wrong packet";
update = false; update = false;
id = pid; id = pid;
break; break;
case ID_MASTER_ANNOUNCE: case ID_MASTER_ANNOUNCE:
qDebug() << "ID_MASTER_ANNOUNCE";
update = false; update = false;
id = pid; id = pid;
break; break;
case ID_CONNECTION_REQUEST_ACCEPTED: case ID_CONNECTION_REQUEST_ACCEPTED:
qDebug() << "ID_CONNECTION_REQUEST_ACCEPTED";
break; break;
default: default:
break; break;
@ -148,8 +194,7 @@ ConnectionState QueryClient::Connect()
case IS_SILENTLY_DISCONNECTING: case IS_SILENTLY_DISCONNECTING:
case IS_DISCONNECTING: case IS_DISCONNECTING:
{ {
qDebug() << "Cannot connect to the master server "<< state; qDebug() << "Cannot connect to the master server. Code:"<< state;
//LOG_MESSAGE_SIMPLE(Log::LOG_WARN, "Cannot connect to master server: %d", masterAddr.ToString());
return IS_NOT_CONNECTED; return IS_NOT_CONNECTED;
} }
case IS_PENDING: case IS_PENDING:

@ -10,6 +10,7 @@
#include <components/openmw-mp/Master/PacketMasterQuery.hpp> #include <components/openmw-mp/Master/PacketMasterQuery.hpp>
#include <components/openmw-mp/Master/PacketMasterUpdate.hpp> #include <components/openmw-mp/Master/PacketMasterUpdate.hpp>
#include <apps/browser/ServerModel.hpp> #include <apps/browser/ServerModel.hpp>
#include <mutex>
class QueryClient class QueryClient
{ {
@ -26,7 +27,7 @@ public:
int Status(); int Status();
private: private:
RakNet::ConnectionState Connect(); RakNet::ConnectionState Connect();
MASTER_PACKETS GetAnswer(); MASTER_PACKETS GetAnswer(MASTER_PACKETS packet);
protected: protected:
QueryClient(); QueryClient();
~QueryClient(); ~QueryClient();
@ -37,6 +38,7 @@ private:
mwmp::PacketMasterQuery *pmq; mwmp::PacketMasterQuery *pmq;
mwmp::PacketMasterUpdate *pmu; mwmp::PacketMasterUpdate *pmu;
std::pair<RakNet::SystemAddress, ServerData> server; std::pair<RakNet::SystemAddress, ServerData> server;
std::mutex mxServers;
}; };

Loading…
Cancel
Save