From 76ea8c74c81b7208ac47b7090f59e28401491ff4 Mon Sep 17 00:00:00 2001 From: Matej Kenda Date: Tue, 19 Nov 2024 16:35:30 +0100 Subject: [PATCH] enh(C++17): Net: Modernisation of socket class declarations. --- Net/include/Poco/Net/DatagramSocket.h | 2 +- Net/include/Poco/Net/DatagramSocketImpl.h | 4 +-- Net/include/Poco/Net/DialogSocket.h | 2 +- Net/include/Poco/Net/MulticastSocket.h | 2 +- Net/include/Poco/Net/ParallelSocketAcceptor.h | 13 +++++----- Net/include/Poco/Net/PollSet.h | 2 +- Net/include/Poco/Net/RawSocket.h | 2 +- Net/include/Poco/Net/RawSocketImpl.h | 4 +-- Net/include/Poco/Net/ServerSocket.h | 2 +- Net/include/Poco/Net/ServerSocketImpl.h | 2 +- Net/include/Poco/Net/Socket.h | 3 ++- Net/include/Poco/Net/SocketAcceptor.h | 9 ++++--- Net/include/Poco/Net/SocketConnector.h | 11 ++++---- Net/include/Poco/Net/SocketNotification.h | 14 +++++----- Net/include/Poco/Net/SocketNotifier.h | 8 +++--- Net/include/Poco/Net/SocketProactor.h | 26 +++++++++---------- Net/include/Poco/Net/SocketStream.h | 14 +++++----- Net/include/Poco/Net/StreamSocket.h | 2 +- Net/include/Poco/Net/StreamSocketImpl.h | 2 +- Net/src/SocketReactor.cpp | 20 +++++++------- 20 files changed, 73 insertions(+), 71 deletions(-) diff --git a/Net/include/Poco/Net/DatagramSocket.h b/Net/include/Poco/Net/DatagramSocket.h index 8c3d69aaf4..82b1b60c9b 100644 --- a/Net/include/Poco/Net/DatagramSocket.h +++ b/Net/include/Poco/Net/DatagramSocket.h @@ -67,7 +67,7 @@ class Net_API DatagramSocket: public Socket /// Creates the DatagramSocket with the SocketImpl /// from another socket. - ~DatagramSocket(); + ~DatagramSocket() override; /// Destroys the DatagramSocket. DatagramSocket& operator = (const Socket& socket); diff --git a/Net/include/Poco/Net/DatagramSocketImpl.h b/Net/include/Poco/Net/DatagramSocketImpl.h index 867af46e54..6c1182e172 100644 --- a/Net/include/Poco/Net/DatagramSocketImpl.h +++ b/Net/include/Poco/Net/DatagramSocketImpl.h @@ -43,9 +43,9 @@ class Net_API DatagramSocketImpl: public SocketImpl /// Creates a StreamSocketImpl using the given native socket. protected: - void init(int af); + void init(int af) override; - ~DatagramSocketImpl(); + ~DatagramSocketImpl() override; }; diff --git a/Net/include/Poco/Net/DialogSocket.h b/Net/include/Poco/Net/DialogSocket.h index b9104d0927..080140410c 100644 --- a/Net/include/Poco/Net/DialogSocket.h +++ b/Net/include/Poco/Net/DialogSocket.h @@ -67,7 +67,7 @@ class Net_API DialogSocket: public StreamSocket DialogSocket(const DialogSocket& socket); /// Creates the DialogSocket as copy of another dialog socket. - ~DialogSocket(); + ~DialogSocket() override; /// Destroys the DialogSocket. DialogSocket& operator = (const Socket& socket); diff --git a/Net/include/Poco/Net/MulticastSocket.h b/Net/include/Poco/Net/MulticastSocket.h index 0bc4f4a1d4..e05513f915 100644 --- a/Net/include/Poco/Net/MulticastSocket.h +++ b/Net/include/Poco/Net/MulticastSocket.h @@ -67,7 +67,7 @@ class Net_API MulticastSocket: public DatagramSocket /// a DatagramSocketImpl, otherwise an InvalidArgumentException /// will be thrown. - ~MulticastSocket(); + ~MulticastSocket() override; /// Destroys the DatagramSocket. MulticastSocket& operator = (const Socket& socket); diff --git a/Net/include/Poco/Net/ParallelSocketAcceptor.h b/Net/include/Poco/Net/ParallelSocketAcceptor.h index 97da2013f9..1e5e2c0355 100644 --- a/Net/include/Poco/Net/ParallelSocketAcceptor.h +++ b/Net/include/Poco/Net/ParallelSocketAcceptor.h @@ -52,7 +52,7 @@ class ParallelSocketAcceptor const std::string& threadName = ""): _threadName(threadName), _socket(socket), - _pReactor(0), + _pReactor(nullptr), _threads(threads), _next(0) /// Creates a ParallelSocketAcceptor using the given ServerSocket, @@ -93,6 +93,10 @@ class ParallelSocketAcceptor } } + ParallelSocketAcceptor() = delete; + ParallelSocketAcceptor(const ParallelSocketAcceptor&) = delete; + ParallelSocketAcceptor& operator = (const ParallelSocketAcceptor&) = delete; + void setReactor(SocketReactor& reactor) /// Sets the reactor for this acceptor. { @@ -140,7 +144,7 @@ class ParallelSocketAcceptor } protected: - typedef std::vector ReactorVec; + using ReactorVec = std::vector; virtual ServiceHandler* createServiceHandler(StreamSocket& socket) /// Create and initialize a new ServiceHandler instance. @@ -172,7 +176,7 @@ class ParallelSocketAcceptor { if ((*it)->has(socket)) return it->get(); } - return 0; + return nullptr; } SocketReactor* reactor() @@ -218,9 +222,6 @@ class ParallelSocketAcceptor } private: - ParallelSocketAcceptor(); - ParallelSocketAcceptor(const ParallelSocketAcceptor&); - ParallelSocketAcceptor& operator = (const ParallelSocketAcceptor&); std::string _threadName; /// Name prefix of sub SocketReactor threads diff --git a/Net/include/Poco/Net/PollSet.h b/Net/include/Poco/Net/PollSet.h index 8dd579bffe..9f1dc7e1f9 100644 --- a/Net/include/Poco/Net/PollSet.h +++ b/Net/include/Poco/Net/PollSet.h @@ -97,7 +97,7 @@ class Net_API PollSet /// Returns the number of sockets monitored. //@ deprecated - int count() const; + POCO_DEPRECATED("Use size() instead") int count() const; /// Returns the number of sockets monitored. /// This method is deprecated. Use size() instead. diff --git a/Net/include/Poco/Net/RawSocket.h b/Net/include/Poco/Net/RawSocket.h index 3382860e4f..771eb0446a 100644 --- a/Net/include/Poco/Net/RawSocket.h +++ b/Net/include/Poco/Net/RawSocket.h @@ -57,7 +57,7 @@ class Net_API RawSocket: public Socket /// Creates the RawSocket with the SocketImpl /// from another socket. - ~RawSocket(); + ~RawSocket() override; /// Destroys the RawSocket. RawSocket& operator = (const Socket& socket); diff --git a/Net/include/Poco/Net/RawSocketImpl.h b/Net/include/Poco/Net/RawSocketImpl.h index fe781f5a5a..e6a9480a7c 100644 --- a/Net/include/Poco/Net/RawSocketImpl.h +++ b/Net/include/Poco/Net/RawSocketImpl.h @@ -43,10 +43,10 @@ class Net_API RawSocketImpl: public SocketImpl /// Creates a RawSocketImpl using the given native socket. protected: - void init(int af); + void init(int af) override; void init2(int af, int proto); - ~RawSocketImpl(); + ~RawSocketImpl() override; }; diff --git a/Net/include/Poco/Net/ServerSocket.h b/Net/include/Poco/Net/ServerSocket.h index e13d46ea0f..b6556cc388 100644 --- a/Net/include/Poco/Net/ServerSocket.h +++ b/Net/include/Poco/Net/ServerSocket.h @@ -64,7 +64,7 @@ class Net_API ServerSocket: public Socket // Creates a socket from an existing file descriptor. // Ownership is taken by poco - virtual ~ServerSocket(); + ~ServerSocket() override; /// Destroys the ServerSocket. ServerSocket& operator = (const Socket& socket); diff --git a/Net/include/Poco/Net/ServerSocketImpl.h b/Net/include/Poco/Net/ServerSocketImpl.h index aaa5c7cc20..b1ea05b98a 100644 --- a/Net/include/Poco/Net/ServerSocketImpl.h +++ b/Net/include/Poco/Net/ServerSocketImpl.h @@ -34,7 +34,7 @@ class Net_API ServerSocketImpl: public SocketImpl /// Creates the ServerSocketImpl. protected: - virtual ~ServerSocketImpl(); + ~ServerSocketImpl() override; /// Destroys the ServerSocketImpl. }; diff --git a/Net/include/Poco/Net/Socket.h b/Net/include/Poco/Net/Socket.h index f7118e9395..4c902d9ea5 100644 --- a/Net/include/Poco/Net/Socket.h +++ b/Net/include/Poco/Net/Socket.h @@ -411,11 +411,12 @@ class FDCompare { public: FDCompare(int fd): _fd(fd) { } + FDCompare() = delete; + inline bool operator()(const Socket& socket) const { return socket.sockfd() == _fd; } private: - FDCompare(); int _fd; }; #endif diff --git a/Net/include/Poco/Net/SocketAcceptor.h b/Net/include/Poco/Net/SocketAcceptor.h index 837ace3c72..78cfd38c77 100644 --- a/Net/include/Poco/Net/SocketAcceptor.h +++ b/Net/include/Poco/Net/SocketAcceptor.h @@ -72,7 +72,7 @@ class SocketAcceptor explicit SocketAcceptor(ServerSocket& socket): _socket(socket), - _pReactor(0) + _pReactor(nullptr) /// Creates a SocketAcceptor, using the given ServerSocket. { } @@ -102,6 +102,10 @@ class SocketAcceptor } } + SocketAcceptor() = delete; + SocketAcceptor(const SocketAcceptor&) = delete; + SocketAcceptor& operator = (const SocketAcceptor&) = delete; + void setReactor(SocketReactor& reactor) /// Sets the reactor for this acceptor. { @@ -176,9 +180,6 @@ class SocketAcceptor } private: - SocketAcceptor(); - SocketAcceptor(const SocketAcceptor&); - SocketAcceptor& operator = (const SocketAcceptor&); ServerSocket _socket; SocketReactor* _pReactor; diff --git a/Net/include/Poco/Net/SocketConnector.h b/Net/include/Poco/Net/SocketConnector.h index 5da0076644..8379138331 100644 --- a/Net/include/Poco/Net/SocketConnector.h +++ b/Net/include/Poco/Net/SocketConnector.h @@ -74,14 +74,14 @@ class SocketConnector { public: explicit SocketConnector(const SocketAddress& address): - _pReactor(0) + _pReactor(nullptr) /// Creates a SocketConnector, using the given Socket. { _socket.connectNB(address); } SocketConnector(const SocketAddress& address, SocketReactor& reactor, bool doRegister = true) : - _pReactor(0) + _pReactor(nullptr) /// Creates an connector, using the given ServerSocket. /// The SocketConnector registers itself with the given SocketReactor. { @@ -102,6 +102,10 @@ class SocketConnector } } + SocketConnector() = delete; + SocketConnector(const SocketConnector&) = delete; + SocketConnector& operator = (const SocketConnector&) = delete; + virtual void registerConnector(SocketReactor& reactor) /// Registers the SocketConnector with a SocketReactor. /// @@ -193,9 +197,6 @@ class SocketConnector } private: - SocketConnector(); - SocketConnector(const SocketConnector&); - SocketConnector& operator = (const SocketConnector&); StreamSocket _socket; SocketReactor* _pReactor; diff --git a/Net/include/Poco/Net/SocketNotification.h b/Net/include/Poco/Net/SocketNotification.h index b2770abceb..27a49766d9 100644 --- a/Net/include/Poco/Net/SocketNotification.h +++ b/Net/include/Poco/Net/SocketNotification.h @@ -38,7 +38,7 @@ class Net_API SocketNotification: public Poco::Notification explicit SocketNotification(SocketReactor* pReactor); /// Creates the SocketNotification for the given SocketReactor. - virtual ~SocketNotification(); + ~SocketNotification() override; /// Destroys the SocketNotification. SocketReactor& source() const; @@ -65,7 +65,7 @@ class Net_API ReadableNotification: public SocketNotification ReadableNotification(SocketReactor* pReactor); /// Creates the ReadableNotification for the given SocketReactor. - ~ReadableNotification(); + ~ReadableNotification() override; /// Destroys the ReadableNotification. }; @@ -77,7 +77,7 @@ class Net_API WritableNotification: public SocketNotification WritableNotification(SocketReactor* pReactor); /// Creates the WritableNotification for the given SocketReactor. - ~WritableNotification(); + ~WritableNotification() override; /// Destroys the WritableNotification. }; @@ -93,7 +93,7 @@ class Net_API ErrorNotification: public SocketNotification int code = 0, const std::string& description = ""); /// Creates the ErrorNotification for the given SocketReactor. - ~ErrorNotification(); + ~ErrorNotification() override; /// Destroys the ErrorNotification. int code() const; @@ -128,7 +128,7 @@ class Net_API TimeoutNotification: public SocketNotification TimeoutNotification(SocketReactor* pReactor); /// Creates the TimeoutNotification for the given SocketReactor. - ~TimeoutNotification(); + ~TimeoutNotification() override; /// Destroys the TimeoutNotification. }; @@ -141,7 +141,7 @@ class Net_API IdleNotification: public SocketNotification IdleNotification(SocketReactor* pReactor); /// Creates the IdleNotification for the given SocketReactor. - ~IdleNotification(); + ~IdleNotification() override; /// Destroys the IdleNotification. }; @@ -154,7 +154,7 @@ class Net_API ShutdownNotification: public SocketNotification ShutdownNotification(SocketReactor* pReactor); /// Creates the ShutdownNotification for the given SocketReactor. - ~ShutdownNotification(); + ~ShutdownNotification() override; /// Destroys the ShutdownNotification. }; diff --git a/Net/include/Poco/Net/SocketNotifier.h b/Net/include/Poco/Net/SocketNotifier.h index 64a5e66c6f..7700777037 100644 --- a/Net/include/Poco/Net/SocketNotifier.h +++ b/Net/include/Poco/Net/SocketNotifier.h @@ -65,13 +65,13 @@ class Net_API SocketNotifier: public Poco::RefCountedObject /// Returns the number of subscribers; protected: - ~SocketNotifier(); + ~SocketNotifier() override; /// Destroys the SocketNotifier. private: - typedef std::multiset EventSet; - typedef Poco::FastMutex MutexType; - typedef MutexType::ScopedLock ScopedLock; + using EventSet = std::multiset; + using MutexType = Poco::FastMutex; + using ScopedLock = MutexType::ScopedLock; EventSet _events; Poco::NotificationCenter _nc; diff --git a/Net/include/Poco/Net/SocketProactor.h b/Net/include/Poco/Net/SocketProactor.h index 7650ceba70..46ff9c0185 100644 --- a/Net/include/Poco/Net/SocketProactor.h +++ b/Net/include/Poco/Net/SocketProactor.h @@ -79,7 +79,7 @@ class Net_API SocketProactor final: public Poco::Runnable SocketProactor& operator=(const SocketProactor&) = delete; SocketProactor& operator=(SocketProactor&&) = delete; - ~SocketProactor(); + ~SocketProactor() override; /// Destroys the SocketProactor. void addWork(const Work& ch, Timestamp::TimeDiff ms = PERMANENT_COMPLETION_HANDLER); @@ -113,7 +113,7 @@ class Net_API SocketProactor final: public Poco::Runnable /// from the front of the schedule queue. /// Default is removal of all functions. - int poll(int* pHandled = 0); + int poll(int* pHandled = nullptr); /// Polls all registered sockets and calls their respective handlers. /// If pHandled is not null, after the call it contains the total number /// of read/write/error socket handlers called. @@ -126,7 +126,7 @@ class Net_API SocketProactor final: public Poco::Runnable /// Returns 1 on successful handler invocation, 0 on /// exception. - void run(); + void run() override; /// Runs the SocketProactor. The reactor will run /// until stop() is called (in a separate thread). @@ -157,13 +157,13 @@ class Net_API SocketProactor final: public Poco::Runnable Poco::Timespan getTimeout() const; /// Returns the timeout. - void addSocket(Socket sock, int mode); + void addSocket(const Socket& sock, int mode); /// Adds the socket to the poll set. - void updateSocket(Socket sock, int mode); + void updateSocket(const Socket& sock, int mode); /// Updates the socket mode in the poll set. - void removeSocket(Socket sock); + void removeSocket(const Socket& sock); /// Removes the socket from the poll set. void addReceiveFrom(Socket sock, Buffer& buf, SocketAddress& addr, Callback&& onCompletion); @@ -212,8 +212,8 @@ class Net_API SocketProactor final: public Poco::Runnable /// If expiredOnly is true, only expired temporary functions /// are called. - typedef Poco::Mutex MutexType; - typedef MutexType::ScopedLock ScopedLock; + using MutexType = Poco::Mutex; + using ScopedLock = MutexType::ScopedLock; static const long DEFAULT_MAX_TIMEOUT_MS = 250; @@ -245,7 +245,7 @@ class Net_API SocketProactor final: public Poco::Runnable { } - ~IONotification() = default; + ~IONotification() override = default; void call() /// Calls the completion handler. @@ -319,7 +319,7 @@ class Net_API SocketProactor final: public Poco::Runnable bool runOne() /// Runs the next I/O completion handler in the queue. { - IONotification* pNf = dynamic_cast(_nq.waitDequeueNotification()); + auto* pNf = dynamic_cast(_nq.waitDequeueNotification()); if (_activity.isStopped()) return false; if (pNf) { @@ -462,19 +462,19 @@ class Net_API SocketProactor final: public Poco::Runnable // inlines // -inline void SocketProactor::addSocket(Socket sock, int mode) +inline void SocketProactor::addSocket(const Socket& sock, int mode) { _pollSet.add(sock, mode | PollSet::POLL_ERROR); } -inline void SocketProactor::updateSocket(Socket sock, int mode) +inline void SocketProactor::updateSocket(const Socket& sock, int mode) { _pollSet.update(sock, mode); } -inline void SocketProactor::removeSocket(Socket sock) +inline void SocketProactor::removeSocket(const Socket& sock) { _pollSet.remove(sock); } diff --git a/Net/include/Poco/Net/SocketStream.h b/Net/include/Poco/Net/SocketStream.h index d8faca9b23..d7cc5d6f1c 100644 --- a/Net/include/Poco/Net/SocketStream.h +++ b/Net/include/Poco/Net/SocketStream.h @@ -42,15 +42,15 @@ class Net_API SocketStreamBuf: public Poco::BufferedBidirectionalStreamBuf /// The socket's SocketImpl must be a StreamSocketImpl, /// otherwise an InvalidArgumentException is thrown. - ~SocketStreamBuf(); + ~SocketStreamBuf() override; /// Destroys the SocketStreamBuf. StreamSocketImpl* socketImpl() const; /// Returns the internal SocketImpl. protected: - int readFromDevice(char* buffer, std::streamsize length); - int writeToDevice(const char* buffer, std::streamsize length); + int readFromDevice(char* buffer, std::streamsize length) override; + int writeToDevice(const char* buffer, std::streamsize length) override; private: enum @@ -76,7 +76,7 @@ class Net_API SocketIOS: public virtual std::ios /// The socket's SocketImpl must be a StreamSocketImpl, /// otherwise an InvalidArgumentException is thrown. - ~SocketIOS(); + ~SocketIOS() override; /// Destroys the SocketIOS. /// /// Flushes the buffer, but does not close the socket. @@ -105,7 +105,7 @@ class Net_API SocketOutputStream: public SocketIOS, public std::ostream /// The socket's SocketImpl must be a StreamSocketImpl, /// otherwise an InvalidArgumentException is thrown. - ~SocketOutputStream(); + ~SocketOutputStream() override; /// Destroys the SocketOutputStream. /// /// Flushes the buffer, but does not close the socket. @@ -131,7 +131,7 @@ class Net_API SocketInputStream: public SocketIOS, public std::istream /// The socket's SocketImpl must be a StreamSocketImpl, /// otherwise an InvalidArgumentException is thrown. - ~SocketInputStream(); + ~SocketInputStream() override; /// Destroys the SocketInputStream. }; @@ -155,7 +155,7 @@ class Net_API SocketStream: public SocketIOS, public std::iostream /// The socket's SocketImpl must be a StreamSocketImpl, /// otherwise an InvalidArgumentException is thrown. - ~SocketStream(); + ~SocketStream() override; /// Destroys the SocketStream. /// /// Flushes the buffer, but does not close the socket. diff --git a/Net/include/Poco/Net/StreamSocket.h b/Net/include/Poco/Net/StreamSocket.h index 0b219a0310..662fb6dd3d 100644 --- a/Net/include/Poco/Net/StreamSocket.h +++ b/Net/include/Poco/Net/StreamSocket.h @@ -69,7 +69,7 @@ class Net_API StreamSocket: public Socket /// Creates the StreamSocket with the SocketImpl /// from another socket. - virtual ~StreamSocket(); + ~StreamSocket() override; /// Destroys the StreamSocket. StreamSocket& operator = (const Socket& socket); diff --git a/Net/include/Poco/Net/StreamSocketImpl.h b/Net/include/Poco/Net/StreamSocketImpl.h index a60a9826bf..733631c088 100644 --- a/Net/include/Poco/Net/StreamSocketImpl.h +++ b/Net/include/Poco/Net/StreamSocketImpl.h @@ -40,7 +40,7 @@ class Net_API StreamSocketImpl: public SocketImpl StreamSocketImpl(poco_socket_t sockfd); /// Creates a StreamSocketImpl using the given native socket. - virtual int sendBytes(const void* buffer, int length, int flags = 0); + int sendBytes(const void* buffer, int length, int flags = 0) override; /// Ensures that all data in buffer is sent if the socket /// is blocking. In case of a non-blocking socket, sends as /// many bytes as possible. diff --git a/Net/src/SocketReactor.cpp b/Net/src/SocketReactor.cpp index 2815d07899..41e9250ea4 100644 --- a/Net/src/SocketReactor.cpp +++ b/Net/src/SocketReactor.cpp @@ -27,7 +27,7 @@ namespace Poco { namespace Net { -SocketReactor::SocketReactor(): _threadAffinity(-1), +SocketReactor::SocketReactor(): _stop(false), _pReadableNotification(new ReadableNotification(this)), _pWritableNotification(new WritableNotification(this)), @@ -64,9 +64,7 @@ SocketReactor::SocketReactor(const Params& params, int threadAffinity): } -SocketReactor::~SocketReactor() -{ -} +SocketReactor::~SocketReactor() = default; void SocketReactor::run() @@ -216,15 +214,15 @@ bool SocketReactor::hasEventHandler(const Socket& socket, const Poco::AbstractOb SocketReactor::NotifierPtr SocketReactor::getNotifier(const Socket& socket, bool makeNew) { const SocketImpl* pImpl = socket.impl(); - if (pImpl == nullptr) return 0; + if (pImpl == nullptr) return nullptr; poco_socket_t sockfd = pImpl->sockfd(); ScopedLock lock(_mutex); - EventHandlerMap::iterator it = _handlers.find(sockfd); + auto it = _handlers.find(sockfd); if (it != _handlers.end()) return it->second; else if (makeNew) return (_handlers[sockfd] = new SocketNotifier(socket)); - return 0; + return nullptr; } @@ -283,12 +281,12 @@ void SocketReactor::dispatch(SocketNotification* pNotification) { ScopedLock lock(_mutex); delegates.reserve(_handlers.size()); - for (EventHandlerMap::iterator it = _handlers.begin(); it != _handlers.end(); ++it) - delegates.push_back(it->second); + for (auto& _handler : _handlers) + delegates.push_back(_handler.second); } - for (std::vector::iterator it = delegates.begin(); it != delegates.end(); ++it) + for (auto& delegate : delegates) { - dispatch(*it, pNotification); + dispatch(delegate, pNotification); } }