feat(Net): Add move semantics to Net (sockets and addresses) #3296

This commit is contained in:
Alex Fabijanic 2021-06-07 11:11:08 +02:00
parent 5219b15b1f
commit 96a645e95b
13 changed files with 304 additions and 127 deletions

View File

@ -156,6 +156,16 @@
// #define POCO_NET_NO_UNIX_SOCKET
// Define to nonzero to enable move
// semantics on classes where it
// introduces a new state.
// For explanation, see
// https://github.com/pocoproject/poco/wiki/Move-Semantics-in-POCO
#ifndef POCO_NEW_STATE_ON_MOVE
#define POCO_NEW_STATE_ON_MOVE 1
#endif
// Windows CE has no locale support
#if defined(_WIN32_WCE)
#define POCO_NO_LOCALE

View File

@ -63,20 +63,10 @@ public:
/// a DatagramSocketImpl, otherwise an InvalidArgumentException
/// will be thrown.
DatagramSocket(Socket&& socket);
/// Creates the DatagramSocket with the SocketImpl
/// from another socket. The SocketImpl must be
/// a DatagramSocketImpl, otherwise an InvalidArgumentException
/// will be thrown.
DatagramSocket(const DatagramSocket& socket);
/// Creates the DatagramSocket with the SocketImpl
/// from another socket.
DatagramSocket(DatagramSocket&& socket);
/// Creates the DatagramSocket with the SocketImpl
/// from another socket.
~DatagramSocket();
/// Destroys the DatagramSocket.
@ -87,6 +77,20 @@ public:
/// attaches the SocketImpl from the other socket and
/// increments the reference count of the SocketImpl.
#if POCO_NEW_STATE_ON_MOVE
DatagramSocket(Socket&& socket);
/// Creates the DatagramSocket with the SocketImpl
/// from another socket and zeroes the other socket's
/// SocketImpl.The SocketImpl must be
/// a DatagramSocketImpl, otherwise an InvalidArgumentException
/// will be thrown.
DatagramSocket(DatagramSocket&& socket);
/// Creates the DatagramSocket with the SocketImpl
/// from another socket and zeroes the other socket's
/// SocketImpl.
DatagramSocket& operator = (Socket&& socket);
/// Assignment move operator.
///
@ -94,13 +98,6 @@ public:
/// attaches the SocketImpl from the other socket and
/// zeroes the other socket's SocketImpl.
DatagramSocket& operator = (const DatagramSocket& socket);
/// Assignment operator.
///
/// Releases the socket's SocketImpl and
/// attaches the SocketImpl from the other socket and
/// increments the reference count of the SocketImpl.
DatagramSocket& operator = (DatagramSocket&& socket);
/// Assignment move operator.
///
@ -108,6 +105,15 @@ public:
/// attaches the SocketImpl from the other socket and
/// zeroes the other socket's SocketImpl.
#endif // POCO_NEW_STATE_ON_MOVE
DatagramSocket& operator = (const DatagramSocket& socket);
/// Assignment operator.
///
/// Releases the socket's SocketImpl and
/// attaches the SocketImpl from the other socket and
/// increments the reference count of the SocketImpl.
void connect(const SocketAddress& address);
/// Restricts incoming and outgoing
/// packets to the specified address.

View File

