mirror of https://github.com/ARMmbed/mbed-os.git
SocketAddress rework
* Add optimised constexpr default constructor. Default construction was previously by a heavyweight defaulted `nsapi_addr_t` parameter. * Remove deprecated resolving constructor. * Take `nsapi_addr_t` inputs by constant reference rather than value. * Inline the trivial getters and setters. * Use `unique_ptr` to manage the text buffer. * Make `operator bool` explicit. * Optimise some methods. * Update to C++11 style (default initialisers, nullptr etc)pull/12683/head
parent
5d64e55880
commit
4eda58893e
|
@ -24,49 +24,27 @@
|
|||
|
||||
|
||||
|
||||
SocketAddress::SocketAddress(nsapi_addr_t addr, uint16_t port)
|
||||
SocketAddress::SocketAddress(const nsapi_addr_t &addr, uint16_t port) : _addr(addr), _port(port)
|
||||
{
|
||||
mem_init();
|
||||
_ip_address = NULL;
|
||||
set_addr(addr);
|
||||
set_port(port);
|
||||
}
|
||||
|
||||
SocketAddress::SocketAddress(const char *addr, uint16_t port)
|
||||
SocketAddress::SocketAddress(const char *addr, uint16_t port) : _port(port)
|
||||
{
|
||||
mem_init();
|
||||
_ip_address = NULL;
|
||||
set_ip_address(addr);
|
||||
set_port(port);
|
||||
}
|
||||
|
||||
SocketAddress::SocketAddress(const void *bytes, nsapi_version_t version, uint16_t port)
|
||||
SocketAddress::SocketAddress(const void *bytes, nsapi_version_t version, uint16_t port) : _port(port)
|
||||
{
|
||||
mem_init();
|
||||
_ip_address = NULL;
|
||||
set_ip_bytes(bytes, version);
|
||||
set_port(port);
|
||||
}
|
||||
|
||||
SocketAddress::SocketAddress(const SocketAddress &addr)
|
||||
SocketAddress::SocketAddress(const SocketAddress &addr) : _addr(addr._addr), _port(addr._port)
|
||||
{
|
||||
mem_init();
|
||||
_ip_address = NULL;
|
||||
set_addr(addr.get_addr());
|
||||
set_port(addr.get_port());
|
||||
}
|
||||
|
||||
void SocketAddress::mem_init(void)
|
||||
{
|
||||
_addr.version = NSAPI_UNSPEC;
|
||||
memset(_addr.bytes, 0, NSAPI_IP_BYTES);
|
||||
_port = 0;
|
||||
}
|
||||
|
||||
bool SocketAddress::set_ip_address(const char *addr)
|
||||
{
|
||||
delete[] _ip_address;
|
||||
_ip_address = NULL;
|
||||
_ip_address.reset();
|
||||
|
||||
if (addr && stoip4(addr, strlen(addr), _addr.bytes)) {
|
||||
_addr.version = NSAPI_IPv4;
|
||||
|
@ -82,9 +60,8 @@ bool SocketAddress::set_ip_address(const char *addr)
|
|||
|
||||
void SocketAddress::set_ip_bytes(const void *bytes, nsapi_version_t version)
|
||||
{
|
||||
nsapi_addr_t addr;
|
||||
nsapi_addr_t addr{};
|
||||
|
||||
addr = nsapi_addr_t();
|
||||
addr.version = version;
|
||||
if (version == NSAPI_IPv6) {
|
||||
memcpy(addr.bytes, bytes, NSAPI_IPv6_BYTES);
|
||||
|
@ -94,54 +71,28 @@ void SocketAddress::set_ip_bytes(const void *bytes, nsapi_version_t version)
|
|||
set_addr(addr);
|
||||
}
|
||||
|
||||
void SocketAddress::set_addr(nsapi_addr_t addr)
|
||||
void SocketAddress::set_addr(const nsapi_addr_t &addr)
|
||||
{
|
||||
delete[] _ip_address;
|
||||
_ip_address = NULL;
|
||||
_ip_address.reset();
|
||||
_addr = addr;
|
||||
}
|
||||
|
||||
void SocketAddress::set_port(uint16_t port)
|
||||
{
|
||||
_port = port;
|
||||
}
|
||||
|
||||
const char *SocketAddress::get_ip_address() const
|
||||
{
|
||||
if (_addr.version == NSAPI_UNSPEC) {
|
||||
return NULL;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
if (!_ip_address) {
|
||||
_ip_address = new char[NSAPI_IP_SIZE];
|
||||
_ip_address.reset(new char[NSAPI_IP_SIZE]);
|
||||
if (_addr.version == NSAPI_IPv4) {
|
||||
ip4tos(_addr.bytes, _ip_address);
|
||||
ip4tos(_addr.bytes, _ip_address.get());
|
||||
} else if (_addr.version == NSAPI_IPv6) {
|
||||
ip6tos(_addr.bytes, _ip_address);
|
||||
ip6tos(_addr.bytes, _ip_address.get());
|
||||
}
|
||||
}
|
||||
|
||||
return _ip_address;
|
||||
}
|
||||
|
||||
const void *SocketAddress::get_ip_bytes() const
|
||||
{
|
||||
return _addr.bytes;
|
||||
}
|
||||
|
||||
nsapi_version_t SocketAddress::get_ip_version() const
|
||||
{
|
||||
return _addr.version;
|
||||
}
|
||||
|
||||
nsapi_addr_t SocketAddress::get_addr() const
|
||||
{
|
||||
return _addr;
|
||||
}
|
||||
|
||||
uint16_t SocketAddress::get_port() const
|
||||
{
|
||||
return _port;
|
||||
return _ip_address.get();
|
||||
}
|
||||
|
||||
SocketAddress::operator bool() const
|
||||
|
@ -169,10 +120,8 @@ SocketAddress::operator bool() const
|
|||
|
||||
SocketAddress &SocketAddress::operator=(const SocketAddress &addr)
|
||||
{
|
||||
delete[] _ip_address;
|
||||
_ip_address = NULL;
|
||||
set_addr(addr.get_addr());
|
||||
set_port(addr.get_port());
|
||||
set_addr(addr._addr);
|
||||
set_port(addr._port);
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
@ -197,21 +146,3 @@ bool operator!=(const SocketAddress &a, const SocketAddress &b)
|
|||
{
|
||||
return !(a == b);
|
||||
}
|
||||
|
||||
void SocketAddress::_SocketAddress(NetworkStack *iface, const char *host, uint16_t port)
|
||||
{
|
||||
_ip_address = NULL;
|
||||
|
||||
// gethostbyname must check for literals, so can call it directly
|
||||
int err = iface->gethostbyname(host, this);
|
||||
_port = port;
|
||||
if (err) {
|
||||
_addr = nsapi_addr_t();
|
||||
_port = 0;
|
||||
}
|
||||
}
|
||||
|
||||
SocketAddress::~SocketAddress()
|
||||
{
|
||||
delete[] _ip_address;
|
||||
}
|
||||
|
|
|
@ -21,6 +21,7 @@
|
|||
#ifndef SOCKET_ADDRESS_H
|
||||
#define SOCKET_ADDRESS_H
|
||||
|
||||
#include <memory>
|
||||
#include "nsapi_types.h"
|
||||
#include "mbed_toolchain.h"
|
||||
|
||||
|
@ -34,38 +35,18 @@ class NetworkInterface;
|
|||
*/
|
||||
class SocketAddress {
|
||||
public:
|
||||
/** Create a SocketAddress from a hostname and port
|
||||
*
|
||||
* The hostname may be either a domain name or an IP address. If the
|
||||
* hostname is an IP address, no network transactions will be performed.
|
||||
*
|
||||
* On failure, the IP address and port will be set to zero
|
||||
*
|
||||
* @tparam S Type of the Network stack
|
||||
* @param stack Network stack to use for DNS resolution
|
||||
* @param host Hostname to resolve
|
||||
* @param port Optional 16-bit port, defaults to 0
|
||||
* @deprecated
|
||||
* Constructors hide possible errors. Replaced by
|
||||
* NetworkInterface::gethostbyname.
|
||||
/** Create an unspecified SocketAddress
|
||||
*/
|
||||
template <typename S>
|
||||
MBED_DEPRECATED_SINCE("mbed-os-5.1.3",
|
||||
"Constructors hide possible errors. Replaced by "
|
||||
"NetworkInterface::gethostbyname.")
|
||||
SocketAddress(S *stack, const char *host, uint16_t port = 0)
|
||||
{
|
||||
_SocketAddress(nsapi_create_stack(stack), host, port);
|
||||
}
|
||||
constexpr SocketAddress() = default;
|
||||
|
||||
/** Create a SocketAddress from a raw IP address and port
|
||||
*
|
||||
* @note To construct from a host name, use NetworkInterface::gethostbyname
|
||||
* @note To construct from a host name, use @ref NetworkInterface::gethostbyname
|
||||
*
|
||||
* @param addr Raw IP address
|
||||
* @param port Optional 16-bit port, defaults to 0
|
||||
*/
|
||||
SocketAddress(nsapi_addr_t addr = nsapi_addr_t(), uint16_t port = 0);
|
||||
SocketAddress(const nsapi_addr_t &addr, uint16_t port = 0);
|
||||
|
||||
/** Create a SocketAddress from an IP address and port
|
||||
*
|
||||
|
@ -89,7 +70,7 @@ public:
|
|||
SocketAddress(const SocketAddress &addr);
|
||||
|
||||
/** Destructor */
|
||||
~SocketAddress();
|
||||
~SocketAddress() = default;
|
||||
|
||||
/** Set the IP address
|
||||
*
|
||||
|
@ -110,13 +91,16 @@ public:
|
|||
*
|
||||
* @param addr Raw IP address
|
||||
*/
|
||||
void set_addr(nsapi_addr_t addr);
|
||||
void set_addr(const nsapi_addr_t &addr);
|
||||
|
||||
/** Set the port
|
||||
*
|
||||
* @param port 16-bit port
|
||||
*/
|
||||
void set_port(uint16_t port);
|
||||
void set_port(uint16_t port)
|
||||
{
|
||||
_port = port;
|
||||
}
|
||||
|
||||
/** Get the human-readable IP address
|
||||
*
|
||||
|
@ -131,31 +115,43 @@ public:
|
|||
*
|
||||
* @return Raw IP address in big-endian order
|
||||
*/
|
||||
const void *get_ip_bytes() const;
|
||||
const void *get_ip_bytes() const
|
||||
{
|
||||
return _addr.bytes;
|
||||
}
|
||||
|
||||
/** Get the IP address version
|
||||
*
|
||||
* @return IP address version, NSAPI_IPv4 or NSAPI_IPv6
|
||||
*/
|
||||
nsapi_version_t get_ip_version() const;
|
||||
nsapi_version_t get_ip_version() const
|
||||
{
|
||||
return _addr.version;
|
||||
}
|
||||
|
||||
/** Get the raw IP address
|
||||
*
|
||||
* @return Raw IP address
|
||||
*/
|
||||
nsapi_addr_t get_addr() const;
|
||||
nsapi_addr_t get_addr() const
|
||||
{
|
||||
return _addr;
|
||||
}
|
||||
|
||||
/** Get the port
|
||||
*
|
||||
* @return The 16-bit port
|
||||
*/
|
||||
uint16_t get_port() const;
|
||||
uint16_t get_port() const
|
||||
{
|
||||
return _port;
|
||||
}
|
||||
|
||||
/** Test if address is zero
|
||||
*
|
||||
* @return True if address is not zero
|
||||
*/
|
||||
operator bool() const;
|
||||
explicit operator bool() const;
|
||||
|
||||
/** Copy address from another SocketAddress
|
||||
*
|
||||
|
@ -178,14 +174,9 @@ public:
|
|||
friend bool operator!=(const SocketAddress &a, const SocketAddress &b);
|
||||
|
||||
private:
|
||||
void _SocketAddress(NetworkStack *iface, const char *host, uint16_t port);
|
||||
|
||||
/** Initialize memory */
|
||||
void mem_init(void);
|
||||
|
||||
mutable char *_ip_address;
|
||||
nsapi_addr_t _addr;
|
||||
uint16_t _port;
|
||||
mutable std::unique_ptr<char[]> _ip_address;
|
||||
nsapi_addr_t _addr{};
|
||||
uint16_t _port = 0;
|
||||
};
|
||||
|
||||
|
||||
|
|
|
@ -88,6 +88,7 @@ emacs
|
|||
json
|
||||
noncopyable
|
||||
sendto
|
||||
gethostbyname
|
||||
multicast
|
||||
multicasts
|
||||
singleshot
|
||||
|
|
Loading…
Reference in New Issue