moved IPAddress stream insertion operator to IPAddress.h, added stream insertion operator for SocketAddress, added BinaryWriter/BinaryReader insertion/extraction for SocketAddress, fixed serialization of IPAddress to BinaryWriter to support both IPv4 and IPv6 addresses

This commit is contained in:
Guenter Obiltschnig
2015-03-18 19:06:10 +01:00
parent c89cab6d6d
commit 2bbff0ca8f
6 changed files with 75 additions and 20 deletions

View File

@@ -26,6 +26,7 @@
#include "Poco/AutoPtr.h" #include "Poco/AutoPtr.h"
#include "Poco/Exception.h" #include "Poco/Exception.h"
#include <vector> #include <vector>
#include <ostream>
namespace Poco { namespace Poco {
@@ -413,6 +414,11 @@ private:
}; };
//
// inlines
//
inline void IPAddress::destruct() inline void IPAddress::destruct()
{ {
#ifdef POCO_HAVE_ALIGNMENT #ifdef POCO_HAVE_ALIGNMENT
@@ -510,11 +516,12 @@ inline char* IPAddress::storage()
#endif #endif
BinaryWriter& operator << (BinaryWriter& writer, const IPAddress& value);
BinaryReader& operator >> (BinaryReader& reader, IPAddress& value);
} } // namespace Poco::Net } } // namespace Poco::Net
Poco::BinaryWriter& Net_API operator << (Poco::BinaryWriter& writer, const Poco::Net::IPAddress& value);
Poco::BinaryReader& Net_API operator >> (Poco::BinaryReader& reader, Poco::Net::IPAddress& value);
std::ostream& Net_API operator << (std::ostream& ostr, const Poco::Net::IPAddress& addr);
#endif // Net_IPAddress_INCLUDED #endif // Net_IPAddress_INCLUDED

View File

@@ -30,6 +30,7 @@
#include "Poco/Mutex.h" #include "Poco/Mutex.h"
#include "Poco/Tuple.h" #include "Poco/Tuple.h"
#include <map> #include <map>
#include <ostream>
namespace Poco { namespace Poco {
@@ -342,8 +343,7 @@ inline bool NetworkInterface::operator == (const NetworkInterface& other) const
} } // namespace Poco::Net } } // namespace Poco::Net
Net_API std::ostream& operator<<(std::ostream& os, const Poco::Net::NetworkInterface::MACAddress& mac); std::ostream& Net_API operator << (std::ostream& ostr, const Poco::Net::NetworkInterface::MACAddress& addr);
Net_API std::ostream& operator<<(std::ostream& os, const Poco::Net::IPAddress& ipAddress);
#endif // POCO_NET_HAS_INTERFACE #endif // POCO_NET_HAS_INTERFACE

View File

@@ -22,9 +22,14 @@
#include "Poco/Net/Net.h" #include "Poco/Net/Net.h"
#include "Poco/Net/SocketAddressImpl.h" #include "Poco/Net/SocketAddressImpl.h"
#include <ostream>
namespace Poco { namespace Poco {
class BinaryReader;
class BinaryWriter;
namespace Net { namespace Net {
@@ -276,4 +281,9 @@ inline bool SocketAddress::operator != (const SocketAddress& socketAddress) cons
} } // namespace Poco::Net } } // namespace Poco::Net
Poco::BinaryWriter& Net_API operator << (Poco::BinaryWriter& writer, const Poco::Net::SocketAddress& value);
Poco::BinaryReader& Net_API operator >> (Poco::BinaryReader& reader, Poco::Net::SocketAddress& value);
std::ostream& Net_API operator << (std::ostream& ostr, const Poco::Net::SocketAddress& address);
#endif // Net_SocketAddress_INCLUDED #endif // Net_SocketAddress_INCLUDED

View File

@@ -563,19 +563,30 @@ IPAddress IPAddress::broadcast()
} }
BinaryWriter& operator << (BinaryWriter& writer, const IPAddress& value) } } // namespace Poco::Net
Poco::BinaryWriter& operator << (Poco::BinaryWriter& writer, const Poco::Net::IPAddress& value)
{ {
writer.stream().write((const char*) value.addr(), value.length()); writer << static_cast<Poco::UInt8>(value.length());
writer.writeRaw(reinterpret_cast<const char*>(value.addr()), value.length());
return writer; return writer;
} }
BinaryReader& operator >> (BinaryReader& reader, IPAddress& value)
Poco::BinaryReader& operator >> (Poco::BinaryReader& reader, Poco::Net::IPAddress& value)
{ {
char buf[sizeof(struct in6_addr)]; char buf[sizeof(struct in6_addr)];
reader.stream().read(buf, value.length()); Poco::UInt8 length;
value = IPAddress(buf, value.length()); reader >> length;
reader.readRaw(buf, length);
value = Poco::Net::IPAddress(buf, length);
return reader; return reader;
} }
} } // namespace Poco::Net std::ostream& operator << (std::ostream& ostr, const Poco::Net::IPAddress& addr)
{
ostr << addr.toString();
return ostr;
}

