enh(C++17): Net: Modernisation of socket class declarations.

This commit is contained in:
Matej Kenda 2024-11-19 16:35:30 +01:00
parent efb0745efc
commit 76ea8c74c8
20 changed files with 73 additions and 71 deletions

View File

@ -67,7 +67,7 @@ public:
/// Creates the DatagramSocket with the SocketImpl /// Creates the DatagramSocket with the SocketImpl
/// from another socket. /// from another socket.
~DatagramSocket(); ~DatagramSocket() override;
/// Destroys the DatagramSocket. /// Destroys the DatagramSocket.
DatagramSocket& operator = (const Socket& socket); DatagramSocket& operator = (const Socket& socket);

View File

@ -43,9 +43,9 @@ public:
/// Creates a StreamSocketImpl using the given native socket. /// Creates a StreamSocketImpl using the given native socket.
protected: protected:
void init(int af); void init(int af) override;
~DatagramSocketImpl(); ~DatagramSocketImpl() override;
}; };

View File

@ -67,7 +67,7 @@ public:
DialogSocket(const DialogSocket& socket); DialogSocket(const DialogSocket& socket);
/// Creates the DialogSocket as copy of another dialog socket. /// Creates the DialogSocket as copy of another dialog socket.
~DialogSocket(); ~DialogSocket() override;
/// Destroys the DialogSocket. /// Destroys the DialogSocket.
DialogSocket& operator = (const Socket& socket); DialogSocket& operator = (const Socket& socket);

View File

@ -67,7 +67,7 @@ public:
/// a DatagramSocketImpl, otherwise an InvalidArgumentException /// a DatagramSocketImpl, otherwise an InvalidArgumentException
/// will be thrown. /// will be thrown.
~MulticastSocket(); ~MulticastSocket() override;
/// Destroys the DatagramSocket. /// Destroys the DatagramSocket.
MulticastSocket& operator = (const Socket& socket); MulticastSocket& operator = (const Socket& socket);

View File

@ -52,7 +52,7 @@ public:
const std::string& threadName = ""): const std::string& threadName = ""):
_threadName(threadName), _threadName(threadName),
_socket(socket), _socket(socket),
_pReactor(0), _pReactor(nullptr),
_threads(threads), _threads(threads),
_next(0) _next(0)
/// Creates a ParallelSocketAcceptor using the given ServerSocket, /// Creates a ParallelSocketAcceptor using the given ServerSocket,
@ -93,6 +93,10 @@ public:
} }
} }
ParallelSocketAcceptor() = delete;
ParallelSocketAcceptor(const ParallelSocketAcceptor&) = delete;
ParallelSocketAcceptor& operator = (const ParallelSocketAcceptor&) = delete;
void setReactor(SocketReactor& reactor) void setReactor(SocketReactor& reactor)
/// Sets the reactor for this acceptor. /// Sets the reactor for this acceptor.
{ {
@ -140,7 +144,7 @@ public:
} }
protected: protected:
typedef std::vector<typename ParallelReactor::Ptr> ReactorVec; using ReactorVec = std::vector<typename ParallelReactor::Ptr>;
virtual ServiceHandler* createServiceHandler(StreamSocket& socket) virtual ServiceHandler* createServiceHandler(StreamSocket& socket)
/// Create and initialize a new ServiceHandler instance. /// Create and initialize a new ServiceHandler instance.
@ -172,7 +176,7 @@ protected:
{ {
if ((*it)->has(socket)) return it->get(); if ((*it)->has(socket)) return it->get();
} }
return 0; return nullptr;
} }
SocketReactor* reactor() SocketReactor* reactor()
@ -218,9 +222,6 @@ protected:
} }
private: private:
ParallelSocketAcceptor();
ParallelSocketAcceptor(const ParallelSocketAcceptor&);
ParallelSocketAcceptor& operator = (const ParallelSocketAcceptor&);
std::string _threadName; std::string _threadName;
/// Name prefix of sub SocketReactor threads /// Name prefix of sub SocketReactor threads

View File

@ -97,7 +97,7 @@ public:
/// Returns the number of sockets monitored. /// Returns the number of sockets monitored.
//@ deprecated //@ deprecated
int count() const; POCO_DEPRECATED("Use size() instead") int count() const;
/// Returns the number of sockets monitored. /// Returns the number of sockets monitored.
/// This method is deprecated. Use size() instead. /// This method is deprecated. Use size() instead.

View File

@ -57,7 +57,7 @@ public:
/// Creates the RawSocket with the SocketImpl /// Creates the RawSocket with the SocketImpl
/// from another socket. /// from another socket.
~RawSocket(); ~RawSocket() override;
/// Destroys the RawSocket. /// Destroys the RawSocket.
RawSocket& operator = (const Socket& socket); RawSocket& operator = (const Socket& socket);

View File

@ -43,10 +43,10 @@ public:
/// Creates a RawSocketImpl using the given native socket. /// Creates a RawSocketImpl using the given native socket.
protected: protected:
void init(int af); void init(int af) override;
void init2(int af, int proto); void init2(int af, int proto);
~RawSocketImpl(); ~RawSocketImpl() override;
}; };

View File

@ -64,7 +64,7 @@ public:
// Creates a socket from an existing file descriptor. // Creates a socket from an existing file descriptor.
// Ownership is taken by poco // Ownership is taken by poco
virtual ~ServerSocket(); ~ServerSocket() override;
/// Destroys the ServerSocket. /// Destroys the ServerSocket.
ServerSocket& operator = (const Socket& socket); ServerSocket& operator = (const Socket& socket);

View File

@ -34,7 +34,7 @@ public:
/// Creates the ServerSocketImpl. /// Creates the ServerSocketImpl.
protected: protected:
virtual ~ServerSocketImpl(); ~ServerSocketImpl() override;
/// Destroys the ServerSocketImpl. /// Destroys the ServerSocketImpl.
}; };

View File

@ -411,11 +411,12 @@ class FDCompare
{ {
public: public:
FDCompare(int fd): _fd(fd) { } FDCompare(int fd): _fd(fd) { }
FDCompare() = delete;
inline bool operator()(const Socket& socket) const inline bool operator()(const Socket& socket) const
{ return socket.sockfd() == _fd; } { return socket.sockfd() == _fd; }
private: private:
FDCompare();
int _fd; int _fd;
}; };
#endif #endif

View File

@ -72,7 +72,7 @@ public:
explicit SocketAcceptor(ServerSocket& socket): explicit SocketAcceptor(ServerSocket& socket):
_socket(socket), _socket(socket),
_pReactor(0) _pReactor(nullptr)
/// Creates a SocketAcceptor, using the given ServerSocket. /// Creates a SocketAcceptor, using the given ServerSocket.
{ {
} }
@ -102,6 +102,10 @@ public:
} }
} }
SocketAcceptor() = delete;
SocketAcceptor(const SocketAcceptor&) = delete;
SocketAcceptor& operator = (const SocketAcceptor&) = delete;
void setReactor(SocketReactor& reactor) void setReactor(SocketReactor& reactor)
/// Sets the reactor for this acceptor. /// Sets the reactor for this acceptor.
{ {
@ -176,9 +180,6 @@ protected:
} }
private: private:
SocketAcceptor();
SocketAcceptor(const SocketAcceptor&);
SocketAcceptor& operator = (const SocketAcceptor&);
ServerSocket _socket; ServerSocket _socket;
SocketReactor* _pReactor; SocketReactor* _pReactor;

View File

@ -74,14 +74,14 @@ class SocketConnector
{ {
public: public:
explicit SocketConnector(const SocketAddress& address): explicit SocketConnector(const SocketAddress& address):
_pReactor(0) _pReactor(nullptr)
/// Creates a SocketConnector, using the given Socket. /// Creates a SocketConnector, using the given Socket.
{ {
_socket.connectNB(address); _socket.connectNB(address);
} }
SocketConnector(const SocketAddress& address, SocketReactor& reactor, bool doRegister = true) : SocketConnector(const SocketAddress& address, SocketReactor& reactor, bool doRegister = true) :
_pReactor(0) _pReactor(nullptr)
/// Creates an connector, using the given ServerSocket. /// Creates an connector, using the given ServerSocket.
/// The SocketConnector registers itself with the given SocketReactor. /// The SocketConnector registers itself with the given SocketReactor.
{ {
@ -102,6 +102,10 @@ public:
} }
} }
SocketConnector() = delete;
SocketConnector(const SocketConnector&) = delete;
SocketConnector& operator = (const SocketConnector&) = delete;
virtual void registerConnector(SocketReactor& reactor) virtual void registerConnector(SocketReactor& reactor)
/// Registers the SocketConnector with a SocketReactor. /// Registers the SocketConnector with a SocketReactor.
/// ///
@ -193,9 +197,6 @@ protected:
} }
private: private:
SocketConnector();
SocketConnector(const SocketConnector&);
SocketConnector& operator = (const SocketConnector&);
StreamSocket _socket; StreamSocket _socket;
SocketReactor* _pReactor; SocketReactor* _pReactor;

