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
/// from another socket.
~DatagramSocket();
~DatagramSocket() override;
/// Destroys the DatagramSocket.
DatagramSocket& operator = (const Socket& socket);

View File

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

View File

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

View File

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

View File

@ -52,7 +52,7 @@ public:
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 @@ public:
}
}
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 @@ public:
}
protected:
typedef std::vector<typename ParallelReactor::Ptr> ReactorVec;
using ReactorVec = std::vector<typename ParallelReactor::Ptr>;
virtual ServiceHandler* createServiceHandler(StreamSocket& socket)
/// Create and initialize a new ServiceHandler instance.
@ -172,7 +176,7 @@ protected:
{
if ((*it)->has(socket)) return it->get();
}
return 0;
return nullptr;
}
SocketReactor* reactor()
@ -218,9 +222,6 @@ protected:
}
private:
ParallelSocketAcceptor();
ParallelSocketAcceptor(const ParallelSocketAcceptor&);
ParallelSocketAcceptor& operator = (const ParallelSocketAcceptor&);
std::string _threadName;
/// Name prefix of sub SocketReactor threads

View File

@ -97,7 +97,7 @@ public:
/// 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.

View File

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

View File

@ -43,10 +43,10 @@ public:
/// 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;
};

View File

@ -64,7 +64,7 @@ public:
// 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);

View File

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

View File

@ -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

View File

@ -72,7 +72,7 @@ public:
explicit SocketAcceptor(ServerSocket& socket):
_socket(socket),
_pReactor(0)
_pReactor(nullptr)
/// 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)
/// Sets the reactor for this acceptor.
{
@ -176,9 +180,6 @@ protected:
}
private:
SocketAcceptor();
SocketAcceptor(const SocketAcceptor&);
SocketAcceptor& operator = (const SocketAcceptor&);
ServerSocket _socket;
SocketReactor* _pReactor;

View File

@ -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 @@ public:
}
}
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 @@ protected:
}
private:
SocketConnector();
SocketConnector(const SocketConnector&);
SocketConnector& operator = (const SocketConnector&);
StreamSocket _socket;
SocketReactor* _pReactor;

View File

@ -38,7 +38,7 @@ public:
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 @@ public:
ReadableNotification(SocketReactor* pReactor);
/// Creates the ReadableNotification for the given SocketReactor.
~ReadableNotification();
~ReadableNotification() override;
/// Destroys the ReadableNotification.
};
@ -77,7 +77,7 @@ public:
WritableNotification(SocketReactor* pReactor);
/// Creates the WritableNotification for the given SocketReactor.
~WritableNotification();
~WritableNotification() override;
/// Destroys the WritableNotification.
};
@ -93,7 +93,7 @@ public:
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 @@ public:
TimeoutNotification(SocketReactor* pReactor);
/// Creates the TimeoutNotification for the given SocketReactor.
~TimeoutNotification();
~TimeoutNotification() override;
/// Destroys the TimeoutNotification.
};
@ -141,7 +141,7 @@ public:
IdleNotification(SocketReactor* pReactor);
/// Creates the IdleNotification for the given SocketReactor.
~IdleNotification();
~IdleNotification() override;
/// Destroys the IdleNotification.
};
@ -154,7 +154,7 @@ public:
ShutdownNotification(SocketReactor* pReactor);
/// Creates the ShutdownNotification for the given SocketReactor.
~ShutdownNotification();
~ShutdownNotification() override;
/// Destroys the ShutdownNotification.
};

View File

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

View File

@ -79,7 +79,7 @@ public:
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 @@ public:
/// 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 @@ public:
/// 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 @@ public:
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 @@ private:
/// 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 @@ private:
{
}
~IONotification() = default;
~IONotification() override = default;
void call()
/// Calls the completion handler.
@ -319,7 +319,7 @@ private:
bool runOne()
/// 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 (pNf)
{
@ -462,19 +462,19 @@ private:
// 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);
}

View File

@ -42,15 +42,15 @@ public:
/// 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 @@ public:
/// 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 @@ public:
/// 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 @@ public:
/// The socket's SocketImpl must be a StreamSocketImpl,
/// otherwise an InvalidArgumentException is thrown.
~SocketInputStream();
~SocketInputStream() override;
/// Destroys the SocketInputStream.
};
@ -155,7 +155,7 @@ public:
/// 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.

View File

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

View File

@ -40,7 +40,7 @@ public:
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.

View File

@ -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<NotifierPtr>::iterator it = delegates.begin(); it != delegates.end(); ++it)
for (auto& delegate : delegates)
{
dispatch(*it, pNotification);
dispatch(delegate, pNotification);
}
}