Browse Source

Update plNetAddress to use plString.

Darryl Pogue 13 years ago
parent
commit
e70cb420f3
  1. 42
      Sources/Plasma/NucleusLib/pnNetCommon/plNetAddress.cpp
  2. 22
      Sources/Plasma/NucleusLib/pnNetCommon/plNetAddress.h
  3. 4
      Sources/Plasma/NucleusLib/pnNetCommon/pnNetCommon.cpp
  4. 3
      Sources/Plasma/NucleusLib/pnNetCommon/pnNetCommon.h

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

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

22
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 #ifndef plNetAddress_h_inc
#define plNetAddress_h_inc #define plNetAddress_h_inc
#include "HeadSpin.h"
#include "plString.h"
#include "hsStlUtils.h" #include "hsStlUtils.h"
#include "hsStream.h" #include "hsStream.h"
@ -73,24 +75,28 @@ class plNetAddress
public: public:
plNetAddress(); plNetAddress();
plNetAddress(uint32_t addr, int port); plNetAddress(uint32_t addr, uint16_t port);
plNetAddress(const char * addr, int port); plNetAddress(const char* addr, uint16_t port);
virtual ~plNetAddress(){} virtual ~plNetAddress(){}
void Clear(); void Clear();
bool SetAnyAddr(); bool SetAnyAddr();
bool SetAnyPort(); bool SetAnyPort();
bool SetPort(int port);
uint16_t GetPort() const;
bool SetPort(uint16_t port);
uint32_t GetHost() const;
bool SetHost(const char* hostname); bool SetHost(const char* hostname);
bool SetHost(uint32_t ip4addr); bool SetHost(uint32_t ip4addr);
int GetPort() const;
std::string GetHostString() const;
uint32_t GetHost() const;
std::string GetHostWithPort() const;
const AddressType& GetAddressInfo() const { return fAddr; } const AddressType& GetAddressInfo() const { return fAddr; }
AddressType& GetAddressInfo() { return fAddr; } AddressType& GetAddressInfo() { return fAddr; }
std::string AsString() const; plString GetHostString() const;
plString GetHostWithPort() const;
plString AsString() const;
void Read(hsStream * stream); void Read(hsStream * stream);
void Write(hsStream * stream); void Write(hsStream * stream);

4
Sources/Plasma/NucleusLib/pnNetCommon/pnNetCommon.cpp

@ -57,11 +57,11 @@ namespace pnNetCommon
#ifndef SERVER #ifndef SERVER
// NOTE: On Win32, WSAStartup() must be called before GetTextAddr() will work. // 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; in_addr in;
memcpy(&in, &binAddr, sizeof(binAddr)); memcpy(&in, &binAddr, sizeof(binAddr));
return inet_ntoa(in); return plString::Format("%s", inet_ntoa(in));
} }
// NOTE: On Win32, WSAStartup() must be called before GetBinAddr() will work. // NOTE: On Win32, WSAStartup() must be called before GetBinAddr() will work.

3
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 #define pnNetCommon_h_inc
#include "HeadSpin.h" #include "HeadSpin.h"
#include "plString.h"
#include "hsStlUtils.h" #include "hsStlUtils.h"
#include "hsRefCnt.h" #include "hsRefCnt.h"
#include "hsStream.h" #include "hsStream.h"
@ -82,7 +83,7 @@ namespace pnNetCommon
#ifndef SERVER #ifndef SERVER
uint32_t GetBinAddr(const char* textAddr); uint32_t GetBinAddr(const char* textAddr);
const char * GetTextAddr(uint32_t binAddr); plString GetTextAddr(uint32_t binAddr);
#endif // SERVER #endif // SERVER
} }

Loading…
Cancel
Save