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

Add explicit copying to socket wrappers

- Allow creating copies of socket wrapper objects with the same
  underlying socket and socket file descriptor via a clone function
- Copy constructors and assignments are still deleted to prevent
  accidental copies
parent a11f6a4c
No related branches found
No related tags found
No related merge requests found
Pipeline #71668 passed
...@@ -78,12 +78,12 @@ public: ...@@ -78,12 +78,12 @@ public:
TcpListener& operator=(TcpListener &&other); TcpListener& operator=(TcpListener &&other);
/** /**
* @brief Copying TcpListener is not allowed. * @brief Copying TcpListener is not allowed. See clone() for explicit copies.
*/ */
TcpListener(const TcpListener &other) = delete; TcpListener(const TcpListener &other) = delete;
/** /**
* @brief Copying TcpListener is not allowed. * @brief Copying TcpListener is not allowed. See clone() for explicit copies.
*/ */
TcpListener& operator=(const TcpListener &other) = delete; TcpListener& operator=(const TcpListener &other) = delete;
...@@ -120,6 +120,27 @@ public: ...@@ -120,6 +120,27 @@ public:
*/ */
void close(); void close();
/**
* @brief Set the behavior for when the TcpListener is destroyed. If autoclose
* is enabled, the socket is closed on destruct. If autoclose is disabled, the
* socket will not be closed automatically.
*
* @param autoclose Enable or disable the autoclose functionality.
*/
void setAutoclose(bool autoclose);
/**
* @brief Create a clone of this socket wrapper object. The clone and the
* original will share the same underlying socket and file descriptor.
* If one of the instances closes the socket, the socket will be closed
* for both. The other instance will not be notified about this, but instead
* socket operations will just fail. Due to this, it might be a good idea to
* disabel autoclose and manually close the socket.
*
* @return A clone of this TcpListener that shares the same underlying socket.
*/
TcpListener clone();
}; };
......
...@@ -80,12 +80,12 @@ public: ...@@ -80,12 +80,12 @@ public:
TcpStream& operator=(TcpStream &&other); TcpStream& operator=(TcpStream &&other);
/** /**
* @brief Copying TcpListener is not allowed. * @brief Copying TcpListener is not allowed. See clone() for explicit copies.
*/ */
TcpStream(const TcpStream &other) = delete; TcpStream(const TcpStream &other) = delete;
/** /**
* @brief Copying TcpListener is not allowed. * @brief Copying TcpListener is not allowed. See clone() for explicit copies.
*/ */
TcpStream& operator=(const TcpStream &other) = delete; TcpStream& operator=(const TcpStream &other) = delete;
...@@ -231,6 +231,27 @@ public: ...@@ -231,6 +231,27 @@ public:
*/ */
bool isClosed() const; bool isClosed() const;
/**
* @brief Set the behavior for when the TcpStream is destroyed. If autoclose
* is enabled, the socket is closed on destruct. If autoclose is disabled, the
* socket will not be closed automatically.
*
* @param autoclose Enable or disable the autoclose functionality.
*/
void setAutoclose(bool autoclose);
/**
* @brief Create a clone of this socket wrapper object. The clone and the
* original will share the same underlying socket and file descriptor.
* If one of the instances closes the socket, the socket will be closed
* for both. The other instance will not be notified about this, but instead
* socket operations will just fail. Due to this, it might be a good idea to
* disabel autoclose and manually close the socket.
*
* @return A clone of this TcpStream that shares the same underlying socket.
*/
TcpStream clone();
friend class TcpListener; friend class TcpListener;
}; };
......
...@@ -98,12 +98,12 @@ public: ...@@ -98,12 +98,12 @@ public:
UdpSocket& operator=(UdpSocket &&other); UdpSocket& operator=(UdpSocket &&other);
/** /**
* @brief Copying UdpSocket is not allowed. * @brief Copying UdpSocket is not allowed. See clone() for explicit copies.
*/ */
UdpSocket(const UdpSocket &other) = delete; UdpSocket(const UdpSocket &other) = delete;
/** /**
* @brief Copying UdpSocket is not allowed. * @brief Copying UdpSocket is not allowed. See clone() for explicit copies.
*/ */
UdpSocket& operator=(const UdpSocket &other) = delete; UdpSocket& operator=(const UdpSocket &other) = delete;
...@@ -217,6 +217,27 @@ public: ...@@ -217,6 +217,27 @@ public:
*/ */
void close(); void close();
/**
* @brief Set the behavior for when the UdpSocket is destroyed. If autoclose
* is enabled, the socket is closed on destruct. If autoclose is disabled, the
* socket will not be closed automatically.
*
* @param autoclose Enable or disable the autoclose functionality.
*/
void setAutoclose(bool autoclose);
/**
* @brief Create a clone of this socket wrapper object. The clone and the
* original will share the same underlying socket and file descriptor.
* If one of the instances closes the socket, the socket will be closed
* for both. The other instance will not be notified about this, but instead
* socket operations will just fail. Due to this, it might be a good idea to
* disabel autoclose and manually close the socket.
*
* @return A clone of this UdpSocket that shares the same underlying socket.
*/
UdpSocket clone();
}; };
......
...@@ -129,4 +129,18 @@ void TcpListener::close() ...@@ -129,4 +129,18 @@ void TcpListener::close()
} }
sockfd = 0; sockfd = 0;
}
void TcpListener::setAutoclose(bool _autoclose)
{
autoclose = _autoclose;
}
TcpListener TcpListener::clone()
{
TcpListener other{local};
other.sockfd = sockfd;
other.autoclose = autoclose;
return other;
} }
\ No newline at end of file
...@@ -259,4 +259,18 @@ const SockAddr & TcpStream::getRemoteAddr() const ...@@ -259,4 +259,18 @@ const SockAddr & TcpStream::getRemoteAddr() const
bool TcpStream::isClosed() const bool TcpStream::isClosed() const
{ {
return sockfd == 0; return sockfd == 0;
}
void TcpStream::setAutoclose(bool _autoclose)
{
autoclose = _autoclose;
}
TcpStream TcpStream::clone()
{
TcpStream other{remote};
other.sockfd = sockfd;
other.autoclose = autoclose;
return other;
} }
\ No newline at end of file
...@@ -190,4 +190,21 @@ void UdpSocket::close() ...@@ -190,4 +190,21 @@ void UdpSocket::close()
::close(sockfd); ::close(sockfd);
} }
sockfd = 0; sockfd = 0;
}
void UdpSocket::setAutoclose(bool _autoclose)
{
autoclose = _autoclose;
}
UdpSocket UdpSocket::clone()
{
UdpSocket other;
other.local = local;
other.sockfd = sockfd;
other.raw_socklen = raw_socklen;
other.address_family = address_family;
other.autoclose = autoclose;
return other;
} }
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment