diff --git a/inc/tcplistener.hpp b/inc/tcplistener.hpp index fbbd7273692fdea6673663119c46b268877e0701..86b1e60defe8623da13f8467f21befb05125c78f 100644 --- a/inc/tcplistener.hpp +++ b/inc/tcplistener.hpp @@ -78,12 +78,12 @@ public: 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; /** - * @brief Copying TcpListener is not allowed. + * @brief Copying TcpListener is not allowed. See clone() for explicit copies. */ TcpListener& operator=(const TcpListener &other) = delete; @@ -120,6 +120,27 @@ public: */ 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(); + }; diff --git a/inc/tcpstream.hpp b/inc/tcpstream.hpp index 8b0ad76035c46ea0ca1628ae3480304f8098e1a1..fcb0801e71ad766535a600902a4949b3ce9e3b45 100644 --- a/inc/tcpstream.hpp +++ b/inc/tcpstream.hpp @@ -80,12 +80,12 @@ public: 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; /** - * @brief Copying TcpListener is not allowed. + * @brief Copying TcpListener is not allowed. See clone() for explicit copies. */ TcpStream& operator=(const TcpStream &other) = delete; @@ -231,6 +231,27 @@ public: */ 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; }; diff --git a/inc/udpsocket.hpp b/inc/udpsocket.hpp index 99771ebff40e3f9bd37b0b5478cfca38c806fece..112e329e654a98923f24676eab6cd52f07eee154 100644 --- a/inc/udpsocket.hpp +++ b/inc/udpsocket.hpp @@ -98,12 +98,12 @@ public: 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; /** - * @brief Copying UdpSocket is not allowed. + * @brief Copying UdpSocket is not allowed. See clone() for explicit copies. */ UdpSocket& operator=(const UdpSocket &other) = delete; @@ -217,6 +217,27 @@ public: */ 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(); + }; diff --git a/src/tcplistener.cpp b/src/tcplistener.cpp index bbab4066d8e3b801948788d288ac2675cb7ae9c9..dc5262f8ae1654d85762fa46eab49ce017593937 100644 --- a/src/tcplistener.cpp +++ b/src/tcplistener.cpp @@ -129,4 +129,18 @@ void TcpListener::close() } 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 diff --git a/src/tcpstream.cpp b/src/tcpstream.cpp index 85efb7e07c579a16ba619f5e5d2cb22755c33bc9..8f6e5fd27a15b44c74b903b7d1849c002264035e 100644 --- a/src/tcpstream.cpp +++ b/src/tcpstream.cpp @@ -259,4 +259,18 @@ const SockAddr & TcpStream::getRemoteAddr() const bool TcpStream::isClosed() const { 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 diff --git a/src/udpsocket.cpp b/src/udpsocket.cpp index 3f5cd111603255134fb9eb16874ed5d3a4663feb..9ef39b484b157e86d277106580dbf93ad7e5b80c 100644 --- a/src/udpsocket.cpp +++ b/src/udpsocket.cpp @@ -190,4 +190,21 @@ void UdpSocket::close() ::close(sockfd); } 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