Browse Source

Update plNetAddress to use plString.

Darryl Pogue 13 years ago
parent
commit
e70cb420f3
  1. 44
      Sources/Plasma/NucleusLib/pnNetCommon/plNetAddress.cpp
  2. 28
      Sources/Plasma/NucleusLib/pnNetCommon/plNetAddress.h
  3. 8
      Sources/Plasma/NucleusLib/pnNetCommon/pnNetCommon.cpp
  4. 5
      Sources/Plasma/NucleusLib/pnNetCommon/pnNetCommon.h

44
Sources/Plasma/NucleusLib/pnNetCommon/plNetAddress.cpp

@ -50,23 +50,20 @@ plNetAddress::plNetAddress()
Clear();
}
plNetAddress::plNetAddress(uint32_t addr, int port)
plNetAddress::plNetAddress(uint32_t addr, uint16_t port)
{
Clear();
SetHost(addr);
SetPort(port);
}
plNetAddress::plNetAddress(const char * addr, int port)
plNetAddress::plNetAddress(const char * addr, uint16_t port)
{
Clear();
SetHost(addr);
SetPort(port);
}
bool plNetAddress::SetAnyAddr()
{
fAddr.sin_family = AF_INET;
@ -74,7 +71,6 @@ bool plNetAddress::SetAnyAddr()
return true;
}
bool plNetAddress::SetAnyPort()
{
fAddr.sin_family = AF_INET;
@ -82,15 +78,13 @@ bool plNetAddress::SetAnyPort()
return true;
}
bool plNetAddress::SetPort(int port)
bool plNetAddress::SetPort(uint16_t port)
{
fAddr.sin_family = AF_INET;
fAddr.sin_port = htons(port);
return true;
}
void plNetAddress::Clear()
{
memset(&fAddr,0,sizeof(fAddr));
@ -98,16 +92,14 @@ void plNetAddress::Clear()
fAddr.sin_addr.s_addr = INADDR_ANY;
}
int plNetAddress::GetPort() const
uint16_t plNetAddress::GetPort() const
{
return ntohs(fAddr.sin_port);
}
std::string plNetAddress::GetHostString() const
plString plNetAddress::GetHostString() const
{
return std::string(pnNetCommon::GetTextAddr(fAddr.sin_addr.s_addr));
return pnNetCommon::GetTextAddr(fAddr.sin_addr.s_addr);
}
uint32_t plNetAddress::GetHost() const
@ -115,17 +107,14 @@ uint32_t plNetAddress::GetHost() const
return fAddr.sin_addr.s_addr;
}
std::string plNetAddress::GetHostWithPort() const
plString plNetAddress::GetHostWithPort() const
{
static const int buf_len = 1024;
char buf[buf_len];
sprintf(buf,"%s:%d",pnNetCommon::GetTextAddr(fAddr.sin_addr.s_addr),GetPort());
return std::string(buf);
plStringStream ss;
ss << pnNetCommon::GetTextAddr(fAddr.sin_addr.s_addr) << ":" << GetPort();
return ss.GetString();
}
bool plNetAddress::SetHost(const char * hostname)
bool plNetAddress::SetHost(const char* hostname)
{
fAddr.sin_addr.s_addr = pnNetCommon::GetBinAddr(hostname);
fAddr.sin_family = AF_INET;
@ -139,13 +128,14 @@ bool plNetAddress::SetHost(uint32_t addr)
return true;
}
std::string plNetAddress::AsString() const
plString plNetAddress::AsString() const
{
char buf[100] = "";
sprintf(buf,"IP:%s:%d",pnNetCommon::GetTextAddr(fAddr.sin_addr.s_addr),GetPort());
return std::string(buf);
}
plStringStream ss;
ss << "IP:" << pnNetCommon::GetTextAddr(fAddr.sin_addr.s_addr);
ss << ":" << GetPort();
return ss.GetString();
}
void plNetAddress::Read(hsStream * s)
{

28
Sources/Plasma/NucleusLib/pnNetCommon/plNetAddress.h

@ -44,6 +44,8 @@ You can contact Cyan Worlds, Inc. by email legal@cyan.com
#ifndef plNetAddress_h_inc
#define plNetAddress_h_inc
#include "HeadSpin.h"
#include "plString.h"
#include "hsStlUtils.h"
#include "hsStream.h"
@ -73,24 +75,28 @@ class plNetAddress
public:
plNetAddress();
plNetAddress(uint32_t addr, int port);
plNetAddress(const char * addr, int port);
plNetAddress(uint32_t addr, uint16_t port);
plNetAddress(const char* addr, uint16_t port);
virtual ~plNetAddress(){}
void Clear();
bool SetAnyAddr();
bool SetAnyPort();
bool SetPort(int port);
bool SetHost(const char * hostname);
bool SetHost(uint32_t ip4addr);
int GetPort() const;
std::string GetHostString() const;
uint16_t GetPort() const;
bool SetPort(uint16_t port);
uint32_t GetHost() const;
std::string GetHostWithPort() const;
const AddressType & GetAddressInfo() const { return fAddr; }
AddressType & GetAddressInfo() { return fAddr; }
bool SetHost(const char* hostname);
bool SetHost(uint32_t ip4addr);
const AddressType& GetAddressInfo() const { return fAddr; }
AddressType& GetAddressInfo() { return fAddr; }
std::string AsString() const;
plString GetHostString() const;
plString GetHostWithPort() const;
plString AsString() const;
void Read(hsStream * stream);
void Write(hsStream * stream);

8
Sources/Plasma/NucleusLib/pnNetCommon/pnNetCommon.cpp

@ -57,11 +57,11 @@ namespace pnNetCommon
#ifndef SERVER
// NOTE: On Win32, WSAStartup() must be called before GetTextAddr() will work.
const char * GetTextAddr(uint32_t binAddr)
plString GetTextAddr(uint32_t binAddr)
{
in_addr in;
memcpy(&in,&binAddr,sizeof(binAddr));
return inet_ntoa(in);
memcpy(&in, &binAddr, sizeof(binAddr));
return plString::Format("%s", inet_ntoa(in));
}
// NOTE: On Win32, WSAStartup() must be called before GetBinAddr() will work.
@ -72,7 +72,7 @@ uint32_t GetBinAddr(const char * textAddr)
if (!textAddr)
return addr;
struct addrinfo * ai = NULL;
struct addrinfo* ai = NULL;
addr = inet_addr(textAddr);
if(addr == INADDR_NONE)

5
Sources/Plasma/NucleusLib/pnNetCommon/pnNetCommon.h

@ -43,6 +43,7 @@ You can contact Cyan Worlds, Inc. by email legal@cyan.com
#define pnNetCommon_h_inc
#include "HeadSpin.h"
#include "plString.h"
#include "hsStlUtils.h"
#include "hsRefCnt.h"
#include "hsStream.h"
@ -81,8 +82,8 @@ namespace pnNetCommon
{
#ifndef SERVER
uint32_t GetBinAddr(const char * textAddr);
const char * GetTextAddr(uint32_t binAddr);
uint32_t GetBinAddr(const char* textAddr);
plString GetTextAddr(uint32_t binAddr);
#endif // SERVER
}

Loading…
Cancel
Save