@ -53,20 +53,10 @@ public:
/// a RawSocketImpl, otherwise an InvalidArgumentException
/// will be thrown.
RawSocket(Socket&& socket);
/// Creates the RawSocket with the SocketImpl
/// from another socket. The SocketImpl must be
/// a RawSocketImpl, otherwise an InvalidArgumentException
/// will be thrown.
RawSocket(const RawSocket& socket);
/// Creates the RawSocket with the SocketImpl
/// from another socket.
RawSocket(RawSocket&& socket);
/// Creates the RawSocket with the SocketImpl
/// from another socket.
~RawSocket();
/// Destroys the RawSocket.
@ -77,12 +67,6 @@ public:
/// attaches the SocketImpl from the other socket and
/// increments the reference count of the SocketImpl.
RawSocket& operator = (Socket&& socket);
/// Assignment operator.
///
/// Releases the socket's SocketImpl and
/// attaches the SocketImpl from the other socket.
RawSocket& operator = (const RawSocket& socket);
/// Assignment operator.
///
@ -90,11 +74,35 @@ public:
/// attaches the SocketImpl from the other socket and
/// increments the reference count of the SocketImpl.
RawSocket& operator = (RawSocket&& socket);
/// Assignment operator.
#if POCO_NEW_STATE_ON_MOVE
RawSocket(Socket&& socket);
/// Creates the RawSocket with the SocketImpl
/// from another socket and zeroes the other socket's
/// SocketImpl.The SocketImpl must be
/// a RawSocketImpl, otherwise an InvalidArgumentException
/// will be thrown.
RawSocket(RawSocket&& socket);
/// Creates the RawSocket with the SocketImpl
/// from another socket and zeroes the other socket's
/// SocketImpl.
RawSocket& operator = (Socket&& socket);
/// Assignment move operator.
///
/// Releases the socket's SocketImpl and
/// attaches the SocketImpl from the other socket.
/// attaches the SocketImpl from the other socket and
/// zeroes the other socket's SocketImpl.
RawSocket& operator = (RawSocket&& socket);
/// Assignment move operator.
///
/// Releases the socket's SocketImpl and
/// attaches the SocketImpl from the other socket and
/// zeroes the other socket's SocketImpl.
#endif //POCO_NEW_STATE_ON_MOVE
void connect(const SocketAddress& address);
/// Restricts incoming and outgoing
@ -168,7 +176,7 @@ protected:
/// Creates the Socket and attaches the given SocketImpl.
/// The socket takes ownership of the SocketImpl.
///
/// The SocketImpl must be a StreamSocketImpl, otherwise
/// The SocketImpl must be a RawSocketImpl, otherwise
/// an InvalidArgumentException will be thrown.
};

View File

@ -56,12 +56,6 @@ public:
/// Attaches the SocketImpl from the other socket and
/// increments the reference count of the SocketImpl.
Socket(Socket&& socket);
/// Move constructor.
///
/// Attaches the SocketImpl from the other socket and
/// zeroes the other socket's SocketImpl.
Socket& operator = (const Socket& socket);
/// Assignment operator.
///
@ -69,13 +63,23 @@ public:
/// attaches the SocketImpl from the other socket and
/// increments the reference count of the SocketImpl.
#if POCO_NEW_STATE_ON_MOVE
Socket(Socket&& socket);
/// Move constructor.
///
/// Attaches the SocketImpl from the other socket and
/// zeroes the other socket's SocketImpl.
Socket& operator = (Socket&& socket);
/// Assignment move operator.
///
/// Releases the socket's SocketImpl and
/// attaches the SocketImpl from the other socket and
/// zeroes the other socket's SocketImpl.
/// Releases the socket's SocketImpl,
/// attaches the SocketImpl from the other socket,
/// and zeroes the other socket's SocketImpl.
#endif // POCO_NEW_STATE_ON_MOVE
virtual ~Socket();
/// Destroys the Socket and releases the
/// SocketImpl.
@ -99,7 +103,10 @@ public:
bool operator >= (const Socket& socket) const;
/// Compares the SocketImpl pointers.
bool isNull() const;
/// Returns true if pointer to implementation is null.
void close();
/// Closes the socket.
@ -416,212 +423,286 @@ inline bool Socket::operator >= (const Socket& socket) const
}
inline bool Socket::isNull() const
{
return _pImpl == nullptr;
}
inline void Socket::close()
{
_pImpl->close();
if (_pImpl) _pImpl->close();
}
inline bool Socket::poll(const Poco::Timespan& timeout, int mode) const
{
poco_assert_dbg(POCO_NEW_STATE_ON_MOVE && _pImpl);
return _pImpl->poll(timeout, mode);
}
inline int Socket::available() const
{
poco_assert_dbg(POCO_NEW_STATE_ON_MOVE && _pImpl);
return _pImpl->available();
}
inline void Socket::setSendBufferSize(int size)
{
poco_assert_dbg(POCO_NEW_STATE_ON_MOVE && _pImpl);
_pImpl->setSendBufferSize(size);
}
inline int Socket::getSendBufferSize() const
{
poco_assert_dbg(POCO_NEW_STATE_ON_MOVE && _pImpl);
return _pImpl->getSendBufferSize();
}
inline void Socket::setReceiveBufferSize(int size)
{
poco_assert_dbg(POCO_NEW_STATE_ON_MOVE && _pImpl);
_pImpl->setReceiveBufferSize(size);
}
inline int Socket::getReceiveBufferSize() const
{
poco_assert_dbg(POCO_NEW_STATE_ON_MOVE && _pImpl);
return _pImpl->getReceiveBufferSize();
}
inline void Socket::setSendTimeout(const Poco::Timespan& timeout)
{
poco_assert_dbg(POCO_NEW_STATE_ON_MOVE && _pImpl);
_pImpl->setSendTimeout(timeout);
}
inline Poco::Timespan Socket::getSendTimeout() const
{
poco_assert_dbg(POCO_NEW_STATE_ON_MOVE && _pImpl);
return _pImpl->getSendTimeout();
}
inline void Socket::setReceiveTimeout(const Poco::Timespan& timeout)
{
poco_assert_dbg(POCO_NEW_STATE_ON_MOVE && _pImpl);
_pImpl->setReceiveTimeout(timeout);
}
inline Poco::Timespan Socket::getReceiveTimeout() const
{
poco_assert_dbg(POCO_NEW_STATE_ON_MOVE && _pImpl);
return _pImpl->getReceiveTimeout();
}
inline void Socket::setOption(int level, int option, int value)
{
poco_assert_dbg(POCO_NEW_STATE_ON_MOVE && _pImpl);
_pImpl->setOption(level, option, value);
}
inline void Socket::setOption(int level, int option, unsigned value)
{
poco_assert_dbg(POCO_NEW_STATE_ON_MOVE && _pImpl);
_pImpl->setOption(level, option, value);
}
inline void Socket::setOption(int level, int option, unsigned char value)
{
poco_assert_dbg(POCO_NEW_STATE_ON_MOVE && _pImpl);
_pImpl->setOption(level, option, value);
}
inline void Socket::setOption(int level, int option, const Poco::Timespan& value)
{
poco_assert_dbg(POCO_NEW_STATE_ON_MOVE && _pImpl);
_pImpl->setOption(level, option, value);
}
inline void Socket::setOption(int level, int option, const IPAddress& value)
{
poco_assert_dbg(POCO_NEW_STATE_ON_MOVE && _pImpl);
_pImpl->setOption(level, option, value);
}
inline void Socket::getOption(int level, int option, int& value) const
{
poco_assert_dbg(POCO_NEW_STATE_ON_MOVE && _pImpl);
_pImpl->getOption(level, option, value);
}
inline void Socket::getOption(int level, int option, unsigned& value) const
{
poco_assert_dbg(POCO_NEW_STATE_ON_MOVE && _pImpl);
_pImpl->getOption(level, option, value);
}
inline void Socket::getOption(int level, int option, unsigned char& value) const
{
poco_assert_dbg(POCO_NEW_STATE_ON_MOVE && _pImpl);
_pImpl->getOption(level, option, value);
}
inline void Socket::getOption(int level, int option, Poco::Timespan& value) const
{
poco_assert_dbg(POCO_NEW_STATE_ON_MOVE && _pImpl);
_pImpl->getOption(level, option, value);
}
inline void Socket::getOption(int level, int option, IPAddress& value) const
{
poco_assert_dbg(POCO_NEW_STATE_ON_MOVE && _pImpl);
_pImpl->getOption(level, option, value);
}
inline void Socket::setLinger(bool on, int seconds)
{
poco_assert_dbg(POCO_NEW_STATE_ON_MOVE && _pImpl);
_pImpl->setLinger(on, seconds);
}
inline void Socket::getLinger(bool& on, int& seconds) const
{
poco_assert_dbg(POCO_NEW_STATE_ON_MOVE && _pImpl);
_pImpl->getLinger(on, seconds);
}
inline void Socket::setNoDelay(bool flag)
{
poco_assert_dbg(POCO_NEW_STATE_ON_MOVE && _pImpl);
_pImpl->setNoDelay(flag);
}
inline bool Socket::getNoDelay() const
{
poco_assert_dbg(POCO_NEW_STATE_ON_MOVE && _pImpl);
return _pImpl->getNoDelay();
}
inline void Socket::setKeepAlive(bool flag)
{
poco_assert_dbg(POCO_NEW_STATE_ON_MOVE && _pImpl);
_pImpl->setKeepAlive(flag);
}
inline bool Socket::getKeepAlive() const
{
poco_assert_dbg(POCO_NEW_STATE_ON_MOVE && _pImpl);
return _pImpl->getKeepAlive();
}
inline void Socket::setReuseAddress(bool flag)
{
poco_assert_dbg(POCO_NEW_STATE_ON_MOVE && _pImpl);
_pImpl->setReuseAddress(flag);
}
inline bool Socket::getReuseAddress() const
{
poco_assert_dbg(POCO_NEW_STATE_ON_MOVE && _pImpl);
return _pImpl->getReuseAddress();
}
inline void Socket::setReusePort(bool flag)
{
poco_assert_dbg(POCO_NEW_STATE_ON_MOVE && _pImpl);
_pImpl->setReusePort(flag);
}
inline bool Socket::getReusePort() const
{
poco_assert_dbg(POCO_NEW_STATE_ON_MOVE && _pImpl);
return _pImpl->getReusePort();
}
inline void Socket::setOOBInline(bool flag)
{
poco_assert_dbg(POCO_NEW_STATE_ON_MOVE && _pImpl);
_pImpl->setOOBInline(flag);
}
inline bool Socket::getOOBInline() const
{
poco_assert_dbg(POCO_NEW_STATE_ON_MOVE && _pImpl);
return _pImpl->getOOBInline();
}
inline void Socket::setBlocking(bool flag)
{
poco_assert_dbg(POCO_NEW_STATE_ON_MOVE && _pImpl);
_pImpl->setBlocking(flag);
}
inline bool Socket::getBlocking() const
{
poco_assert_dbg(POCO_NEW_STATE_ON_MOVE && _pImpl);
return _pImpl->getBlocking();
}
@ -634,24 +715,32 @@ inline SocketImpl* Socket::impl() const
inline poco_socket_t Socket::sockfd() const
{
poco_assert_dbg(POCO_NEW_STATE_ON_MOVE && _pImpl);
return _pImpl->sockfd();
}
inline SocketAddress Socket::address() const
{
poco_assert_dbg(POCO_NEW_STATE_ON_MOVE && _pImpl);
return _pImpl->address();
}
inline SocketAddress Socket::peerAddress() const
{
poco_assert_dbg(POCO_NEW_STATE_ON_MOVE && _pImpl);
return _pImpl->peerAddress();
}
inline bool Socket::secure() const
{
poco_assert_dbg(POCO_NEW_STATE_ON_MOVE && _pImpl);
return _pImpl->secure();
}
@ -674,6 +763,8 @@ inline bool Socket::supportsIPv6()
inline void Socket::init(int af)
{
poco_assert_dbg(POCO_NEW_STATE_ON_MOVE && _pImpl);
_pImpl->init(af);
}

