Consts and references

This commit is contained in:
Kilian Hofmann 2022-09-09 20:10:38 +02:00
parent b050c23577
commit 699e2a4784
16 changed files with 156 additions and 179 deletions

View File

@ -4,6 +4,7 @@
#include <cstdint>
#include <fstream>
#include <string>
#include <utility>
#include <vector>
#include "constants.h"
@ -24,15 +25,19 @@ namespace germanairlinesva_logbook
class Logbook
{
private:
std::vector<std::uint8_t> file;
std::vector<LogbookEntry> entries;
void fromFile(const std::string &file);
void readVersion1(std::ifstream &in);
public:
Logbook();
void addEntry(LogbookEntry entry);
void toFile();
template <class... Args> inline void addEntry(Args &&...args)
{
this->entries.emplace_back(std::forward<Args>(args)...);
}
void toFile() const;
};
} // namespace germanairlinesva_logbook

View File

@ -71,14 +71,14 @@ namespace germanairlinesva_logbook
* STRLEN | STRING
* Postamble (5)
* FLOAT32 | BITFIELD
* --------------+---
* POINTS | FLAGS
* FLOAT | BITFIELD
* -------+---------
* POINTS | FLAGS
* Flags Bitfield
* 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0
* ---+---+---+---+---+---+---+-----
* NIL|NIL|NIL|NIL|NIL|NIL|NIL|FILED
* 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0
* ----+-----+-----+-----+-----+-----+-----+------
* NIL | NIL | NIL | NIL | NIL | NIL | NIL | FILED
*/
class LogbookEntry
{
@ -145,21 +145,8 @@ namespace germanairlinesva_logbook
float points,
std::uint8_t flags);
inline std::uint8_t *getBinaryData() { return file.data(); }
inline std::size_t getBinaryLength() { return file.size(); }
std::string to_string() const
{
std::ostringstream str;
// TO STR
return str.str();
}
friend inline std::ostream &operator<<(std::ostream &os,
const LogbookEntry &gate)
{
return os << gate.to_string();
}
inline const std::uint8_t *getBinaryData() const { return file.data(); }
inline std::size_t getBinaryLength() const { return file.size(); }
};
} // namespace germanairlinesva_logbook

View File

@ -28,10 +28,10 @@ namespace germanairlinesva_recording
std::vector<std::uint8_t> file{'V', 'G', 'A', 'R', '\0', 1};
public:
void addSegment(PathSegment segment);
void addSegment(const PathSegment &segment);
inline std::uint8_t *getBinaryData() { return file.data(); }
inline std::size_t getBinaryLength() { return file.size(); }
inline const std::uint8_t *getBinaryData() const { return file.data(); }
inline std::size_t getBinaryLength() const { return file.size(); }
};
} // namespace germanairlinesva_recording

View File

@ -31,8 +31,8 @@ namespace germanairlinesva_recording
std::uint16_t groundSpeed,
struct germanairlinesva_geodata::point coordinates);
inline std::uint8_t *getBinaryData() { return file.data(); }
inline std::size_t getBinaryLength() { return file.size(); }
inline const std::uint8_t *getBinaryData() const { return file.data(); }
inline std::size_t getBinaryLength() const { return file.size(); }
friend inline bool operator==(const PathSegment &lhs,
const PathSegment &rhs)

View File

@ -4,13 +4,8 @@ namespace germanairlinesva_logbook
{
Logbook::Logbook()
{
if (germanairlinesva_util::fileExists(
XPLANE_PLUGIN_DIRECTORY LOGBOOK)) {
if (germanairlinesva_util::fileExists(XPLANE_PLUGIN_DIRECTORY LOGBOOK)) {
this->fromFile(XPLANE_PLUGIN_DIRECTORY LOGBOOK);
} else {
char header[] = {'V', 'G', 'A', 'L', '\0', 1};
this->file.resize(6);
std::memcpy(file.data(), &header, 6);
}
}
@ -32,30 +27,18 @@ namespace germanairlinesva_logbook
}
}
void Logbook::readVersion1(std::ifstream &in)
{
in.seekg(0, std::fstream::end);
std::streampos fileSize = in.tellg();
in.seekg(0, std::ios::beg);
void Logbook::readVersion1(std::ifstream &in) {}
this->file.resize(fileSize);
in.read(reinterpret_cast<char *>(this->file.data()), fileSize);
in.close();
}
void Logbook::addEntry(LogbookEntry entry)
void Logbook::toFile() const
{
std::size_t size = file.size();
file.resize(file.size() + entry.getBinaryLength());
std::uint8_t *bufPtr = file.data() + size;
std::memcpy(bufPtr, entry.getBinaryData(), entry.getBinaryLength());
}
void Logbook::toFile()
{
std::ofstream out(XPLANE_PLUGIN_DIRECTORY LOGBOOK,
std::fstream::binary);
out.write(reinterpret_cast<char *>(this->file.data()), this->file.size());
std::ofstream out(XPLANE_PLUGIN_DIRECTORY LOGBOOK, std::fstream::binary);
char header[] = {'V', 'G', 'A', 'L', '\0', 1};
out.write(header, 6);
for (const LogbookEntry &entry : this->entries) {
out.write(reinterpret_cast<const char *>(entry.getBinaryData()),
entry.getBinaryLength());
}
out.close();
}
} // namespace germanairlinesva_logbook

View File

