mirror of
https://github.com/BeamMP/BeamMP-Server.git
synced 2025-07-01 23:35:41 +00:00
commit
This commit is contained in:
parent
104856938a
commit
3c244c7e7f
1
.gitignore
vendored
1
.gitignore
vendored
@ -21,6 +21,7 @@ out/
|
|||||||
|
|
||||||
#Clion Files
|
#Clion Files
|
||||||
cmake-build-debug/
|
cmake-build-debug/
|
||||||
|
cmake-build-release/
|
||||||
# Build results
|
# Build results
|
||||||
[Dd]ebug/
|
[Dd]ebug/
|
||||||
[Dd]ebugPublic/
|
[Dd]ebugPublic/
|
||||||
|
1
.idea/.name
generated
Normal file
1
.idea/.name
generated
Normal file
@ -0,0 +1 @@
|
|||||||
|
BeamMP-Server
|
@ -1,7 +1,7 @@
|
|||||||
cmake_minimum_required(VERSION 3.15)
|
cmake_minimum_required(VERSION 3.15)
|
||||||
project(BeamNG-MP-Server)
|
project(BeamMP-Server)
|
||||||
|
|
||||||
set(CMAKE_CXX_STANDARD 14)
|
set(CMAKE_CXX_STANDARD 14)
|
||||||
|
|
||||||
add_executable(BeamNG-MP-Server src/main.cpp src/logger.cpp src/config.cpp src/Network/Server.cpp src/Network/enet.h src/Network/DataParser.cpp src/heartbeat.cpp src/heartbeat.h src/Network/ClientHandler.cpp)
|
add_executable(BeamMP-Server src/main.cpp src/logger.cpp src/config.cpp src/Network/Server.cpp src/Network/enet.h src/Network/DataParser.cpp src/heartbeat.cpp src/heartbeat.h src/Network/ClientHandler.cpp src/Network/functions.cpp)
|
||||||
target_link_libraries(BeamNG-MP-Server winmm ws2_32)
|
target_link_libraries(BeamMP-Server winmm ws2_32)
|
@ -3,23 +3,30 @@
|
|||||||
//
|
//
|
||||||
#include "enet.h"
|
#include "enet.h"
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
#include "../logger.h"
|
#include "../logger.h"
|
||||||
|
|
||||||
using namespace std;
|
std::vector<std::string> Split(const std::string& String,const std::string& delimiter);
|
||||||
char Name[20] = "";
|
void OnConnect(ENetPeer*peer,const std::string& data);
|
||||||
void ParseData(ENetPacket*packet,ENetPeer*peer){ //here we will parse the data
|
|
||||||
enet_uint8* Data = packet->data;
|
void ParseData(ENetPacket*packet,ENetPeer*peer){ //here we will parse the data
|
||||||
|
std::string Packet = (char*)packet->data;
|
||||||
|
int off = stoi(Packet.substr(0, 2));
|
||||||
|
std::string header = Packet.substr(2, off - 2), data = Packet.substr(off);
|
||||||
|
std::vector<std::string> split;
|
||||||
|
|
||||||
|
if(!header.empty()) {
|
||||||
|
std::cout << header << " header size : " << header.size() << std::endl;
|
||||||
|
split = Split(header, ":"); //1st is reliable - 2nd is Code - 3rd is VehID
|
||||||
|
}
|
||||||
|
if(!data.empty()){
|
||||||
|
switch (stoi(split.at(1))){
|
||||||
|
case 2000:
|
||||||
|
OnConnect(peer,data);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
//std::cout << data << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
cout << "Data : " << packet->data << std::endl;
|
|
||||||
cout << "Size : " << strlen(reinterpret_cast<const char *>(packet->data)) << std::endl;
|
|
||||||
/*if(strcmp((char*)peer->Name,"Client information")==0){ //Checks if the Client has no name
|
|
||||||
sprintf(Name,"%s",Data);
|
|
||||||
peer->Name = (void *)Name;
|
|
||||||
char Info[100];
|
|
||||||
sprintf(Info,"Client Name is %s ID : %u\n",Name,peer->connectID); //ID System
|
|
||||||
info(Info); //Logs the data
|
|
||||||
peer->serverVehicleID = (int)peer->connectID; //test to see if it works
|
|
||||||
sprintf(Info,"%s ServerVehicleID : %d GameVehicleID : %d",Name,peer->serverVehicleID,peer->gameVehicleID[0]);
|
|
||||||
info(Info);
|
|
||||||
}*/
|
|
||||||
}
|
}
|
@ -5,11 +5,11 @@
|
|||||||
#define ENET_IMPLEMENTATION
|
#define ENET_IMPLEMENTATION
|
||||||
#include "enet.h"
|
#include "enet.h"
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <stdio.h>
|
#include <cstdio>
|
||||||
#include "../logger.h"
|
#include "../logger.h"
|
||||||
|
|
||||||
void ParseData(ENetPacket*packet,ENetPeer*peer); //Data Parser
|
void ParseData(ENetPacket*packet,ENetPeer*peer); //Data Parser
|
||||||
void NameRequest(ENetPeer*peer);
|
|
||||||
void SendToAll(ENetHost *server,ENetEvent event);
|
|
||||||
ENetPacket* packet;
|
ENetPacket* packet;
|
||||||
|
|
||||||
|
|
||||||
@ -20,13 +20,10 @@ void host_server(ENetHost *server) {
|
|||||||
case ENET_EVENT_TYPE_CONNECT:
|
case ENET_EVENT_TYPE_CONNECT:
|
||||||
printf("A new client connected from %x:%u.\n", event.peer->address.host, event.peer->address.port); //Help xD
|
printf("A new client connected from %x:%u.\n", event.peer->address.host, event.peer->address.port); //Help xD
|
||||||
//the data should be the client info could be name for now it's Client information
|
//the data should be the client info could be name for now it's Client information
|
||||||
NameRequest(event.peer);
|
|
||||||
event.peer->Name = (void *)"Client information";
|
event.peer->Name = (void *)"Client information";
|
||||||
event.peer->gameVehicleID[0] = 15;
|
event.peer->gameVehicleID[0] = 15;
|
||||||
event.peer->serverVehicleID = 17;
|
event.peer->serverVehicleID = 17;
|
||||||
|
|
||||||
SendToAll(server,event);
|
|
||||||
|
|
||||||
break;
|
break;
|
||||||
case ENET_EVENT_TYPE_RECEIVE:
|
case ENET_EVENT_TYPE_RECEIVE:
|
||||||
|
|
||||||
@ -38,12 +35,12 @@ void host_server(ENetHost *server) {
|
|||||||
case ENET_EVENT_TYPE_DISCONNECT:
|
case ENET_EVENT_TYPE_DISCONNECT:
|
||||||
printf ("%s disconnected.\n", (char *)event.peer->Name);
|
printf ("%s disconnected.\n", (char *)event.peer->Name);
|
||||||
// Reset the peer's client information.
|
// Reset the peer's client information.
|
||||||
event.peer->Name = NULL;
|
event.peer->Name = nullptr;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case ENET_EVENT_TYPE_DISCONNECT_TIMEOUT:
|
case ENET_EVENT_TYPE_DISCONNECT_TIMEOUT:
|
||||||
printf ("%s timed out.\n", (char *)event.peer->Name);
|
printf ("%s timed out.\n", (char *)event.peer->Name);
|
||||||
event.peer->Name = NULL;
|
event.peer->Name = nullptr;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case ENET_EVENT_TYPE_NONE: break;
|
case ENET_EVENT_TYPE_NONE: break;
|
||||||
@ -66,7 +63,7 @@ void ServerMain(int Port, int MaxClients) {
|
|||||||
//create a server
|
//create a server
|
||||||
info("starting server with a maximum of " + to_string(MaxClients) + " Clients...");
|
info("starting server with a maximum of " + to_string(MaxClients) + " Clients...");
|
||||||
server = enet_host_create(&address, MaxClients, 2, 0, 0);
|
server = enet_host_create(&address, MaxClients, 2, 0, 0);
|
||||||
if (server == NULL) {
|
if (server == nullptr) {
|
||||||
error("An error occurred while trying to create a server host.");
|
error("An error occurred while trying to create a server host.");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
30
src/Network/functions.cpp
Normal file
30
src/Network/functions.cpp
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
///
|
||||||
|
/// Created by Anonymous275 on 4/1/2020
|
||||||
|
///
|
||||||
|
#include "enet.h"
|
||||||
|
#include <vector>
|
||||||
|
#include "../logger.h"
|
||||||
|
|
||||||
|
std::vector<std::string> Split(const std::string& String,const std::string& delimiter){
|
||||||
|
std::vector<std::string> Val;
|
||||||
|
size_t pos = 0;
|
||||||
|
std::string token,s = String;
|
||||||
|
while ((pos = s.find(delimiter)) != std::string::npos) {
|
||||||
|
token = s.substr(0, pos);
|
||||||
|
Val.push_back(token);
|
||||||
|
s.erase(0, pos + delimiter.length());
|
||||||
|
}
|
||||||
|
Val.push_back(s);
|
||||||
|
return Val;
|
||||||
|
}
|
||||||
|
|
||||||
|
void OnConnect(ENetPeer*peer,const std::string& data){
|
||||||
|
std::vector<std::string> Data = Split(data,":");
|
||||||
|
if(strcmp((char*)peer->Name,"Client information")==0){ //Checks if the Client has no name
|
||||||
|
peer->Name = (void *)Data.at(0).c_str();
|
||||||
|
char Info[100];
|
||||||
|
info("Client Name is " + Data.at(0) + " ID : " + std::to_string(peer->connectID)); //ID System //Logs the data
|
||||||
|
peer->serverVehicleID = (int)peer->connectID; //test to see if it works
|
||||||
|
info(Data.at(0)+" ServerVehicleID : "+std::to_string(peer->serverVehicleID)+" GameVehicleID : " + std::to_string(peer->gameVehicleID[0]));
|
||||||
|
}
|
||||||
|
}
|
@ -2,7 +2,6 @@
|
|||||||
// Created by Anonymous275 on 1/28/2020.
|
// Created by Anonymous275 on 1/28/2020.
|
||||||
//
|
//
|
||||||
|
|
||||||
#include "main.h"
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
@ -5,6 +5,7 @@
|
|||||||
|
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include "logger.h"
|
#include "logger.h"
|
||||||
|
#include <string>
|
||||||
void addToLog(basic_string<char> Data);
|
void addToLog(basic_string<char> Data);
|
||||||
using namespace std;
|
using namespace std;
|
||||||
int loggerlevel;
|
int loggerlevel;
|
||||||
@ -81,14 +82,14 @@ stringstream getDate() {
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
void info(basic_string<char> toPrint) {
|
void info(const std::string& toPrint) {
|
||||||
if (loggerlevel <= 2){
|
if (loggerlevel <= 2){
|
||||||
cout << getDate().str() << "[INFO] " << toPrint << endl;
|
cout << getDate().str() << "[INFO] " << toPrint << endl;
|
||||||
addToLog(getDate().str() + "[INFO] " + toPrint + "\n");
|
addToLog(getDate().str() + "[INFO] " + toPrint + "\n");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void error(basic_string<char> toPrint) {
|
void error(const std::string& toPrint) {
|
||||||
if (loggerlevel <= 4) {
|
if (loggerlevel <= 4) {
|
||||||
cout << getDate().str() << "[ERROR] " << toPrint << endl;
|
cout << getDate().str() << "[ERROR] " << toPrint << endl;
|
||||||
addToLog(getDate().str() + "[ERROR] " + toPrint + "\n");
|
addToLog(getDate().str() + "[ERROR] " + toPrint + "\n");
|
||||||
@ -96,7 +97,7 @@ void error(basic_string<char> toPrint) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void warn(basic_string<char> toPrint) {
|
void warn(const std::string& toPrint) {
|
||||||
if (loggerlevel <= 3) {
|
if (loggerlevel <= 3) {
|
||||||
cout << getDate().str() << "[WARN] " << toPrint << endl;
|
cout << getDate().str() << "[WARN] " << toPrint << endl;
|
||||||
addToLog(getDate().str() + "[WARN] " + toPrint + "\n");
|
addToLog(getDate().str() + "[WARN] " + toPrint + "\n");
|
||||||
@ -104,7 +105,7 @@ void warn(basic_string<char> toPrint) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void debug(basic_string<char> toPrint) {
|
void debug(const std::string& toPrint) {
|
||||||
if (loggerlevel <= 1) {
|
if (loggerlevel <= 1) {
|
||||||
cout << getDate().str() << "[DEBUG] " << toPrint << endl;
|
cout << getDate().str() << "[DEBUG] " << toPrint << endl;
|
||||||
addToLog(getDate().str() + "[DEBUG] " + toPrint + "\n");
|
addToLog(getDate().str() + "[DEBUG] " + toPrint + "\n");
|
||||||
|
@ -10,7 +10,7 @@ using namespace std;
|
|||||||
extern int loggerlevel;
|
extern int loggerlevel;
|
||||||
stringstream getDate();
|
stringstream getDate();
|
||||||
void setLoggerLevel(char level_string[]);
|
void setLoggerLevel(char level_string[]);
|
||||||
void info(basic_string<char> toPrint);
|
void info(const std::string& toPrint);
|
||||||
void warn(basic_string<char> toPrint);
|
void warn(const std::string& toPrint);
|
||||||
void error(basic_string<char> toPrint);
|
void error(const std::string& toPrint);
|
||||||
void debug(basic_string<char> toPrint);
|
void debug(const std::string& toPrint);
|
||||||
|
@ -2,7 +2,6 @@
|
|||||||
// Created by Anonymous275 on 28.01.2020.
|
// Created by Anonymous275 on 28.01.2020.
|
||||||
//
|
//
|
||||||
|
|
||||||
#include "main.h"
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
|
12
src/main.h
12
src/main.h
@ -1,12 +0,0 @@
|
|||||||
//
|
|
||||||
// Created by Anonymous275 on 28.01.2020.
|
|
||||||
//
|
|
||||||
|
|
||||||
#ifndef BEAMNG_MP_SERVER_MAIN_H
|
|
||||||
#define BEAMNG_MP_SERVER_MAIN_H
|
|
||||||
|
|
||||||
class main {
|
|
||||||
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif //BEAMNG_MP_SERVER_MAIN_H
|
|
Loading…
x
Reference in New Issue
Block a user