View File

@ -38,7 +38,7 @@ public:
explicit SocketNotification(SocketReactor* pReactor); explicit SocketNotification(SocketReactor* pReactor);
/// Creates the SocketNotification for the given SocketReactor. /// Creates the SocketNotification for the given SocketReactor.
virtual ~SocketNotification(); ~SocketNotification() override;
/// Destroys the SocketNotification. /// Destroys the SocketNotification.
SocketReactor& source() const; SocketReactor& source() const;
@ -65,7 +65,7 @@ public:
ReadableNotification(SocketReactor* pReactor); ReadableNotification(SocketReactor* pReactor);
/// Creates the ReadableNotification for the given SocketReactor. /// Creates the ReadableNotification for the given SocketReactor.
~ReadableNotification(); ~ReadableNotification() override;
/// Destroys the ReadableNotification. /// Destroys the ReadableNotification.
}; };
@ -77,7 +77,7 @@ public:
WritableNotification(SocketReactor* pReactor); WritableNotification(SocketReactor* pReactor);
/// Creates the WritableNotification for the given SocketReactor. /// Creates the WritableNotification for the given SocketReactor.
~WritableNotification(); ~WritableNotification() override;
/// Destroys the WritableNotification. /// Destroys the WritableNotification.
}; };
@ -93,7 +93,7 @@ public:
int code = 0, const std::string& description = ""); int code = 0, const std::string& description = "");
/// Creates the ErrorNotification for the given SocketReactor. /// Creates the ErrorNotification for the given SocketReactor.
~ErrorNotification(); ~ErrorNotification() override;
/// Destroys the ErrorNotification. /// Destroys the ErrorNotification.
int code() const; int code() const;
@ -128,7 +128,7 @@ public:
TimeoutNotification(SocketReactor* pReactor); TimeoutNotification(SocketReactor* pReactor);
/// Creates the TimeoutNotification for the given SocketReactor. /// Creates the TimeoutNotification for the given SocketReactor.
~TimeoutNotification(); ~TimeoutNotification() override;
/// Destroys the TimeoutNotification. /// Destroys the TimeoutNotification.
}; };
@ -141,7 +141,7 @@ public:
IdleNotification(SocketReactor* pReactor); IdleNotification(SocketReactor* pReactor);
/// Creates the IdleNotification for the given SocketReactor. /// Creates the IdleNotification for the given SocketReactor.
~IdleNotification(); ~IdleNotification() override;
/// Destroys the IdleNotification. /// Destroys the IdleNotification.
}; };
@ -154,7 +154,7 @@ public:
ShutdownNotification(SocketReactor* pReactor); ShutdownNotification(SocketReactor* pReactor);
/// Creates the ShutdownNotification for the given SocketReactor. /// Creates the ShutdownNotification for the given SocketReactor.
~ShutdownNotification(); ~ShutdownNotification() override;
/// Destroys the ShutdownNotification. /// Destroys the ShutdownNotification.
}; };

View File

@ -65,13 +65,13 @@ public:
/// Returns the number of subscribers; /// Returns the number of subscribers;
protected: protected:
~SocketNotifier(); ~SocketNotifier() override;
/// Destroys the SocketNotifier. /// Destroys the SocketNotifier.
private: private:
typedef std::multiset<SocketNotification*> EventSet; using EventSet = std::multiset<SocketNotification *>;
typedef Poco::FastMutex MutexType; using MutexType = Poco::FastMutex;
typedef MutexType::ScopedLock ScopedLock; using ScopedLock = MutexType::ScopedLock;
EventSet _events; EventSet _events;
Poco::NotificationCenter _nc; Poco::NotificationCenter _nc;

View File

