More networking progress

This commit is contained in:
momo5502 2022-10-24 21:00:22 +02:00
parent ee86be0cf5
commit e89ab95048
3 changed files with 48 additions and 18 deletions

View File

@ -1,4 +1,5 @@
#include <std_include.hpp>
#include "network.hpp"
#include "loader/component_loader.hpp"
#include "scheduler.hpp"
@ -11,9 +12,6 @@ namespace network
{
namespace
{
using data_view = std::basic_string_view<uint8_t>;
using callback = std::function<void(const game::netadr_t&, const data_view&)>;
std::unordered_map<std::string, callback>& get_callbacks()
{
static std::unordered_map<std::string, callback> callbacks{};
@ -91,11 +89,31 @@ namespace network
throw std::runtime_error("Failed to bind socket");
}
}
}
void on(const std::string& command, const callback& callback)
{
get_callbacks()[utils::string::to_lower(command)] = callback;
}
void send(const game::netadr_t& address, const std::string& command, const std::string& data, const char separator)
{
std::string packet = "\xFF\xFF\xFF\xFF";
packet.append(command);
packet.push_back(separator);
packet.append(data);
send_data(address, packet);
}
void send_data(const game::netadr_t& address, const void* data, const size_t size)
{
game::NET_SendPacket(game::NS_CLIENT1, static_cast<int>(size), data, &address);
}
void send_data(const game::netadr_t& address, const std::string& data)
{
send_data(address, data.data(), data.size());
}
class component final : public component_interface
@ -134,19 +152,16 @@ namespace network
/*std::thread([]
{
while (true)
{
{
MessageBoxA(0, 0, 0, 0);
std::string data = utils::string::va("\xFF\xFF\xFF\xFF" "getservers S1 %i full empty", 1);
game::netadr_t addr{};
addr.type = game::NA_RAWIP;
addr.port = 20810;
*reinterpret_cast<unsigned long*>(&addr.ipv4.a) = inet_addr("116.203.183.23");
addr.localNetID = game::NS_CLIENT1;
game::NET_SendPacket(game::NS_CLIENT1, static_cast<int>(data.size()), data.data(), &addr);
}
send(addr, "getservers", utils::string::va("T7 %i full empty", 1));
}
}).detach();*/
}

View File

@ -0,0 +1,15 @@
#pragma once
#include "game/game.hpp"
namespace network
{
using data_view = std::basic_string_view<uint8_t>;
using callback = std::function<void(const game::netadr_t&, const data_view&)>;
void on(const std::string& command, const callback& callback);
void send(const game::netadr_t& address, const std::string& command, const std::string& data = {},
char separator = ' ');
void send_data(const game::netadr_t& address, const void* data, size_t length);
void send_data(const game::netadr_t& address, const std::string& data);
}

View File

@ -23,7 +23,7 @@ namespace game
WEAK symbol<bool(uint64_t, int*, bool)> Live_GetConnectivityInformation{0x141E0C410_g};
// NET
WEAK symbol<bool(netsrc_t sock, int length, const void* data, netadr_t* to)> NET_SendPacket{0x142332F70_g};
WEAK symbol<bool(netsrc_t sock, int length, const void* data, const netadr_t* to)> NET_SendPacket{0x142332F70_g};
// Sys
WEAK symbol<void()> Sys_ShowConsole{0x142333F80_g};
WEAK symbol<TLSData*()> Sys_GetTLS{0x142184210_g};