add ip to identifiers, changed value format

This commit is contained in:
Lion Kortlepel 2021-07-03 01:43:29 +02:00
parent 95188042c5
commit 9423831937
No known key found for this signature in database
GPG Key ID: 4322FF2B4C71259B
4 changed files with 35 additions and 24 deletions

View File

@ -12,6 +12,12 @@
class TServer; class TServer;
struct TConnection final {
SOCKET Socket;
struct sockaddr SockAddr;
socklen_t SockAddrLen;
};
class TClient final { class TClient final {
public: public:
using TSetOfVehicleData = std::vector<TVehicleData>; using TSetOfVehicleData = std::vector<TVehicleData>;
@ -30,7 +36,7 @@ public:
TVehicleDataLockPair GetAllCars(); TVehicleDataLockPair GetAllCars();
void SetName(const std::string& Name) { mName = Name; } void SetName(const std::string& Name) { mName = Name; }
void SetRoles(const std::string& Role) { mRole = Role; } void SetRoles(const std::string& Role) { mRole = Role; }
void AddIdentifier(const std::string& ID) { mIdentifiers.insert(ID); }; void SetIdentifier(const std::string& key, const std::string& value) { mIdentifiers[key] = value; }
std::string GetCarData(int Ident); std::string GetCarData(int Ident);
void SetUDPAddr(sockaddr_in Addr) { mUDPAddress = Addr; } void SetUDPAddr(sockaddr_in Addr) { mUDPAddress = Addr; }
void SetDownSock(SOCKET CSock) { mSocket[1] = CSock; } void SetDownSock(SOCKET CSock) { mSocket[1] = CSock; }
@ -38,7 +44,7 @@ public:
void SetStatus(int Status) { mStatus = Status; } void SetStatus(int Status) { mStatus = Status; }
// locks // locks
void DeleteCar(int Ident); void DeleteCar(int Ident);
[[nodiscard]] std::set<std::string> GetIdentifiers() const { return mIdentifiers; } [[nodiscard]] const std::unordered_map<std::string, std::string>& GetIdentifiers() const { return mIdentifiers; }
[[nodiscard]] sockaddr_in GetUDPAddr() const { return mUDPAddress; } [[nodiscard]] sockaddr_in GetUDPAddr() const { return mUDPAddress; }
[[nodiscard]] SOCKET GetDownSock() const { return mSocket[1]; } [[nodiscard]] SOCKET GetDownSock() const { return mSocket[1]; }
[[nodiscard]] SOCKET GetTCPSock() const { return mSocket[0]; } [[nodiscard]] SOCKET GetTCPSock() const { return mSocket[0]; }
@ -78,7 +84,7 @@ private:
bool mIsSyncing = false; bool mIsSyncing = false;
mutable std::mutex mMissedPacketsMutex; mutable std::mutex mMissedPacketsMutex;
std::queue<std::string> mPacketsSync; std::queue<std::string> mPacketsSync;
std::set<std::string> mIdentifiers; std::unordered_map<std::string, std::string> mIdentifiers;
bool mIsGuest = false; bool mIsGuest = false;
std::mutex mVehicleDataMutex; std::mutex mVehicleDataMutex;
TSetOfVehicleData mVehicleData; TSetOfVehicleData mVehicleData;

View File

@ -4,6 +4,8 @@
#include "TResourceManager.h" #include "TResourceManager.h"
#include "TServer.h" #include "TServer.h"
struct TConnection;
class TNetwork { class TNetwork {
public: public:
TNetwork(TServer& Server, TPPSMonitor& PPSMonitor, TResourceManager& ResourceManager); TNetwork(TServer& Server, TPPSMonitor& PPSMonitor, TResourceManager& ResourceManager);
@ -15,8 +17,8 @@ public:
std::string TCPRcv(TClient& c); std::string TCPRcv(TClient& c);
void ClientKick(TClient& c, const std::string& R); void ClientKick(TClient& c, const std::string& R);
[[nodiscard]] bool SyncClient(const std::weak_ptr<TClient>& c); [[nodiscard]] bool SyncClient(const std::weak_ptr<TClient>& c);
void Identify(SOCKET TCPSock); void Identify(const TConnection& client);
void Authentication(SOCKET TCPSock); void Authentication(const TConnection& ClientConnection);
[[nodiscard]] bool CheckBytes(TClient& c, int32_t BytesRcv); [[nodiscard]] bool CheckBytes(TClient& c, int32_t BytesRcv);
void SyncResources(TClient& c); void SyncResources(TClient& c);
[[nodiscard]] bool UDPSend(TClient& Client, std::string Data) const; [[nodiscard]] bool UDPSend(TClient& Client, std::string Data) const;

View File

@ -376,12 +376,11 @@ int lua_GetIdentifiers(lua_State* L) {
if (IDs.empty()) if (IDs.empty())
return 0; return 0;
LuaTable::Begin(L); LuaTable::Begin(L);
for (const std::string& ID : IDs) { for (const auto& Pair : IDs) {
LuaTable::BeginEntry(L, ID.substr(0, ID.find(':')).c_str()); LuaTable::BeginEntry(L, Pair.first);
lua_pushstring(L, ID.c_str()); lua_pushstring(L, Pair.second.c_str());
LuaTable::EndEntry(L); LuaTable::EndEntry(L);
} }
// LuaTable::End(L, "");
} else } else
return 0; return 0;
} else { } else {

View File

@ -163,7 +163,7 @@ void TNetwork::TCPServerMain() {
#else // unix #else // unix
// wondering why we need slightly different implementations of this? // wondering why we need slightly different implementations of this?
// ask ms. // ask ms.
SOCKET client = -1; TConnection client {};
SOCKET Listener = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); SOCKET Listener = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
int optval = 1; int optval = 1;
setsockopt(Listener, SOL_SOCKET, SO_REUSEPORT, &optval, sizeof(optval)); setsockopt(Listener, SOL_SOCKET, SO_REUSEPORT, &optval, sizeof(optval));
@ -193,8 +193,9 @@ void TNetwork::TCPServerMain() {
debug("shutdown during TCP wait for accept loop"); debug("shutdown during TCP wait for accept loop");
break; break;
} }
client = accept(Listener, nullptr, nullptr); client.SockAddrLen = sizeof(client.SockAddr);
if (client == -1) { client.Socket = accept(Listener, &client.SockAddr, &client.SockAddrLen);
if (client.Socket == -1) {
warn(("Got an invalid client socket on connect! Skipping...")); warn(("Got an invalid client socket on connect! Skipping..."));
continue; continue;
} }
@ -203,11 +204,11 @@ void TNetwork::TCPServerMain() {
} catch (const std::exception& e) { } catch (const std::exception& e) {
error(("fatal: ") + std::string(e.what())); error(("fatal: ") + std::string(e.what()));
} }
} while (client); } while (client.Socket);
debug("all ok, arrived at " + std::string(__func__) + ":" + std::to_string(__LINE__)); debug("all ok, arrived at " + std::string(__func__) + ":" + std::to_string(__LINE__));
CloseSocketProper(client); CloseSocketProper(client.Socket);
#endif #endif
} }
@ -216,19 +217,19 @@ void TNetwork::TCPServerMain() {
#include "Json.h" #include "Json.h"
namespace json = rapidjson; namespace json = rapidjson;
void TNetwork::Identify(SOCKET TCPSock) { void TNetwork::Identify(const TConnection& client) {
RegisterThreadAuto(); RegisterThreadAuto();
char Code; char Code;
if (recv(TCPSock, &Code, 1, 0) != 1) { if (recv(client.Socket, &Code, 1, 0) != 1) {
CloseSocketProper(TCPSock); CloseSocketProper(client.Socket);
return; return;
} }
if (Code == 'C') { if (Code == 'C') {
Authentication(TCPSock); Authentication(client);
} else if (Code == 'D') { } else if (Code == 'D') {
HandleDownload(TCPSock); HandleDownload(client.Socket);
} else { } else {
CloseSocketProper(TCPSock); CloseSocketProper(client.Socket);
} }
} }
@ -251,11 +252,12 @@ void TNetwork::HandleDownload(SOCKET TCPSock) {
}); });
} }
void TNetwork::Authentication(SOCKET TCPSock) { void TNetwork::Authentication(const TConnection& ClientConnection) {
auto Client = CreateClient(TCPSock); auto Client = CreateClient(ClientConnection.Socket);
Client->SetIdentifier("ip", inet_ntoa(reinterpret_cast<const struct sockaddr_in*>(&ClientConnection.SockAddr)->sin_addr));
std::string Rc; std::string Rc;
info("Identifying new client..."); info("Identifying new ClientConnection...");
Rc = TCPRcv(*Client); Rc = TCPRcv(*Client);
@ -323,7 +325,9 @@ void TNetwork::Authentication(SOCKET TCPSock) {
Client->SetRoles(AuthResponse["roles"].GetString()); Client->SetRoles(AuthResponse["roles"].GetString());
Client->SetIsGuest(AuthResponse["guest"].GetBool()); Client->SetIsGuest(AuthResponse["guest"].GetBool());
for (const auto& ID : AuthResponse["identifiers"].GetArray()) { for (const auto& ID : AuthResponse["identifiers"].GetArray()) {
Client->AddIdentifier(ID.GetString()); auto Raw = std::string(ID.GetString());
auto SepIndex = Raw.find(':');
Client->SetIdentifier(Raw.substr(0, SepIndex), Raw.substr(SepIndex + 1));
} }
} else { } else {
ClientKick(*Client, "Invalid authentication data!"); ClientKick(*Client, "Invalid authentication data!");