View File

@@ -38,12 +38,13 @@
#include <iostream> #include <iostream>
#include <iomanip> #include <iomanip>
using Poco::NumberFormatter; using Poco::NumberFormatter;
using Poco::FastMutex; using Poco::FastMutex;
using Poco::format; using Poco::format;
std::ostream& operator<<(std::ostream& os, const Poco::Net::NetworkInterface::MACAddress& mac) std::ostream& operator << (std::ostream& os, const Poco::Net::NetworkInterface::MACAddress& mac)
{ {
std::ios state(0); std::ios state(0);
state.copyfmt(os); state.copyfmt(os);
@@ -56,13 +57,6 @@ std::ostream& operator<<(std::ostream& os, const Poco::Net::NetworkInterface::MA
return os; return os;
} }
std::ostream& operator<<(std::ostream& os, const Poco::Net::IPAddress& ipAddress)
{
os << ipAddress.toString();
return os;
}
namespace Poco { namespace Poco {
namespace Net { namespace Net {
@@ -1363,6 +1357,7 @@ namespace Net {
namespace { namespace {
NetworkInterface::Type fromNative(u_char nativeType) NetworkInterface::Type fromNative(u_char nativeType)
{ {
switch (nativeType) switch (nativeType)
@@ -1383,6 +1378,7 @@ NetworkInterface::Type fromNative(u_char nativeType)
} }
} }
void setInterfaceParams(struct ifaddrs* iface, NetworkInterfaceImpl& impl) void setInterfaceParams(struct ifaddrs* iface, NetworkInterfaceImpl& impl)
{ {
struct sockaddr_dl* sdl = (struct sockaddr_dl*) iface->ifa_addr; struct sockaddr_dl* sdl = (struct sockaddr_dl*) iface->ifa_addr;
@@ -1395,6 +1391,7 @@ void setInterfaceParams(struct ifaddrs* iface, NetworkInterfaceImpl& impl)
impl.setType(fromNative(sdl->sdl_type)); impl.setType(fromNative(sdl->sdl_type));
} }
} // namespace } // namespace
@@ -1532,6 +1529,7 @@ namespace Net {
namespace { namespace {
static NetworkInterface::Type fromNative(unsigned arphrd) static NetworkInterface::Type fromNative(unsigned arphrd)
{ {
switch (arphrd) switch (arphrd)
@@ -1566,6 +1564,7 @@ void setInterfaceParams(struct ifaddrs* iface, NetworkInterfaceImpl& impl)
#endif #endif
} }

View File

@@ -21,6 +21,8 @@
#include "Poco/RefCountedObject.h" #include "Poco/RefCountedObject.h"
#include "Poco/NumberParser.h" #include "Poco/NumberParser.h"
#include "Poco/NumberFormatter.h" #include "Poco/NumberFormatter.h"
#include "Poco/BinaryReader.h"
#include "Poco/BinaryWriter.h"
#include <algorithm> #include <algorithm>
#include <cstring> #include <cstring>
@@ -268,3 +270,29 @@ Poco::UInt16 SocketAddress::resolveService(const std::string& service)
} } // namespace Poco::Net } } // namespace Poco::Net
Poco::BinaryWriter& operator << (Poco::BinaryWriter& writer, const Poco::Net::SocketAddress& value)
{
writer << value.host();
writer << value.port();
return writer;
}
Poco::BinaryReader& operator >> (Poco::BinaryReader& reader, Poco::Net::SocketAddress& value)
{
Poco::Net::IPAddress host;
reader >> host;
Poco::UInt16 port;
reader >> port;
value = Poco::Net::SocketAddress(host, port);
return reader;
}
inline std::ostream& operator << (std::ostream& ostr, const Poco::Net::SocketAddress& address)
{
ostr << address.toString();
return ostr;
}