View File

@ -60,20 +60,10 @@ public:
/// a StreamSocketImpl, otherwise an InvalidArgumentException
/// will be thrown.
StreamSocket(Socket&& socket);
/// Creates the StreamSocket with the SocketImpl
/// from another socket. The SocketImpl must be
/// a StreamSocketImpl, otherwise an InvalidArgumentException
/// will be thrown.
StreamSocket(const StreamSocket& socket);
/// Creates the StreamSocket with the SocketImpl
/// from another socket.
StreamSocket(StreamSocket&& socket);
/// Creates the StreamSocket with the SocketImpl
/// from another socket.
virtual ~StreamSocket();
/// Destroys the StreamSocket.
@ -91,12 +81,35 @@ public:
/// attaches the SocketImpl from the other socket and
/// increments the reference count of the SocketImpl.
StreamSocket& operator = (StreamSocket&& socket);
/// Move-assignment operator.
#if POCO_NEW_STATE_ON_MOVE
StreamSocket(Socket&& socket);
/// Creates the StreamSocket with the SocketImpl
/// from another socket and zeroes the other socket's
/// SocketImpl.The SocketImpl must be
/// a StreamSocketImpl, otherwise an InvalidArgumentException
/// will be thrown.
StreamSocket(StreamSocket&& socket);
/// Creates the StreamSocket with the SocketImpl
/// from another socket and zeroes the other socket's
/// SocketImpl.
StreamSocket& operator = (Socket&& socket);
/// Assignment move operator.
///
/// Releases the socket's SocketImpl and
/// attaches the SocketImpl from the other socket and
/// increments the reference count of the SocketImpl.
/// zeroes the other socket's SocketImpl.
StreamSocket& operator = (StreamSocket&& socket);
/// Assignment move operator.
///
/// Releases the socket's SocketImpl and
/// attaches the SocketImpl from the other socket and
/// zeroes the other socket's SocketImpl.
#endif //POCO_NEW_STATE_ON_MOVE
void connect(const SocketAddress& address);
/// Initializes the socket and establishes a connection to

