mirror of
https://github.com/BeamMP/BeamMP-Server.git
synced 2026-10-22 16:17:40 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f55931da73 | ||
|
|
7ece770eb7 | ||
|
|
312e991814 | ||
|
|
2585affd80 | ||
|
|
d1bfdde406 | ||
|
|
1b83b4bb3e | ||
|
|
e7a5050e1b | ||
|
|
b50f82166b | ||
|
|
b21d4062c6 | ||
|
|
cd4cc3e630 | ||
|
|
a8dfa4b004 | ||
|
|
69233f4185 | ||
|
|
38de5be365 | ||
|
|
14c69893e8 |
No files matched your search
@@ -85,10 +85,12 @@ public:
|
||||
[[nodiscard]] std::string GetName() const { return mName; }
|
||||
void SetUnicycleID(int ID) { mUnicycleID = ID; }
|
||||
void SetID(int ID) { mID = ID; }
|
||||
void SetSelfPing(uint16_t ping ) { mSelfPing = ping; }
|
||||
[[nodiscard]] int GetOpenCarID() const;
|
||||
[[nodiscard]] int GetCarCount() const;
|
||||
void ClearCars();
|
||||
[[nodiscard]] int GetID() const { return mID; }
|
||||
[[nodiscard]] uint16_t GetSelfPing() const { return mSelfPing; }
|
||||
[[nodiscard]] int GetUnicycleID() const { return mUnicycleID; }
|
||||
[[nodiscard]] bool IsUDPConnected() const { return mIsUDPConnected; }
|
||||
[[nodiscard]] bool IsSynced() const { return mIsSynced; }
|
||||
@@ -139,6 +141,7 @@ private:
|
||||
std::string mRole;
|
||||
std::string mDID;
|
||||
int mID = -1;
|
||||
uint16_t mSelfPing = 0;
|
||||
std::chrono::time_point<std::chrono::high_resolution_clock> mLastPingTime = std::chrono::high_resolution_clock::now();
|
||||
std::vector<uint8_t> mMagic;
|
||||
};
|
||||
|
||||
+1
-1
@@ -129,7 +129,7 @@ private:
|
||||
static inline std::mutex mShutdownHandlersMutex {};
|
||||
static inline std::deque<TShutdownHandler> mShutdownHandlers {};
|
||||
|
||||
static inline Version mVersion { 3, 9, 3 };
|
||||
static inline Version mVersion { 3, 9, 4 };
|
||||
};
|
||||
|
||||
/// Used to static_assert in std::visit
|
||||
|
||||
@@ -53,7 +53,10 @@ public:
|
||||
|
||||
io_context& IoCtx() { return mIoCtxPoller.IoCtx(); }
|
||||
|
||||
uint64_t GetServerTimeMS() const { return std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - TimeSyncStart).count();}
|
||||
double GetServerTime() const { return std::chrono::duration<double>(std::chrono::steady_clock::now() - TimeSyncStart).count(); }
|
||||
private:
|
||||
std::chrono::time_point<std::chrono::steady_clock> TimeSyncStart;
|
||||
TIoPollThread mIoCtxPoller;
|
||||
TClientSet mClients;
|
||||
mutable RWMutex mClientsMutex;
|
||||
|
||||
+18
-12
@@ -693,6 +693,22 @@ std::string LuaAPI::FS::ConcatPaths(sol::variadic_args Args) {
|
||||
return Result;
|
||||
}
|
||||
|
||||
static bool IsLuaArray(const sol::table& table) {
|
||||
for (const auto& pair : table) {
|
||||
if (pair.first.get_type() != sol::type::number) {
|
||||
return false;
|
||||
}
|
||||
if (!pair.first.is<int>()) {
|
||||
return false;
|
||||
}
|
||||
int key = pair.first.as<int>();
|
||||
if (key < 1) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
static void JsonEncodeRecursive(nlohmann::json& json, const sol::object& left, const sol::object& right, bool is_array, size_t depth = 0) {
|
||||
if (depth > 100) {
|
||||
beammp_lua_error("json serialize will not go deeper than 100 nested tables, internal references assumed, aborted this path");
|
||||
@@ -762,12 +778,7 @@ static void JsonEncodeRecursive(nlohmann::json& json, const sol::object& left, c
|
||||
if (right.as<sol::table>().empty()) {
|
||||
value = nlohmann::json::object();
|
||||
} else {
|
||||
bool local_is_array = true;
|
||||
for (const auto& pair : right.as<sol::table>()) {
|
||||
if (pair.first.get_type() != sol::type::number) {
|
||||
local_is_array = false;
|
||||
}
|
||||
}
|
||||
bool local_is_array = IsLuaArray(right.as<sol::table>());
|
||||
for (const auto& pair : right.as<sol::table>()) {
|
||||
JsonEncodeRecursive(value, pair.first, pair.second, local_is_array, depth + 1);
|
||||
}
|
||||
@@ -790,12 +801,7 @@ std::string LuaAPI::MP::JsonEncode(const sol::table& object) {
|
||||
if (object.as<sol::table>().empty()) {
|
||||
json = nlohmann::json::object();
|
||||
} else {
|
||||
bool is_array = true;
|
||||
for (const auto& pair : object.as<sol::table>()) {
|
||||
if (pair.first.get_type() != sol::type::number) {
|
||||
is_array = false;
|
||||
}
|
||||
}
|
||||
bool is_array = IsLuaArray(object.as<sol::table>());
|
||||
for (const auto& entry : object) {
|
||||
JsonEncodeRecursive(json, entry.first, entry.second, is_array);
|
||||
}
|
||||
|
||||
@@ -1012,6 +1012,14 @@ TLuaEngine::StateThreadData::StateThreadData(const std::string& Name, TLuaStateI
|
||||
}
|
||||
MPTable.set_function("Get", &LuaAPI::MP::Get);
|
||||
|
||||
MPTable.set_function("GetServerTimeMS", [this](const sol::this_state ts) {
|
||||
return make_object(sol::state_view(ts), this->mEngine->Server().GetServerTimeMS());
|
||||
});
|
||||
|
||||
MPTable.set_function("GetServerTime", [this](const sol::this_state ts) {
|
||||
return make_object(sol::state_view(ts), this->mEngine->Server().GetServerTime());
|
||||
});
|
||||
|
||||
auto UtilTable = StateView.create_named_table("Util");
|
||||
UtilTable.set_function("LogDebug", [this](sol::variadic_args args) {
|
||||
std::string ToPrint = "";
|
||||
|
||||
+34
-7
@@ -198,13 +198,7 @@ void TNetwork::UDPServerMain() {
|
||||
if (Client->GetID() == ID) {
|
||||
if (Client->GetUDPAddr() == boost::asio::ip::udp::endpoint { } && !Client->IsUDPConnected() && !Client->GetMagic().empty()) {
|
||||
if (Data.size() != 66) {
|
||||
beammp_debugf("Invalid size for UDP value. IP: {} ID: {}, Size: {}", remote_client_ep.address().to_string(), ID, Data.size());
|
||||
std::stringstream hexData;
|
||||
for (auto c : Data) {
|
||||
hexData << std::hex << std::setw(2) << std::setfill('0') << static_cast<int>(c);
|
||||
hexData << " ";
|
||||
}
|
||||
beammp_debug(hexData.str());
|
||||
beammp_debugf("Invalid size for UDP value. IP: {} ID: {}", remote_client_ep.address().to_string(), ID);
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -825,6 +819,39 @@ void TNetwork::UpdatePlayer(TClient& Client) {
|
||||
});
|
||||
Packet = Packet.substr(0, Packet.length() - 1);
|
||||
Client.EnqueuePacket(StringToVector(Packet));
|
||||
|
||||
std::vector<uint8_t> PingsPacket = StringToVector("Sp");
|
||||
|
||||
PingsPacket.push_back(0);
|
||||
|
||||
uint8_t amount = 0;
|
||||
|
||||
mServer.ForEachClient([&](const std::weak_ptr<TClient>& ClientPtr) -> bool {
|
||||
if (amount == UINT8_MAX) {
|
||||
beammp_debugf("Client ping packet overflow");
|
||||
return false;
|
||||
}
|
||||
ReadLock Lock(mServer.GetClientMutex());
|
||||
if (const auto c = ClientPtr.lock()) {
|
||||
int ID = c->GetID();
|
||||
if (ID < 0 || ID > UINT8_MAX) {
|
||||
beammp_debugf("Invalid ID found: {}", ID);
|
||||
return true;
|
||||
}
|
||||
PingsPacket.push_back(static_cast<uint8_t>(ID));
|
||||
const uint16_t ping = c->GetSelfPing();
|
||||
|
||||
PingsPacket.push_back(static_cast<uint8_t>(ping));
|
||||
PingsPacket.push_back(static_cast<uint8_t>(ping >> 8));
|
||||
|
||||
amount++;
|
||||
}
|
||||
return true;
|
||||
});
|
||||
|
||||
PingsPacket[2] = amount;
|
||||
|
||||
Client.EnqueuePacket(PingsPacket);
|
||||
//(void)Respond(Client, Packet, true);
|
||||
}
|
||||
|
||||
|
||||
@@ -123,6 +123,12 @@ TEST_CASE("GetPidVid") {
|
||||
TServer::TServer(const std::vector<std::string_view>& Arguments) {
|
||||
beammp_info("BeamMP Server v" + Application::ServerVersionString());
|
||||
Application::SetSubsystemStatus("Server", Application::Status::Starting);
|
||||
std::mt19937_64 TimeSyncRandomDevice(std::random_device{}());
|
||||
|
||||
TimeSyncStart = std::chrono::steady_clock::now()
|
||||
- std::chrono::duration_cast<std::chrono::steady_clock::duration>(
|
||||
std::chrono::duration<double>(std::uniform_real_distribution<double>(0.0, 2592000.0)(TimeSyncRandomDevice))
|
||||
);
|
||||
Application::SetSubsystemStatus("Server", Application::Status::Good);
|
||||
}
|
||||
|
||||
@@ -210,6 +216,30 @@ void TServer::GlobalParser(const std::weak_ptr<TClient>& Client, std::vector<uin
|
||||
return;
|
||||
}
|
||||
switch (Code) {
|
||||
case 't':
|
||||
if (!udp && Packet.size() == 11) {
|
||||
uint64_t Time = GetServerTimeMS();
|
||||
|
||||
std::vector<uint8_t> ServerTime(sizeof(uint64_t));
|
||||
std::memcpy(ServerTime.data(), &Time, sizeof(uint64_t));
|
||||
|
||||
uint16_t selfPing = 0;
|
||||
std::memcpy(&selfPing, &Packet[Packet.size() - 2], sizeof(uint16_t));
|
||||
|
||||
LockedClient->SetSelfPing(selfPing);
|
||||
|
||||
Packet.erase(Packet.end() - 2,Packet.end());
|
||||
auto Data = Packet;
|
||||
|
||||
beammp_debugf("Sending server time {} to client '{}' ({}), client time: {}", Time, LockedClient->GetName(), LockedClient->GetID(), reinterpret_cast<uint64_t>(Data.data()));
|
||||
|
||||
Data.insert(Data.end(), ServerTime.begin(), ServerTime.end());
|
||||
|
||||
if (!Network.Respond(*LockedClient, Data, true)) {
|
||||
Network.DisconnectClient(*LockedClient, "Failed to send time");
|
||||
}
|
||||
}
|
||||
return;
|
||||
case 'H': // initial connection
|
||||
if (udp) {
|
||||
beammp_debugf("Received 'H' packet over UDP from client '{}' ({}), ignoring it", LockedClient->GetName(), LockedClient->GetID());
|
||||
|
||||
Reference in new issue
Block a user