mirror of
https://github.com/SantaSpeen/BeamMP-Server.git
synced 2025-07-02 11:05:25 +00:00
Fix Console on Unix, adapt console behavior to that of a traditional
console, add Assert.h, add clang-format file with modified WebKit style
This commit is contained in:
parent
13e79e407c
commit
eead954bf9
5
.clang-format
Normal file
5
.clang-format
Normal file
@ -0,0 +1,5 @@
|
||||
---
|
||||
BasedOnStyle: WebKit
|
||||
BreakBeforeBraces: Attach
|
||||
|
||||
...
|
59
include/Assert.h
Normal file
59
include/Assert.h
Normal file
@ -0,0 +1,59 @@
|
||||
// Author: lionkor
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
|
||||
#include "Logger.h"
|
||||
|
||||
static const char* const ANSI_RESET = "\u001b[0m";
|
||||
|
||||
static const char* const ANSI_BLACK = "\u001b[30m";
|
||||
static const char* const ANSI_RED = "\u001b[31m";
|
||||
static const char* const ANSI_GREEN = "\u001b[32m";
|
||||
static const char* const ANSI_YELLOW = "\u001b[33m";
|
||||
static const char* const ANSI_BLUE = "\u001b[34m";
|
||||
static const char* const ANSI_MAGENTA = "\u001b[35m";
|
||||
static const char* const ANSI_CYAN = "\u001b[36m";
|
||||
static const char* const ANSI_WHITE = "\u001b[37m";
|
||||
|
||||
static const char* const ANSI_BLACK_BOLD = "\u001b[30;1m";
|
||||
static const char* const ANSI_RED_BOLD = "\u001b[31;1m";
|
||||
static const char* const ANSI_GREEN_BOLD = "\u001b[32;1m";
|
||||
static const char* const ANSI_YELLOW_BOLD = "\u001b[33;1m";
|
||||
static const char* const ANSI_BLUE_BOLD = "\u001b[34;1m";
|
||||
static const char* const ANSI_MAGENTA_BOLD = "\u001b[35;1m";
|
||||
static const char* const ANSI_CYAN_BOLD = "\u001b[36;1m";
|
||||
static const char* const ANSI_WHITE_BOLD = "\u001b[37;1m";
|
||||
|
||||
static const char* const ANSI_BOLD = "\u001b[1m";
|
||||
static const char* const ANSI_UNDERLINE = "\u001b[4m";
|
||||
|
||||
inline void _assert(const char* file, const char* function, unsigned line,
|
||||
const char* condition_string, bool result) {
|
||||
if (!result) {
|
||||
#if DEBUG
|
||||
fprintf(stderr,
|
||||
"%sASSERTION FAILED%s at %s%s:%u%s \n\t-> in %s%s%s, Line %u: \n\t\t-> "
|
||||
"Failed Condition: %s%s%s\n",
|
||||
ANSI_RED_BOLD, ANSI_RESET, ANSI_UNDERLINE, file, line, ANSI_RESET,
|
||||
ANSI_BOLD, function, ANSI_RESET, line, ANSI_RED, condition_string,
|
||||
ANSI_RESET);
|
||||
fprintf(stderr, "%s... terminating with SIGABRT ...%s\n", ANSI_BOLD, ANSI_RESET);
|
||||
abort();
|
||||
#else
|
||||
char buf[2048];
|
||||
sprintf(buf,
|
||||
"%s=> ASSERTION `%s` FAILED IN RELEASE BUILD%s%s -> IGNORING FAILED ASSERTION "
|
||||
"& HOPING IT WON'T CRASH%s\n",
|
||||
ANSI_RED_BOLD, condition_string, ANSI_RESET, ANSI_RED, ANSI_RESET);
|
||||
error(buf);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
#ifndef ASSERT
|
||||
#define Assert(cond) _assert(__FILE__, __func__, __LINE__, #cond, (cond))
|
||||
#endif // ASSERT
|
||||
#define AssertNotReachable() _assert(__FILE__, __func__, __LINE__, "reached unreachable code", false)
|
@ -3,12 +3,13 @@
|
||||
///
|
||||
#pragma once
|
||||
#include <mutex>
|
||||
#include "Assert.h"
|
||||
class Client;
|
||||
void GParser(Client*c, const std::string&Packet);
|
||||
class Buffer{
|
||||
public:
|
||||
void Handle(Client*c,const std::string& Data){
|
||||
if(c == nullptr)return;
|
||||
Assert(c);
|
||||
Buf += Data;
|
||||
Manage(c);
|
||||
}
|
||||
@ -18,6 +19,7 @@ public:
|
||||
private:
|
||||
std::string Buf;
|
||||
void Manage(Client*c){
|
||||
Assert(c);
|
||||
if(!Buf.empty()){
|
||||
std::string::size_type p;
|
||||
if (Buf.at(0) == '\n'){
|
||||
|
@ -8,6 +8,8 @@
|
||||
#include <conio.h>
|
||||
#else // *nix
|
||||
typedef unsigned long DWORD, *PDWORD, *LPDWORD;
|
||||
#include <termios.h>
|
||||
#include <unistd.h>
|
||||
#endif // __WIN32
|
||||
#include "Logger.h"
|
||||
#include <iostream>
|
||||
@ -20,6 +22,7 @@ std::string CInputBuff;
|
||||
std::mutex MLock;
|
||||
Lua* LuaConsole;
|
||||
void HandleInput(const std::string& cmd){
|
||||
std::cout << std::endl;
|
||||
if (cmd == "exit") {
|
||||
exit(0);
|
||||
}else LuaConsole->Execute(cmd);
|
||||
@ -28,13 +31,12 @@ void HandleInput(const std::string& cmd){
|
||||
void ProcessOut(){
|
||||
static size_t len = 2;
|
||||
if(QConsoleOut.empty() && len == CInputBuff.length())return;
|
||||
printf("%c[2K\r", 27);
|
||||
for(const std::string& msg : QConsoleOut)
|
||||
if(!msg.empty())std::cout << msg;
|
||||
MLock.lock();
|
||||
QConsoleOut.clear();
|
||||
MLock.unlock();
|
||||
std::cout << "> " << CInputBuff;
|
||||
std::cout << "> " << CInputBuff << std::flush;
|
||||
len = CInputBuff.length();
|
||||
}
|
||||
|
||||
@ -50,6 +52,32 @@ void ConsoleOut(const std::string& msg){
|
||||
ProcessOut();
|
||||
}
|
||||
}
|
||||
|
||||
#ifndef __WIN32
|
||||
static int _getch()
|
||||
{
|
||||
char buf = 0;
|
||||
struct termios old;
|
||||
fflush(stdout);
|
||||
if(tcgetattr(0, &old) < 0)
|
||||
perror("tcsetattr()");
|
||||
old.c_lflag &= ~unsigned(ICANON);
|
||||
old.c_lflag &= ~unsigned(ECHO);
|
||||
old.c_cc[VMIN] = 1;
|
||||
old.c_cc[VTIME] = 0;
|
||||
if(tcsetattr(0, TCSANOW, &old) < 0)
|
||||
perror("tcsetattr ICANON");
|
||||
if(read(0, &buf, 1) < 0)
|
||||
perror("read()");
|
||||
old.c_lflag |= ICANON;
|
||||
old.c_lflag |= ECHO;
|
||||
if(tcsetattr(0, TCSADRAIN, &old) < 0)
|
||||
perror("tcsetattr ~ICANON");
|
||||
// no echo printf("%c\n", buf);
|
||||
return buf;
|
||||
}
|
||||
#endif // __WIN32
|
||||
|
||||
void SetupConsole(){
|
||||
#ifdef __WIN32
|
||||
DWORD outMode = 0;
|
||||
@ -71,19 +99,28 @@ void SetupConsole(){
|
||||
std::this_thread::sleep_for(std::chrono::seconds(3));
|
||||
exit(GetLastError());
|
||||
}
|
||||
#else
|
||||
#endif // __WIN32
|
||||
}
|
||||
|
||||
[[noreturn]] void ReadCin(){
|
||||
while (true){
|
||||
int In = getchar();
|
||||
if (In == 13) {
|
||||
int In = _getch();
|
||||
if (In == 13 || In == '\n') {
|
||||
if(!CInputBuff.empty()) {
|
||||
HandleInput(CInputBuff);
|
||||
CInputBuff.clear();
|
||||
}
|
||||
}else if(In == 8){
|
||||
if(!CInputBuff.empty())CInputBuff.pop_back();
|
||||
}else CInputBuff += char(In);
|
||||
} else if (In == 4) {
|
||||
CInputBuff = "exit";
|
||||
HandleInput(CInputBuff);
|
||||
CInputBuff.clear();
|
||||
}else {
|
||||
printf("%c[2K\r", 27);
|
||||
CInputBuff += char(In);
|
||||
}
|
||||
}
|
||||
}
|
||||
void ConsoleInit(){
|
||||
|
@ -163,7 +163,7 @@ void GlobalParser(Client*c, const std::string& Pack){
|
||||
}
|
||||
|
||||
switch (Code) {
|
||||
case 'P':
|
||||
case 'P': // initial connection
|
||||
Respond(c, Sec("P") + std::to_string(c->GetID()),true);
|
||||
SyncClient(c);
|
||||
return;
|
||||
|
@ -1,10 +1,10 @@
|
||||
///
|
||||
/// Created by Anonymous275 on 8/1/2020
|
||||
///
|
||||
#include "Security/Enc.h"
|
||||
#include "Client.hpp"
|
||||
#include "Settings.h"
|
||||
#include "Logger.h"
|
||||
#include "Security/Enc.h"
|
||||
#include "Settings.h"
|
||||
#include "UnixCompat.h"
|
||||
#include <fstream>
|
||||
|
||||
@ -14,13 +14,16 @@
|
||||
#endif // __linux
|
||||
|
||||
void STCPSend(Client* c, std::string Data) {
|
||||
if(c == nullptr)return;
|
||||
if (c == nullptr)
|
||||
return;
|
||||
ssize_t BytesSent = send(c->GetTCPSock(), Data.c_str(), size_t(Data.size()), 0);
|
||||
Data.clear();
|
||||
if (BytesSent == 0) {
|
||||
if(c->GetStatus() > -1)c->SetStatus(-1);
|
||||
if (c->GetStatus() > -1)
|
||||
c->SetStatus(-1);
|
||||
} else if (BytesSent < 0) {
|
||||
if(c->GetStatus() > -1)c->SetStatus(-1);
|
||||
if (c->GetStatus() > -1)
|
||||
c->SetStatus(-1);
|
||||
closesocket(c->GetTCPSock());
|
||||
}
|
||||
}
|
||||
@ -58,9 +61,11 @@ void SendFile(Client*c,const std::string&Name){
|
||||
}
|
||||
|
||||
void Parse(Client* c, const std::string& Packet) {
|
||||
if(c == nullptr || Packet.empty())return;
|
||||
if (c == nullptr || Packet.empty())
|
||||
return;
|
||||
char Code = Packet.at(0), SubCode = 0;
|
||||
if(Packet.length() > 1)SubCode = Packet.at(1);
|
||||
if (Packet.length() > 1)
|
||||
SubCode = Packet.at(1);
|
||||
switch (Code) {
|
||||
case 'f':
|
||||
SendFile(c, Packet.substr(1));
|
||||
@ -69,7 +74,8 @@ void Parse(Client*c,const std::string&Packet){
|
||||
if (SubCode == 'R') {
|
||||
debug(Sec("Sending Mod Info"));
|
||||
std::string ToSend = FileList + FileSizes;
|
||||
if(ToSend.empty())ToSend = "-";
|
||||
if (ToSend.empty())
|
||||
ToSend = "-";
|
||||
STCPSend(c, ToSend);
|
||||
}
|
||||
return;
|
||||
@ -78,31 +84,37 @@ void Parse(Client*c,const std::string&Packet){
|
||||
}
|
||||
}
|
||||
bool STCPRecv(Client* c) {
|
||||
if(c == nullptr)return false;
|
||||
if (c == nullptr)
|
||||
return false;
|
||||
char buf[200];
|
||||
size_t len = 200;
|
||||
ZeroMemory(buf, len);
|
||||
ssize_t BytesRcv = recv(c->GetTCPSock(), buf, len, 0);
|
||||
if (BytesRcv == 0) {
|
||||
if(c->GetStatus() > -1)c->SetStatus(-1);
|
||||
if (c->GetStatus() > -1)
|
||||
c->SetStatus(-1);
|
||||
closesocket(c->GetTCPSock());
|
||||
return false;
|
||||
} else if (BytesRcv < 0) {
|
||||
if(c->GetStatus() > -1)c->SetStatus(-1);
|
||||
if (c->GetStatus() > -1)
|
||||
c->SetStatus(-1);
|
||||
closesocket(c->GetTCPSock());
|
||||
return false;
|
||||
}
|
||||
if(strcmp(buf,"Done") == 0)return false;
|
||||
if (strcmp(buf, "Done") == 0)
|
||||
return false;
|
||||
std::string Ret(buf, size_t(BytesRcv));
|
||||
Parse(c, Ret);
|
||||
return true;
|
||||
}
|
||||
|
||||
void SyncResources(Client* c) {
|
||||
if(c == nullptr)return;
|
||||
if (c == nullptr)
|
||||
return;
|
||||
try {
|
||||
STCPSend(c, Sec("WS"));
|
||||
while(c->GetStatus() > -1 && STCPRecv(c));
|
||||
while (c->GetStatus() > -1 && STCPRecv(c))
|
||||
;
|
||||
} catch (std::exception& e) {
|
||||
except(Sec("Exception! : ") + std::string(e.what()));
|
||||
c->SetStatus(-1);
|
||||
|
@ -2,19 +2,19 @@
|
||||
/// Created by Anonymous275 on 5/8/2020
|
||||
///
|
||||
///UDP
|
||||
#include "Security/Enc.h"
|
||||
#include "Compressor.h"
|
||||
#include "Client.hpp"
|
||||
#include "Settings.h"
|
||||
#include "Network.h"
|
||||
#include "Compressor.h"
|
||||
#include "Logger.h"
|
||||
#include "Network.h"
|
||||
#include "Security/Enc.h"
|
||||
#include "Settings.h"
|
||||
#include "UnixCompat.h"
|
||||
#include <sstream>
|
||||
#include <vector>
|
||||
#include <thread>
|
||||
#include <array>
|
||||
#include <cmath>
|
||||
#include <cstring>
|
||||
#include <sstream>
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
int FC(const std::string& s, const std::string& p, int n);
|
||||
struct PacketData {
|
||||
int ID;
|
||||
@ -32,7 +32,8 @@ SOCKET UDPSock;
|
||||
std::set<PacketData*> DataAcks;
|
||||
std::set<SplitData*> SplitPackets;
|
||||
void UDPSend(Client* c, std::string Data) {
|
||||
if(c == nullptr || !c->isConnected || c->GetStatus() < 0)return;
|
||||
if (c == nullptr || !c->isConnected || c->GetStatus() < 0)
|
||||
return;
|
||||
sockaddr_in Addr = c->GetUDPAddr();
|
||||
socklen_t AddrSize = sizeof(c->GetUDPAddr());
|
||||
Data = Data.substr(0, Data.find(char(0)));
|
||||
@ -44,12 +45,14 @@ void UDPSend(Client*c,std::string Data){
|
||||
#ifdef __WIN32
|
||||
if (sendOk != 0) {
|
||||
debug(Sec("(UDP) Send Failed Code : ") + std::to_string(WSAGetLastError()));
|
||||
if(c->GetStatus() > -1)c->SetStatus(-1);
|
||||
if (c->GetStatus() > -1)
|
||||
c->SetStatus(-1);
|
||||
}
|
||||
#else // unix
|
||||
if (sendOk != 0) {
|
||||
debug(Sec("(UDP) Send Failed Code : ") + std::string(strerror(errno)));
|
||||
if(c->GetStatus() > -1)c->SetStatus(-1);
|
||||
if (c->GetStatus() > -1)
|
||||
c->SetStatus(-1);
|
||||
}
|
||||
#endif // __WIN32
|
||||
}
|
||||
@ -64,14 +67,18 @@ void AckID(int ID){
|
||||
}
|
||||
int PacktID() {
|
||||
static int ID = -1;
|
||||
if(ID > 999999)ID = 0;
|
||||
else ID++;
|
||||
if (ID > 999999)
|
||||
ID = 0;
|
||||
else
|
||||
ID++;
|
||||
return ID;
|
||||
}
|
||||
int SplitID() {
|
||||
static int SID = -1;
|
||||
if(SID > 999999)SID = 0;
|
||||
else SID++;
|
||||
if (SID > 999999)
|
||||
SID = 0;
|
||||
else
|
||||
SID++;
|
||||
return SID;
|
||||
}
|
||||
void SendLarge(Client* c, std::string Data) {
|
||||
@ -83,16 +90,14 @@ void SendLarge(Client*c,std::string Data){
|
||||
int S = 1, Split = int(ceil(float(pckt.length()) / 1000));
|
||||
int SID = SplitID();
|
||||
while (pckt.length() > 1000) {
|
||||
Packet = "SC|"+std::to_string(S)+"|"+std::to_string(Split)+"|"+std::to_string(ID)+"|"+
|
||||
std::to_string(SID)+"|"+pckt.substr(0,1000);
|
||||
Packet = "SC|" + std::to_string(S) + "|" + std::to_string(Split) + "|" + std::to_string(ID) + "|" + std::to_string(SID) + "|" + pckt.substr(0, 1000);
|
||||
DataAcks.insert(new PacketData { ID, c, Packet, 1 });
|
||||
UDPSend(c, Packet);
|
||||
pckt = pckt.substr(1000);
|
||||
S++;
|
||||
ID = PacktID();
|
||||
}
|
||||
Packet = "SC|"+std::to_string(S)+"|"+std::to_string(Split)+"|"+
|
||||
std::to_string(ID)+"|"+std::to_string(SID)+"|"+pckt;
|
||||
Packet = "SC|" + std::to_string(S) + "|" + std::to_string(Split) + "|" + std::to_string(ID) + "|" + std::to_string(SID) + "|" + pckt;
|
||||
DataAcks.insert(new PacketData { ID, c, Packet, 1 });
|
||||
UDPSend(c, Packet);
|
||||
} else {
|
||||
@ -125,9 +130,11 @@ bool Handled(Client*c,int ID){
|
||||
for (HandledC* h : HandledIDs) {
|
||||
if (h->c == c) {
|
||||
for (int id : h->HandledIDs) {
|
||||
if(id == ID)return true;
|
||||
if (id == ID)
|
||||
return true;
|
||||
}
|
||||
if(h->Pos > 99)h->Pos = 0;
|
||||
if (h->Pos > 99)
|
||||
h->Pos = 0;
|
||||
h->HandledIDs.at(h->Pos) = ID;
|
||||
h->Pos++;
|
||||
handle = true;
|
||||
@ -142,7 +149,8 @@ bool Handled(Client*c,int ID){
|
||||
if (!handle) {
|
||||
HandledC* h = GetHandled(c);
|
||||
ResetIDs(h);
|
||||
if (h->Pos > 99)h->Pos = 0;
|
||||
if (h->Pos > 99)
|
||||
h->Pos = 0;
|
||||
h->HandledIDs.at(h->Pos) = ID;
|
||||
h->Pos++;
|
||||
h->c = c;
|
||||
@ -168,7 +176,8 @@ std::string UDPRcvFromClient(sockaddr_in& client){
|
||||
|
||||
SplitData* GetSplit(int SplitID) {
|
||||
for (SplitData* a : SplitPackets) {
|
||||
if(a->ID == SplitID)return a;
|
||||
if (a->ID == SplitID)
|
||||
return a;
|
||||
}
|
||||
auto* SP = new SplitData();
|
||||
SplitPackets.insert(SP);
|
||||
@ -176,14 +185,16 @@ SplitData*GetSplit(int SplitID){
|
||||
}
|
||||
void HandleChunk(Client* c, const std::string& Data) {
|
||||
int pos = FC(Data, "|", 5);
|
||||
if(pos == -1)return;
|
||||
if (pos == -1)
|
||||
return;
|
||||
std::stringstream ss(Data.substr(0, size_t(pos++)));
|
||||
std::string t;
|
||||
int I = -1;
|
||||
//Current Max ID SID
|
||||
std::vector<int> Num(4, 0);
|
||||
while (std::getline(ss, t, '|')) {
|
||||
if(I >= 0)Num.at(size_t(I)) = std::stoi(t);
|
||||
if (I >= 0)
|
||||
Num.at(size_t(I)) = std::stoi(t);
|
||||
I++;
|
||||
}
|
||||
std::string ack = "TRG:" + std::to_string(Num.at(2));
|
||||
@ -285,7 +296,8 @@ void LOOP(){
|
||||
sockaddr_in client {};
|
||||
std::string Data = UDPRcvFromClient(client); //Receives any data from Socket
|
||||
auto Pos = Data.find(':');
|
||||
if(Data.empty() || Pos < 0 || Pos > 2)continue;
|
||||
if (Data.empty() || Pos < 0 || Pos > 2)
|
||||
continue;
|
||||
/*char clientIp[256];
|
||||
ZeroMemory(clientIp, 256); ///Code to get IP we don't need that yet
|
||||
inet_ntop(AF_INET, &client.sin_addr, clientIp, 256);*/
|
||||
@ -326,7 +338,8 @@ void LOOP(){
|
||||
sockaddr_in client {};
|
||||
std::string Data = UDPRcvFromClient(client); //Receives any data from Socket
|
||||
size_t Pos = Data.find(':');
|
||||
if(Data.empty() || Pos > 2)continue;
|
||||
if (Data.empty() || Pos > 2)
|
||||
continue;
|
||||
/*char clientIp[256];
|
||||
ZeroMemory(clientIp, 256); ///Code to get IP we don't need that yet
|
||||
inet_ntop(AF_INET, &client.sin_addr, clientIp, 256);*/
|
||||
|
Loading…
x
Reference in New Issue
Block a user