View File

@ -176,18 +176,11 @@ public:
/// Creates a WebSocket from another Socket, which must be a WebSocket,
/// otherwise a Poco::InvalidArgumentException will be thrown.
WebSocket(Socket&& socket);
/// Creates a WebSocket from another Socket, which must be a WebSocket,
/// otherwise a Poco::InvalidArgumentException will be thrown.
WebSocket(const WebSocket& socket);
/// Creates a WebSocket from another WebSocket.
WebSocket(WebSocket&& socket);
/// Creates a WebSocket from another WebSocket.
virtual ~WebSocket();
/// Destroys the StreamSocket.
/// Destroys the WebSocket.
WebSocket& operator = (const Socket& socket);
/// Assignment operator.
@ -195,17 +188,38 @@ public:
/// The other socket must be a WebSocket, otherwise a Poco::InvalidArgumentException
/// will be thrown.
WebSocket& operator = (Socket&& socket);
/// Assignment operator.
///
/// The other socket must be a WebSocket, otherwise a Poco::InvalidArgumentException
/// will be thrown.
WebSocket& operator = (const WebSocket& socket);
/// Assignment operator.
#if POCO_NEW_STATE_ON_MOVE
WebSocket(Socket&& socket);
/// Creates the WebSocket with the SocketImpl
/// from another socket and zeroes the other socket's
/// SocketImpl.The SocketImpl must be
/// a WebSocketImpl, otherwise an InvalidArgumentException
/// will be thrown.
WebSocket(WebSocket&& socket);
/// Creates the WebSocket with the SocketImpl
/// from another socket and zeroes the other socket's
/// SocketImpl.
WebSocket& operator = (Socket&& socket);
/// Assignment move operator.
///
/// Releases the socket's SocketImpl and
/// attaches the SocketImpl from the other socket and
/// zeroes the other socket's SocketImpl.
WebSocket& operator = (WebSocket&& socket);
/// Move-assignment operator.
/// Assignment move operator.
///
/// Releases the socket's SocketImpl and
/// attaches the SocketImpl from the other socket and
/// zeroes the other socket's SocketImpl.
#endif //POCO_NEW_STATE_ON_MOVE
void shutdown();
/// Sends a Close control frame to the server end of

View File

