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

View File

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

View File

@ -22,9 +22,14 @@
#include "Poco/Net/Net.h"
#include "Poco/Net/SocketAddressImpl.h"
#include <ostream>
namespace Poco {
class BinaryReader;
class BinaryWriter;
namespace Net {
@ -276,4 +281,9 @@ inline bool SocketAddress::operator != (const SocketAddress& socketAddress) cons
} } // 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

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;
}
BinaryReader& operator >> (BinaryReader& reader, IPAddress& value)
Poco::BinaryReader& operator >> (Poco::BinaryReader& reader, Poco::Net::IPAddress& value)
{
char buf[sizeof(struct in6_addr)];
reader.stream().read(buf, value.length());
value = IPAddress(buf, value.length());
Poco::UInt8 length;
reader >> length;
reader.readRaw(buf, length);
value = Poco::Net::IPAddress(buf, length);
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 <iomanip>
using Poco::NumberFormatter;
using Poco::FastMutex;
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);
state.copyfmt(os);
@ -56,13 +57,6 @@ std::ostream& operator<<(std::ostream& os, const Poco::Net::NetworkInterface::MA
return os;
}
std::ostream& operator<<(std::ostream& os, const Poco::Net::IPAddress& ipAddress)
{
os << ipAddress.toString();
return os;
}
namespace Poco {
namespace Net {
@ -1363,6 +1357,7 @@ namespace Net {
namespace {
NetworkInterface::Type fromNative(u_char nativeType)
{
switch (nativeType)
@ -1383,6 +1378,7 @@ NetworkInterface::Type fromNative(u_char nativeType)
}
}
void setInterfaceParams(struct ifaddrs* iface, NetworkInterfaceImpl& impl)
{
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));
}
} // namespace
@ -1532,6 +1529,7 @@ namespace Net {
namespace {
static NetworkInterface::Type fromNative(unsigned arphrd)
{
switch (arphrd)
@ -1566,6 +1564,7 @@ void setInterfaceParams(struct ifaddrs* iface, NetworkInterfaceImpl& impl)
#endif
}

View File

@ -21,6 +21,8 @@
#include "Poco/RefCountedObject.h"
#include "Poco/NumberParser.h"
#include "Poco/NumberFormatter.h"
#include "Poco/BinaryReader.h"
#include "Poco/BinaryWriter.h"
#include <algorithm>
#include <cstring>
@ -268,3 +270,29 @@ Poco::UInt16 SocketAddress::resolveService(const std::string& service)
} } // 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;
}