Skip to content
Snippets Groups Projects
Commit a11f6a4c authored by Daniel Müller's avatar Daniel Müller :speech_balloon:
Browse files

Fix move for socket wrappers

- Move constructor & assignments for the socket wrappers didn't
  invalidate the moved from
- That caused the moved from object to close the socket on destruct
parent 82357e8e
No related branches found
No related tags found
No related merge requests found
......@@ -29,6 +29,11 @@ private:
*/
int sockfd;
/**
* @brief If set to true, the socket is automatically closed on destruction
*/
bool autoclose = false;
public:
/**
......@@ -69,8 +74,8 @@ public:
*/
~TcpListener();
TcpListener(TcpListener &&other) = default;
TcpListener& operator=(TcpListener &&other) = default;
TcpListener(TcpListener &&other);
TcpListener& operator=(TcpListener &&other);
/**
* @brief Copying TcpListener is not allowed.
......
......@@ -30,6 +30,11 @@ private:
*/
int sockfd;
/**
* @brief If set to true, the socket is automatically closed on destruction
*/
bool autoclose = false;
public:
/**
......@@ -71,8 +76,8 @@ public:
*/
~TcpStream();
TcpStream(TcpStream &&other) = default;
TcpStream& operator=(TcpStream &&other) = default;
TcpStream(TcpStream &&other);
TcpStream& operator=(TcpStream &&other);
/**
* @brief Copying TcpListener is not allowed.
......
......@@ -37,6 +37,11 @@ private:
*/
int address_family;
/**
* @brief If set to true, the socket is automatically closed on destruction
*/
bool autoclose = false;
public:
/**
......@@ -88,8 +93,9 @@ public:
*/
~UdpSocket();
UdpSocket(UdpSocket &&other) = default;
UdpSocket& operator=(UdpSocket &&other) = default;
UdpSocket(UdpSocket &&other);
UdpSocket& operator=(UdpSocket &&other);
/**
* @brief Copying UdpSocket is not allowed.
......
......@@ -28,7 +28,26 @@ TcpListener::TcpListener(const std::string &localAddressPort)
TcpListener::~TcpListener()
{
// Close the socket to prevent leaking open file descriptors
close();
if (autoclose) close();
}
TcpListener::TcpListener(TcpListener &&other)
: local{other.local}, sockfd{other.sockfd}, autoclose{other.autoclose}
{
// Invalidate other socket
other.sockfd = 0;
}
TcpListener& TcpListener::operator=(TcpListener &&other)
{
local = other.local;
sockfd = other.sockfd;
autoclose = other.autoclose;
// Invalidate other socket
other.sockfd = 0;
return *this;
}
void TcpListener::listen(int connectionQueue)
......
......@@ -27,7 +27,26 @@ TcpStream::TcpStream(const std::string &remoteAddressPort)
TcpStream::~TcpStream()
{
close();
if (autoclose) close();
}
TcpStream::TcpStream(TcpStream &&other)
: remote{other.remote}, sockfd{other.sockfd}, autoclose{other.autoclose}
{
// Invalidate moved from socket
other.sockfd = 0;
}
TcpStream& TcpStream::operator=(TcpStream &&other)
{
remote = other.remote;
sockfd = other.sockfd;
autoclose = other.autoclose;
// Invalidate moved from socket
other.sockfd = 0;
return *this;
}
void TcpStream::connect()
......
......@@ -45,7 +45,29 @@ UdpSocket::UdpSocket(const std::string &localAddressPort)
UdpSocket::~UdpSocket()
{
close();
if (autoclose) close();
}
UdpSocket::UdpSocket(UdpSocket &&other)
: local{other.local}, sockfd{other.sockfd}, raw_socklen{other.raw_socklen},
address_family{other.address_family}, autoclose{other.autoclose}
{
// Invalidate the moved from socket
other.sockfd = 0;
}
UdpSocket& UdpSocket::operator=(UdpSocket &&other)
{
local = other.local;
sockfd = other.sockfd;
raw_socklen = other.raw_socklen;
address_family = other.address_family;
autoclose = other.autoclose;
// Invalidate the moved from socket
other.sockfd = 0;
return *this;
}
void UdpSocket::bind()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment