Namespaces and formatting

This commit is contained in:
2022-10-03 23:52:38 +02:00
parent 42acea35bb
commit eafe0b18f6
21 changed files with 493 additions and 433 deletions
+47 -34
View File
@@ -1,51 +1,64 @@
#ifndef GERMANAIRLINESVA_GACONNECTOR_SOCKET_TYPES_H
#define GERMANAIRLINESVA_GACONNECTOR_SOCKET_TYPES_H
#ifndef GERMANAIRLINESVA_GACONNECTOR_WEBSOCKET_TYPES_H
#define GERMANAIRLINESVA_GACONNECTOR_WEBSOCKET_TYPES_H
#include <cstdint>
namespace germanairlinesva
{
namespace websocket
namespace gaconnector
{
namespace websocket
{
#pragma pack(push) /* push current alignment to stack */
#pragma pack(1) /* set alignment to 1 byte boundary */
/* Structures and enums */
struct data {
std::int32_t pause = 0;
float pBrake = 0;
std::int32_t onGrnd = 0;
float totFuelKg = 0;
float truHdg = 0;
double alt = 0;
float gs = 0;
float ias = 0;
float vs = 0;
double lat = 0;
double lon = 0;
float ff[8] = {0, 0, 0, 0, 0, 0, 0, 0};
float maxSpd = 0;
char path[513] = "";
float uptime = 0;
float magHeading = 0;
float payloadKg = 0;
float totalWeightKg = 0;
};
/* Structures and enums */
struct data {
std::int32_t pause = 0;
float pBrake = 0;
std::int32_t onGrnd = 0;
float totFuelKg = 0;
float truHdg = 0;
double alt = 0;
float gs = 0;
float ias = 0;
float vs = 0;
double lat = 0;
double lon = 0;
float ff[8] = {0, 0, 0, 0, 0, 0, 0, 0};
float maxSpd = 0;
char path[513] = "";
float uptime = 0;
float magHeading = 0;
float payloadKg = 0;
float totalWeightKg = 0;
};
enum commands { PROCESS, SAVE, LOAD, TEXT, TIME, UNPAUSE, PAUSE, PORT, END };
enum commands {
PROCESS,
SAVE,
LOAD,
TEXT,
TIME,
UNPAUSE,
PAUSE,
PORT,
END
};
struct command_base {
enum commands type;
};
struct command_base {
enum commands type;
};
struct command_port {
double latitude;
double longitude;
float trueHeading;
};
struct command_port {
double latitude;
double longitude;
float trueHeading;
};
#pragma pack(pop) /* restore original alignment from stack */
} // namespace websocket
} // namespace websocket
} // namespace gaconnector
} // namespace germanairlinesva
#endif
+23 -20
View File
@@ -1,5 +1,5 @@
#ifndef GERMANAIRLINESVA_GACONNECTOR_WEBSOCKET_H
#define GERMANAIRLINESVA_GACONNECTOR_WEBSOCKET_H
#ifndef GERMANAIRLINESVA_GACONNECTOR_WEBSOCKET_WEBSOCKET_H
#define GERMANAIRLINESVA_GACONNECTOR_WEBSOCKET_WEBSOCKET_H
#define BUFSIZE 1024
#define MD5LEN 16
@@ -20,27 +20,30 @@
namespace germanairlinesva
{
namespace websocket
namespace gaconnector
{
class Websocket
namespace websocket
{
private:
char lastPath[513] = "";
char lastHash[2 * MD5LEN + 1] = "";
ix::WebSocket *webSocket = nullptr;
std::string host;
std::string user;
std::function<void(const std::string)> toLog;
class Websocket
{
private:
char lastPath[513] = "";
char lastHash[2 * MD5LEN + 1] = "";
ix::WebSocket *webSocket = nullptr;
std::string host;
std::string user;
std::function<void(const std::string)> toLog;
public:
Websocket(std::string host,
std::string user,
std::function<void(const std::string)> toLog);
~Websocket();
void onClientMessageCallback(const ix::WebSocketMessagePtr &msg);
void sendData(data &d);
};
} // namespace websocket
public:
Websocket(std::string host,
std::string user,
std::function<void(const std::string)> toLog);
~Websocket();
void onClientMessageCallback(const ix::WebSocketMessagePtr &msg);
void sendData(data &d);
};
} // namespace websocket
} // namespace gaconnector
} // namespace germanairlinesva
#endif
+91 -88
View File
@@ -2,113 +2,116 @@
namespace germanairlinesva
{
namespace websocket
namespace gaconnector
{
Websocket::Websocket(std::string host,
std::string user,
std::function<void(const std::string)> toLog)
: host(host), user(user), toLog(std::move(toLog))
namespace websocket
{
Websocket::Websocket(std::string host,
std::string user,
std::function<void(const std::string)> toLog)
: host(host), user(user), toLog(std::move(toLog))
{
#ifdef IBM
// Required on Windows
ix::initNetSystem();
// Required on Windows
ix::initNetSystem();
#endif
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();
}
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()
{
this->webSocket->stop();
this->toLog("WebSocket stopped");
Websocket::~Websocket()
{
this->webSocket->stop();
this->toLog("WebSocket stopped");
#ifdef IBM
// Required on Windows
ix::uninitNetSystem();
// Required on Windows
ix::uninitNetSystem();
#endif
}
}
void Websocket::onClientMessageCallback(const ix::WebSocketMessagePtr &msg)
{
if (msg->type == ix::WebSocketMessageType::Open) {
std::stringstream debug_msg;
debug_msg << std::endl << "New connection" << std::endl;
debug_msg << "Uri: " << msg->openInfo.uri << std::endl;
debug_msg << "Headers:" << std::endl;
for (const auto &it : msg->openInfo.headers) {
debug_msg << it.first << ": " << it.second << std::endl;
}
this->toLog(debug_msg.str());
this->webSocket->send("MASTER:" + user);
this->toLog("Connecting as " + user);
} else if (msg->type == ix::WebSocketMessageType::Close) {
if (msg->closeInfo.reason.compare("DUPLICATE") == 0) {
this->webSocket->disableAutomaticReconnection();
this->toLog("Disconnected due to beeing a duplicate simualtor");
} else {
void Websocket::onClientMessageCallback(const ix::WebSocketMessagePtr &msg)
{
if (msg->type == ix::WebSocketMessageType::Open) {
std::stringstream debug_msg;
debug_msg << std::endl << "Connection closed" << std::endl;
debug_msg << "Code: " << msg->closeInfo.code << std::endl;
debug_msg << "Reason: " << msg->closeInfo.reason << std::endl;
debug_msg << "Remote: " << msg->closeInfo.remote << std::endl;
debug_msg << std::endl << "New connection" << std::endl;
debug_msg << "Uri: " << msg->openInfo.uri << std::endl;
debug_msg << "Headers:" << std::endl;
for (const auto &it : msg->openInfo.headers) {
debug_msg << it.first << ": " << it.second << std::endl;
}
this->toLog(debug_msg.str());
}
} else if (msg->type == ix::WebSocketMessageType::Error) {
std::stringstream debug_msg;
debug_msg << std::endl << "Connection error" << std::endl;
debug_msg << "Decompression: " << msg->errorInfo.decompressionError
<< std::endl;
debug_msg << "HTTP status: " << msg->errorInfo.http_status << std::endl;
debug_msg << "Reason: " << msg->errorInfo.reason << std::endl;
debug_msg << "Retries: " << msg->errorInfo.retries << std::endl;
debug_msg << "Wait time: " << msg->errorInfo.wait_time << std::endl;
this->webSocket->send("MASTER:" + user);
this->toLog("Connecting as " + user);
} else if (msg->type == ix::WebSocketMessageType::Close) {
if (msg->closeInfo.reason.compare("DUPLICATE") == 0) {
this->webSocket->disableAutomaticReconnection();
this->toLog(debug_msg.str());
} else if (msg->type == ix::WebSocketMessageType::Message) {
if (!msg->str.empty()) {
this->toLog(msg->str);
}
}
}
this->toLog("Disconnected due to beeing a duplicate simualtor");
} else {
std::stringstream debug_msg;
void Websocket::sendData(data &d)
{
if (strcmp(d.path, this->lastPath) != 0) {
strcpy(this->lastPath, d.path);
if (util::generateMD5(d.path, this->lastHash, this->toLog)) {
strcpy(this->lastHash, "NOT SET");
debug_msg << std::endl << "Connection closed" << std::endl;
debug_msg << "Code: " << msg->closeInfo.code << std::endl;
debug_msg << "Reason: " << msg->closeInfo.reason << std::endl;
debug_msg << "Remote: " << msg->closeInfo.remote << std::endl;
this->toLog(debug_msg.str());
}
} else if (msg->type == ix::WebSocketMessageType::Error) {
std::stringstream debug_msg;
debug_msg << std::endl << "Connection error" << std::endl;
debug_msg << "Decompression: " << msg->errorInfo.decompressionError
<< std::endl;
debug_msg << "HTTP status: " << msg->errorInfo.http_status << std::endl;
debug_msg << "Reason: " << msg->errorInfo.reason << std::endl;
debug_msg << "Retries: " << msg->errorInfo.retries << std::endl;
debug_msg << "Wait time: " << msg->errorInfo.wait_time << std::endl;
this->toLog(debug_msg.str());
} else if (msg->type == ix::WebSocketMessageType::Message) {
if (!msg->str.empty()) {
this->toLog(msg->str);
}
}
}
nlohmann::json json = {
{"altitude", d.alt},
{"vs", d.vs},
{"ias", d.ias},
{"magHdg", d.magHeading},
{"truHdg", d.truHdg},
{"totFuel", d.totFuelKg},
{"fuelFlow", d.ff},
{"hash", this->lastHash},
};
void Websocket::sendData(data &d)
{
if (strcmp(d.path, this->lastPath) != 0) {
strcpy(this->lastPath, d.path);
if (utilities::generateMD5(d.path, this->lastHash, this->toLog)) {
strcpy(this->lastHash, "NOT SET");
}
}
if (this->webSocket != nullptr) {
std::ostringstream msg;
msg << "SEND:" << user << ":" << json.dump();
this->webSocket->send(msg.str(), false);
nlohmann::json json = {
{"altitude", d.alt},
{"vs", d.vs},
{"ias", d.ias},
{"magHdg", d.magHeading},
{"truHdg", d.truHdg},
{"totFuel", d.totFuelKg},
{"fuelFlow", d.ff},
{"hash", this->lastHash},
};
if (this->webSocket != nullptr) {
std::ostringstream msg;
msg << "SEND:" << user << ":" << json.dump();
this->webSocket->send(msg.str(), false);
}
}
}
} // namespace websocket
} // namespace websocket
} // namespace gaconnector
} // namespace germanairlinesva