forked from teamnwah/openmw-tes3coop
Compare commits
1 Commits
0.7.0
...
coverity_s
Author | SHA1 | Date |
---|---|---|
Stanislav Zhukov | 1d84cbe963 | 8 years ago |
@ -1,16 +0,0 @@
|
||||
root = true
|
||||
|
||||
[*.cpp]
|
||||
indent_style = space
|
||||
indent_size = 4
|
||||
insert_final_newline = true
|
||||
|
||||
[*.hpp]
|
||||
indent_style = space
|
||||
indent_size = 4
|
||||
insert_final_newline = true
|
||||
|
||||
[*.glsl]
|
||||
indent_style = space
|
||||
indent_size = 4
|
||||
insert_final_newline = false
|
@ -1,69 +0,0 @@
|
||||
stages:
|
||||
- build
|
||||
|
||||
Debian:
|
||||
tags:
|
||||
- docker
|
||||
- linux
|
||||
image: gcc
|
||||
cache:
|
||||
key: apt-cache
|
||||
paths:
|
||||
- apt-cache/
|
||||
before_script:
|
||||
- export APT_CACHE_DIR=`pwd`/apt-cache && mkdir -pv $APT_CACHE_DIR
|
||||
- apt-get update -yq
|
||||
- apt-get -o dir::cache::archives="$APT_CACHE_DIR" install -y cmake libboost-filesystem-dev libboost-program-options-dev libboost-system-dev libavcodec-dev libavformat-dev libavutil-dev libswscale-dev libswresample-dev libsdl2-dev libqt4-dev libopenal-dev libopenscenegraph-3.4-dev libunshield-dev libtinyxml-dev
|
||||
# - apt-get install -y libmygui-dev libbullet-dev # to be updated to latest below because stretch is too old
|
||||
- curl http://ftp.us.debian.org/debian/pool/main/b/bullet/libbullet-dev_2.87+dfsg-2_amd64.deb -o libbullet-dev_2.87+dfsg-2_amd64.deb
|
||||
- curl http://ftp.us.debian.org/debian/pool/main/b/bullet/libbullet2.87_2.87+dfsg-2_amd64.deb -o libbullet2.87_2.87+dfsg-2_amd64.deb
|
||||
- curl http://ftp.us.debian.org/debian/pool/main/m/mygui/libmygui.openglplatform0debian1v5_3.2.2+dfsg-1_amd64.deb -o libmygui.openglplatform0debian1v5_3.2.2+dfsg-1_amd64.deb
|
||||
- curl http://ftp.us.debian.org/debian/pool/main/m/mygui/libmyguiengine3debian1v5_3.2.2+dfsg-1_amd64.deb -o libmyguiengine3debian1v5_3.2.2+dfsg-1_amd64.deb
|
||||
- curl http://ftp.us.debian.org/debian/pool/main/m/mygui/libmygui-dev_3.2.2+dfsg-1_amd64.deb -o libmygui-dev_3.2.2+dfsg-1_amd64.deb
|
||||
- dpkg --ignore-depends=libmygui.ogreplatform0debian1v5 -i *.deb
|
||||
stage: build
|
||||
script:
|
||||
- cores_to_use=$((`nproc`-2)); if (( $cores_to_use < 1 )); then cores_to_use=1; fi
|
||||
- mkdir build; cd build; cmake -DCMAKE_BUILD_TYPE=MinSizeRel ../
|
||||
- make -j$cores_to_use
|
||||
- DESTDIR=artifacts make install
|
||||
artifacts:
|
||||
paths:
|
||||
- build/artifacts/
|
||||
MacOS:
|
||||
tags:
|
||||
- macos
|
||||
- xcode
|
||||
except:
|
||||
- branches # because our CI VMs are not public, MRs can't use them and timeout
|
||||
stage: build
|
||||
allow_failure: true
|
||||
script:
|
||||
- rm -fr build/* # remove anything in the build directory
|
||||
- CI/before_install.osx.sh
|
||||
- CI/before_script.osx.sh
|
||||
- cd build; make -j2 package
|
||||
artifacts:
|
||||
paths:
|
||||
- build/OpenMW-*.dmg
|
||||
|
||||
Windows:
|
||||
tags:
|
||||
- win10
|
||||
- msvc2017
|
||||
except:
|
||||
- branches # because our CI VMs are not public, MRs can't use them and timeout
|
||||
stage: build
|
||||
allow_failure: true
|
||||
script:
|
||||
# - env # turn on for debugging
|
||||
- sh %CI_PROJECT_DIR%/CI/before_script.msvc.sh -c Release -p x64 -v 2017 -V
|
||||
- SET msBuildLocation="C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\MSBuild\15.0\Bin\msbuild.exe"
|
||||
- call %msBuildLocation% MSVC2017_64\OpenMW.sln /t:Build /p:Configuration=Release /m:%NUMBER_OF_PROCESSORS%
|
||||
- 7z a OpenMW_MSVC2017_64_%CI_BUILD_REF_NAME%_%CI_BUILD_ID%.zip %CI_PROJECT_DIR%\MSVC2017_64\Release\
|
||||
cache:
|
||||
paths:
|
||||
- deps
|
||||
artifacts:
|
||||
paths:
|
||||
- "*.zip"
|
@ -1,11 +0,0 @@
|
||||
#!/bin/sh
|
||||
|
||||
cd build
|
||||
|
||||
DATE=`date +'%d%m%Y'`
|
||||
SHORT_COMMIT=`git rev-parse --short ${TRAVIS_COMMIT}`
|
||||
TARGET_FILENAME="OpenMW-${DATE}-${SHORT_COMMIT}.dmg"
|
||||
|
||||
if ! curl --ssl -u $OSX_FTP_USER:$OSX_FTP_PASSWORD "${OSX_FTP_URL}" --silent | grep $SHORT_COMMIT > /dev/null; then
|
||||
curl --ssl --ftp-create-dirs -T *.dmg -u $OSX_FTP_USER:$OSX_FTP_PASSWORD "${OSX_FTP_URL}${TARGET_FILENAME}"
|
||||
fi
|
@ -0,0 +1,266 @@
|
||||
//
|
||||
// Created by koncord on 07.01.17.
|
||||
//
|
||||
|
||||
#include <cassert>
|
||||
#include <QtCore/QTime>
|
||||
#include "NetController.hpp"
|
||||
#include "qdebug.h"
|
||||
|
||||
#include <RakPeer.h>
|
||||
#include <RakSleep.h>
|
||||
|
||||
#include <sstream>
|
||||
|
||||
#include <QJsonDocument>
|
||||
#include <QJsonArray>
|
||||
#include <QJsonObject>
|
||||
#include <memory>
|
||||
#include <QtWidgets/QMessageBox>
|
||||
|
||||
using namespace std;
|
||||
|
||||
NetController *NetController::mThis = nullptr;
|
||||
|
||||
NetController *NetController::get()
|
||||
{
|
||||
assert(mThis);
|
||||
return mThis;
|
||||
}
|
||||
|
||||
void NetController::Create(std::string addr, unsigned short port)
|
||||
{
|
||||
assert(!mThis);
|
||||
mThis = new NetController(addr, port);
|
||||
}
|
||||
|
||||
void NetController::Destroy()
|
||||
{
|
||||
assert(mThis);
|
||||
delete mThis;
|
||||
mThis = nullptr;
|
||||
}
|
||||
|
||||
NetController::NetController(std::string addr, unsigned short port) : httpNetwork(addr, port)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
NetController::~NetController()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
struct pattern
|
||||
{
|
||||
pattern(QString value): value(value) {}
|
||||
bool operator()(const ServerData &data)
|
||||
{
|
||||
return value == data.addr;
|
||||
}
|
||||
QString value;
|
||||
};
|
||||
|
||||
void NetController::setData(QString address, QJsonObject server, ServerModel *model)
|
||||
{
|
||||
QModelIndex mi = model->index(0, ServerData::ADDR);
|
||||
model->setData(mi, address);
|
||||
|
||||
mi = model->index(0, ServerData::PLAYERS);
|
||||
model->setData(mi, server["players"].toInt());
|
||||
|
||||
mi = model->index(0, ServerData::MAX_PLAYERS);
|
||||
model->setData(mi, server["max_players"].toInt());
|
||||
|
||||
mi = model->index(0, ServerData::HOSTNAME);
|
||||
model->setData(mi, server["hostname"].toString());
|
||||
|
||||
mi = model->index(0, ServerData::MODNAME);
|
||||
model->setData(mi, server["modname"].toString());
|
||||
|
||||
mi = model->index(0, ServerData::VERSION);
|
||||
model->setData(mi, server["version"].toString());
|
||||
|
||||
mi = model->index(0, ServerData::PASSW);
|
||||
model->setData(mi, server["passw"].toBool());
|
||||
|
||||
mi = model->index(0, ServerData::PING);
|
||||
|
||||
// This *should* fix a crash when a port isn't returned by data.
|
||||
if(!address.contains(":"))
|
||||
address.append(":25565");
|
||||
QStringList addr = address.split(":");
|
||||
model->setData(mi, PingRakNetServer(addr[0].toLatin1().data(), addr[1].toUShort()));
|
||||
}
|
||||
|
||||
bool NetController::downloadInfo(QAbstractItemModel *pModel, QModelIndex index)
|
||||
{
|
||||
ServerModel *model = ((ServerModel *) pModel);
|
||||
|
||||
/*
|
||||
* download stuff
|
||||
*/
|
||||
|
||||
QString data;
|
||||
QJsonParseError err;
|
||||
|
||||
if(index.isValid() && index.row() >= 0)
|
||||
{
|
||||
const ServerData &sd = model->myData[index.row()];
|
||||
while(true)
|
||||
{
|
||||
data = QString::fromStdString(httpNetwork.getData((QString("/api/servers/") + sd.addr).toLatin1()));
|
||||
if (!data.isEmpty() && data != "NO_CONTENT" && data != "LOST_CONNECTION")
|
||||
break;
|
||||
RakSleep(30);
|
||||
}
|
||||
qDebug() << "Content for \"" << sd.addr << "\": " << data;
|
||||
|
||||
if(data == "bad request" || data == "not found") // TODO: if server is not registered we should download info directly from the server
|
||||
{
|
||||
qDebug() << "Server is not registered";
|
||||
return false;
|
||||
}
|
||||
|
||||
QJsonDocument jsonDocument = QJsonDocument::fromJson(data.toLatin1(), &err);
|
||||
QJsonObject server = jsonDocument.object()["server"].toObject();
|
||||
|
||||
setData(sd.addr, server, model);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
while (true)
|
||||
{
|
||||
data = QString::fromStdString(httpNetwork.getData("/api/servers"));
|
||||
if (!data.isEmpty() && data != "NO_CONTENT" && data != "LOST_CONNECTION")
|
||||
break;
|
||||
RakSleep(30);
|
||||
}
|
||||
|
||||
if(data == "UNKNOWN_ADDRESS")
|
||||
{
|
||||
QMessageBox::critical(0, "Error", "Cannot connect to the master server!");
|
||||
return false;
|
||||
}
|
||||
|
||||
qDebug() << "Content: " << data;
|
||||
|
||||
QJsonDocument jsonDocument = QJsonDocument::fromJson(data.toLatin1(), &err);
|
||||
|
||||
QJsonObject listServers = jsonDocument.object()["list servers"].toObject();
|
||||
|
||||
for(auto iter = listServers.begin(); iter != listServers.end(); iter++)
|
||||
{
|
||||
QJsonObject server = iter->toObject();
|
||||
qDebug() << iter.key();
|
||||
qDebug() << server["hostname"].toString();
|
||||
qDebug() << server["modname"].toString();
|
||||
qDebug() << server["players"].toInt();
|
||||
qDebug() << server["max_players"].toInt();
|
||||
qDebug() << server["version"].toString();
|
||||
qDebug() << server["passw"].toBool();
|
||||
|
||||
QVector<ServerData>::Iterator value = std::find_if(model->myData.begin(), model->myData.end(), pattern(iter.key()));
|
||||
if(value == model->myData.end())
|
||||
model->insertRow(0);
|
||||
|
||||
setData(iter.key(), server, model);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool NetController::updateInfo(QAbstractItemModel *pModel, QModelIndex index)
|
||||
{
|
||||
ServerModel *model = ((ServerModel*)pModel);
|
||||
|
||||
bool result;
|
||||
if (index.isValid() && index.row() >= 0)
|
||||
result = downloadInfo(pModel, index);
|
||||
else
|
||||
{
|
||||
for (auto iter = model->myData.begin(); iter != model->myData.end(); iter++)
|
||||
{
|
||||
qDebug() << iter->addr;
|
||||
}
|
||||
model->removeRows(0, model->rowCount(index));
|
||||
result = downloadInfo(pModel, index);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
void NetController::updateInfo()
|
||||
{
|
||||
QString data;
|
||||
QString uri = "/api/servers/" + sd->addr;
|
||||
while (true)
|
||||
{
|
||||
data = QString::fromStdString(httpNetwork.getData(uri.toLatin1()));
|
||||
if (!data.isEmpty() && data != "NO_CONTENT" && data != "LOST_CONNECTION")
|
||||
break;
|
||||
RakSleep(30);
|
||||
}
|
||||
|
||||
if(data == "UNKNOWN_ADDRESS")
|
||||
{
|
||||
QMessageBox::critical(0, "Error", "Cannot connect to the master server!");
|
||||
return;
|
||||
}
|
||||
|
||||
qDebug() << "Content: " << data;
|
||||
|
||||
QJsonParseError err;
|
||||
QJsonDocument jsonDocument = QJsonDocument::fromJson(data.toLatin1(), &err);
|
||||
|
||||
QMap<QString, QVariant> map = jsonDocument.toVariant().toMap()["server"].toMap();
|
||||
|
||||
qDebug() << sd->addr;
|
||||
qDebug() << map["hostname"].toString();
|
||||
qDebug() << map["modname"].toString();
|
||||
qDebug() << map["players"].toInt();
|
||||
qDebug() << map["max_players"].toInt();
|
||||
qDebug() << map["version"].toString();
|
||||
qDebug() << map["passw"].toBool();
|
||||
|
||||
sd->hostName = map["hostname"].toString();
|
||||
sd->modName = map["modname"].toString();
|
||||
sd->players = map["players"].toInt();
|
||||
sd->maxPlayers = map["max_players"].toInt();
|
||||
|
||||
if(!sd->addr.contains(":"))
|
||||
sd->addr.append(":25565");
|
||||
QStringList addr = sd->addr.split(":");
|
||||
sd->ping = PingRakNetServer(addr[0].toLatin1(), addr[1].toUShort());
|
||||
if(sd->ping != PING_UNREACHABLE)
|
||||
sed = getExtendedData(addr[0].toLatin1(), addr[1].toUShort());
|
||||
else
|
||||
qDebug() << "Server is unreachable";
|
||||
}
|
||||
|
||||
QStringList NetController::players()
|
||||
{
|
||||
QStringList listPlayers;
|
||||
for(auto player = sed.players.begin(); player != sed.players.end(); player++)
|
||||
listPlayers.push_back(player->c_str());
|
||||
return listPlayers;
|
||||
}
|
||||
|
||||
QStringList NetController::plugins()
|
||||
{
|
||||
QStringList listPlugins;
|
||||
for(auto plugin = sed.plugins.begin(); plugin != sed.plugins.end(); plugin++)
|
||||
listPlugins.push_back(plugin->c_str());
|
||||
return listPlugins;
|
||||
}
|
||||
|
||||
void NetController::selectServer(ServerData *pServerData)
|
||||
{
|
||||
sd = pServerData;
|
||||
}
|
||||
|
||||
ServerData *NetController::selectedServer()
|
||||
{
|
||||
return sd;
|
||||
}
|
||||
|
@ -0,0 +1,42 @@
|
||||
//
|
||||
// Created by koncord on 07.01.17.
|
||||
//
|
||||
|
||||
#ifndef NEWLAUNCHER_NETCONTROLLER_HPP
|
||||
#define NEWLAUNCHER_NETCONTROLLER_HPP
|
||||
|
||||
|
||||
#include "ServerModel.hpp"
|
||||
#include "netutils/HTTPNetwork.hpp"
|
||||
#include "netutils/Utils.hpp"
|
||||
|
||||
struct ServerModel;
|
||||
|
||||
class NetController
|
||||
{
|
||||
public:
|
||||
static NetController *get();
|
||||
static void Create(std::string addr, unsigned short port);
|
||||
static void Destroy();
|
||||
bool updateInfo(QAbstractItemModel *pModel, QModelIndex index= QModelIndex());
|
||||
void updateInfo();
|
||||
QStringList players();
|
||||
QStringList plugins();
|
||||
void selectServer(ServerData *pServerData);
|
||||
ServerData *selectedServer();
|
||||
protected:
|
||||
NetController(std::string addr, unsigned short port);
|
||||
~NetController();
|
||||
private:
|
||||
NetController(const NetController &controller);
|
||||
bool downloadInfo(QAbstractItemModel *pModel, QModelIndex index);
|
||||
void setData(QString addr, QJsonObject server, ServerModel *model);
|
||||
|
||||
static NetController *mThis;
|
||||
ServerData *sd;
|
||||
HTTPNetwork httpNetwork;
|
||||
ServerExtendedData sed;
|
||||
};
|
||||
|
||||
|
||||
#endif //NEWLAUNCHER_NETCONTROLLER_HPP
|
@ -1,56 +0,0 @@
|
||||
//
|
||||
// Created by koncord on 03.05.17.
|
||||
//
|
||||
|
||||
#include "PingHelper.hpp"
|
||||
#include "ServerModel.hpp"
|
||||
#include <QDebug>
|
||||
#include "PingUpdater.hpp"
|
||||
|
||||
void PingHelper::Add(int row, const AddrPair &addrPair)
|
||||
{
|
||||
pingUpdater->addServer(row, addrPair);
|
||||
if (!pingThread->isRunning())
|
||||
pingThread->start();
|
||||
}
|
||||
|
||||
void PingHelper::Reset()
|
||||
{
|
||||
//if (pingThread->isRunning())
|
||||
Stop();
|
||||
}
|
||||
|
||||
void PingHelper::Stop()
|
||||
{
|
||||
emit pingUpdater->stop();
|
||||
}
|
||||
|
||||
void PingHelper::SetModel(QAbstractTableModel *model)
|
||||
{
|
||||
this->model = model;
|
||||
}
|
||||
|
||||
void PingHelper::update(int row, unsigned ping)
|
||||
{
|
||||
model->setData(model->index(row, ServerData::PING), ping);
|
||||
}
|
||||
|
||||
PingHelper &PingHelper::Get()
|
||||
{
|
||||
static PingHelper helper;
|
||||
return helper;
|
||||
}
|
||||
|
||||
PingHelper::PingHelper() : QObject()
|
||||
{
|
||||
pingThread = new QThread;
|
||||
pingUpdater = new PingUpdater;
|
||||
pingUpdater->moveToThread(pingThread);
|
||||
|
||||
connect(pingThread, SIGNAL(started()), pingUpdater, SLOT(process()));
|
||||
connect(pingUpdater, SIGNAL(start()), pingThread, SLOT(start()));
|
||||
connect(pingUpdater, SIGNAL(finished()), pingThread, SLOT(quit()));
|
||||
connect(this, SIGNAL(stop()), pingUpdater, SLOT(stop()));
|
||||
//connect(pingUpdater, SIGNAL(finished()), pingUpdater, SLOT(deleteLater()));
|
||||
connect(pingUpdater, SIGNAL(updateModel(int, unsigned)), this, SLOT(update(int, unsigned)));
|
||||
}
|
@ -1,42 +0,0 @@
|
||||
//
|
||||
// Created by koncord on 03.05.17.
|
||||
//
|
||||
|
||||
#ifndef OPENMW_PINGHELPER_HPP
|
||||
#define OPENMW_PINGHELPER_HPP
|
||||
|
||||
#include <QObject>
|
||||
#include <QAbstractTableModel>
|
||||
#include <QThread>
|
||||
#include "Types.hpp"
|
||||
|
||||
class PingUpdater;
|
||||
|
||||
class PingHelper : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
|
||||
void Reset();
|
||||
void Add(int row, const AddrPair &addrPair);
|
||||
void Stop();
|
||||
void SetModel(QAbstractTableModel *model);
|
||||
//void UpdateImmedialy(PingUpdater::AddrPair addrPair);
|
||||
static PingHelper &Get();
|
||||
|
||||
PingHelper(const PingHelper&) = delete;
|
||||
PingHelper& operator=(const PingHelper&) = delete;
|
||||
private:
|
||||
PingHelper();
|
||||
signals:
|
||||
void stop();
|
||||
public slots:
|
||||
void update(int row, unsigned ping);
|
||||
private:
|
||||
QThread *pingThread;
|
||||
PingUpdater *pingUpdater;
|
||||
QAbstractTableModel *model;
|
||||
};
|
||||
|
||||
|
||||
#endif //OPENMW_PINGHELPER_HPP
|
@ -1,50 +0,0 @@
|
||||
//
|
||||
// Created by koncord on 02.05.17.
|
||||
//
|
||||
|
||||
#include "PingUpdater.hpp"
|
||||
#include "netutils/Utils.hpp"
|
||||
#include <QDebug>
|
||||
#include <QModelIndex>
|
||||
#include <QThread>
|
||||
|
||||
void PingUpdater::stop()
|
||||
{
|
||||
servers.clear();
|
||||
run = false;
|
||||
}
|
||||
|
||||
void PingUpdater::addServer(int row, const AddrPair &addr)
|
||||
{
|
||||
servers.push_back({row, addr});
|
||||
run = true;
|
||||
emit start();
|
||||
}
|
||||
|
||||
void PingUpdater::process()
|
||||
{
|
||||
while (run)
|
||||
{
|
||||
if (servers.count() == 0)
|
||||
{
|
||||
QThread::msleep(1000);
|
||||
if (servers.count() == 0)
|
||||
{
|
||||
qDebug() << "PingUpdater stopped due to inactivity";
|
||||
run = false;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
ServerRow server = servers.back();
|
||||
servers.pop_back();
|
||||
|
||||
unsigned ping = PingRakNetServer(server.second.first.toLatin1(), server.second.second);
|
||||
|
||||
qDebug() << "Pong from" << server.second.first + "|" + QString::number(server.second.second)
|
||||
<< ":" << ping << "ms" << "Sizeof servers: " << servers.size();
|
||||
|
||||
emit updateModel(server.first, ping);
|
||||
}
|
||||
emit finished();
|
||||
}
|
@ -1,31 +0,0 @@
|
||||
//
|
||||
// Created by koncord on 02.05.17.
|
||||
//
|
||||
|
||||
#ifndef OPENMW_PINGUPDATER_HPP
|
||||
#define OPENMW_PINGUPDATER_HPP
|
||||
|
||||
#include <QObject>
|
||||
#include <QVector>
|
||||
|
||||
#include "Types.hpp"
|
||||
|
||||
class PingUpdater : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
void addServer(int row, const AddrPair &addrPair);
|
||||
public slots:
|
||||
void stop();
|
||||
void process();
|
||||
signals:
|
||||
void start();
|
||||
void updateModel(int row, unsigned ping);
|
||||
void finished();
|
||||
private:
|
||||
QVector<ServerRow> servers;
|
||||
bool run;
|
||||
};
|
||||
|
||||
|
||||
#endif //OPENMW_PINGUPDATER_HPP
|
@ -1,87 +0,0 @@
|
||||
//
|
||||
// Created by koncord on 27.05.17.
|
||||
//
|
||||
|
||||
#include "netutils/QueryClient.hpp"
|
||||
#include "netutils/Utils.hpp"
|
||||
#include "QueryHelper.hpp"
|
||||
#include "PingHelper.hpp"
|
||||
|
||||
QueryUpdate *queryUpdate;
|
||||
|
||||
|
||||
QueryHelper::QueryHelper(QAbstractItemModel *model)
|
||||
{
|
||||
qRegisterMetaType<QueryData>("QueryData");
|
||||
queryThread = new QThread;
|
||||
queryUpdate = new QueryUpdate;
|
||||
_model = model;
|
||||
connect(queryThread, SIGNAL(started()), queryUpdate, SLOT(process()));
|
||||
connect(queryUpdate, SIGNAL(finished()), queryThread, SLOT(quit()));
|
||||
connect(queryUpdate, &QueryUpdate::finished, [this](){emit finished();});
|
||||
connect(queryUpdate, SIGNAL(updateModel(const QString&, unsigned short, const QueryData&)),
|
||||
this, SLOT(update(const QString&, unsigned short, const QueryData&)));
|
||||
queryUpdate->moveToThread(queryThread);
|
||||
}
|
||||
|
||||
void QueryHelper::refresh()
|
||||
{
|
||||
if (!queryThread->isRunning())
|
||||
{
|
||||
_model->removeRows(0, _model->rowCount());
|
||||
PingHelper::Get().Stop();
|
||||
queryThread->start();
|
||||
emit started();
|
||||
}
|
||||
}
|
||||
|
||||
void QueryHelper::terminate()
|
||||
{
|
||||
queryThread->terminate();
|
||||
}
|
||||
|
||||
void QueryHelper::update(const QString &addr, unsigned short port, const QueryData& data)
|
||||
{
|
||||
ServerModel *model = ((ServerModel*)_model);
|
||||
model->insertRow(model->rowCount());
|
||||
int row = model->rowCount() - 1;
|
||||
|
||||
QModelIndex mi = model->index(row, ServerData::ADDR);
|
||||
model->setData(mi, addr + ":" + QString::number(port));
|
||||
|
||||
mi = model->index(row, ServerData::PLAYERS);
|
||||
model->setData(mi, (int)data.players.size());
|
||||
|
||||
mi = model->index(row, ServerData::MAX_PLAYERS);
|
||||
model->setData(mi, data.GetMaxPlayers());
|
||||
|
||||
mi = model->index(row, ServerData::HOSTNAME);
|
||||
model->setData(mi, data.GetName());
|
||||
|
||||
mi = model->index(row, ServerData::MODNAME);
|
||||
model->setData(mi, data.GetGameMode());
|
||||
|
||||
mi = model->index(row, ServerData::VERSION);
|
||||
model->setData(mi, data.GetVersion());
|
||||
|
||||
mi = model->index(row, ServerData::PASSW);
|
||||
model->setData(mi, data.GetPassword() == 1);
|
||||
|
||||
mi = model->index(row, ServerData::PING);
|
||||
model->setData(mi, PING_UNREACHABLE);
|
||||
PingHelper::Get().Add(row, {addr, port});
|
||||
}
|
||||
|
||||
void QueryUpdate::process()
|
||||
{
|
||||
auto data = QueryClient::Get().Query();
|
||||
if (QueryClient::Get().Status() != ID_MASTER_QUERY)
|
||||
{
|
||||
emit finished();
|
||||
return;
|
||||
}
|
||||
|
||||
for (const auto &server : data)
|
||||
emit updateModel(server.first.ToString(false), server.first.GetPort(), server.second);
|
||||
emit finished();
|
||||
}
|
@ -1,46 +0,0 @@
|
||||
//
|
||||
// Created by koncord on 27.05.17.
|
||||
//
|
||||
|
||||
|
||||
#ifndef OPENMW_QUERYHELPER_HPP
|
||||
#define OPENMW_QUERYHELPER_HPP
|
||||
|
||||
|
||||
#include <QObject>
|
||||
#include <vector>
|
||||
#include <QAbstractItemModel>
|
||||
#include <components/openmw-mp/Master/MasterData.hpp>
|
||||
|
||||
Q_DECLARE_METATYPE(QueryData)
|
||||
|
||||
class QueryHelper : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
explicit QueryHelper(QAbstractItemModel *model);
|
||||
public slots:
|
||||
void refresh();
|
||||
void terminate();
|
||||
private slots:
|
||||
void update(const QString &addr, unsigned short port, const QueryData& data);
|
||||
signals:
|
||||
void finished();
|
||||
void started();
|
||||
private:
|
||||
QThread *queryThread;
|
||||
QAbstractItemModel *_model;
|
||||
};
|
||||
|
||||
class QueryUpdate : public QObject
|
||||
{
|
||||
friend class QueryHelper;
|
||||
Q_OBJECT
|
||||
signals:
|
||||
void finished();
|
||||
void updateModel(const QString &addr, unsigned short port, const QueryData& data);
|
||||
public slots:
|
||||
void process();
|
||||
};
|
||||
|
||||
#endif //OPENMW_QUERYHELPER_HPP
|
@ -1,15 +0,0 @@
|
||||
//
|
||||
// Created by koncord on 07.05.17.
|
||||
//
|
||||
|
||||
#ifndef OPENMW_TYPES_HPP
|
||||
#define OPENMW_TYPES_HPP
|
||||
|
||||
#include <QPair>
|
||||
#include <QString>
|
||||
|
||||
typedef QPair <QString, unsigned short> AddrPair;
|
||||
typedef QPair <int, AddrPair> ServerRow;
|
||||
|
||||
|
||||
#endif //OPENMW_TYPES_HPP
|
@ -1,212 +0,0 @@
|
||||
//
|
||||
// Created by koncord on 24.04.17.
|
||||
//
|
||||
|
||||
#include "QueryClient.hpp"
|
||||
#include <RakSleep.h>
|
||||
#include <components/openmw-mp/NetworkMessages.hpp>
|
||||
#include <iostream>
|
||||
#include <components/openmw-mp/Version.hpp>
|
||||
#include <qdebug.h>
|
||||
|
||||
using namespace RakNet;
|
||||
using namespace std;
|
||||
using namespace mwmp;
|
||||
|
||||
QueryClient::QueryClient()
|
||||
{
|
||||
peer = RakPeerInterface::GetInstance();
|
||||
pmq = new PacketMasterQuery(peer);
|
||||
pmu = new PacketMasterUpdate(peer);
|
||||
RakNet::SocketDescriptor sd;
|
||||
peer->Startup(8, &sd, 1);
|
||||
status = -1;
|
||||
}
|
||||
|
||||
QueryClient::~QueryClient()
|
||||
{
|
||||
delete pmq;
|
||||
delete pmu;
|
||||
RakPeerInterface::DestroyInstance(peer);
|
||||
}
|
||||
|
||||
void QueryClient::SetServer(const string &addr, unsigned short port)
|
||||
{
|
||||
masterAddr = SystemAddress(addr.c_str(), port);
|
||||
}
|
||||
|
||||
QueryClient &QueryClient::Get()
|
||||
{
|
||||
static QueryClient myInstance;
|
||||
return myInstance;
|
||||
}
|
||||
|
||||
map<SystemAddress, QueryData> QueryClient::Query()
|
||||
{
|
||||
map<SystemAddress, QueryData> query;
|
||||
BitStream bs;
|
||||
bs.Write((unsigned char) (ID_MASTER_QUERY));
|
||||
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;
|
||||
}
|
||||
|
||||
int code = peer->Send(&bs, HIGH_PRIORITY, RELIABLE_ORDERED, CHANNEL_MASTER, masterAddr, false);
|
||||
|
||||
if (code == 0)
|
||||
{
|
||||
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.");
|
||||
|
||||
return query;
|
||||
}
|
||||
|
||||
pair<SystemAddress, QueryData> QueryClient::Update(const RakNet::SystemAddress &addr)
|
||||
{
|
||||
qDebug() << "Locking mutex in QueryClient::Update(RakNet::SystemAddress addr)";
|
||||
pair<SystemAddress, QueryData> server;
|
||||
BitStream bs;
|
||||
bs.Write((unsigned char) (ID_MASTER_UPDATE));
|
||||
bs.Write(addr);
|
||||
|
||||
mxServers.lock();
|
||||
status = -1;
|
||||
int attempts = 3;
|
||||
pmu->SetServer(&server);
|
||||
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;
|
||||
}
|
||||
|
||||
MASTER_PACKETS QueryClient::GetAnswer(MASTER_PACKETS waitingPacket)
|
||||
{
|
||||
RakNet::Packet *packet;
|
||||
bool update = true;
|
||||
unsigned char pid = 0;
|
||||
int id = -1;
|
||||
while (update)
|
||||
{
|
||||
for (packet = peer->Receive(); packet; peer->DeallocatePacket(packet), packet = peer->Receive())
|
||||
{
|
||||
BitStream data(packet->data, packet->length, false);
|
||||
pmq->SetReadStream(&data);
|
||||
pmu->SetReadStream(&data);
|
||||
data.Read(pid);
|
||||
switch(pid)
|
||||
{
|
||||
case ID_CONNECTION_LOST:
|
||||
qDebug() << "ID_CONNECTION_LOST";
|
||||
case ID_DISCONNECTION_NOTIFICATION:
|
||||
qDebug() << "Disconnected";
|
||||
update = false;
|
||||
break;
|
||||
case ID_MASTER_QUERY:
|
||||
qDebug() << "ID_MASTER_QUERY";
|
||||
if (waitingPacket == ID_MASTER_QUERY)
|
||||
pmq->Read();
|
||||
else
|
||||
qDebug() << "Got wrong packet";
|
||||
update = false;
|
||||
id = pid;
|
||||
break;
|
||||
case ID_MASTER_UPDATE:
|
||||
qDebug() << "ID_MASTER_UPDATE";
|
||||
if (waitingPacket == ID_MASTER_UPDATE)
|
||||
pmu->Read();
|
||||
else
|
||||
qDebug() << "Got wrong packet";
|
||||
update = false;
|
||||
id = pid;
|
||||
break;
|
||||
case ID_MASTER_ANNOUNCE:
|
||||
qDebug() << "ID_MASTER_ANNOUNCE";
|
||||
update = false;
|
||||
id = pid;
|
||||
break;
|
||||
case ID_CONNECTION_REQUEST_ACCEPTED:
|
||||
qDebug() << "ID_CONNECTION_REQUEST_ACCEPTED";
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
RakSleep(500);
|
||||
}
|
||||
return (MASTER_PACKETS)(id);
|
||||
}
|
||||
|
||||
ConnectionState QueryClient::Connect()
|
||||
{
|
||||
|
||||
ConnectionAttemptResult car = peer->Connect(masterAddr.ToString(false), masterAddr.GetPort(), TES3MP_MASTERSERVER_PASSW,
|
||||
strlen(TES3MP_MASTERSERVER_PASSW), nullptr, 0, 5, 500);
|
||||
|
||||
while (true)
|
||||
{
|
||||
ConnectionState state = peer->GetConnectionState(masterAddr);
|
||||
switch (state)
|
||||
{
|
||||
case IS_CONNECTED:
|
||||
qDebug() << "Connected";
|
||||
return IS_CONNECTED;
|
||||
case IS_NOT_CONNECTED:
|
||||
case IS_DISCONNECTED:
|
||||
case IS_SILENTLY_DISCONNECTING:
|
||||
case IS_DISCONNECTING:
|
||||
{
|
||||
qDebug() << "Cannot connect to the master server. Code:"<< state;
|
||||
return IS_NOT_CONNECTED;
|
||||
}
|
||||
case IS_PENDING:
|
||||
case IS_CONNECTING:
|
||||
qDebug() << "Pending";
|
||||
break;
|
||||
}
|
||||
RakSleep(500);
|
||||
}
|
||||
}
|
||||
|
||||
int QueryClient::Status()
|
||||
{
|
||||
return status;
|
||||
}
|
@ -1,46 +0,0 @@
|
||||
//
|
||||
// Created by koncord on 24.04.17.
|
||||
//
|
||||
|
||||
#ifndef OPENMW_QUERYCLIENT_HPP
|
||||
#define OPENMW_QUERYCLIENT_HPP
|
||||
|
||||
#include <string>
|
||||
#include <RakPeerInterface.h>
|
||||
#include <components/openmw-mp/Master/PacketMasterQuery.hpp>
|
||||
#include <components/openmw-mp/Master/PacketMasterUpdate.hpp>
|
||||
#include <apps/browser/ServerModel.hpp>
|
||||
#include <mutex>
|
||||
|
||||
class QueryClient
|
||||
{
|
||||
public:
|
||||
QueryClient(QueryClient const &) = delete;
|
||||
QueryClient(QueryClient &&) = delete;
|
||||
QueryClient &operator=(QueryClient const &) = delete;
|
||||
QueryClient &operator=(QueryClient &&) = delete;
|
||||
|
||||
static QueryClient &Get();
|
||||
void SetServer(const std::string &addr, unsigned short port);
|
||||
std::map<RakNet::SystemAddress, QueryData> Query();
|
||||
std::pair<RakNet::SystemAddress, QueryData> Update(const RakNet::SystemAddress &addr);
|
||||
int Status();
|
||||
private:
|
||||
RakNet::ConnectionState Connect();
|
||||
MASTER_PACKETS GetAnswer(MASTER_PACKETS packet);
|
||||
protected:
|
||||
QueryClient();
|
||||
~QueryClient();
|
||||
private:
|
||||
int status;
|
||||
RakNet::RakPeerInterface *peer;
|
||||
RakNet::SystemAddress masterAddr;
|
||||
mwmp::PacketMasterQuery *pmq;
|
||||
mwmp::PacketMasterUpdate *pmu;
|
||||
std::pair<RakNet::SystemAddress, ServerData> server;
|
||||
std::mutex mxServers;
|
||||
|
||||
};
|
||||
|
||||
|
||||
#endif //OPENMW_QUERYCLIENT_HPP
|
@ -1,18 +0,0 @@
|
||||
#include "importproj.h"
|
||||
|
||||
#include <components/esm/esmreader.hpp>
|
||||
|
||||
namespace ESSImport
|
||||
{
|
||||
|
||||
void ESSImport::PROJ::load(ESM::ESMReader& esm)
|
||||
{
|
||||
while (esm.isNextSub("PNAM"))
|
||||
{
|
||||
PNAM pnam;
|
||||
esm.getHT(pnam);
|
||||
mProjectiles.push_back(pnam);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -1,47 +0,0 @@
|
||||
#ifndef OPENMW_ESSIMPORT_IMPORTPROJ_H
|
||||
#define OPENMW_ESSIMPORT_IMPORTPROJ_H
|
||||
|
||||
#include <vector>
|
||||
#include <components/esm/esmcommon.hpp>
|
||||
#include <components/esm/util.hpp>
|
||||
|
||||
namespace ESM
|
||||
{
|
||||
class ESMReader;
|
||||
}
|
||||
|
||||
namespace ESSImport
|
||||
{
|
||||
|
||||
struct PROJ
|
||||
{
|
||||
|
||||
#pragma pack(push)
|
||||
#pragma pack(1)
|
||||
struct PNAM // 184 bytes
|
||||
{
|
||||
float mAttackStrength;
|
||||
float mSpeed;
|
||||
unsigned char mUnknown[4*2];
|
||||
float mFlightTime;
|
||||
int mSplmIndex; // reference to a SPLM record (0 for ballistic projectiles)
|
||||
unsigned char mUnknown2[4];
|
||||
ESM::Vector3 mVelocity;
|
||||
ESM::Vector3 mPosition;
|
||||
unsigned char mUnknown3[4*9];
|
||||
ESM::NAME32 mActorId; // indexed refID (with the exception of "PlayerSaveGame")
|
||||
ESM::NAME32 mArrowId;
|
||||
ESM::NAME32 mBowId;
|
||||
|
||||
bool isMagic() const { return mSplmIndex != 0; }
|
||||
};
|
||||
#pragma pack(pop)
|
||||
|
||||
std::vector<PNAM> mProjectiles;
|
||||
|
||||
void load(ESM::ESMReader& esm);
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif
|
@ -1,43 +0,0 @@
|
||||
#include "importsplm.h"
|
||||
|
||||
#include <components/esm/esmreader.hpp>
|
||||
|
||||
namespace ESSImport
|
||||
{
|
||||
|
||||
void SPLM::load(ESM::ESMReader& esm)
|
||||
{
|
||||
while (esm.isNextSub("NAME"))
|
||||
{
|
||||
ActiveSpell spell;
|
||||
esm.getHT(spell.mIndex);
|
||||
esm.getHNT(spell.mSPDT, "SPDT");
|
||||
spell.mTarget = esm.getHNOString("TNAM");
|
||||
|
||||
while (esm.isNextSub("NPDT"))
|
||||
{
|
||||
ActiveEffect effect;
|
||||
esm.getHT(effect.mNPDT);
|
||||
|
||||
// Effect-specific subrecords can follow:
|
||||
// - INAM for disintegration and bound effects
|
||||
// - CNAM for summoning and command effects
|
||||
// - VNAM for vampirism
|
||||
// NOTE: There can be multiple INAMs per effect.
|
||||
// TODO: Needs more research.
|
||||
|
||||
esm.skipHSubUntil("NAM0"); // sentinel
|
||||
esm.getSubName();
|
||||
esm.skipHSub();
|
||||
|
||||
spell.mActiveEffects.push_back(effect);
|
||||
}
|
||||
|
||||
unsigned char xnam; // sentinel
|
||||
esm.getHNT(xnam, "XNAM");
|
||||
|
||||
mActiveSpells.push_back(spell);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -1,81 +0,0 @@
|
||||
#ifndef OPENMW_ESSIMPORT_IMPORTSPLM_H
|
||||
#define OPENMW_ESSIMPORT_IMPORTSPLM_H
|
||||
|
||||
#include <vector>
|
||||
#include <components/esm/esmcommon.hpp>
|
||||
#include <components/esm/util.hpp>
|
||||
|
||||
namespace ESM
|
||||
{
|
||||
class ESMReader;
|
||||
}
|
||||
|
||||
namespace ESSImport
|
||||
{
|
||||
|
||||
struct SPLM
|
||||
{
|
||||
|
||||
#pragma pack(push)
|
||||
#pragma pack(1)
|
||||
struct SPDT // 160 bytes
|
||||
{
|
||||
int mType; // 1 = spell, 2 = enchantment, 3 = potion
|
||||
ESM::NAME32 mId; // base ID of a spell/enchantment/potion
|
||||
unsigned char mUnknown[4*4];
|
||||
ESM::NAME32 mCasterId;
|
||||
ESM::NAME32 mSourceId; // empty for spells
|
||||
unsigned char mUnknown2[4*11];
|
||||
};
|
||||
|
||||
struct NPDT // 56 bytes
|
||||
{
|
||||
ESM::NAME32 mAffectedActorId;
|
||||
unsigned char mUnknown[4*2];
|
||||
int mMagnitude;
|
||||
float mSecondsActive;
|
||||
unsigned char mUnknown2[4*2];
|
||||
};
|
||||
|
||||
struct INAM // 40 bytes
|
||||
{
|
||||
int mUnknown;
|
||||
unsigned char mUnknown2;
|
||||
ESM::FIXED_STRING<35> mItemId; // disintegrated item / bound item / item to re-equip after expiration
|
||||
};
|
||||
|
||||
struct CNAM // 36 bytes
|
||||
{
|
||||
int mUnknown; // seems to always be 0
|
||||
ESM::NAME32 mSummonedOrCommandedActor[32];
|
||||
};
|
||||
|
||||
struct VNAM // 4 bytes
|
||||
{
|
||||
int mUnknown;
|
||||
};
|
||||
|
||||
|
||||
#pragma pack(pop)
|
||||
|
||||
struct ActiveEffect
|
||||
{
|
||||
NPDT mNPDT;
|
||||
};
|
||||
|
||||
struct ActiveSpell
|
||||
{
|
||||
int mIndex;
|
||||
SPDT mSPDT;
|
||||
std::string mTarget;
|
||||
std::vector<ActiveEffect> mActiveEffects;
|
||||
};
|
||||
|
||||
std::vector<ActiveSpell> mActiveSpells;
|
||||
|
||||
void load(ESM::ESMReader& esm);
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif
|
@ -1,176 +0,0 @@
|
||||
#include "advancedpage.hpp"
|
||||
|
||||
#include <components/config/gamesettings.hpp>
|
||||
#include <components/config/launchersettings.hpp>
|
||||
#include <QFileDialog>
|
||||
#include <QCompleter>
|
||||
#include <components/contentselector/view/contentselector.hpp>
|
||||
#include <components/contentselector/model/esmfile.hpp>
|
||||
|
||||
Launcher::AdvancedPage::AdvancedPage(Files::ConfigurationManager &cfg,
|
||||
Config::GameSettings &gameSettings,
|
||||
Settings::Manager &engineSettings, QWidget *parent)
|
||||
: QWidget(parent)
|
||||
, mCfgMgr(cfg)
|
||||
, mGameSettings(gameSettings)
|
||||
, mEngineSettings(engineSettings)
|
||||
{
|
||||
setObjectName ("AdvancedPage");
|
||||
setupUi(this);
|
||||
|
||||
loadSettings();
|
||||
}
|
||||
|
||||
void Launcher::AdvancedPage::loadCellsForAutocomplete(QStringList cellNames) {
|
||||
// Set up an auto-completer for the "Start default character at" field
|
||||
auto *completer = new QCompleter(cellNames);
|
||||
completer->setCompletionMode(QCompleter::PopupCompletion);
|
||||
completer->setCaseSensitivity(Qt::CaseSensitivity::CaseInsensitive);
|
||||
startDefaultCharacterAtField->setCompleter(completer);
|
||||
|
||||
}
|
||||
|
||||
void Launcher::AdvancedPage::on_skipMenuCheckBox_stateChanged(int state) {
|
||||
startDefaultCharacterAtLabel->setEnabled(state == Qt::Checked);
|
||||
startDefaultCharacterAtField->setEnabled(state == Qt::Checked);
|
||||
}
|
||||
|
||||
void Launcher::AdvancedPage::on_runScriptAfterStartupBrowseButton_clicked()
|
||||
{
|
||||
QString scriptFile = QFileDialog::getOpenFileName(
|
||||
this,
|
||||
QObject::tr("Select script file"),
|
||||
QDir::currentPath(),
|
||||
QString(tr("Text file (*.txt)")));
|
||||
|
||||
if (scriptFile.isEmpty())
|
||||
return;
|
||||
|
||||
QFileInfo info(scriptFile);
|
||||
|
||||
if (!info.exists() || !info.isReadable())
|
||||
return;
|
||||
|
||||
const QString path(QDir::toNativeSeparators(info.absoluteFilePath()));
|
||||
runScriptAfterStartupField->setText(path);
|
||||
}
|
||||
|
||||
bool Launcher::AdvancedPage::loadSettings()
|
||||
{
|
||||
// Testing
|
||||
bool skipMenu = mGameSettings.value("skip-menu").toInt() == 1;
|
||||
if (skipMenu) {
|
||||
skipMenuCheckBox->setCheckState(Qt::Checked);
|
||||
}
|
||||
startDefaultCharacterAtLabel->setEnabled(skipMenu);
|
||||
startDefaultCharacterAtField->setEnabled(skipMenu);
|
||||
|
||||
startDefaultCharacterAtField->setText(mGameSettings.value("start"));
|
||||
runScriptAfterStartupField->setText(mGameSettings.value("script-run"));
|
||||
|
||||
// Game Settings
|
||||
loadSettingBool(canLootDuringDeathAnimationCheckBox, "can loot during death animation", "Game");
|
||||
loadSettingBool(followersAttackOnSightCheckBox, "followers attack on sight", "Game");
|
||||
loadSettingBool(preventMerchantEquippingCheckBox, "prevent merchant equipping", "Game");
|
||||
loadSettingBool(rebalanceSoulGemValuesCheckBox, "rebalance soul gem values", "Game");
|
||||
loadSettingBool(chargeForEveryFollowerCheckBox, "charge for every follower travelling", "Game");
|
||||
loadSettingBool(enchantedWeaponsMagicalCheckBox, "enchanted weapons are magical", "Game");
|
||||
loadSettingBool(permanentBarterDispositionChangeCheckBox, "barter disposition change is permanent", "Game");
|
||||
|
||||
// Input Settings
|
||||
loadSettingBool(allowThirdPersonZoomCheckBox, "allow third person zoom", "Input");
|
||||
loadSettingBool(grabCursorCheckBox, "grab cursor", "Input");
|
||||
loadSettingBool(toggleSneakCheckBox, "toggle sneak", "Input");
|
||||
|
||||
// Saves Settings
|
||||
loadSettingBool(timePlayedCheckbox, "timeplayed", "Saves");
|
||||
maximumQuicksavesComboBox->setValue(mEngineSettings.getInt("max quicksaves", "Saves"));
|
||||
|
||||
// User Interface Settings
|
||||
loadSettingBool(showEffectDurationCheckBox, "show effect duration", "Game");
|
||||
loadSettingBool(showEnchantChanceCheckBox, "show enchant chance", "Game");
|
||||
loadSettingBool(showMeleeInfoCheckBox, "show melee info", "Game");
|
||||
loadSettingBool(showProjectileDamageCheckBox, "show projectile damage", "Game");
|
||||
int showOwnedIndex = mEngineSettings.getInt("show owned", "Game");
|
||||
// Match the index with the option (only 0, 1, 2, or 3 are valid). Will default to 0 if invalid.
|
||||
if (showOwnedIndex >= 0 && showOwnedIndex <= 3)
|
||||
showOwnedComboBox->setCurrentIndex(showOwnedIndex);
|
||||
|
||||
// Other Settings
|
||||
QString screenshotFormatString = QString::fromStdString(mEngineSettings.getString("screenshot format", "General")).toUpper();
|
||||
if (screenshotFormatComboBox->findText(screenshotFormatString) == -1)
|
||||
screenshotFormatComboBox->addItem(screenshotFormatString);
|
||||
screenshotFormatComboBox->setCurrentIndex(screenshotFormatComboBox->findText(screenshotFormatString));
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void Launcher::AdvancedPage::saveSettings()
|
||||
{
|
||||
// Ensure we only set the new settings if they changed. This is to avoid cluttering the
|
||||
// user settings file (which by definition should only contain settings the user has touched)
|
||||
|
||||
// Testing
|
||||
int skipMenu = skipMenuCheckBox->checkState() == Qt::Checked;
|
||||
if (skipMenu != mGameSettings.value("skip-menu").toInt())
|
||||
mGameSettings.setValue("skip-menu", QString::number(skipMenu));
|
||||
|
||||
QString startCell = startDefaultCharacterAtField->text();
|
||||
if (startCell != mGameSettings.value("start")) {
|
||||
mGameSettings.setValue("start", startCell);
|
||||
}
|
||||
QString scriptRun = runScriptAfterStartupField->text();
|
||||
if (scriptRun != mGameSettings.value("script-run"))
|
||||
mGameSettings.setValue("script-run", scriptRun);
|
||||
|
||||
// Game Settings
|
||||
saveSettingBool(canLootDuringDeathAnimationCheckBox, "can loot during death animation", "Game");
|
||||
saveSettingBool(followersAttackOnSightCheckBox, "followers attack on sight", "Game");
|
||||
saveSettingBool(preventMerchantEquippingCheckBox, "prevent merchant equipping", "Game");
|
||||
saveSettingBool(rebalanceSoulGemValuesCheckBox, "rebalance soul gem values", "Game");
|
||||
saveSettingBool(chargeForEveryFollowerCheckBox, "charge for every follower travelling", "Game");
|
||||
saveSettingBool(enchantedWeaponsMagicalCheckBox, "enchanted weapons are magical", "Game");
|
||||
saveSettingBool(permanentBarterDispositionChangeCheckBox, "barter disposition change is permanent", "Game");
|
||||
|
||||
// Input Settings
|
||||
saveSettingBool(allowThirdPersonZoomCheckBox, "allow third person zoom", "Input");
|
||||
saveSettingBool(grabCursorCheckBox, "grab cursor", "Input");
|
||||
saveSettingBool(toggleSneakCheckBox, "toggle sneak", "Input");
|
||||
|
||||
// Saves Settings
|
||||
saveSettingBool(timePlayedCheckbox, "timeplayed", "Saves");
|
||||
int maximumQuicksaves = maximumQuicksavesComboBox->value();
|
||||
if (maximumQuicksaves != mEngineSettings.getInt("max quicksaves", "Saves")) {
|
||||
mEngineSettings.setInt("max quicksaves", "Saves", maximumQuicksaves);
|
||||
}
|
||||
|
||||
// User Interface Settings
|
||||
saveSettingBool(showEffectDurationCheckBox, "show effect duration", "Game");
|
||||
saveSettingBool(showEnchantChanceCheckBox, "show enchant chance", "Game");
|
||||
saveSettingBool(showMeleeInfoCheckBox, "show melee info", "Game");
|
||||
saveSettingBool(showProjectileDamageCheckBox, "show projectile damage", "Game");
|
||||
int showOwnedCurrentIndex = showOwnedComboBox->currentIndex();
|
||||
if (showOwnedCurrentIndex != mEngineSettings.getInt("show owned", "Game"))
|
||||
mEngineSettings.setInt("show owned", "Game", showOwnedCurrentIndex);
|
||||
|
||||
// Other Settings
|
||||
std::string screenshotFormatString = screenshotFormatComboBox->currentText().toLower().toStdString();
|
||||
if (screenshotFormatString != mEngineSettings.getString("screenshot format", "General"))
|
||||
mEngineSettings.setString("screenshot format", "General", screenshotFormatString);
|
||||
}
|
||||
|
||||
void Launcher::AdvancedPage::loadSettingBool(QCheckBox *checkbox, const std::string &setting, const std::string &group) {
|
||||
if (mEngineSettings.getBool(setting, group))
|
||||
checkbox->setCheckState(Qt::Checked);
|
||||
}
|
||||
|
||||
void Launcher::AdvancedPage::saveSettingBool(QCheckBox *checkbox, const std::string &setting, const std::string &group) {
|
||||
bool cValue = checkbox->checkState();
|
||||
if (cValue != mEngineSettings.getBool(setting, group))
|
||||
mEngineSettings.setBool(setting, group, cValue);
|
||||
}
|
||||
|
||||
void Launcher::AdvancedPage::slotLoadedCellsChanged(QStringList cellNames)
|
||||
{
|
||||
loadCellsForAutocomplete(cellNames);
|
||||
}
|
@ -1,47 +0,0 @@
|
||||
#ifndef ADVANCEDPAGE_H
|
||||
#define ADVANCEDPAGE_H
|
||||
|
||||
#include <QWidget>
|
||||
|
||||
#include "ui_advancedpage.h"
|
||||
|
||||
#include <components/settings/settings.hpp>
|
||||
|
||||
namespace Files { struct ConfigurationManager; }
|
||||
namespace Config { class GameSettings; }
|
||||
|
||||
namespace Launcher
|
||||
{
|
||||
class AdvancedPage : public QWidget, private Ui::AdvancedPage
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
AdvancedPage(Files::ConfigurationManager &cfg, Config::GameSettings &gameSettings,
|
||||
Settings::Manager &engineSettings, QWidget *parent = 0);
|
||||
|
||||
bool loadSettings();
|
||||
void saveSettings();
|
||||
|
||||
public slots:
|
||||
void slotLoadedCellsChanged(QStringList cellNames);
|
||||
|
||||
private slots:
|
||||
void on_skipMenuCheckBox_stateChanged(int state);
|
||||
void on_runScriptAfterStartupBrowseButton_clicked();
|
||||
|
||||
private:
|
||||
Files::ConfigurationManager &mCfgMgr;
|
||||
Config::GameSettings &mGameSettings;
|
||||
Settings::Manager &mEngineSettings;
|
||||
|
||||
/**
|
||||
* Load the cells associated with the given content files for use in autocomplete
|
||||
* @param filePaths the file paths of the content files to be examined
|
||||
*/
|
||||
void loadCellsForAutocomplete(QStringList filePaths);
|
||||
void loadSettingBool(QCheckBox *checkbox, const std::string& setting, const std::string& group);
|
||||
void saveSettingBool(QCheckBox *checkbox, const std::string& setting, const std::string& group);
|
||||
};
|
||||
}
|
||||
#endif
|
@ -1,48 +0,0 @@
|
||||
#include "cellnameloader.hpp"
|
||||
|
||||
#include <components/esm/loadcell.hpp>
|
||||
#include <components/contentselector/view/contentselector.hpp>
|
||||
|
||||
QSet<QString> CellNameLoader::getCellNames(QStringList &contentPaths)
|
||||
{
|
||||
QSet<QString> cellNames;
|
||||
ESM::ESMReader esmReader;
|
||||
|
||||
// Loop through all content files
|
||||
for (auto &contentPath : contentPaths) {
|
||||
esmReader.open(contentPath.toStdString());
|
||||
|
||||
// Loop through all records
|
||||
while(esmReader.hasMoreRecs())
|
||||
{
|
||||
ESM::NAME recordName = esmReader.getRecName();
|
||||
esmReader.getRecHeader();
|
||||
|
||||
if (isCellRecord(recordName)) {
|
||||
QString cellName = getCellName(esmReader);
|
||||
if (!cellName.isEmpty()) {
|
||||
cellNames.insert(cellName);
|
||||
}
|
||||
}
|
||||
|
||||
// Stop loading content for this record and continue to the next
|
||||
esmReader.skipRecord();
|
||||
}
|
||||
}
|
||||
|
||||
return cellNames;
|
||||
}
|
||||
|
||||
bool CellNameLoader::isCellRecord(ESM::NAME &recordName)
|
||||
{
|
||||
return recordName.intval == ESM::REC_CELL;
|
||||
}
|
||||
|
||||
QString CellNameLoader::getCellName(ESM::ESMReader &esmReader)
|
||||
{
|
||||
ESM::Cell cell;
|
||||
bool isDeleted = false;
|
||||
cell.loadNameAndData(esmReader, isDeleted);
|
||||
|
||||
return QString::fromStdString(cell.mName);
|
||||
}
|
@ -1,41 +0,0 @@
|
||||
#ifndef OPENMW_CELLNAMELOADER_H
|
||||
#define OPENMW_CELLNAMELOADER_H
|
||||
|
||||
#include <QComboBox>
|
||||
#include <QSet>
|
||||
#include <QString>
|
||||
|
||||
#include <components/esm/esmreader.hpp>
|
||||
|
||||
namespace ESM {class ESMReader; struct Cell;}
|
||||
namespace ContentSelectorView {class ContentSelector;}
|
||||
|
||||
class CellNameLoader {
|
||||
|
||||
public:
|
||||
|
||||
/**
|
||||
* Returns the names of all cells contained within the given content files
|
||||
* @param contentPaths the file paths of each content file to be examined
|
||||
* @return the names of all cells
|
||||
*/
|
||||
QSet<QString> getCellNames(QStringList &contentPaths);
|
||||
|
||||
private:
|
||||
/**
|
||||
* Returns whether or not the given record is of type "Cell"
|
||||
* @param name The name associated with the record
|
||||
* @return whether or not the given record is of type "Cell"
|
||||
*/
|
||||
bool isCellRecord(ESM::NAME &name);
|
||||
|
||||
/**
|
||||
* Returns the name of the cell
|
||||
* @param esmReader the reader currently pointed to a loaded cell
|
||||
* @return the name of the cell
|
||||
*/
|
||||
QString getCellName(ESM::ESMReader &esmReader);
|
||||
};
|
||||
|
||||
|
||||
#endif //OPENMW_CELLNAMELOADER_H
|
@ -1,32 +0,0 @@
|
||||
project(masterserver)
|
||||
|
||||
#set(CMAKE_CXX_STANDARD 14)
|
||||
add_definitions(-std=gnu++14)
|
||||
|
||||
include_directories("./")
|
||||
|
||||
set(SOURCE_FILES main.cpp MasterServer.cpp MasterServer.hpp RestServer.cpp RestServer.hpp)
|
||||
|
||||
add_executable(masterserver ${SOURCE_FILES})
|
||||
target_link_libraries(masterserver ${RakNet_LIBRARY} components)
|
||||
|
||||
option(BUILD_MASTER_TEST "build master server test program" OFF)
|
||||
|
||||
if(BUILD_MASTER_TEST)
|
||||
add_executable(ServerTest ServerTest.cpp)
|
||||
target_link_libraries(ServerTest ${RakNet_LIBRARY} components)
|
||||
endif()
|
||||
|
||||
if (UNIX)
|
||||
# Fix for not visible pthreads functions for linker with glibc 2.15
|
||||
if(NOT APPLE)
|
||||
target_link_libraries(masterserver ${CMAKE_THREAD_LIBS_INIT})
|
||||
if(BUILD_MASTER_TEST)
|
||||
target_link_libraries(ServerTest ${CMAKE_THREAD_LIBS_INIT})
|
||||
endif()
|
||||
endif(NOT APPLE)
|
||||
endif(UNIX)
|
||||
|
||||
if(WIN32)
|
||||
target_link_libraries(masterserver wsock32)
|
||||
endif(WIN32)
|
@ -1,236 +0,0 @@
|
||||
//
|
||||
// Created by koncord on 21.04.17.
|
||||
//
|
||||
|
||||
#include <RakPeerInterface.h>
|
||||
#include <RakSleep.h>
|
||||
#include <BitStream.h>
|
||||
#include <iostream>
|
||||
#include "MasterServer.hpp"
|
||||
|
||||
#include <components/openmw-mp/Master/PacketMasterQuery.hpp>
|
||||
#include <components/openmw-mp/Master/PacketMasterUpdate.hpp>
|
||||
#include <components/openmw-mp/Master/PacketMasterAnnounce.hpp>
|
||||
#include <components/openmw-mp/Version.hpp>
|
||||
|
||||
using namespace RakNet;
|
||||
using namespace std;
|
||||
using namespace mwmp;
|
||||
using namespace chrono;
|
||||
|
||||
MasterServer::MasterServer(unsigned short maxConnections, unsigned short port)
|
||||
{
|
||||
peer = RakPeerInterface::GetInstance();
|
||||
sockdescr = SocketDescriptor(port, 0);
|
||||
peer->Startup(maxConnections, &sockdescr, 1, 1000);
|
||||
|
||||
peer->SetMaximumIncomingConnections(maxConnections);
|
||||
peer->SetIncomingPassword(TES3MP_MASTERSERVER_PASSW, (int) strlen(TES3MP_MASTERSERVER_PASSW));
|
||||
run = false;
|
||||
}
|
||||
|
||||
MasterServer::~MasterServer()
|
||||
{
|
||||
Stop(true);
|
||||
}
|
||||
|
||||
using namespace chrono;
|
||||
|
||||
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();
|
||||
|
||||
auto now = steady_clock::now();
|
||||
if (now - startTime >= 60s)
|
||||
{
|
||||
startTime = steady_clock::now();
|
||||
for (auto it = servers.begin(); it != servers.end();)
|
||||
{
|
||||
|
||||
if (it->second.lastUpdate + 60s <= now)
|
||||
servers.erase(it++);
|
||||
else ++it;
|
||||
}
|
||||
for(auto id = pendingACKs.begin(); id != pendingACKs.end();)
|
||||
{
|
||||
if(now - id->second >= 30s)
|
||||
{
|
||||
cout << "timeout: " << peer->GetSystemAddressFromGuid(id->first).ToString() << endl;
|
||||
peer->CloseConnection(id->first, true);
|
||||
id = pendingACKs.erase(id);
|
||||
}
|
||||
else
|
||||
++id;
|
||||
}
|
||||
}
|
||||
|
||||
if (packet == nullptr)
|
||||
RakSleep(10);
|
||||
else
|
||||
for (; packet; peer->DeallocatePacket(packet), packet = peer->Receive())
|
||||
{
|
||||
BitStream data(packet->data, packet->length, false);
|
||||
data.Read(packetId);
|
||||
switch (packetId)
|
||||
{
|
||||
case ID_NEW_INCOMING_CONNECTION:
|
||||
cout << "New incoming connection: " << packet->systemAddress.ToString() << endl;
|
||||
break;
|
||||
case ID_DISCONNECTION_NOTIFICATION:
|
||||
cout << "Disconnected: " << packet->systemAddress.ToString() << endl;
|
||||
break;
|
||||
case ID_CONNECTION_LOST:
|
||||
cout << "Connection lost: " << packet->systemAddress.ToString() << endl;
|
||||
break;
|
||||
case ID_MASTER_QUERY:
|
||||
{
|
||||
pmq.SetServers(reinterpret_cast<map<SystemAddress, QueryData> *>(&servers));
|
||||
pmq.Send(packet->systemAddress);
|
||||
pendingACKs[packet->guid] = steady_clock::now();
|
||||
|
||||
cout << "Sent info about all " << servers.size() << " servers to "
|
||||
<< packet->systemAddress.ToString() << endl;
|
||||
break;
|
||||
}
|
||||
case ID_MASTER_UPDATE:
|
||||
{
|
||||
SystemAddress addr;
|
||||
data.Read(addr); // update 1 server
|
||||
|
||||
ServerIter it = servers.find(addr);
|
||||
if (it != servers.end())
|
||||
{
|
||||
pair<SystemAddress, QueryData> pairPtr(it->first, static_cast<QueryData>(it->second));
|
||||
pmu.SetServer(&pairPtr);
|
||||
pmu.Send(packet->systemAddress);
|
||||
pendingACKs[packet->guid] = steady_clock::now();
|
||||
cout << "Sent info about " << addr.ToString() << " to " << packet->systemAddress.ToString()
|
||||
<< endl;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case ID_MASTER_ANNOUNCE:
|
||||
{
|
||||
ServerIter iter = servers.find(packet->systemAddress);
|
||||
|
||||
pma.SetReadStream(&data);
|
||||
SServer server;
|
||||
pma.SetServer(&server);
|
||||
pma.Read();
|
||||
|
||||
auto keepAliveFunc = [&]() {
|
||||
iter->second.lastUpdate = now;
|
||||
pma.SetFunc(PacketMasterAnnounce::FUNCTION_KEEP);
|
||||
pma.Send(packet->systemAddress);
|
||||
pendingACKs[packet->guid] = steady_clock::now();
|
||||
};
|
||||
|
||||
if (iter != servers.end())
|
||||
{
|
||||
if (pma.GetFunc() == PacketMasterAnnounce::FUNCTION_DELETE)
|
||||
{
|
||||
servers.erase(iter);
|
||||
cout << "Deleted";
|
||||
pma.Send(packet->systemAddress);
|
||||
pendingACKs[packet->guid] = steady_clock::now();
|
||||
}
|
||||
else if (pma.GetFunc() == PacketMasterAnnounce::FUNCTION_ANNOUNCE)
|
||||
{
|
||||
cout << "Updated";
|
||||
iter->second = server;
|
||||
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);
|
||||
pendingACKs[packet->guid] = steady_clock::now();
|
||||
}
|
||||
cout << " server " << packet->systemAddress.ToString() << endl;
|
||||
break;
|
||||
}
|
||||
case ID_SND_RECEIPT_ACKED:
|
||||
uint32_t num;
|
||||
memcpy(&num, packet->data+1, 4);
|
||||
cout << "Packet with id " << num << " was delivered." << endl;
|
||||
pendingACKs.erase(packet->guid);
|
||||
peer->CloseConnection(packet->systemAddress, true);
|
||||
break;
|
||||
default:
|
||||
cout << "Wrong packet. id " << (unsigned) packet->data[0] << " packet length " << packet->length << " from " << packet->systemAddress.ToString() << endl;
|
||||
peer->CloseConnection(packet->systemAddress, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
peer->Shutdown(1000);
|
||||
RakPeerInterface::DestroyInstance(peer);
|
||||
cout << "Server thread stopped" << endl;
|
||||
}
|
||||
|
||||
void MasterServer::Start()
|
||||
{
|
||||
if (!run)
|
||||
{
|
||||
run = true;
|
||||
tMasterThread = thread(&MasterServer::Thread, this);
|
||||
cout << "Started" << endl;
|
||||
}
|
||||
}
|
||||
|
||||
void MasterServer::Stop(bool wait)
|
||||
{
|
||||
if (run)
|
||||
{
|
||||
run = false;
|
||||
if (wait && tMasterThread.joinable())
|
||||
tMasterThread.join();
|
||||
}
|
||||
}
|
||||
|
||||
bool MasterServer::isRunning()
|
||||
{
|
||||
return run;
|
||||
}
|
||||
|
||||
void MasterServer::Wait()
|
||||
{
|
||||
if (run)
|
||||
{
|
||||
if (tMasterThread.joinable())
|
||||
tMasterThread.join();
|
||||
}
|
||||
}
|
||||
|
||||
MasterServer::ServerMap *MasterServer::GetServers()
|
||||
{
|
||||
return &servers;
|
||||
}
|
@ -1,55 +0,0 @@
|
||||
//
|
||||
// Created by koncord on 21.04.17.
|
||||
//
|
||||
|
||||
#ifndef NEWMASTERPROTO_MASTERSERVER_HPP
|
||||
#define NEWMASTERPROTO_MASTERSERVER_HPP
|
||||
|
||||
#include <thread>
|
||||
#include <chrono>
|
||||
#include <RakPeerInterface.h>
|
||||
#include <components/openmw-mp/Master/MasterData.hpp>
|
||||
|
||||
class MasterServer
|
||||
{
|
||||
public:
|
||||
struct Ban
|
||||
{
|
||||
RakNet::SystemAddress sa;
|
||||
bool permanent;
|
||||
struct Date
|
||||
{
|
||||
} date;
|
||||
};
|
||||
struct SServer : QueryData
|
||||
{
|
||||
std::chrono::steady_clock::time_point lastUpdate;
|
||||
};
|
||||
typedef std::map<RakNet::SystemAddress, SServer> ServerMap;
|
||||
//typedef ServerMap::const_iterator ServerCIter;
|
||||
typedef ServerMap::iterator ServerIter;
|
||||
|
||||
MasterServer(unsigned short maxConnections, unsigned short port);
|
||||
~MasterServer();
|
||||
|
||||
void Start();
|
||||
void Stop(bool wait = false);
|
||||
bool isRunning();
|
||||
void Wait();
|
||||
|
||||
ServerMap* GetServers();
|
||||
|
||||
private:
|
||||
void Thread();
|
||||
|
||||
private:
|
||||
std::thread tMasterThread;
|
||||
RakNet::RakPeerInterface* peer;
|
||||
RakNet::SocketDescriptor sockdescr;
|
||||
ServerMap servers;
|
||||
bool run;
|
||||
std::map<RakNet::RakNetGUID, std::chrono::steady_clock::time_point> pendingACKs;
|
||||
};
|
||||
|
||||
|
||||
#endif //NEWMASTERPROTO_MASTERSERVER_HPP
|
@ -1,192 +0,0 @@
|
||||
//
|
||||
// Created by koncord on 13.05.17.
|
||||
//
|
||||
|
||||
#include "RestServer.hpp"
|
||||
|
||||
#include <boost/property_tree/ptree.hpp>
|
||||
#include <boost/property_tree/json_parser.hpp>
|
||||
|
||||
using namespace std;
|
||||
using namespace chrono;
|
||||
using namespace boost::property_tree;
|
||||
|
||||
static string response201 = "HTTP/1.1 201 Created\r\nContent-Length: 7\r\n\r\nCreated";
|
||||
static string response202 = "HTTP/1.1 202 Accepted\r\nContent-Length: 8\r\n\r\nAccepted";
|
||||
static string response400 = "HTTP/1.1 400 Bad Request\r\nContent-Length: 11\r\n\r\nbad request";
|
||||
|
||||
inline void ResponseStr(HttpServer::Response &response, string content, string type = "", string code = "200 OK")
|
||||
{
|
||||
response << "HTTP/1.1 " << code << "\r\n";
|
||||
if (!type.empty())
|
||||
response << "Content-Type: " << type <<"\r\n";
|
||||
response << "Content-Length: " << content.length() << "\r\n\r\n" << content;
|
||||
}
|
||||
|
||||
inline void ptreeToServer(boost::property_tree::ptree &pt, MasterServer::SServer &server)
|
||||
{
|
||||
server.SetName(pt.get<string>("hostname").c_str());
|
||||
server.SetGameMode(pt.get<string>("modname").c_str());
|
||||
server.SetVersion(pt.get<string>("version").c_str());
|
||||
server.SetPassword(pt.get<bool>("passw"));
|
||||
//server.query_port = pt.get<unsigned short>("query_port");
|
||||
server.SetPlayers(pt.get<unsigned>("players"));
|
||||
server.SetMaxPlayers(pt.get<unsigned>("max_players"));
|
||||
}
|
||||
|
||||
inline void queryToStringStream(stringstream &ss, string addr, MasterServer::SServer &query)
|
||||
{
|
||||
ss <<"\"" << addr << "\":{";
|
||||
ss << "\"modname\": \"" << query.GetGameMode() << "\"" << ", ";
|
||||
ss << "\"passw\": " << (query.GetPassword() ? "true" : "false") << ", ";
|
||||
ss << "\"hostname\": \"" << query.GetName() << "\"" << ", ";
|
||||
ss << "\"query_port\": " << 0 << ", ";
|
||||
ss << "\"last_update\": " << duration_cast<seconds>(steady_clock::now() - query.lastUpdate).count() << ", ";
|
||||
ss << "\"players\": " << query.GetPlayers() << ", ";
|
||||
ss << "\"version\": \"" << query.GetVersion() << "\"" << ", ";
|
||||
ss << "\"max_players\": " << query.GetMaxPlayers();
|
||||
ss << "}";
|
||||
}
|
||||
|
||||
RestServer::RestServer(unsigned short port, MasterServer::ServerMap *pMap) : serverMap(pMap)
|
||||
{
|
||||
httpServer.config.port = port;
|
||||
}
|
||||
|
||||
void RestServer::start()
|
||||
{
|
||||
static const string ValidIpAddressRegex = "(?:[0-9]{1,3}\\.){3}[0-9]{1,3}";
|
||||
static const string ValidPortRegex = "(?:[0-9]{1,4}|[1-5][0-9]{4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-5])$";
|
||||
static const string ServersRegex = "^/api/servers(?:/(" + ValidIpAddressRegex + "\\:" + ValidPortRegex + "))?";
|
||||
|
||||
httpServer.resource[ServersRegex]["GET"] = [this](auto response, auto request) {
|
||||
if (request->path_match[1].length() > 0)
|
||||
{
|
||||
try
|
||||
{
|
||||
stringstream ss;
|
||||
ss << "{";
|
||||
auto addr = request->path_match[1].str();
|
||||
auto port = (unsigned short)stoi(&(addr[addr.find(':')+1]));
|
||||
queryToStringStream(ss, "server", serverMap->at(RakNet::SystemAddress(addr.c_str(), port)));
|
||||
ss << "}";
|
||||
ResponseStr(*response, ss.str(), "application/json");
|
||||
}
|
||||
catch(out_of_range e)
|
||||
{
|
||||
*response << response400;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
static string str;
|
||||
|
||||
//if (updatedCache)
|
||||
{
|
||||
stringstream ss;
|
||||
ss << "{";
|
||||
ss << "\"list servers\":{";
|
||||
for (auto query = serverMap->begin(); query != serverMap->end(); query++)
|
||||
{
|
||||
queryToStringStream(ss, query->first.ToString(true, ':'), query->second);
|
||||
if (next(query) != serverMap->end())
|
||||
ss << ", ";
|
||||
}
|
||||
ss << "}}";
|
||||
ResponseStr(*response, ss.str(), "application/json");
|
||||
updatedCache = false;
|
||||
}
|
||||
*response << str;
|
||||
}
|
||||
};
|
||||
|
||||
//Add query for < 0.6 servers
|
||||
httpServer.resource[ServersRegex]["POST"] = [this](auto response, auto request) {
|
||||
try
|
||||
{
|
||||
ptree pt;
|
||||
read_json(request->content, pt);
|
||||
|
||||
MasterServer::SServer server;
|
||||
ptreeToServer(pt, server);
|
||||
|
||||
unsigned short port = pt.get<unsigned short>("port");
|
||||
server.lastUpdate = steady_clock::now();
|
||||
serverMap->insert({RakNet::SystemAddress(request->remote_endpoint_address.c_str(), port), server});
|
||||
updatedCache = true;
|
||||
|
||||
*response << response201;
|
||||
}
|
||||
catch (exception& e)
|
||||
{
|
||||
cout << e.what() << endl;
|
||||
*response << response400;
|
||||
}
|
||||
};
|
||||
|
||||
//Update query for < 0.6 servers
|
||||
httpServer.resource[ServersRegex]["PUT"] = [this](auto response, auto request) {
|
||||
auto addr = request->path_match[1].str();
|
||||
auto port = (unsigned short)stoi(&(addr[addr.find(':')+1]));
|
||||
|
||||
auto query = serverMap->find(RakNet::SystemAddress(request->remote_endpoint_address.c_str(), port));
|
||||
|
||||
if (query == serverMap->end())
|
||||
{
|
||||
cout << request->remote_endpoint_address + ": Trying to update a non-existent server or without permissions." << endl;
|
||||
*response << response400;
|
||||
return;
|
||||
}
|
||||
|
||||
if (request->content.size() != 0)
|
||||
{
|
||||
try
|
||||
{
|
||||
ptree pt;
|
||||
read_json(request->content, pt);
|
||||
|
||||
ptreeToServer(pt, query->second);
|
||||
|
||||
updatedCache = true;
|
||||
}
|
||||
catch(exception &e)
|
||||
{
|
||||
cout << e.what() << endl;
|
||||
*response << response400;
|
||||
}
|
||||
}
|
||||
|
||||
query->second.lastUpdate = steady_clock::now();
|
||||
|
||||
*response << response202;
|
||||
};
|
||||
|
||||
httpServer.resource["/api/servers/info"]["GET"] = [this](auto response, auto /*request*/) {
|
||||
stringstream ss;
|
||||
ss << '{';
|
||||
ss << "\"servers\": " << serverMap->size();
|
||||
unsigned int players = 0;
|
||||
for (auto s : *serverMap)
|
||||
players += s.second.GetPlayers();
|
||||
ss << ", \"players\": " << players;
|
||||
ss << "}";
|
||||
|
||||
ResponseStr(*response, ss.str(), "application/json");
|
||||
};
|
||||
|
||||
httpServer.default_resource["GET"]=[](auto response, auto /*request*/) {
|
||||
*response << response400;
|
||||
};
|
||||
|
||||
httpServer.start();
|
||||
}
|
||||
|
||||
void RestServer::cacheUpdated()
|
||||
{
|
||||
updatedCache = true;
|
||||
}
|
||||
|
||||
void RestServer::stop()
|
||||
{
|
||||
httpServer.stop();
|
||||
}
|
@ -1,30 +0,0 @@
|
||||
//
|
||||
// Created by koncord on 13.05.17.
|
||||
//
|
||||
|
||||
#ifndef NEWRESTAPI_RESTSERVER_HPP
|
||||
#define NEWRESTAPI_RESTSERVER_HPP
|
||||
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
#include "MasterServer.hpp"
|
||||
#include "SimpleWeb/http_server.hpp"
|
||||
|
||||
typedef SimpleWeb::Server<SimpleWeb::HTTP> HttpServer;
|
||||
|
||||
class RestServer
|
||||
{
|
||||
public:
|
||||
RestServer(unsigned short port, MasterServer::ServerMap *pMap);
|
||||
void start();
|
||||
void stop();
|
||||
void cacheUpdated();
|
||||
|
||||
private:
|
||||
HttpServer httpServer;
|
||||
MasterServer::ServerMap *serverMap;
|
||||
bool updatedCache = true;
|
||||
};
|
||||
|
||||
|
||||
#endif //NEWRESTAPI_RESTSERVER_HPP
|
@ -1,186 +0,0 @@
|
||||
//
|
||||
// Created by koncord on 21.04.17.
|
||||
//
|
||||
|
||||
#include <RakPeerInterface.h>
|
||||
#include <RakSleep.h>
|
||||
#include <BitStream.h>
|
||||
#include <iostream>
|
||||
#include <Kbhit.h>
|
||||
#include <Gets.h>
|
||||
#include <components/openmw-mp/Master/MasterData.hpp>
|
||||
#include <components/openmw-mp/Master/PacketMasterAnnounce.hpp>
|
||||
#include <components/openmw-mp/Master/PacketMasterUpdate.hpp>
|
||||
#include <components/openmw-mp/Master/PacketMasterQuery.hpp>
|
||||
#include <components/openmw-mp/NetworkMessages.hpp>
|
||||
|
||||
using namespace std;
|
||||
using namespace RakNet;
|
||||
using namespace mwmp;
|
||||
|
||||
int main()
|
||||
{
|
||||
cout << "Server test" << endl;
|
||||
|
||||
SystemAddress masterAddr("127.0.0.1", 25560);
|
||||
|
||||
RakPeerInterface *peer = RakNet::RakPeerInterface::GetInstance();
|
||||
|
||||
RakNet::SocketDescriptor sd(25565, 0);
|
||||
peer->Startup(8, &sd, 1);
|
||||
|
||||
ConnectionAttemptResult result = peer->Connect(masterAddr.ToString(false), masterAddr.GetPort(), "pass",
|
||||
(int)(strlen("pass")), 0, 0, 5, 500);
|
||||
|
||||
assert(result == RakNet::CONNECTION_ATTEMPT_STARTED);
|
||||
|
||||
char message[2048];
|
||||
BitStream send;
|
||||
|
||||
PacketMasterQuery pmq(peer);
|
||||
pmq.SetSendStream(&send);
|
||||
|
||||
PacketMasterAnnounce pma(peer);
|
||||
pma.SetSendStream(&send);
|
||||
|
||||
while (true)
|
||||
{
|
||||
RakSleep(30);
|
||||
|
||||
if (kbhit())
|
||||
{
|
||||
Gets(message, sizeof(message));
|
||||
|
||||
if (strcmp(message, "quit") == 0)
|
||||
{
|
||||
puts("Quitting.");
|
||||
break;
|
||||
}
|
||||
else if (strcmp(message, "send") == 0)
|
||||
{
|
||||
puts("Sending data about server");
|
||||
QueryData server;
|
||||
server.SetName("Super Server");
|
||||
server.SetPlayers(0);
|
||||
server.SetMaxPlayers(0);
|
||||
|
||||
pma.SetServer(&server);
|
||||
pma.SetFunc(PacketMasterAnnounce::FUNCTION_ANNOUNCE);
|
||||
pma.Send(masterAddr);
|
||||
}
|
||||
else if (strcmp(message, "get") == 0)
|
||||
{
|
||||
puts("Request query info");
|
||||
send.Reset();
|
||||
send.Write((unsigned char) (ID_MASTER_QUERY));
|
||||
peer->Send(&send, HIGH_PRIORITY, RELIABLE_ORDERED, CHANNEL_MASTER, masterAddr, false);
|
||||
}
|
||||
else if (strcmp(message, "getme") == 0)
|
||||
{
|
||||
send.Reset();
|
||||
send.Write((unsigned char) (ID_MASTER_UPDATE));
|
||||
send.Write(SystemAddress("127.0.0.1", 25565));
|
||||
peer->Send(&send, HIGH_PRIORITY, RELIABLE_ORDERED, CHANNEL_MASTER, masterAddr, false);
|
||||
}
|
||||
else if (strcmp(message, "status") == 0)
|
||||
{
|
||||
cout << (peer->GetConnectionState(masterAddr) == IS_CONNECTED ? "Connected" : "Not connected") << endl;
|
||||
}
|
||||
else if (strcmp(message, "keep") == 0)
|
||||
{
|
||||
cout << "Sending keep alive" << endl;
|
||||
pma.SetFunc(PacketMasterAnnounce::FUNCTION_KEEP);
|
||||
pma.Send(masterAddr);
|
||||
}
|
||||
}
|
||||
|
||||
for (RakNet::Packet *packet = peer->Receive(); packet; peer->DeallocatePacket(packet), packet = peer->Receive())
|
||||
{
|
||||
BitStream data(packet->data, packet->length, false);
|
||||
unsigned char packetID;
|
||||
data.Read(packetID);
|
||||
switch (packetID)
|
||||
{
|
||||
case ID_DISCONNECTION_NOTIFICATION:
|
||||
// Connection lost normally
|
||||
printf("ID_DISCONNECTION_NOTIFICATION\n");
|
||||
break;
|
||||
case ID_ALREADY_CONNECTED:
|
||||
// Connection lost normally
|
||||
printf("ID_ALREADY_CONNECTED with guid %lu\n", packet->guid.g);
|
||||
break;
|
||||
case ID_INCOMPATIBLE_PROTOCOL_VERSION:
|
||||
printf("ID_INCOMPATIBLE_PROTOCOL_VERSION\n");
|
||||
break;
|
||||
case ID_REMOTE_DISCONNECTION_NOTIFICATION: // Server telling the clients of another client disconnecting gracefully. You can manually broadcast this in a peer to peer enviroment if you want.
|
||||
printf("ID_REMOTE_DISCONNECTION_NOTIFICATION\n");
|
||||
break;
|
||||
case ID_REMOTE_CONNECTION_LOST: // Server telling the clients of another client disconnecting forcefully. You can manually broadcast this in a peer to peer enviroment if you want.
|
||||
printf("ID_REMOTE_CONNECTION_LOST\n");
|
||||
break;
|
||||
case ID_REMOTE_NEW_INCOMING_CONNECTION: // Server telling the clients of another client connecting. You can manually broadcast this in a peer to peer enviroment if you want.
|
||||
printf("ID_REMOTE_NEW_INCOMING_CONNECTION\n");
|
||||
break;
|
||||
case ID_CONNECTION_BANNED: // Banned from this server
|
||||
printf("We are banned from this server.\n");
|
||||
break;
|
||||
case ID_CONNECTION_ATTEMPT_FAILED:
|
||||
printf("Connection attempt failed\n");
|
||||
break;
|
||||
case ID_NO_FREE_INCOMING_CONNECTIONS:
|
||||
// Sorry, the server is full. I don't do anything here but
|
||||
// A real app should tell the user
|
||||
printf("ID_NO_FREE_INCOMING_CONNECTIONS\n");
|
||||
break;
|
||||
|
||||
case ID_INVALID_PASSWORD:
|
||||
printf("ID_INVALID_PASSWORD\n");
|
||||
break;
|
||||
|
||||
case ID_CONNECTION_LOST:
|
||||
// Couldn't deliver a reliable packet - i.e. the other system was abnormally
|
||||
// terminated
|
||||
printf("ID_CONNECTION_LOST\n");
|
||||
return 0;
|
||||
break;
|
||||
|
||||
case ID_CONNECTION_REQUEST_ACCEPTED:
|
||||
// This tells the client they have connected
|
||||
printf("ID_CONNECTION_REQUEST_ACCEPTED to %s with GUID %s\n", packet->systemAddress.ToString(true),
|
||||
packet->guid.ToString());
|
||||
printf("My external address is %s\n", peer->GetExternalID(packet->systemAddress).ToString(true));
|
||||
break;
|
||||
case ID_MASTER_QUERY:
|
||||
{
|
||||
map<SystemAddress, QueryData> servers;
|
||||
|
||||
pmq.SetReadStream(&data);
|
||||
pmq.SetServers(&servers);
|
||||
pmq.Read();
|
||||
|
||||
cout << "Received query data about " << servers.size() << " servers" << endl;
|
||||
|
||||
for (auto serv : servers)
|
||||
cout << serv.second.GetName() << endl;
|
||||
|
||||
break;
|
||||
}
|
||||
case ID_MASTER_UPDATE:
|
||||
{
|
||||
pair<SystemAddress, QueryData> serverPair;
|
||||
PacketMasterUpdate pmu(peer);
|
||||
pmu.SetReadStream(&data);
|
||||
pmu.SetServer(&serverPair);
|
||||
pmu.Read();
|
||||
cout << "Received info about " << serverPair.first.ToString() << endl;
|
||||
cout << serverPair.second.GetName() << endl;
|
||||
break;
|
||||
}
|
||||
default:
|
||||
cout << "Wrong packet" << endl;
|
||||
}
|
||||
}
|
||||
}
|
||||
peer->Shutdown(1000);
|
||||
RakPeerInterface::DestroyInstance(peer);
|
||||
}
|
@ -1,511 +0,0 @@
|
||||
#ifndef BASE_SERVER_HPP
|
||||
#define BASE_SERVER_HPP
|
||||
|
||||
#include <boost/asio.hpp>
|
||||
#include <boost/algorithm/string/predicate.hpp>
|
||||
#include <boost/functional/hash.hpp>
|
||||
|
||||
#include <map>
|
||||
#include <unordered_map>
|
||||
#include <thread>
|
||||
#include <functional>
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include <regex>
|
||||
|
||||
#ifndef CASE_INSENSITIVE_EQUALS_AND_HASH
|
||||
#define CASE_INSENSITIVE_EQUALS_AND_HASH
|
||||
|
||||
//Based on http://www.boost.org/doc/libs/1_60_0/doc/html/unordered/hash_equality.html
|
||||
struct case_insensitive_equals
|
||||
{
|
||||
bool operator()(const std::string &key1, const std::string &key2) const
|
||||
{
|
||||
return boost::algorithm::iequals(key1, key2);
|
||||
}
|
||||
};
|
||||
|
||||
struct case_insensitive_hash
|
||||
{
|
||||
size_t operator()(const std::string &key) const
|
||||
{
|
||||
std::size_t seed = 0;
|
||||
for (auto &c: key)
|
||||
boost::hash_combine(seed, std::tolower(c));
|
||||
return seed;
|
||||
}
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
namespace SimpleWeb
|
||||
{
|
||||
template<class socket_type>
|
||||
class Server;
|
||||
|
||||
template<class socket_type>
|
||||
class ServerBase
|
||||
{
|
||||
public:
|
||||
virtual ~ServerBase()
|
||||
{}
|
||||
|
||||
class Response : public std::ostream
|
||||
{
|
||||
friend class ServerBase<socket_type>;
|
||||
|
||||
boost::asio::streambuf streambuf;
|
||||
|
||||
std::shared_ptr<socket_type> socket;
|
||||
|
||||
Response(const std::shared_ptr<socket_type> &socket) : std::ostream(&streambuf), socket(socket)
|
||||
{}
|
||||
|
||||
public:
|
||||
size_t size()
|
||||
{
|
||||
return streambuf.size();
|
||||
}
|
||||
|
||||
/// If true, force server to close the connection after the response have been sent.
|
||||
///
|
||||
/// This is useful when implementing a HTTP/1.0-server sending content
|
||||
/// without specifying the content length.
|
||||
bool close_connection_after_response = false;
|
||||
};
|
||||
|
||||
class Content : public std::istream
|
||||
{
|
||||
friend class ServerBase<socket_type>;
|
||||
|
||||
public:
|
||||
size_t size()
|
||||
{
|
||||
return streambuf.size();
|
||||
}
|
||||
|
||||
std::string string()
|
||||
{
|
||||
std::stringstream ss;
|
||||
ss << rdbuf();
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
private:
|
||||
boost::asio::streambuf &streambuf;
|
||||
|
||||
Content(boost::asio::streambuf &streambuf) : std::istream(&streambuf), streambuf(streambuf)
|
||||
{}
|
||||
};
|
||||
|
||||
class Request
|
||||
{
|
||||
friend class ServerBase<socket_type>;
|
||||
|
||||
friend class Server<socket_type>;
|
||||
|
||||
public:
|
||||
std::string method, path, http_version;
|
||||
|
||||
Content content;
|
||||
|
||||
std::unordered_multimap<std::string, std::string, case_insensitive_hash, case_insensitive_equals> header;
|
||||
|
||||
std::smatch path_match;
|
||||
|
||||
std::string remote_endpoint_address;
|
||||
unsigned short remote_endpoint_port;
|
||||
|
||||
private:
|
||||
Request(const socket_type &socket) : content(streambuf)
|
||||
{
|
||||
try
|
||||
{
|
||||
remote_endpoint_address = socket.lowest_layer().remote_endpoint().address().to_string();
|
||||
remote_endpoint_port = socket.lowest_layer().remote_endpoint().port();
|
||||
}
|
||||
catch (...)
|
||||
{}
|
||||
}
|
||||
|
||||
boost::asio::streambuf streambuf;
|
||||
};
|
||||
|
||||
class Config
|
||||
{
|
||||
friend class ServerBase<socket_type>;
|
||||
|
||||
Config(unsigned short port) : port(port)
|
||||
{}
|
||||
|
||||
public:
|
||||
/// Port number to use. Defaults to 80 for HTTP and 443 for HTTPS.
|
||||
unsigned short port;
|
||||
/// Number of threads that the server will use when start() is called. Defaults to 1 thread.
|
||||
size_t thread_pool_size = 1;
|
||||
/// Timeout on request handling. Defaults to 5 seconds.
|
||||
size_t timeout_request = 5;
|
||||
/// Timeout on content handling. Defaults to 300 seconds.
|
||||
size_t timeout_content = 300;
|
||||
/// IPv4 address in dotted decimal form or IPv6 address in hexadecimal notation.
|
||||
/// If empty, the address will be any address.
|
||||
std::string address;
|
||||
/// Set to false to avoid binding the socket to an address that is already in use. Defaults to true.
|
||||
bool reuse_address = true;
|
||||
};
|
||||
|
||||
///Set before calling start().
|
||||
Config config;
|
||||
|
||||
private:
|
||||
class regex_orderable : public std::regex
|
||||
{
|
||||
std::string str;
|
||||
public:
|
||||
regex_orderable(const char *regex_cstr) : std::regex(regex_cstr), str(regex_cstr)
|
||||
{}
|
||||
|
||||
regex_orderable(const std::string ®ex_str) : std::regex(regex_str), str(regex_str)
|
||||
{}
|
||||
|
||||
bool operator<(const regex_orderable &rhs) const
|
||||
{
|
||||
return str < rhs.str;
|
||||
}
|
||||
};
|
||||
|
||||
public:
|
||||
/// Warning: do not add or remove resources after start() is called
|
||||
std::map<regex_orderable, std::map<std::string,
|
||||
std::function<void(std::shared_ptr<typename ServerBase<socket_type>::Response>,
|
||||
std::shared_ptr<typename ServerBase<socket_type>::Request>)>>>
|
||||
resource;
|
||||
|
||||
std::map<std::string,
|
||||
std::function<void(std::shared_ptr<typename ServerBase<socket_type>::Response>,
|
||||
std::shared_ptr<typename ServerBase<socket_type>::Request>)>> default_resource;
|
||||
|
||||
std::function<
|
||||
void(std::shared_ptr<typename ServerBase<socket_type>::Request>,
|
||||
const boost::system::error_code &)>
|
||||
on_error;
|
||||
|
||||
std::function<void(std::shared_ptr<socket_type> socket,
|
||||
std::shared_ptr<typename ServerBase<socket_type>::Request>)> on_upgrade;
|
||||
|
||||
virtual void start()
|
||||
{
|
||||
if (!io_service)
|
||||
io_service = std::make_shared<boost::asio::io_service>();
|
||||
|
||||
if (io_service->stopped())
|
||||
io_service->reset();
|
||||
|
||||
boost::asio::ip::tcp::endpoint endpoint;
|
||||
if (config.address.size() > 0)
|
||||
endpoint = boost::asio::ip::tcp::endpoint(boost::asio::ip::address::from_string(config.address),
|
||||
config.port);
|
||||
else
|
||||
endpoint = boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), config.port);
|
||||
|
||||
if (!acceptor)
|
||||
acceptor = std::unique_ptr<boost::asio::ip::tcp::acceptor>(
|
||||
new boost::asio::ip::tcp::acceptor(*io_service));
|
||||
acceptor->open(endpoint.protocol());
|
||||
acceptor->set_option(boost::asio::socket_base::reuse_address(config.reuse_address));
|
||||
acceptor->bind(endpoint);
|
||||
acceptor->listen();
|
||||
|
||||
accept();
|
||||
|
||||
//If thread_pool_size>1, start m_io_service.run() in (thread_pool_size-1) threads for thread-pooling
|
||||
threads.clear();
|
||||
for (size_t c = 1; c < config.thread_pool_size; c++)
|
||||
{
|
||||
threads.emplace_back([this]()
|
||||
{
|
||||
io_service->run();
|
||||
});
|
||||
}
|
||||
|
||||
//Main thread
|
||||
if (config.thread_pool_size > 0)
|
||||
io_service->run();
|
||||
|
||||
//Wait for the rest of the threads, if any, to finish as well
|
||||
for (auto &t: threads)
|
||||
{
|
||||
t.join();
|
||||
}
|
||||
}
|
||||
|
||||
void stop()
|
||||
{
|
||||
acceptor->close();
|
||||
if (config.thread_pool_size > 0)
|
||||
io_service->stop();
|
||||
}
|
||||
|
||||
///Use this function if you need to recursively send parts of a longer message
|
||||
void send(const std::shared_ptr<Response> &response,
|
||||
const std::function<void(const boost::system::error_code &)> &callback = nullptr) const
|
||||
{
|
||||
boost::asio::async_write(*response->socket, response->streambuf, [this, response, callback]
|
||||
(const boost::system::error_code &ec, size_t /*bytes_transferred*/)
|
||||
{
|
||||
if (callback)
|
||||
callback(ec);
|
||||
});
|
||||
}
|
||||
|
||||
/// If you have your own boost::asio::io_service, store its pointer here before running start().
|
||||
/// You might also want to set config.thread_pool_size to 0.
|
||||
std::shared_ptr<boost::asio::io_service> io_service;
|
||||
protected:
|
||||
std::unique_ptr<boost::asio::ip::tcp::acceptor> acceptor;
|
||||
std::vector<std::thread> threads;
|
||||
|
||||
ServerBase(unsigned short port) : config(port)
|
||||
{}
|
||||
|
||||
virtual void accept()=0;
|
||||
|
||||
std::shared_ptr<boost::asio::deadline_timer>
|
||||
get_timeout_timer(const std::shared_ptr<socket_type> &socket, long seconds)
|
||||
{
|
||||
if (seconds == 0)
|
||||
return nullptr;
|
||||
|
||||
auto timer = std::make_shared<boost::asio::deadline_timer>(*io_service);
|
||||
timer->expires_from_now(boost::posix_time::seconds(seconds));
|
||||
timer->async_wait([socket](const boost::system::error_code &ec)
|
||||
{
|
||||
if (!ec)
|
||||
{
|
||||
boost::system::error_code ec;
|
||||
socket->lowest_layer().shutdown(boost::asio::ip::tcp::socket::shutdown_both, ec);
|
||||
socket->lowest_layer().close();
|
||||
}
|
||||
});
|
||||
return timer;
|
||||
}
|
||||
|
||||
void read_request_and_content(const std::shared_ptr<socket_type> &socket)
|
||||
{
|
||||
//Create new streambuf (Request::streambuf) for async_read_until()
|
||||
//shared_ptr is used to pass temporary objects to the asynchronous functions
|
||||
std::shared_ptr<Request> request(new Request(*socket));
|
||||
|
||||
//Set timeout on the following boost::asio::async-read or write function
|
||||
auto timer = this->get_timeout_timer(socket, config.timeout_request);
|
||||
|
||||
boost::asio::async_read_until(*socket, request->streambuf, "\r\n\r\n", [this, socket, request, timer]
|
||||
(const boost::system::error_code &ec,
|
||||
size_t bytes_transferred)
|
||||
{
|
||||
if (timer)
|
||||
timer->cancel();
|
||||
if (!ec)
|
||||
{
|
||||
//request->streambuf.size() is not necessarily the same as bytes_transferred, from Boost-docs:
|
||||
//"After a successful async_read_until operation, the streambuf may contain additional data beyond the delimiter"
|
||||
//The chosen solution is to extract lines from the stream directly when parsing the header. What is left of the
|
||||
//streambuf (maybe some bytes of the content) is appended to in the async_read-function below (for retrieving content).
|
||||
size_t num_additional_bytes =
|
||||
request->streambuf.size() - bytes_transferred;
|
||||
|
||||
if (!this->parse_request(request))
|
||||
return;
|
||||
|
||||
//If content, read that as well
|
||||
auto it = request->header.find("Content-Length");
|
||||
if (it != request->header.end())
|
||||
{
|
||||
unsigned long long content_length;
|
||||
try
|
||||
{
|
||||
content_length = stoull(it->second);
|
||||
}
|
||||
catch (const std::exception &e)
|
||||
{
|
||||
if (on_error)
|
||||
on_error(request, boost::system::error_code(
|
||||
boost::system::errc::protocol_error,
|
||||
boost::system::generic_category()));
|
||||
return;
|
||||
}
|
||||
if (content_length > num_additional_bytes)
|
||||
{
|
||||
//Set timeout on the following boost::asio::async-read or write function
|
||||
auto timer = this->get_timeout_timer(socket,
|
||||
config.timeout_content);
|
||||
boost::asio::async_read(*socket, request->streambuf,
|
||||
boost::asio::transfer_exactly(
|
||||
content_length -
|
||||
num_additional_bytes),
|
||||
[this, socket, request, timer]
|
||||
(const boost::system::error_code &ec,
|
||||
size_t /*bytes_transferred*/)
|
||||
{
|
||||
if (timer)
|
||||
timer->cancel();
|
||||
if (!ec)
|
||||
this->find_resource(socket,
|
||||
request);
|
||||
else if (on_error)
|
||||
on_error(request, ec);
|
||||
});
|
||||
}
|
||||
else
|
||||
this->find_resource(socket, request);
|
||||
}
|
||||
else
|
||||
this->find_resource(socket, request);
|
||||
}
|
||||
else if (on_error)
|
||||
on_error(request, ec);
|
||||
});
|
||||
}
|
||||
|
||||
bool parse_request(const std::shared_ptr<Request> &request) const
|
||||
{
|
||||
std::string line;
|
||||
getline(request->content, line);
|
||||
size_t method_end;
|
||||
if ((method_end = line.find(' ')) != std::string::npos)
|
||||
{
|
||||
size_t path_end;
|
||||
if ((path_end = line.find(' ', method_end + 1)) != std::string::npos)
|
||||
{
|
||||
request->method = line.substr(0, method_end);
|
||||
request->path = line.substr(method_end + 1, path_end - method_end - 1);
|
||||
|
||||
size_t protocol_end;
|
||||
if ((protocol_end = line.find('/', path_end + 1)) != std::string::npos)
|
||||
{
|
||||
if (line.compare(path_end + 1, protocol_end - path_end - 1, "HTTP") != 0)
|
||||
return false;
|
||||
request->http_version = line.substr(protocol_end + 1, line.size() - protocol_end - 2);
|
||||
}
|
||||
else
|
||||
return false;
|
||||
|
||||
getline(request->content, line);
|
||||
size_t param_end;
|
||||
while ((param_end = line.find(':')) != std::string::npos)
|
||||
{
|
||||
size_t value_start = param_end + 1;
|
||||
if ((value_start) < line.size())
|
||||
{
|
||||
if (line[value_start] == ' ')
|
||||
value_start++;
|
||||
if (value_start < line.size())
|
||||
request->header.emplace(line.substr(0, param_end),
|
||||
line.substr(value_start, line.size() - value_start - 1));
|
||||
}
|
||||
|
||||
getline(request->content, line);
|
||||
}
|
||||
}
|
||||
else
|
||||
return false;
|
||||
}
|
||||
else
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
void find_resource(const std::shared_ptr<socket_type> &socket, const std::shared_ptr<Request> &request)
|
||||
{
|
||||
//Upgrade connection
|
||||
if (on_upgrade)
|
||||
{
|
||||
auto it = request->header.find("Upgrade");
|
||||
if (it != request->header.end())
|
||||
{
|
||||
on_upgrade(socket, request);
|
||||
return;
|
||||
}
|
||||
}
|
||||
//Find path- and method-match, and call write_response
|
||||
for (auto ®ex_method: resource)
|
||||
{
|
||||
auto it = regex_method.second.find(request->method);
|
||||
if (it != regex_method.second.end())
|
||||
{
|
||||
std::smatch sm_res;
|
||||
if (std::regex_match(request->path, sm_res, regex_method.first))
|
||||
{
|
||||
request->path_match = std::move(sm_res);
|
||||
write_response(socket, request, it->second);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
auto it = default_resource.find(request->method);
|
||||
if (it != default_resource.end())
|
||||
{
|
||||
write_response(socket, request, it->second);
|
||||
}
|
||||
}
|
||||
|
||||
void write_response(const std::shared_ptr<socket_type> &socket, const std::shared_ptr<Request> &request,
|
||||
std::function<void(std::shared_ptr<typename ServerBase<socket_type>::Response>,
|
||||
std::shared_ptr<
|
||||
typename ServerBase<socket_type>::Request>)> &resource_function)
|
||||
{
|
||||
//Set timeout on the following boost::asio::async-read or write function
|
||||
auto timer = this->get_timeout_timer(socket, config.timeout_content);
|
||||
|
||||
auto response = std::shared_ptr<Response>(new Response(socket), [this, request, timer]
|
||||
(Response *response_ptr)
|
||||
{
|
||||
auto response = std::shared_ptr<Response>(response_ptr);
|
||||
this->send(response, [this, response, request, timer](
|
||||
const boost::system::error_code &ec)
|
||||
{
|
||||
if (timer)
|
||||
timer->cancel();
|
||||
if (!ec)
|
||||
{
|
||||
if (response->close_connection_after_response)
|
||||
return;
|
||||
|
||||
auto range = request->header.equal_range(
|
||||
"Connection");
|
||||
for (auto it = range.first; it != range.second; it++)
|
||||
{
|
||||
if (boost::iequals(it->second, "close"))
|
||||
{
|
||||
return;
|
||||
}
|
||||
else if (boost::iequals(it->second, "keep-alive"))
|
||||
{
|
||||
this->read_request_and_content(
|
||||
response->socket);
|
||||
return;
|
||||
}
|
||||
}
|
||||
if (request->http_version >= "1.1")
|
||||
this->read_request_and_content(response->socket);
|
||||
}
|
||||
else if (on_error)
|
||||
on_error(request, ec);
|
||||
});
|
||||
});
|
||||
|
||||
try
|
||||
{
|
||||
resource_function(response, request);
|
||||
}
|
||||
catch (const std::exception &e)
|
||||
{
|
||||
if (on_error)
|
||||
on_error(request, boost::system::error_code(boost::system::errc::operation_canceled,
|
||||
boost::system::generic_category()));
|
||||
return;
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
#endif //BASE_SERVER_HPP
|
@ -1,55 +0,0 @@
|
||||
/*
|
||||
* https://github.com/eidheim/Simple-Web-Server/
|
||||
*
|
||||
* The MIT License (MIT)
|
||||
* Copyright (c) 2014-2016 Ole Christian Eidheim
|
||||
*/
|
||||
|
||||
#ifndef SERVER_HTTP_HPP
|
||||
#define SERVER_HTTP_HPP
|
||||
|
||||
#include "base_server.hpp"
|
||||
|
||||
namespace SimpleWeb
|
||||
{
|
||||
|
||||
template<class socket_type>
|
||||
class Server : public ServerBase<socket_type> {};
|
||||
|
||||
typedef boost::asio::ip::tcp::socket HTTP;
|
||||
|
||||
template<>
|
||||
class Server<HTTP> : public ServerBase<HTTP>
|
||||
{
|
||||
public:
|
||||
Server() : ServerBase<HTTP>::ServerBase(80)
|
||||
{}
|
||||
|
||||
protected:
|
||||
virtual void accept()
|
||||
{
|
||||
//Create new socket for this connection
|
||||
//Shared_ptr is used to pass temporary objects to the asynchronous functions
|
||||
auto socket = std::make_shared<HTTP>(*io_service);
|
||||
|
||||
acceptor->async_accept(*socket, [this, socket](const boost::system::error_code &ec)
|
||||
{
|
||||
//Immediately start accepting a new connection (if io_service hasn't been stopped)
|
||||
if (ec != boost::asio::error::operation_aborted)
|
||||
accept();
|
||||
|
||||
if (!ec)
|
||||
{
|
||||
boost::asio::ip::tcp::no_delay option(true);
|
||||
socket->set_option(option);
|
||||
|
||||
this->read_request_and_content(socket);
|
||||
}
|
||||
else if (on_error)
|
||||
on_error(std::shared_ptr<Request>(new Request(*socket)), ec);
|
||||
});
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
#endif //SERVER_HTTP_HPP
|
@ -1,91 +0,0 @@
|
||||
#ifndef HTTPS_SERVER_HPP
|
||||
#define HTTPS_SERVER_HPP
|
||||
|
||||
#include "base_server.hpp"
|
||||
#include <boost/asio/ssl.hpp>
|
||||
#include <openssl/ssl.h>
|
||||
#include <algorithm>
|
||||
|
||||
namespace SimpleWeb
|
||||
{
|
||||
typedef boost::asio::ssl::stream<boost::asio::ip::tcp::socket> HTTPS;
|
||||
|
||||
template<>
|
||||
class Server<HTTPS> : public ServerBase<HTTPS>
|
||||
{
|
||||
std::string session_id_context;
|
||||
bool set_session_id_context = false;
|
||||
public:
|
||||
Server(const std::string &cert_file, const std::string &private_key_file,
|
||||
const std::string &verify_file = std::string()) : ServerBase<HTTPS>::ServerBase(443),
|
||||
context(boost::asio::ssl::context::tlsv12)
|
||||
{
|
||||
context.use_certificate_chain_file(cert_file);
|
||||
context.use_private_key_file(private_key_file, boost::asio::ssl::context::pem);
|
||||
|
||||
if (verify_file.size() > 0)
|
||||
{
|
||||
context.load_verify_file(verify_file);
|
||||
context.set_verify_mode(boost::asio::ssl::verify_peer | boost::asio::ssl::verify_fail_if_no_peer_cert |
|
||||
boost::asio::ssl::verify_client_once);
|
||||
set_session_id_context = true;
|
||||
}
|
||||
}
|
||||
|
||||
void start()
|
||||
{
|
||||
if (set_session_id_context)
|
||||
{
|
||||
// Creating session_id_context from address:port but reversed due to small SSL_MAX_SSL_SESSION_ID_LENGTH
|
||||
session_id_context = std::to_string(config.port) + ':';
|
||||
session_id_context.append(config.address.rbegin(), config.address.rend());
|
||||
SSL_CTX_set_session_id_context(context.native_handle(),
|
||||
reinterpret_cast<const unsigned char *>(session_id_context.data()),
|
||||
std::min<size_t>(session_id_context.size(),
|
||||
SSL_MAX_SSL_SESSION_ID_LENGTH));
|
||||
}
|
||||
ServerBase::start();
|
||||
}
|
||||
|
||||
protected:
|
||||
boost::asio::ssl::context context;
|
||||
|
||||
virtual void accept()
|
||||
{
|
||||
//Create new socket for this connection
|
||||
//Shared_ptr is used to pass temporary objects to the asynchronous functions
|
||||
auto socket = std::make_shared<HTTPS>(*io_service, context);
|
||||
|
||||
acceptor->async_accept((*socket).lowest_layer(), [this, socket](const boost::system::error_code &ec)
|
||||
{
|
||||
//Immediately start accepting a new connection (if io_service hasn't been stopped)
|
||||
if (ec != boost::asio::error::operation_aborted)
|
||||
accept();
|
||||
|
||||
|
||||
if (!ec)
|
||||
{
|
||||
boost::asio::ip::tcp::no_delay option(true);
|
||||
socket->lowest_layer().set_option(option);
|
||||
|
||||
//Set timeout on the following boost::asio::ssl::stream::async_handshake
|
||||
auto timer = get_timeout_timer(socket, config.timeout_request);
|
||||
socket->async_handshake(boost::asio::ssl::stream_base::server, [this, socket, timer]
|
||||
(const boost::system::error_code &ec)
|
||||
{
|
||||
if (timer)
|
||||
timer->cancel();
|
||||
if (!ec)
|
||||
read_request_and_content(socket);
|
||||
else if (on_error)
|
||||
on_error(std::shared_ptr<Request>(new Request(*socket)), ec);
|
||||
});
|
||||
}
|
||||
else if (on_error)
|
||||
on_error(std::shared_ptr<Request>(new Request(*socket)), ec);
|
||||
});
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
#endif //HTTPS_SERVER_HPP
|
@ -1,37 +0,0 @@
|
||||
#include <iostream>
|
||||
#include <Kbhit.h>
|
||||
#include <RakSleep.h>
|
||||
#include "MasterServer.hpp"
|
||||
#include "RestServer.hpp"
|
||||
|
||||
using namespace RakNet;
|
||||
using namespace std;
|
||||
|
||||
unique_ptr<RestServer> restServer;
|
||||
unique_ptr<MasterServer> masterServer;
|
||||
bool run = true;
|
||||
|
||||
int main()
|
||||
{
|
||||
masterServer.reset(new MasterServer(2000, 25560));
|
||||
restServer.reset(new RestServer(8080, masterServer->GetServers()));
|
||||
|
||||
auto onExit = [](int /*sig*/){
|
||||
restServer->stop();
|
||||
masterServer->Stop(false);
|
||||
masterServer->Wait();
|
||||
run = false;
|
||||
};
|
||||
|
||||
signal(SIGINT, onExit);
|
||||
signal(SIGTERM, onExit);
|
||||
|
||||
masterServer->Start();
|
||||
|
||||
thread server_thread([]() { restServer->start(); });
|
||||
|
||||
server_thread.join();
|
||||
masterServer->Wait();
|
||||
|
||||
return 0;
|
||||
}
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue