diff --git a/src/Network/DataParser.cpp b/src/Network/DataParser.cpp index 7a2f8a8..02e5b32 100644 --- a/src/Network/DataParser.cpp +++ b/src/Network/DataParser.cpp @@ -1,6 +1,7 @@ -// -// Created by Anonymous275 on 4/2/2020. -// +/// +/// Created by Anonymous275 on 4/2/2020 +/// + #include "enet.h" #include #include @@ -8,7 +9,6 @@ #include "../logger.h" std::vector Split(const std::string& String,const std::string& delimiter); -void OnConnect(ENetPeer*peer,const std::string& data); void ParseData(ENetPacket*packet,ENetPeer*peer){ //here we will parse the data std::string Packet = (char*)packet->data; @@ -21,12 +21,7 @@ void ParseData(ENetPacket*packet,ENetPeer*peer){ //here we will parse the data 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; + std::cout << data << std::endl; } } \ No newline at end of file diff --git a/src/Network/Server.cpp b/src/Network/Server.cpp index 69dd8bf..151b9a9 100644 --- a/src/Network/Server.cpp +++ b/src/Network/Server.cpp @@ -1,6 +1,6 @@ -// -// Created by Anonymous275 on 4/2/2020. -// +/// +/// Created by Anonymous275 on 4/2/2020 +/// #define ENET_IMPLEMENTATION #include "enet.h" @@ -9,6 +9,7 @@ #include "../logger.h" void ParseData(ENetPacket*packet,ENetPeer*peer); //Data Parser +void OnConnect(ENetPeer*peer); ENetPacket* packet; @@ -18,16 +19,18 @@ void host_server(ENetHost *server) { while (enet_host_service(server, &event, 2) > 0) { switch (event.type) { 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); //the data should be the client info could be name for now it's Client information event.peer->Name = (void *)"Client information"; event.peer->gameVehicleID[0] = 15; event.peer->serverVehicleID = 17; - + OnConnect(event.peer); break; + case ENET_EVENT_TYPE_RECEIVE: - ParseData(event.packet,event.peer/*->dataLength,event.packet->data, (char *)event.peer->data, event.channelID*/); //We grab and Parse the Data + ParseData(event.packet,event.peer); + /*->dataLength,event.packet->data, (char *)event.peer->data, event.channelID*/ //We grab and Parse the Data /* Clean up the packet now that we're done using it. */ enet_packet_destroy (event.packet); break; diff --git a/src/Network/functions.cpp b/src/Network/functions.cpp index cd14dc3..39f3c56 100644 --- a/src/Network/functions.cpp +++ b/src/Network/functions.cpp @@ -18,13 +18,9 @@ std::vector Split(const std::string& String,const std::string& deli return Val; } -void OnConnect(ENetPeer*peer,const std::string& data){ - std::vector 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])); - } +void OnConnect(ENetPeer*peer){ + ENetPacket* packet = enet_packet_create ("NameRequest", //Send A Name Request to the Client + strlen ("NameRequest") + 1, + ENET_PACKET_FLAG_RELIABLE); //Create A reliable packet using the data + enet_peer_send(peer, 0, packet); } diff --git a/src/config.cpp b/src/config.cpp index c664522..135a027 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -1,6 +1,6 @@ -// -// Created by Anonymous275 on 1/28/2020. -// +/// +/// Created by Anonymous275 on 1/28/2020 +/// #include #include @@ -8,15 +8,16 @@ #include "logger.h" using namespace std; //nameSpace STD void GenerateConfig(); -string RemoveComments(string Line); +string RemoveComments(const string& Line); string convertToString(char* a, int size); -void SetValues(string Line, int Index); -void SetMainValues(bool D,int P,int MP,string Name,string serverName); +void SetValues(const string& Line, int Index); +void SetMainValues(bool,int,int,string,string,string); bool D; int P; int MP; string M; string S; +string F; //Generates or Reads Config @@ -34,7 +35,7 @@ void ParseConfig(){ index++; } } - SetMainValues(D,P,MP,M,S); //gives the values to Main + SetMainValues(D,P,MP,M,S,F); //gives the values to Main }else{ info("Config Not Found Generating A new One"); GenerateConfig(); @@ -44,12 +45,12 @@ void ParseConfig(){ -void SetValues(string Line, int Index) { +void SetValues(const string& Line, int Index) { int i = 0, state = 0; char Data[50] = ""; bool Switch = false; if (Index > 3) { Switch = true; } - for (char &c : Line) { + for (char c : Line) { if (Switch) { if (c == '\"') { state++; } if (state > 0 && state < 2) { @@ -71,7 +72,7 @@ void SetValues(string Line, int Index) { bool Boolean = (convertToString(Data,i-1).find("true") != string::npos);//searches for "true" switch (Index){ case 1 : - if(Boolean){D = true;}else{D = false;}//checks and sets the Debug Value + D = Boolean;//checks and sets the Debug Value break; case 2 : P = stoi(Data, &sz);//sets the Port break; @@ -80,6 +81,7 @@ void SetValues(string Line, int Index) { case 4 : M = Data; //Map break; case 5 : S = Data; //Name + case 6 : F = Data; //File name } } @@ -94,15 +96,16 @@ void GenerateConfig(){ "Port = 30814 # Port to run the server on\n" "MaxPlayers = 10 # Maximum Amount of Clients\n" "Map = \"levels/gridmap/level.json\"\n" - "Name = \"BeamNG-MP FTW\""; + "Name = \"BeamNG-MP FTW\"\n" + "use = \"/Resources\""; FileStream.close(); } -string RemoveComments(string Line){ +string RemoveComments(const string& Line){ int i = 0; char Data[50] = ""; - for(char& c : Line) { + for(char c : Line) { if(c == '#'){break;} //when it finds the # it will stop Data[i] = c; i++; diff --git a/src/heartbeat.cpp b/src/heartbeat.cpp index 033692b..ae6b1f3 100644 --- a/src/heartbeat.cpp +++ b/src/heartbeat.cpp @@ -1,6 +1,6 @@ -// -// Created by Mitch on 04/02/2020. -// +/// +/// Created by Mitch on 04/02/2020 +/// #include "heartbeat.h" #include diff --git a/src/logger.cpp b/src/logger.cpp index 0f59f17..2934e27 100644 --- a/src/logger.cpp +++ b/src/logger.cpp @@ -1,6 +1,6 @@ -// -// Created by jojos38 on 28.01.2020. -// +/// +/// Created by jojos38 on 28/01/2020 +/// #include @@ -32,7 +32,7 @@ void setLoggerLevel(char level_string[]) { stringstream getDate() { // current date/time based on current system - time_t now = time(0); + time_t now = time(nullptr); tm* ltm = localtime(&now); int month = 1 + ltm->tm_mon; diff --git a/src/main.cpp b/src/main.cpp index 9992e85..116ffd5 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,6 +1,6 @@ -// -// Created by Anonymous275 on 28.01.2020. -// +/// +/// Created by Anonymous275 on 28/01/2020 +/// #include #include @@ -21,6 +21,7 @@ int Port = 30814; int MaxPlayers = 10; string MapName = "levels/gridmap/level.json"; string ServerName = "BeamNG-MP FTW"; +string Resource = "/Resources"; //Entry int main() { @@ -41,14 +42,16 @@ void DebugData(){ cout << "MaxPlayers : " << MaxPlayers << "\n"; cout << "MapName : " << MapName << "\n"; cout << "ServerName : " << ServerName << "\n"; + cout << "File : " << Resource << "\n"; } -void SetMainValues(bool D, int P,int MP,string Name,string serverName){ +void SetMainValues(bool D, int P,int MP,string Name,string serverName,string filename){ Debug = D; Port = P; MapName = Name; ServerName = serverName; MaxPlayers = MP; + Resource = filename; } void LogInit(){