@ -79,7 +79,7 @@ public:
SocketProactor& operator=(const SocketProactor&) = delete; SocketProactor& operator=(const SocketProactor&) = delete;
SocketProactor& operator=(SocketProactor&&) = delete; SocketProactor& operator=(SocketProactor&&) = delete;
~SocketProactor(); ~SocketProactor() override;
/// Destroys the SocketProactor. /// Destroys the SocketProactor.
void addWork(const Work& ch, Timestamp::TimeDiff ms = PERMANENT_COMPLETION_HANDLER); void addWork(const Work& ch, Timestamp::TimeDiff ms = PERMANENT_COMPLETION_HANDLER);
@ -113,7 +113,7 @@ public:
/// from the front of the schedule queue. /// from the front of the schedule queue.
/// Default is removal of all functions. /// 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. /// Polls all registered sockets and calls their respective handlers.
/// If pHandled is not null, after the call it contains the total number /// If pHandled is not null, after the call it contains the total number
/// of read/write/error socket handlers called. /// of read/write/error socket handlers called.
@ -126,7 +126,7 @@ public:
/// Returns 1 on successful handler invocation, 0 on /// Returns 1 on successful handler invocation, 0 on
/// exception. /// exception.
void run(); void run() override;
/// Runs the SocketProactor. The reactor will run /// Runs the SocketProactor. The reactor will run
/// until stop() is called (in a separate thread). /// until stop() is called (in a separate thread).
@ -157,13 +157,13 @@ public:
Poco::Timespan getTimeout() const; Poco::Timespan getTimeout() const;
/// Returns the timeout. /// Returns the timeout.
void addSocket(Socket sock, int mode); void addSocket(const Socket& sock, int mode);
/// Adds the socket to the poll set. /// 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. /// Updates the socket mode in the poll set.
void removeSocket(Socket sock); void removeSocket(const Socket& sock);
/// Removes the socket from the poll set. /// Removes the socket from the poll set.
void addReceiveFrom(Socket sock, Buffer& buf, SocketAddress& addr, Callback&& onCompletion); void addReceiveFrom(Socket sock, Buffer& buf, SocketAddress& addr, Callback&& onCompletion);
@ -212,8 +212,8 @@ private:
/// If expiredOnly is true, only expired temporary functions /// If expiredOnly is true, only expired temporary functions
/// are called. /// are called.
typedef Poco::Mutex MutexType; using MutexType = Poco::Mutex;
typedef MutexType::ScopedLock ScopedLock; using ScopedLock = MutexType::ScopedLock;
static const long DEFAULT_MAX_TIMEOUT_MS = 250; static const long DEFAULT_MAX_TIMEOUT_MS = 250;
@ -245,7 +245,7 @@ private:
{ {
} }
~IONotification() = default; ~IONotification() override = default;
void call() void call()
/// Calls the completion handler. /// Calls the completion handler.
@ -319,7 +319,7 @@ private:
bool runOne() bool runOne()
/// Runs the next I/O completion handler in the queue. /// Runs the next I/O completion handler in the queue.
{ {
IONotification* pNf = dynamic_cast<IONotification*>(_nq.waitDequeueNotification()); auto* pNf = dynamic_cast<IONotification*>(_nq.waitDequeueNotification());
if (_activity.isStopped()) return false; if (_activity.isStopped()) return false;
if (pNf) if (pNf)
{ {
@ -462,19 +462,19 @@ private:
// inlines // 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); _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); _pollSet.update(sock, mode);
} }
inline void SocketProactor::removeSocket(Socket sock) inline void SocketProactor::removeSocket(const Socket& sock)
{ {
_pollSet.remove(sock); _pollSet.remove(sock);
} }

View File

@ -42,15 +42,15 @@ public:
/// The socket's SocketImpl must be a StreamSocketImpl, /// The socket's SocketImpl must be a StreamSocketImpl,
/// otherwise an InvalidArgumentException is thrown. /// otherwise an InvalidArgumentException is thrown.
~SocketStreamBuf(); ~SocketStreamBuf() override;
/// Destroys the SocketStreamBuf. /// Destroys the SocketStreamBuf.
StreamSocketImpl* socketImpl() const; StreamSocketImpl* socketImpl() const;
/// Returns the internal SocketImpl. /// Returns the internal SocketImpl.
protected: protected:
int readFromDevice(char* buffer, std::streamsize length); int readFromDevice(char* buffer, std::streamsize length) override;
int writeToDevice(const char* buffer, std::streamsize length); int writeToDevice(const char* buffer, std::streamsize length) override;
private: private:
enum enum
@ -76,7 +76,7 @@ public:
/// The socket's SocketImpl must be a StreamSocketImpl, /// The socket's SocketImpl must be a StreamSocketImpl,
/// otherwise an InvalidArgumentException is thrown. /// otherwise an InvalidArgumentException is thrown.
~SocketIOS(); ~SocketIOS() override;
/// Destroys the SocketIOS. /// Destroys the SocketIOS.
/// ///
/// Flushes the buffer, but does not close the socket. /// Flushes the buffer, but does not close the socket.
@ -105,7 +105,7 @@ public:
/// The socket's SocketImpl must be a StreamSocketImpl, /// The socket's SocketImpl must be a StreamSocketImpl,
/// otherwise an InvalidArgumentException is thrown. /// otherwise an InvalidArgumentException is thrown.
~SocketOutputStream(); ~SocketOutputStream() override;
/// Destroys the SocketOutputStream. /// Destroys the SocketOutputStream.
/// ///
/// Flushes the buffer, but does not close the socket. /// Flushes the buffer, but does not close the socket.
@ -131,7 +131,7 @@ public:
/// The socket's SocketImpl must be a StreamSocketImpl, /// The socket's SocketImpl must be a StreamSocketImpl,
/// otherwise an InvalidArgumentException is thrown. /// otherwise an InvalidArgumentException is thrown.
~SocketInputStream(); ~SocketInputStream() override;
/// Destroys the SocketInputStream. /// Destroys the SocketInputStream.
}; };
@ -155,7 +155,7 @@ public:
/// The socket's SocketImpl must be a StreamSocketImpl, /// The socket's SocketImpl must be a StreamSocketImpl,
/// otherwise an InvalidArgumentException is thrown. /// otherwise an InvalidArgumentException is thrown.
~SocketStream(); ~SocketStream() override;
/// Destroys the SocketStream. /// Destroys the SocketStream.
/// ///
/// Flushes the buffer, but does not close the socket. /// Flushes the buffer, but does not close the socket.

