mirror of
https://github.com/SantaSpeen/BeamMP-Server.git
synced 2026-06-17 23:40:52 +00:00
4h of work
This commit is contained in:
+2
-1
@@ -8,5 +8,6 @@ set(CMAKE_CXX_STANDARD 14)
|
|||||||
|
|
||||||
add_executable(BeamMP-Server src/main.cpp src/http.cpp src/logger.cpp src/config.cpp src/Network/Server.cpp
|
add_executable(BeamMP-Server src/main.cpp src/http.cpp src/logger.cpp src/config.cpp src/Network/Server.cpp
|
||||||
src/Network/enet.h src/Network/DataParser.cpp src/heartbeat.cpp
|
src/Network/enet.h src/Network/DataParser.cpp src/heartbeat.cpp
|
||||||
src/Network/ClientHandler.cpp src/Network/functions.cpp src/Settings.hpp)
|
src/Network/ClientHandler.cpp src/Network/functions.cpp src/Settings.hpp
|
||||||
|
src/Resources.cpp src/Network/TCPClientHandler.cpp)
|
||||||
target_link_libraries(BeamMP-Server winmm ws2_32 libcurl_a)
|
target_link_libraries(BeamMP-Server winmm ws2_32 libcurl_a)
|
||||||
@@ -8,6 +8,7 @@
|
|||||||
#include <cstdio>
|
#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 OnConnect(ENetPeer*peer);
|
void OnConnect(ENetPeer*peer);
|
||||||
|
|
||||||
@@ -82,3 +83,64 @@ void ServerMain(int Port, int MaxClients) {
|
|||||||
enet_deinitialize();
|
enet_deinitialize();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void CreateNewThread(void*);
|
||||||
|
|
||||||
|
void TCPMain(int Port){
|
||||||
|
info("Starting TCP Server on port " + to_string(Port));
|
||||||
|
|
||||||
|
WSADATA wsaData;
|
||||||
|
int iResult;
|
||||||
|
sockaddr_in addr{};
|
||||||
|
SOCKET sock,client;
|
||||||
|
addr.sin_family = AF_INET;
|
||||||
|
addr.sin_port = htons(Port);
|
||||||
|
|
||||||
|
iResult = WSAStartup(MAKEWORD(2,2),&wsaData);
|
||||||
|
|
||||||
|
if(iResult)
|
||||||
|
{
|
||||||
|
printf("WSA startup failed");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
sock = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
|
||||||
|
|
||||||
|
if(sock == INVALID_SOCKET)
|
||||||
|
{
|
||||||
|
printf("Invalid socket");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
iResult = bind(sock,(sockaddr*)&addr,sizeof(sockaddr_in ));
|
||||||
|
|
||||||
|
if(iResult)
|
||||||
|
{
|
||||||
|
|
||||||
|
printf("bind failed %lu",GetLastError());
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
iResult = listen(sock,SOMAXCONN);
|
||||||
|
|
||||||
|
if(iResult)
|
||||||
|
{
|
||||||
|
|
||||||
|
printf("iResult failed %lu",GetLastError());
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
while(client = accept(sock,nullptr,nullptr))
|
||||||
|
{
|
||||||
|
if(client == INVALID_SOCKET)
|
||||||
|
{
|
||||||
|
printf("invalid client socket\n");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
CreateNewThread((void*)&client);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,118 @@
|
|||||||
|
///
|
||||||
|
/// Created by Anonymous275 on 4/11/2020
|
||||||
|
///
|
||||||
|
|
||||||
|
#include <thread>
|
||||||
|
#include <string>
|
||||||
|
#include <fstream>
|
||||||
|
#include <iostream>
|
||||||
|
#include <WinSock2.h>
|
||||||
|
#include "../Settings.hpp"
|
||||||
|
|
||||||
|
int ParseAndSend(SOCKET Client, std::string Data){
|
||||||
|
std::string Response, Packet;
|
||||||
|
char ID = Data.at(0);
|
||||||
|
int Prev = 0,DataSent = 0, Size = 0;
|
||||||
|
bool FileSent = true;
|
||||||
|
switch (ID){
|
||||||
|
case 'a' :
|
||||||
|
Response = FileList;
|
||||||
|
break;
|
||||||
|
case 'b' :
|
||||||
|
FileSent = false;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
std::string FLocation = Data.substr(1);
|
||||||
|
if(FileList.find(FLocation) == std::string::npos)return -1;
|
||||||
|
do {
|
||||||
|
if(!FileSent){
|
||||||
|
std::ifstream f;
|
||||||
|
f.open(FLocation.c_str(), std::ios::binary);
|
||||||
|
if(f.good()){
|
||||||
|
if(!Size){
|
||||||
|
Size = f.seekg(0, std::ios_base::end).tellg();
|
||||||
|
Response.resize(Size);
|
||||||
|
f.seekg(0, std::ios_base::beg);
|
||||||
|
f.read(&Response[0], Size);
|
||||||
|
f.close();
|
||||||
|
}else{f.close();}
|
||||||
|
|
||||||
|
if(DataSent != Size){
|
||||||
|
if((Size-DataSent) < 65535){
|
||||||
|
Packet = Response.substr(Prev,(Size-DataSent));
|
||||||
|
DataSent += (Size-DataSent);
|
||||||
|
Response.clear();
|
||||||
|
}else{
|
||||||
|
DataSent += 65535;
|
||||||
|
Packet = Response.substr(Prev,65535);
|
||||||
|
}
|
||||||
|
Prev = DataSent;
|
||||||
|
}else{
|
||||||
|
Size = DataSent = Prev = 0;
|
||||||
|
Response = "End of file";
|
||||||
|
FileSent = true;
|
||||||
|
Packet.clear();
|
||||||
|
}
|
||||||
|
}else{
|
||||||
|
FileSent = true;
|
||||||
|
Response = "Cannot Open File " + FLocation;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int iSendResult;
|
||||||
|
|
||||||
|
if(!Packet.empty())iSendResult = send(Client, Packet.c_str(), Packet.length(), 0);
|
||||||
|
else iSendResult = send(Client, Response.c_str(), Response.length(), 0);
|
||||||
|
|
||||||
|
if (iSendResult == SOCKET_ERROR) {
|
||||||
|
printf("send failed with error: %d\n", WSAGetLastError());
|
||||||
|
closesocket(Client);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
}while(!FileSent);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void Client(void* ClientData){
|
||||||
|
SOCKET Client = *(SOCKET*)ClientData;
|
||||||
|
printf("Client connected\n");
|
||||||
|
int iResult, iSendResult;
|
||||||
|
char recvbuf[65535];
|
||||||
|
int recvbuflen = 65535;
|
||||||
|
|
||||||
|
do {
|
||||||
|
iResult = recv(Client, recvbuf, recvbuflen, 0);
|
||||||
|
if (iResult > 0) {
|
||||||
|
//printf("Bytes received: %d\n", iResult);
|
||||||
|
std::string Data = recvbuf,Response;
|
||||||
|
Data.resize(iResult);
|
||||||
|
|
||||||
|
if(ParseAndSend(Client,Data) == -1)break;
|
||||||
|
|
||||||
|
// Echo the buffer back to the sender
|
||||||
|
/*iSendResult = send(Client, Response.c_str(), Response.length(), 0);
|
||||||
|
if (iSendResult == SOCKET_ERROR) {
|
||||||
|
printf("send failed with error: %d\n", WSAGetLastError());
|
||||||
|
closesocket(Client);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
printf("Bytes sent: %d\n", iSendResult);*/
|
||||||
|
}
|
||||||
|
else if (iResult == 0)
|
||||||
|
printf("Connection closing...\n");
|
||||||
|
else {
|
||||||
|
printf("recv failed with error: %d\n", WSAGetLastError());
|
||||||
|
closesocket(Client);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
} while (iResult > 0);
|
||||||
|
std::cout << "Client Closed" << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void CreateNewThread(void* ClientData){
|
||||||
|
std::cout << "New Client" << std::endl;
|
||||||
|
std::thread NewClient(Client,ClientData);
|
||||||
|
NewClient.detach();
|
||||||
|
}
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
///
|
||||||
|
/// Created by Anonymous275 on 4/11/2020
|
||||||
|
///
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
#include <algorithm>
|
||||||
|
#include <filesystem>
|
||||||
|
|
||||||
|
namespace fs = std::experimental::filesystem;
|
||||||
|
|
||||||
|
std::string FileList;
|
||||||
|
|
||||||
|
void HandleResources(const std::string& path){
|
||||||
|
struct stat info{};
|
||||||
|
if(stat( "Resources", &info) != 0){
|
||||||
|
_wmkdir(L"Resources");
|
||||||
|
}
|
||||||
|
for (const auto & entry : fs::directory_iterator(path)){
|
||||||
|
FileList += entry.path().string() + ";";
|
||||||
|
}
|
||||||
|
std::replace(FileList.begin(),FileList.end(),'\\','/');
|
||||||
|
}
|
||||||
+3
-2
@@ -1,13 +1,14 @@
|
|||||||
///
|
///
|
||||||
/// Created by Anonymous275 on 4/10/2020
|
/// Created by Anonymous275 on 4/10/2020
|
||||||
///
|
///
|
||||||
extern std::string MapName;
|
|
||||||
extern bool Private;
|
extern bool Private;
|
||||||
extern int MaxPlayers;
|
extern int MaxPlayers;
|
||||||
extern int UDPPort;
|
extern int UDPPort;
|
||||||
extern int TCPPort;
|
extern int TCPPort;
|
||||||
|
extern int PlayerCount;
|
||||||
|
extern std::string MapName;
|
||||||
extern std::string ServerName;
|
extern std::string ServerName;
|
||||||
extern std::string Resource;
|
extern std::string Resource;
|
||||||
extern std::string ServerVersion;
|
extern std::string ServerVersion;
|
||||||
extern std::string ClientVersion;
|
extern std::string ClientVersion;
|
||||||
extern int PlayerCount;
|
extern std::string FileList;
|
||||||
|
|||||||
+16
-17
@@ -11,14 +11,10 @@ void GenerateConfig();
|
|||||||
string RemoveComments(const string& Line);
|
string RemoveComments(const string& Line);
|
||||||
string convertToString(char* a, int size);
|
string convertToString(char* a, int size);
|
||||||
void SetValues(const string& Line, int Index);
|
void SetValues(const string& Line, int Index);
|
||||||
void SetMainValues(bool,int,int,string,string,string);
|
void SetMainValues(bool,int,int,int,string,string,string);
|
||||||
bool D;
|
bool D;
|
||||||
int P;
|
int P,FP,MP;
|
||||||
int MP;
|
string M,S,F;
|
||||||
string M;
|
|
||||||
string S;
|
|
||||||
string F;
|
|
||||||
|
|
||||||
|
|
||||||
//Generates or Reads Config
|
//Generates or Reads Config
|
||||||
void ParseConfig(){
|
void ParseConfig(){
|
||||||
@@ -35,7 +31,7 @@ void ParseConfig(){
|
|||||||
index++;
|
index++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
SetMainValues(D,P,MP,M,S,F); //gives the values to Main
|
SetMainValues(D,P,FP,MP,M,S,F); //gives the values to Main
|
||||||
}else{
|
}else{
|
||||||
info("Config Not Found Generating A new One");
|
info("Config Not Found Generating A new One");
|
||||||
GenerateConfig();
|
GenerateConfig();
|
||||||
@@ -49,7 +45,7 @@ void SetValues(const string& Line, int Index) {
|
|||||||
int i = 0, state = 0;
|
int i = 0, state = 0;
|
||||||
char Data[50] = "";
|
char Data[50] = "";
|
||||||
bool Switch = false;
|
bool Switch = false;
|
||||||
if (Index > 3) { Switch = true; }
|
if (Index > 4) { Switch = true; }
|
||||||
for (char c : Line) {
|
for (char c : Line) {
|
||||||
if (Switch) {
|
if (Switch) {
|
||||||
if (c == '\"') { state++; }
|
if (c == '\"') { state++; }
|
||||||
@@ -76,12 +72,14 @@ void SetValues(const string& Line, int Index) {
|
|||||||
break;
|
break;
|
||||||
case 2 : P = stoi(Data, &sz);//sets the Port
|
case 2 : P = stoi(Data, &sz);//sets the Port
|
||||||
break;
|
break;
|
||||||
case 3 : MP = stoi(Data, &sz);//sets the Max Amount of player
|
case 3 : FP = stoi(Data, &sz);//sets the TCP File Port
|
||||||
break;
|
break;
|
||||||
case 4 : M = Data; //Map
|
case 4 : MP = stoi(Data, &sz); //sets the Max Amount of player
|
||||||
break;
|
break;
|
||||||
case 5 : S = Data; //Name
|
case 5 : M = Data; //Map
|
||||||
case 6 : F = Data; //File name
|
break;
|
||||||
|
case 6 : S = Data; //Name
|
||||||
|
case 7 : F = Data; //File name
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -91,13 +89,14 @@ void SetValues(const string& Line, int Index) {
|
|||||||
void GenerateConfig(){
|
void GenerateConfig(){
|
||||||
ofstream FileStream;
|
ofstream FileStream;
|
||||||
FileStream.open ("Server.cfg");
|
FileStream.open ("Server.cfg");
|
||||||
FileStream << "# This is the BeamNG-MP Server Configuration File\n"
|
FileStream << "# This is the BeamMP Server Configuration File\n"
|
||||||
"Debug = false # true or false to enable debug console output\n"
|
"Debug = false # true or false to enable debug console output\n"
|
||||||
"Port = 30814 # Port to run the server on\n"
|
"Port = 30814 # Port to run the server on\n"
|
||||||
|
"FilePort = 30814 # Port to transfer Files\n"
|
||||||
"MaxPlayers = 10 # Maximum Amount of Clients\n"
|
"MaxPlayers = 10 # Maximum Amount of Clients\n"
|
||||||
"Map = \"levels/gridmap/level.json\"\n"
|
"Map = \"/levels/gridmap/info.json\" # Default Map\n"
|
||||||
"Name = \"BeamNG-MP FTW\"\n"
|
"Name = \"BeamMP New Server\" # Server Name\n"
|
||||||
"use = \"/Resources\"";
|
"use = \"Resources\" # Resource file name";
|
||||||
FileStream.close();
|
FileStream.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -17,13 +17,11 @@ void PostHTTP(const std::string& IP,const std::string& Fields);
|
|||||||
void Heartbeat()
|
void Heartbeat()
|
||||||
{
|
{
|
||||||
string UUID = HTTP_REQUEST("https://beamng-mp.com/new-server-startup",443);
|
string UUID = HTTP_REQUEST("https://beamng-mp.com/new-server-startup",443);
|
||||||
std::cout << "UUID GEN : " << UUID << std::endl;
|
|
||||||
std::string State = Private ? "true" : "false";
|
std::string State = Private ? "true" : "false";
|
||||||
while(true)
|
while(true)
|
||||||
{
|
{
|
||||||
PostHTTP("https://beamng-mp.com/heartbeat","uuid="+UUID+"&players="+to_string(PlayerCount)+"&maxplayers="+to_string(MaxPlayers)+"&port="
|
PostHTTP("https://beamng-mp.com/heartbeat","uuid="+UUID+"&players="+to_string(PlayerCount)+"&maxplayers="+to_string(MaxPlayers)+"&port="
|
||||||
+ to_string(UDPPort) + "&map=" + MapName + "&private="+State+"&serverversion="+ServerVersion+"&clientversion="+ClientVersion+"&name="+ServerName);
|
+ to_string(UDPPort) + "&map=" + MapName + "&private="+State+"&serverversion="+ServerVersion+"&clientversion="+ClientVersion+"&name="+ServerName);
|
||||||
|
|
||||||
std::this_thread::sleep_for (std::chrono::seconds(5));
|
std::this_thread::sleep_for (std::chrono::seconds(5));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-1
@@ -8,6 +8,7 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
|
|
||||||
static size_t WriteCallback(void *contents, size_t size, size_t nmemb, void *userp)
|
static size_t WriteCallback(void *contents, size_t size, size_t nmemb, void *userp)
|
||||||
{
|
{
|
||||||
((std::string*)userp)->append((char*)contents, size * nmemb);
|
((std::string*)userp)->append((char*)contents, size * nmemb);
|
||||||
@@ -44,5 +45,4 @@ void PostHTTP(const std::string& IP,const std::string& Fields){
|
|||||||
res = curl_easy_perform(curl);
|
res = curl_easy_perform(curl);
|
||||||
curl_easy_cleanup(curl);
|
curl_easy_cleanup(curl);
|
||||||
}
|
}
|
||||||
std::cout << "Buffer : " << readBuffer << std::endl;
|
|
||||||
}
|
}
|
||||||
+17
-12
@@ -9,7 +9,7 @@
|
|||||||
#include <chrono>
|
#include <chrono>
|
||||||
#include <thread>
|
#include <thread>
|
||||||
|
|
||||||
using namespace std; //nameSpace STD
|
using namespace std;
|
||||||
void DebugData();
|
void DebugData();
|
||||||
void LogInit();
|
void LogInit();
|
||||||
void ParseConfig();
|
void ParseConfig();
|
||||||
@@ -22,38 +22,43 @@ string MapName = "levels/gridmap/level.json";
|
|||||||
bool Private = false;
|
bool Private = false;
|
||||||
int MaxPlayers = 10;
|
int MaxPlayers = 10;
|
||||||
int UDPPort = 30814;
|
int UDPPort = 30814;
|
||||||
int TCPPort = 0;
|
int TCPPort = 30814;
|
||||||
string ServerName = "BeamMP Server";
|
string ServerName = "BeamMP Server";
|
||||||
string Resource = "/Resources";
|
string Resource = "Resources";
|
||||||
string ServerVersion = "0.1";
|
string ServerVersion = "0.1";
|
||||||
string ClientVersion = "0.21";
|
string ClientVersion = "0.21";
|
||||||
|
void HandleResources(const std::string& path);
|
||||||
|
void TCPMain(int Port);
|
||||||
//Entry
|
//Entry
|
||||||
int main() {
|
int main() {
|
||||||
LogInit();
|
LogInit();
|
||||||
ParseConfig();
|
ParseConfig();
|
||||||
|
HandleResources(Resource);
|
||||||
HeartbeatInit();
|
HeartbeatInit();
|
||||||
if(Debug){ //checks if debug is on
|
if(Debug){ //checks if debug is on
|
||||||
DebugData(); //Prints Debug Data
|
DebugData(); //Prints Debug Data
|
||||||
}
|
}
|
||||||
setLoggerLevel("ALL");
|
setLoggerLevel("ALL");
|
||||||
|
std::thread TCPThread(TCPMain,TCPPort);
|
||||||
|
TCPThread.detach();
|
||||||
ServerMain(UDPPort, MaxPlayers);
|
ServerMain(UDPPort, MaxPlayers);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void DebugData(){
|
void DebugData(){
|
||||||
cout << "Debug : true" << "\n";
|
cout << "Debug : true" << endl;
|
||||||
cout << "Port : " << UDPPort << "\n";
|
cout << "Port : " << UDPPort << endl;
|
||||||
cout << "MaxPlayers : " << MaxPlayers << "\n";
|
cout << "TCP Port : " << TCPPort << endl;
|
||||||
cout << "MapName : " << MapName << "\n";
|
cout << "MaxPlayers : " << MaxPlayers << endl;
|
||||||
cout << "ServerName : " << ServerName << "\n";
|
cout << "MapName : " << MapName << endl;
|
||||||
cout << "File : " << Resource << "\n";
|
cout << "ServerName : " << ServerName << endl;
|
||||||
|
cout << "File : " << Resource << endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
void SetMainValues(bool D, int P,int MP,string Name,string serverName,string filename){
|
void SetMainValues(bool D, int P, int FP,int MP,string Name,string serverName,string filename){
|
||||||
Debug = D;
|
Debug = D;
|
||||||
UDPPort = P;
|
UDPPort = P;
|
||||||
|
TCPPort = FP;
|
||||||
MapName = Name;
|
MapName = Name;
|
||||||
ServerName = serverName;
|
ServerName = serverName;
|
||||||
MaxPlayers = MP;
|
MaxPlayers = MP;
|
||||||
|
|||||||
Reference in New Issue
Block a user