@ -50,23 +50,11 @@ DatagramSocket::DatagramSocket(const Socket& socket): Socket(socket)
}
DatagramSocket::DatagramSocket(Socket&& socket): Socket(std::move(socket))
{
if (!dynamic_cast<DatagramSocketImpl*>(impl()))
throw InvalidArgumentException("Cannot assign incompatible socket");
}
DatagramSocket::DatagramSocket(const DatagramSocket& socket): Socket(socket)
{
}
DatagramSocket::DatagramSocket(DatagramSocket&& socket): Socket(std::move(socket))
{
}
DatagramSocket::DatagramSocket(SocketImpl* pImpl): Socket(pImpl)
{
if (!dynamic_cast<DatagramSocketImpl*>(impl()))
@ -88,6 +76,19 @@ DatagramSocket& DatagramSocket::operator = (const Socket& socket)
return *this;
}
#if POCO_NEW_STATE_ON_MOVE
DatagramSocket::DatagramSocket(DatagramSocket&& socket): Socket(std::move(socket))
{
}
DatagramSocket::DatagramSocket(Socket&& socket): Socket(std::move(socket))
{
if (!dynamic_cast<DatagramSocketImpl*>(impl()))
throw InvalidArgumentException("Cannot assign incompatible socket");
}
DatagramSocket& DatagramSocket::operator = (Socket&& socket)
{
@ -99,19 +100,20 @@ DatagramSocket& DatagramSocket::operator = (Socket&& socket)
}
DatagramSocket& DatagramSocket::operator = (const DatagramSocket& socket)
{
Socket::operator = (socket);
return *this;
}
DatagramSocket& DatagramSocket::operator = (DatagramSocket&& socket)
{
Socket::operator = (std::move(socket));
return *this;
}
#endif // POCO_NEW_STATE_ON_MOVE
DatagramSocket& DatagramSocket::operator = (const DatagramSocket& socket)
{
Socket::operator = (socket);
return *this;
}
void DatagramSocket::connect(const SocketAddress& address)
{

View File

@ -72,7 +72,6 @@ IPAddress::IPAddress(const IPAddress& addr)
IPAddress::IPAddress(IPAddress&& addr): _pImpl(std::move(addr._pImpl))
{
addr._pImpl = nullptr;
}
@ -247,7 +246,6 @@ IPAddress& IPAddress::operator = (const IPAddress& addr)
IPAddress& IPAddress::operator = (IPAddress&& addr)
{
_pImpl = std::move(addr._pImpl);
addr._pImpl = nullptr;
return *this;
}

View File

@ -50,6 +50,7 @@ Socket::Socket(const Socket& socket):
_pImpl->duplicate();
}
#if POCO_NEW_STATE_ON_MOVE
Socket::Socket(Socket&& socket):
_pImpl(socket._pImpl)
@ -60,18 +61,6 @@ Socket::Socket(Socket&& socket):
}
Socket& Socket::operator = (const Socket& socket)
{
if (&socket != this)
{
if (_pImpl) _pImpl->release();
_pImpl = socket._pImpl;
if (_pImpl) _pImpl->duplicate();
}
return *this;
}
Socket& Socket::operator = (Socket&& socket)
{
if (&socket != this)
@ -83,6 +72,19 @@ Socket& Socket::operator = (Socket&& socket)
return *this;
}
#endif // POCO_NEW_STATE_ON_MOVE
Socket& Socket::operator = (const Socket& socket)
{
if (&socket != this)
{
if (_pImpl) _pImpl->release();
_pImpl = socket._pImpl;
if (_pImpl) _pImpl->duplicate();
}
return *this;
}
Socket::~Socket()
{

View File

@ -153,7 +153,6 @@ SocketAddress::SocketAddress(const SocketAddress& socketAddress)
SocketAddress::SocketAddress(SocketAddress&& socketAddress):
_pImpl(std::move(socketAddress._pImpl))
{
socketAddress._pImpl = nullptr;
}
@ -213,7 +212,6 @@ SocketAddress& SocketAddress::operator = (const SocketAddress& socketAddress)
SocketAddress& SocketAddress::operator = (SocketAddress&& socketAddress)
{
_pImpl = std::move(socketAddress._pImpl);
socketAddress._pImpl = nullptr;
return *this;
}

View File

@ -51,23 +51,11 @@ StreamSocket::StreamSocket(const Socket& socket): Socket(socket)
}
StreamSocket::StreamSocket(Socket&& socket): Socket(std::move(socket))
{
if (!dynamic_cast<StreamSocketImpl*>(impl()))
throw InvalidArgumentException("Cannot assign incompatible socket");
}
StreamSocket::StreamSocket(const StreamSocket& socket): Socket(socket)
{
}
StreamSocket::StreamSocket(StreamSocket&& socket): Socket(std::move(socket))
{
}
StreamSocket::StreamSocket(SocketImpl* pImpl): Socket(pImpl)
{
if (!dynamic_cast<StreamSocketImpl*>(impl()))
@ -96,6 +84,25 @@ StreamSocket& StreamSocket::operator = (const StreamSocket& socket)
return *this;
}
#if POCO_NEW_STATE_ON_MOVE
StreamSocket::StreamSocket(Socket&& socket): Socket(std::move(socket))
{
if (!dynamic_cast<StreamSocketImpl*>(impl()))
throw InvalidArgumentException("Cannot assign incompatible socket");
}
StreamSocket::StreamSocket(StreamSocket&& socket): Socket(std::move(socket))
{
}
StreamSocket& StreamSocket::operator = (Socket&& socket)
{
Socket::operator = (std::move(socket));
return *this;
}
StreamSocket& StreamSocket::operator = (StreamSocket&& socket)
{
@ -103,6 +110,7 @@ StreamSocket& StreamSocket::operator = (StreamSocket&& socket)
return *this;
}
#endif // POCO_NEW_STATE_ON_MOVE
void StreamSocket::connect(const SocketAddress& address)
{

View File

@ -69,7 +69,11 @@ void DatagramSocketTest::testMoveDatagramSocket()
char buffer[256];
ss0.connect(SocketAddress("127.0.0.1", echoServer.port()));
DatagramSocket ss(std::move(ss0));
assertTrue (ss0.impl() == nullptr);
#if POCO_NEW_STATE_ON_MOVE
assertTrue (ss0.isNull());
#else
assertFalse (ss0.isNull());
#endif
int n = ss.sendBytes("hello", 5);
assertTrue (n == 5);
n = ss.receiveBytes(buffer, sizeof(buffer));
@ -82,7 +86,11 @@ void DatagramSocketTest::testMoveDatagramSocket()
assertTrue (ss.impl());
assertTrue (ss0.impl() == ss.impl());
ss = std::move(ss0);
assertTrue (ss0.impl() == nullptr);
#if POCO_NEW_STATE_ON_MOVE
assertTrue (ss0.isNull());
#else
assertFalse (ss0.isNull());
#endif
assertTrue (ss.impl());
n = ss.sendBytes("hello", 5);
assertTrue (n == 5);

View File

@ -70,13 +70,32 @@ void SocketTest::testMoveStreamSocket()
StreamSocket ss0 = StreamSocket();
ss0.connect(SocketAddress("127.0.0.1", echoServer.port()));
StreamSocket ss(std::move(ss0));
#if POCO_NEW_STATE_ON_MOVE
assertTrue (ss0.isNull());
#else
assertFalse (ss0.isNull());
#endif
char buffer[256];
std::memset(buffer, 0, sizeof(buffer));
ss0 = ss;
assertTrue (ss0.impl());
assertTrue (ss.impl());
assertTrue (ss0.impl() == ss.impl());
ss = std::move(ss0);
#if POCO_NEW_STATE_ON_MOVE
assertTrue (ss0.isNull());
#else
assertFalse (ss0.isNull());
#endif
assertTrue (ss.impl());
int n = ss.sendBytes("hello", 5);
assertTrue (n == 5);
char buffer[256];
n = ss.receiveBytes(buffer, sizeof(buffer));
assertTrue (n == 5);
assertTrue (std::string(buffer, n) == "hello");
ss.close();
ss0.close();
}