generated from AfonsoCMSousa/CPP-Template
neovim btw
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
#include "file.hpp"
|
||||
|
||||
vector<string> read_file(const char *filePath) {
|
||||
ifstream file(filePath);
|
||||
if (!file.is_open()) {
|
||||
return vector<string>();
|
||||
}
|
||||
|
||||
vector<string> lines;
|
||||
string line;
|
||||
|
||||
while (getline(file, line)) {
|
||||
lines.push_back(line);
|
||||
}
|
||||
|
||||
file.close();
|
||||
return lines;
|
||||
}
|
||||
|
||||
app_info parce_args(int argc, char *argv[]) {
|
||||
// example
|
||||
// ./player_tracker 130 12000 13000
|
||||
// -id -serverin -serverout
|
||||
app_info __processed_info;
|
||||
|
||||
if (argc <= 1) {
|
||||
throw invalid_argument("No argc provided");
|
||||
}
|
||||
|
||||
__processed_info.app_id = (u_int16_t)atoi(argv[1]);
|
||||
__processed_info.app_port_in = (u_int16_t)atoi(argv[2]);
|
||||
__processed_info.app_port_out = (u_int16_t)atoi(argv[3]);
|
||||
|
||||
// Parce .env
|
||||
vector<string> __read_lines = read_file("./.env");
|
||||
map<string, string> __env_args;
|
||||
|
||||
for (size_t i = 0; i < __read_lines.size() - 1; i++) {
|
||||
string token = __read_lines[i].substr(0, __read_lines[i].find(" = "));
|
||||
|
||||
__read_lines[i].erase(0, __read_lines[i].find(" = ") + 3);
|
||||
|
||||
__env_args[token] = __read_lines[i];
|
||||
}
|
||||
|
||||
// DEBUG
|
||||
// for (const auto& [key, value] : __env_args) {
|
||||
// std::cout << '[' << key << "] = " << value << "; ";
|
||||
// }
|
||||
|
||||
__processed_info.app_api_socket_path = __env_args["API_SOCKET_PATH"].c_str();
|
||||
__processed_info.max_players = (u_int16_t)atoi(__env_args["MAX_PLAYERS"].c_str());
|
||||
__processed_info.app_server_out_ip = __env_args["SERVER_OUT_IP"].c_str();
|
||||
|
||||
return __processed_info;
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
#ifndef FILE_H
|
||||
#define FILE_H
|
||||
|
||||
#include <stdio.h>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <fstream>
|
||||
#include <stdexcept>
|
||||
#include <map>
|
||||
|
||||
#include "server_structs.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
vector<string> read_file(const char *filePath);
|
||||
app_info parce_args(int argc, char *argv[]);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,33 @@
|
||||
#ifndef NET_HPP
|
||||
#define NET_HPP
|
||||
|
||||
#include <iostream>
|
||||
#include <stdexcept>
|
||||
#include <cstring>
|
||||
#include <sys/socket.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "server_structs.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
class Socket {
|
||||
private:
|
||||
// Socket file descriptor
|
||||
int sock;
|
||||
struct sockaddr_in server_addr;
|
||||
// Socket Data
|
||||
api_packet packet_data;
|
||||
|
||||
|
||||
public:
|
||||
Socket();
|
||||
~Socket();
|
||||
|
||||
void connect(const char *ip, uint16_t port);
|
||||
void send();
|
||||
void send(const api_packet &data);
|
||||
};
|
||||
|
||||
#endif // NET_HPP
|
||||
@@ -4,6 +4,18 @@
|
||||
#include <sys/cdefs.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
typedef struct app_info {
|
||||
// From args
|
||||
u_int16_t app_id;
|
||||
u_int16_t app_port_in;
|
||||
u_int16_t app_port_out;
|
||||
|
||||
// From .env
|
||||
const char *app_api_socket_path;
|
||||
u_int16_t max_players;
|
||||
const char *app_server_out_ip;
|
||||
} __attribute__((packed)) app_info;
|
||||
|
||||
typedef struct handshake {
|
||||
// [not used in the current Remote Telemtry version by AC]
|
||||
// In future versions it will identify the platform type of the client.
|
||||
|
||||
Reference in New Issue
Block a user