iw4x-client/src/Components/Modules/Network.hpp

102 lines
3.2 KiB
C++
Raw Normal View History

2017-01-20 08:36:52 -05:00
#pragma once
2017-01-19 16:23:59 -05:00
namespace Components
{
class Network : public Component
{
public:
class Address
{
public:
2022-11-16 12:25:21 -05:00
Address() { setType(Game::NA_BAD); }
2018-12-17 08:29:18 -05:00
Address(const std::string& addrString);
2017-01-19 16:23:59 -05:00
Address(sockaddr* addr);
Address(sockaddr addr) : Address(&addr) {}
Address(sockaddr_in addr) : Address(&addr) {}
Address(sockaddr_in* addr) : Address(reinterpret_cast<sockaddr*>(addr)) {}
Address(Game::netadr_t addr) : address(addr) {}
Address(Game::netadr_t* addr) : Address(*addr) {}
2022-05-20 18:12:46 -04:00
Address(const Address& obj) = default;
bool operator!=(const Address &obj) const { return !(*this == obj); }
2017-06-25 15:54:35 -04:00
bool operator==(const Address &obj) const;
2017-01-19 16:23:59 -05:00
void setPort(unsigned short port);
2022-08-19 19:10:35 -04:00
[[nodiscard]] unsigned short getPort() const;
2017-01-19 16:23:59 -05:00
void setIP(DWORD ip);
void setIP(Game::netIP_t ip);
2022-08-19 19:10:35 -04:00
[[nodiscard]] Game::netIP_t getIP() const;
2017-01-19 16:23:59 -05:00
void setType(Game::netadrtype_t type);
2022-08-19 19:10:35 -04:00
[[nodiscard]] Game::netadrtype_t getType() const;
2017-01-19 16:23:59 -05:00
2022-08-19 19:10:35 -04:00
[[nodiscard]] sockaddr getSockAddr();
2017-01-19 16:23:59 -05:00
void toSockAddr(sockaddr* addr);
void toSockAddr(sockaddr_in* addr);
Game::netadr_t* get();
2022-08-19 19:10:35 -04:00
[[nodiscard]] const char* getCString() const;
[[nodiscard]] std::string getString() const;
2017-01-19 16:23:59 -05:00
2023-02-06 14:34:08 -05:00
[[nodiscard]] bool isLocal() const noexcept;
[[nodiscard]] bool isSelf() const noexcept;
[[nodiscard]] bool isValid() const noexcept;
[[nodiscard]] bool isLoopback() const noexcept;
2017-01-19 16:23:59 -05:00
private:
Game::netadr_t address;
};
typedef void(CallbackRaw)();
2022-05-20 18:12:46 -04:00
using NetworkCallback = std::function<void(Address&, const std::string&)>;
2017-01-19 16:23:59 -05:00
Network();
2023-02-06 14:34:08 -05:00
static std::uint16_t GetPort();
2022-08-19 19:10:35 -04:00
static void OnStart(const Utils::Slot<CallbackRaw>& callback);
2017-01-19 16:23:59 -05:00
// Send quake-styled binary data
2018-12-17 08:29:18 -05:00
static void Send(Address target, const std::string& data);
static void Send(Game::netsrc_t type, Address target, const std::string& data);
2017-01-19 16:23:59 -05:00
// Allows sending raw data without quake header
2018-12-17 08:29:18 -05:00
static void SendRaw(Address target, const std::string& data);
static void SendRaw(Game::netsrc_t type, Address target, const std::string& data);
2017-01-19 16:23:59 -05:00
// Send quake-style command using binary data
2018-12-17 08:29:18 -05:00
static void SendCommand(Address target, const std::string& command, const std::string& data = "");
static void SendCommand(Game::netsrc_t type, Address target, const std::string& command, const std::string& data = "");
2017-01-19 16:23:59 -05:00
2018-12-17 08:29:18 -05:00
static void Broadcast(unsigned short port, const std::string& data);
static void BroadcastRange(unsigned int min, unsigned int max, const std::string& data);
static void BroadcastAll(const std::string& data);
2017-01-19 16:23:59 -05:00
2022-08-19 19:10:35 -04:00
static void OnClientPacket(const std::string& command, const NetworkCallback& callback);
2022-05-20 18:12:46 -04:00
2017-01-19 16:23:59 -05:00
private:
static Utils::Signal<CallbackRaw> StartupSignal;
2022-08-19 19:10:35 -04:00
static std::unordered_map<std::string, NetworkCallback> CL_Callbacks;
2017-01-19 16:23:59 -05:00
static void NetworkStart();
static void NetworkStartStub();
static void PacketErrorCheck();
2021-09-12 07:47:35 -04:00
static void SV_ExecuteClientMessageStub(Game::client_t* client, Game::msg_t* msg);
2022-05-20 18:12:46 -04:00
2022-08-19 19:10:35 -04:00
static bool CL_HandleCommand(Game::netadr_t* address, const char* command, const Game::msg_t* message);
2022-05-20 18:12:46 -04:00
static void CL_HandleCommandStub();
2017-01-19 16:23:59 -05:00
};
}
2017-06-25 15:54:35 -04:00
template <>
struct std::hash<Components::Network::Address>
{
2022-08-19 19:10:35 -04:00
std::size_t operator()(const Components::Network::Address& k) const noexcept
2017-06-25 15:54:35 -04:00
{
2022-08-19 19:10:35 -04:00
return std::hash<std::string>()(k.getString());
2017-06-25 15:54:35 -04:00
}
};