View File

@ -69,7 +69,7 @@ public:
/// Creates the StreamSocket with the SocketImpl /// Creates the StreamSocket with the SocketImpl
/// from another socket. /// from another socket.
virtual ~StreamSocket(); ~StreamSocket() override;
/// Destroys the StreamSocket. /// Destroys the StreamSocket.
StreamSocket& operator = (const Socket& socket); StreamSocket& operator = (const Socket& socket);

View File

@ -40,7 +40,7 @@ public:
StreamSocketImpl(poco_socket_t sockfd); StreamSocketImpl(poco_socket_t sockfd);
/// Creates a StreamSocketImpl using the given native socket. /// 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 /// Ensures that all data in buffer is sent if the socket
/// is blocking. In case of a non-blocking socket, sends as /// is blocking. In case of a non-blocking socket, sends as
/// many bytes as possible. /// many bytes as possible.

View File

@ -27,7 +27,7 @@ namespace Poco {
namespace Net { namespace Net {
SocketReactor::SocketReactor(): _threadAffinity(-1), SocketReactor::SocketReactor():
_stop(false), _stop(false),
_pReadableNotification(new ReadableNotification(this)), _pReadableNotification(new ReadableNotification(this)),
_pWritableNotification(new WritableNotification(this)), _pWritableNotification(new WritableNotification(this)),
@ -64,9 +64,7 @@ SocketReactor::SocketReactor(const Params& params, int threadAffinity):
} }
SocketReactor::~SocketReactor() SocketReactor::~SocketReactor() = default;
{
}
void SocketReactor::run() 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) SocketReactor::NotifierPtr SocketReactor::getNotifier(const Socket& socket, bool makeNew)
{ {
const SocketImpl* pImpl = socket.impl(); const SocketImpl* pImpl = socket.impl();
if (pImpl == nullptr) return 0; if (pImpl == nullptr) return nullptr;
poco_socket_t sockfd = pImpl->sockfd(); poco_socket_t sockfd = pImpl->sockfd();
ScopedLock lock(_mutex); ScopedLock lock(_mutex);
EventHandlerMap::iterator it = _handlers.find(sockfd); auto it = _handlers.find(sockfd);
if (it != _handlers.end()) return it->second; if (it != _handlers.end()) return it->second;
else if (makeNew) return (_handlers[sockfd] = new SocketNotifier(socket)); 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); ScopedLock lock(_mutex);
delegates.reserve(_handlers.size()); delegates.reserve(_handlers.size());
for (EventHandlerMap::iterator it = _handlers.begin(); it != _handlers.end(); ++it) for (auto& _handler : _handlers)
delegates.push_back(it->second); delegates.push_back(_handler.second);
} }
for (std::vector<NotifierPtr>::iterator it = delegates.begin(); it != delegates.end(); ++it) for (auto& delegate : delegates)
{ {
dispatch(*it, pNotification); dispatch(delegate, pNotification);
} }
} }