2017-01-20 08:36:52 -05:00
|
|
|
#pragma once
|
|
|
|
|
2017-01-19 16:23:59 -05:00
|
|
|
#define NETWORK_MAX_PACKETS_PER_SECOND 100'000
|
|
|
|
|
|
|
|
namespace Components
|
|
|
|
{
|
|
|
|
class Network : public Component
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
class Address
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
Address() { setType(Game::netadrtype_t::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) {}
|
|
|
|
Address(const Address& obj) : address(obj.address) {};
|
2017-06-25 15:54:35 -04:00
|
|
|
bool operator!=(const Address &obj) const { return !(*this == obj); };
|
|
|
|
bool operator==(const Address &obj) const;
|
2017-01-19 16:23:59 -05:00
|
|
|
|
|
|
|
void setPort(unsigned short port);
|
|
|
|
unsigned short getPort();
|
|
|
|
|
|
|
|
void setIP(DWORD ip);
|
|
|
|
void setIP(Game::netIP_t ip);
|
|
|
|
Game::netIP_t getIP();
|
|
|
|
|
|
|
|
void setType(Game::netadrtype_t type);
|
|
|
|
Game::netadrtype_t getType();
|
|
|
|
|
|
|
|
sockaddr getSockAddr();
|
|
|
|
void toSockAddr(sockaddr* addr);
|
|
|
|
void toSockAddr(sockaddr_in* addr);
|
|
|
|
Game::netadr_t* get();
|
2017-06-25 15:54:35 -04:00
|
|
|
const char* getCString() const;
|
|
|
|
std::string getString() const;
|
2017-01-19 16:23:59 -05:00
|
|
|
|
|
|
|
bool isLocal();
|
|
|
|
bool isSelf();
|
|
|
|
bool isValid();
|
|
|
|
bool isLoopback();
|
|
|
|
|
|
|
|
private:
|
|
|
|
Game::netadr_t address;
|
|
|
|
};
|
|
|
|
|
2018-12-17 08:29:18 -05:00
|
|
|
typedef void(Callback)(Address address, const std::string& data);
|
2017-01-19 16:23:59 -05:00
|
|
|
typedef void(CallbackRaw)();
|
|
|
|
|
|
|
|
Network();
|
|
|
|
|
2017-05-31 12:03:05 -04:00
|
|
|
static unsigned short GetPort();
|
|
|
|
|
2018-12-17 08:29:18 -05:00
|
|
|
static void Handle(const std::string& packet, Utils::Slot<Callback> callback);
|
2017-01-19 16:23:59 -05:00
|
|
|
static void OnStart(Utils::Slot<CallbackRaw> callback);
|
2020-11-14 03:17:20 -05:00
|
|
|
|
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
|
|
|
|
|
|
|
private:
|
|
|
|
static std::string SelectedPacket;
|
|
|
|
static Utils::Signal<CallbackRaw> StartupSignal;
|
|
|
|
static std::map<std::string, Utils::Slot<Callback>> PacketHandlers;
|
|
|
|
|
|
|
|
static int PacketInterceptionHandler(const char* packet);
|
|
|
|
static void DeployPacket(Game::netadr_t* from, Game::msg_t* msg);
|
|
|
|
static void DeployPacketStub();
|
|
|
|
|
|
|
|
static void NetworkStart();
|
|
|
|
static void NetworkStartStub();
|
|
|
|
|
|
|
|
static void PacketErrorCheck();
|
2021-09-12 07:47:35 -04:00
|
|
|
|
2021-09-12 09:16:41 -04:00
|
|
|
static void SV_ExecuteClientMessageStub(Game::client_t* client, Game::msg_t* msg);
|
2017-01-19 16:23:59 -05:00
|
|
|
};
|
|
|
|
}
|
2017-06-25 15:54:35 -04:00
|
|
|
|
|
|
|
template <>
|
|
|
|
struct std::hash<Components::Network::Address>
|
|
|
|
{
|
|
|
|
std::size_t operator()(const Components::Network::Address& k) const
|
|
|
|
{
|
|
|
|
return (std::hash<std::string>()(k.getString()));
|
|
|
|
}
|
|
|
|
};
|