@ -2,7 +2,7 @@
namespace germanairlinesva_recording
{
void PathRecording::addSegment(PathSegment segment)
void PathRecording::addSegment(const PathSegment &segment)
{
file.resize(file.size() + segment.getBinaryLength());
std::uint8_t *bufPtr = 6 + file.data() + count * segment.getBinaryLength();

View File

@ -28,7 +28,7 @@ namespace germanairlinesva_simdata
// From database
Gate::Gate(std::string designator,
germanairlinesva_geodata::point center,
struct germanairlinesva_geodata::point center,
std::uint8_t radius)
{
this->designator = designator;

View File

@ -29,7 +29,7 @@ namespace germanairlinesva_simdata
{
private:
std::string designator;
germanairlinesva_geodata::point center;
struct germanairlinesva_geodata::point center;
std::uint8_t radius;
std::vector<std::uint8_t> file;
@ -41,13 +41,13 @@ namespace germanairlinesva_simdata
std::uint8_t radius);
// From database
Gate(std::string designator,
germanairlinesva_geodata::point center,
struct germanairlinesva_geodata::point center,
std::uint8_t radius);
inline std::uint8_t *getBinaryData() { return file.data(); }
inline std::size_t getBinaryLength() { return file.size(); }
inline const std::uint8_t *getBinaryData() const { return file.data(); }
inline std::size_t getBinaryLength() const { return file.size(); }
std::string to_string() const
inline const std::string to_string() const
{
std::ostringstream str;
str << "Gate " << this->designator << " at " << this->center.latitude

View File

@ -49,10 +49,10 @@ namespace germanairlinesva_simdata
std::uint16_t length,
std::uint16_t trueHeading);
inline std::uint8_t *getBinaryData() { return file.data(); }
inline std::size_t getBinaryLength() { return file.size(); }
inline const std::uint8_t *getBinaryData() const { return file.data(); }
inline std::size_t getBinaryLength() const { return file.size(); }
std::string to_string() const
inline const std::string to_string() const
{
std::ostringstream str;
str << "Runway " << this->designator << " with bounds "

View File

@ -12,9 +12,9 @@
namespace germanairlinesva_simdata
{
int scan(
const char *defaultFile,
const char *sceneryPack,
const char *logFile,
const std::string defaultFile,
const std::string sceneryPack,
const std::string logFile,
std::map<std::string, std::pair<std::vector<Gate>, std::vector<Runway>>>
&airports);
@ -24,10 +24,12 @@ namespace germanairlinesva_simdata
std::map<std::string, std::pair<std::vector<Gate>, std::vector<Runway>>>
&airports,
std::ofstream &logfile);
void makeGate15(std::vector<Gate> *gates, std::vector<std::string> fields);
void makeRunway(std::vector<Runway> *runways,
std::vector<std::string> fields);
void makeGate1300(std::vector<Gate> *gates, std::vector<std::string> fields);
void makeGate15(std::vector<Gate> &gates,
const std::vector<std::string> &fields);
void makeRunway(std::vector<Runway> &runways,
const std::vector<std::string> &fields);
void makeGate1300(std::vector<Gate> &gates,
const std::vector<std::string> &fields);
} // namespace germanairlinesva_simdata

View File

@ -113,7 +113,7 @@ namespace germanairlinesva_simdata
static_cast<char *>(calloc(designatorLength + 1, sizeof(char)));
in.read(designator, designatorLength + 1);
// Center
germanairlinesva_geodata::point center;
struct germanairlinesva_geodata::point center;
in.read(reinterpret_cast<char *>(&center), sizeof(center));
// Radius
std::uint8_t radius;
@ -133,7 +133,7 @@ namespace germanairlinesva_simdata
static_cast<char *>(calloc(designatorLength + 1, sizeof(char)));
in.read(designator, designatorLength + 1);
// Bounds
germanairlinesva_geodata::box bounds;
struct germanairlinesva_geodata::box bounds;
in.read(reinterpret_cast<char *>(&bounds), sizeof(bounds));
// Width
std::uint8_t width;

View File

@ -3,9 +3,9 @@
namespace germanairlinesva_simdata
{
int scan(
const char *defaultFile,
const char *sceneryPack,
const char *logFile,
const std::string defaultFile,
const std::string sceneryPack,
const std::string logFile,
std::map<std::string, std::pair<std::vector<Gate>, std::vector<Runway>>>
&airports)
{
@ -38,7 +38,7 @@ namespace germanairlinesva_simdata
std::string path =
germanairlinesva_util::rtrim_copy(line.substr(pos + 13)) +
"Earth nav data/apt.dat";
packs.push_back(path);
packs.emplace_back(path);
}
}
std::reverse(packs.begin(), packs.end());
@ -104,7 +104,7 @@ namespace germanairlinesva_simdata
apCount += 1;
logfile << "\t<" << kind << "> " << line << std::endl;
} else if (currentIcao != nullptr && fields[0] == "15") {
makeGate15(&tmpGates, fields);
makeGate15(tmpGates, fields);
logfile << "\t\t<GATE OLD> " << line << std::endl;
} else if (fields[0] == "16" || fields[0] == "17") {
// Write to file if ICAO is valid, and we have gates and runways
@ -122,10 +122,10 @@ namespace germanairlinesva_simdata
currentIcao = nullptr;
logfile << "\t<" << kind << " SKIPPED> " << line << std::endl;
} else if (currentIcao != nullptr && fields[0] == "100") {
makeRunway(&tmpRunways, fields);
makeRunway(tmpRunways, fields);
logfile << "\t\t<RUNWAY> " << line << std::endl;
} else if (currentIcao != nullptr && fields[0] == "1300") {
makeGate1300(&tmpGates, fields);
makeGate1300(tmpGates, fields);
logfile << "\t\t<GATE> " << line << std::endl;
}
}
@ -139,7 +139,8 @@ namespace germanairlinesva_simdata
<< validCount << " are valid" << std::endl;
}
void makeGate15(std::vector<Gate> *gates, std::vector<std::string> fields)
void makeGate15(std::vector<Gate> &gates,
const std::vector<std::string> &fields)
{
std::string gateName;
for (size_t j = 4; j < fields.size() - 1; j++) {
@ -148,27 +149,31 @@ namespace germanairlinesva_simdata
gateName += fields.back();
gateName = std::regex_replace(gateName, std::regex{","}, "0");
gates->push_back(
Gate{gateName, std::stod(fields[1]), std::stod(fields[2]), 40});
gates.emplace_back(gateName,
std::stod(fields[1]),
std::stod(fields[2]),
40);
}
void makeRunway(std::vector<Runway> *runways, std::vector<std::string> fields)
void makeRunway(std::vector<Runway> &runways,
const std::vector<std::string> &fields)
{
runways->push_back(Runway{fields[8],
std::stod(fields[9]),
std::stod(fields[10]),
std::stod(fields[18]),
std::stod(fields[19]),
std::stod(fields[1])});
runways->push_back(Runway{fields[17],
std::stod(fields[18]),
std::stod(fields[19]),
std::stod(fields[9]),
std::stod(fields[10]),
std::stod(fields[1])});
runways.emplace_back(fields[8],
std::stod(fields[9]),
std::stod(fields[10]),
std::stod(fields[18]),
std::stod(fields[19]),
std::stod(fields[1]));
runways.emplace_back(fields[17],
std::stod(fields[18]),
std::stod(fields[19]),
std::stod(fields[9]),
std::stod(fields[10]),
std::stod(fields[1]));
}
void makeGate1300(std::vector<Gate> *gates, std::vector<std::string> fields)
void makeGate1300(std::vector<Gate> &gates,
const std::vector<std::string> &fields)
{
std::string gateName;
for (size_t j = 6; j < fields.size() - 1; j++) {
@ -177,7 +182,9 @@ namespace germanairlinesva_simdata
gateName += fields.back();
gateName = std::regex_replace(gateName, std::regex{","}, "0");
gates->push_back(
Gate{gateName, std::stod(fields[1]), std::stod(fields[2]), 40});
gates.emplace_back(gateName,
std::stod(fields[1]),
std::stod(fields[2]),
40);
}
} // namespace germanairlinesva_simdata

View File

@ -39,7 +39,6 @@
#include <vector>
namespace germanairlinesva_util
{
template <typename T>
@ -293,7 +292,7 @@ namespace germanairlinesva_util
std::string item;
while (getline(ss, item, delim)) {
result.push_back(item);
result.emplace_back(item);
}
return result;

View File

@ -3,12 +3,14 @@
#include <cstdint>
#pragma pack(push) /* push current alignment to stack */
#pragma pack(1) /* set alignment to 1 byte boundary */
namespace germanairlinesva_websocket
{
#pragma pack(push) /* push current alignment to stack */
#pragma pack(1) /* set alignment to 1 byte boundary */
/* Structures and enums */
typedef struct data {
struct data {
std::int32_t pause = 0;
float pBrake = 0;
std::int32_t onGrnd = 0;
@ -27,30 +29,19 @@ namespace germanairlinesva_websocket
float magHeading = 0;
float payloadKg = 0;
float totalWeightKg = 0;
} data;
};
typedef enum commands {
PROCESS,
SAVE,
LOAD,
TEXT,
TIME,
UNPAUSE,
PAUSE,
PORT,
END
} commands;
enum commands { PROCESS, SAVE, LOAD, TEXT, TIME, UNPAUSE, PAUSE, PORT, END };
typedef struct command_base {
commands type;
} command_base;
struct command_base {
enum commands type;
};
typedef struct command_port {
struct command_port {
double latitude;
double longitude;
float trueHeading;
} command_port;
};
#pragma pack(pop) /* restore original alignment from stack */
} // namespace germanairlinesva_websocket

View File

@ -12,19 +12,20 @@ namespace germanairlinesva_websocket
ix::initNetSystem();
#endif
webSocket = new ix::WebSocket();
webSocket->enableAutomaticReconnection();
webSocket->setUrl(host);
webSocket->setOnMessageCallback([this](const ix::WebSocketMessagePtr &msg) {
this->onClientMessageCallback(msg);
});
webSocket->start();
this->webSocket = new ix::WebSocket();
this->webSocket->enableAutomaticReconnection();
this->webSocket->setUrl(host);
this->webSocket->setOnMessageCallback(
[this](const ix::WebSocketMessagePtr &msg) {
this->onClientMessageCallback(msg);
});
this->webSocket->start();
}
Websocket::~Websocket()
{
webSocket->stop();
toLog("WebSocket stopped");
this->webSocket->stop();
this->toLog("WebSocket stopped");
#ifdef IBM
// Required on Windows
ix::uninitNetSystem();
@ -43,15 +44,15 @@ namespace germanairlinesva_websocket
debug_msg << it.first << ": " << it.second << std::endl;
}
toLog(debug_msg.str());
this->toLog(debug_msg.str());
webSocket->send("MASTER:" + user);
toLog("Connecting as " + user);
this->webSocket->send("MASTER:" + user);
this->toLog("Connecting as " + user);
} else if (msg->type == ix::WebSocketMessageType::Close) {
if (msg->closeInfo.reason.compare("DUPLICATE")) {
webSocket->disableAutomaticReconnection();
this->webSocket->disableAutomaticReconnection();
toLog("Disconnected due to beeing a duplicate simualtor");
this->toLog("Disconnected due to beeing a duplicate simualtor");
} else {
std::stringstream debug_msg;
@ -60,7 +61,7 @@ namespace germanairlinesva_websocket
debug_msg << "Reason: " << msg->closeInfo.reason << std::endl;
debug_msg << "Remote: " << msg->closeInfo.remote << std::endl;
toLog(debug_msg.str());
this->toLog(debug_msg.str());
}
} else if (msg->type == ix::WebSocketMessageType::Error) {
std::stringstream debug_msg;
@ -73,20 +74,22 @@ namespace germanairlinesva_websocket
debug_msg << "Retries: " << msg->errorInfo.retries << std::endl;
debug_msg << "Wait time: " << msg->errorInfo.wait_time << std::endl;
toLog(debug_msg.str());
this->toLog(debug_msg.str());
} else if (msg->type == ix::WebSocketMessageType::Message) {
if (!msg->str.empty()) {
toLog(msg->str);
this->toLog(msg->str);
}
}
}
void Websocket::sendData(data &d)
{
if (strcmp(d.path, lastPath) != 0) {
strcpy(lastPath, d.path);
if (germanairlinesva_util::generateMD5(d.path, lastHash, toLog)) {
strcpy(lastHash, "NOT SET");
if (strcmp(d.path, this->lastPath) != 0) {
strcpy(this->lastPath, d.path);
if (germanairlinesva_util::generateMD5(d.path,
this->lastHash,
this->toLog)) {
strcpy(this->lastHash, "NOT SET");
}
}
@ -98,13 +101,13 @@ namespace germanairlinesva_websocket
{"truHdg", d.truHdg},
{"totFuel", d.totFuelKg},
{"fuelFlow", d.ff},
{"hash", lastHash},
{"hash", this->lastHash},
};
if (webSocket != nullptr) {
if (this->webSocket != nullptr) {
std::ostringstream msg;
msg << "SEND:" << user << ":" << json.dump();
webSocket->send(msg.str(), false);
this->webSocket->send(msg.str(), false);
}
}
} // namespace germanairlinesva_websocket

View File

@ -46,7 +46,7 @@ XPLMDataRef pitch;
XPLMDataRef roll;
XPLMDataRef quaternion;
germanairlinesva_websocket::data toSend;
struct germanairlinesva_websocket::data toSend;
germanairlinesva_recording::PathRecording p;
/*
@ -171,35 +171,35 @@ PLUGIN_API int XPluginStart(char *outName, char *outSig, char *outDesc)
toLog("Logbook Test");
germanairlinesva_logbook::Logbook logbook;
logbook.addEntry({"08.09.2022",
"1000",
"L049",
"D-ALFA",
"John F. Kennedy International Aiport / EDDF",
"A1",
"14L",
"Gander International Airport / CYQX",
"10",
"03",
"10:00",
"10:20",
"13:20",
"13:30",
210.5,
20.1,
5012.4156,
8.87,
5041.3856,
7.1,
971.14,
2.41,
980.65,
-165.23,
1,
1.2012,
"2022-09-08_VGA1000",
5.5,
1});
logbook.addEntry("08.09.2022",
"1000",
"L049",
"D-ALFA",
"John F. Kennedy International Aiport / EDDF",
"A1",
"14L",
"Gander International Airport / CYQX",
"10",
"03",
"10:00",
"10:20",
"13:20",
"13:30",
210.5,
20.1,
5012.4156,
8.87,
5041.3856,
7.1,
971.14,
2.41,
980.65,
-165.23,
1,
1.2012,
"2022-09-08_VGA1000",
5.5,
1);
logbook.toFile();
return 1;
@ -281,7 +281,7 @@ void serverWorker()
germanairlinesva_util::setThreadName("GAServerWorker");
while (!wantsExit) {
germanairlinesva_websocket::data copy;
struct germanairlinesva_websocket::data copy;
{
const std::lock_guard<std::mutex> lock(mutex);