r5sdk/r5dev/tier1/NetAdr2.h
Kawe Mazidjatari 88b3336758 Many small code improvements and optimizations
* Use c++ methods as much as possible.
* Use enum types for accessing NavMesh objects from array.
* Use size_t for for loops when testing against size types.
* Don't compute strlen twice of more on the same string.
* Don't use unnecessary c string casts if there is a method with a std::string overload.
* Don't create string objects from string pointers if we could use them directly.
* Don't initialize RCON password twice on each change, and don't set if the new password equals the old.
2022-08-11 11:07:45 +02:00

102 lines
2.3 KiB
C++

#pragma once
#include "tier1/bitbuf.h"
enum class netadrtype_t
{
NA_NULL = 0,
NA_LOOPBACK,
NA_IP,
};
enum class netadrversion_t
{
NA_INVALID = -1,
NA_V4 = 4,
NA_V6 = 6,
};
class CNetAdr2
{
public:
CNetAdr2(void) {};
CNetAdr2(const string& svInAdr);
CNetAdr2(const string& svInAdr, const string& svInPort);
~CNetAdr2(void);
void SetIP(const string& svInAdr);
void SetPort(const string& svInPort);
void SetIPAndPort(string svInAdr);
void SetIPAndPort(string svInAdr, string svInPort);
void SetType(const netadrtype_t& type);
void SetVersion(void);
void SetFromSocket(const int& hSocket);
bool SetFromSockadr(sockaddr_storage* s);
string GetIP(bool bBaseOnly) const;
string GetPort(void) const;
string GetPort(string svInPort) const;
string GetIPAndPort(void) const;
netadrtype_t GetType(void) const;
netadrversion_t GetVersion(void) const;
string GetBase(void) const;
string GetBase(const string& svInAdr) const;
vector<string> GetParts(void) const;
int GetSize(void) const;
int GetFamily(void) const;
void ToSockadr(sockaddr_storage* pSadr) const;
void ToAdrinfo(addrinfo* pHint) const;
bool IsValidPort(const string& svInPort) const;
bool IsLocalhost(void) const;
bool IsLoopback(void) const;
bool IsReservedAdr(void) const;
bool CompareAdr(const CNetAdr2& adr2, bool bBaseOnly) const;
bool CompareClassBAdr(const CNetAdr2& adr2) const;
bool CompareClassCAdr(const CNetAdr2& adr2) const;
void Clear(void);
private:
string m_svip;
netadrtype_t m_type;
netadrversion_t m_version;
sockaddr_storage* m_sadr;
};
class v_netadr_t // !TODO: Move this to 'NetAdr.h' instead and adjust existing class to new system.
{
public:
inline netadrtype_t GetType(void) const
{
return this->type;
}
inline string GetAddress(void) const
{
// Select a static buffer
static char s[4][INET6_ADDRSTRLEN];
static int slot = 0;
int useSlot = (slot++) % 4;
// Render into it
inet_ntop(AF_INET6, &this->adr, s[useSlot], sizeof(s[0]));
// Pray the caller uses it before it gets clobbered
return s[useSlot];
}
inline uint16_t GetPort(void) const
{
return this->port;
}
inline bool IsReliable(void) const
{
return this->reliable;
}
netadrtype_t type{};
IN6_ADDR adr{};
uint16_t port{};
bool field_16{};
bool reliable{};
};