enh(Net): some C++ modernisation

This commit is contained in:
Matej Kenda 2024-03-19 11:11:20 +01:00
parent 30e2d88f21
commit 0fe4451eb2
14 changed files with 33 additions and 31 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

@ -38,7 +38,7 @@ class MultiSocketPoller
/// state, the reading/error handling actions are delegated to the reader. /// state, the reading/error handling actions are delegated to the reader.
{ {
public: public:
MultiSocketPoller(typename UDPHandlerImpl<S>::List& handlers, const Poco::Net::SocketAddress& sa, int nSockets = 10, Poco::Timespan timeout = 250000): MultiSocketPoller(typename UDPHandlerImpl<S>::List& handlers, const Poco::Net::SocketAddress& sa, int nSockets = 10, const Poco::Timespan& timeout = 250000):
_address(sa), _address(sa),
_timeout(timeout), _timeout(timeout),
_reader(handlers) _reader(handlers)

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

@ -144,7 +144,7 @@
#define POCO_NO_DATA NO_DATA #define POCO_NO_DATA NO_DATA
#elif defined(POCO_OS_FAMILY_UNIX) #elif defined(POCO_OS_FAMILY_UNIX)
#include <unistd.h> #include <unistd.h>
#include <errno.h> #include <cerrno>
#include <sys/types.h> #include <sys/types.h>
#include <sys/socket.h> #include <sys/socket.h>
#include <sys/un.h> #include <sys/un.h>
@ -369,12 +369,12 @@ namespace Net {
#if defined(POCO_OS_FAMILY_WINDOWS) #if defined(POCO_OS_FAMILY_WINDOWS)
typedef WSABUF SocketBuf; using SocketBuf = WSABUF;
#elif defined(POCO_OS_FAMILY_UNIX) // TODO: may need more refinement #elif defined(POCO_OS_FAMILY_UNIX) // TODO: may need more refinement
typedef iovec SocketBuf; using SocketBuf = iovec;
#endif #endif
typedef std::vector<SocketBuf> SocketBufVec; using SocketBufVec = std::vector<SocketBuf>;
inline int SocketBufVecSize(const SocketBufVec& sbv) inline int SocketBufVecSize(const SocketBufVec& sbv)
/// Returns total length of all SocketBufs in the vector. /// Returns total length of all SocketBufs in the vector.

View File

@ -490,7 +490,7 @@ protected:
SocketImpl(poco_socket_t sockfd); SocketImpl(poco_socket_t sockfd);
/// Creates a SocketImpl using the given native socket. /// Creates a SocketImpl using the given native socket.
virtual ~SocketImpl(); ~SocketImpl() override;
/// Destroys the SocketImpl. /// Destroys the SocketImpl.
/// Closes the socket if it is still open. /// Closes the socket if it is still open.

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

@ -44,10 +44,10 @@ public:
/// If listen is true, a thread is launched where client can receive /// If listen is true, a thread is launched where client can receive
/// responses rom the server. /// responses rom the server.
virtual ~UDPClient(); ~UDPClient() override;
/// Destroys UDPClient. /// Destroys UDPClient.
void run(); void run() override;
/// Runs listener (typically invoked internally, in separate thread). /// Runs listener (typically invoked internally, in separate thread).
SocketAddress address() const; SocketAddress address() const;

View File

@ -63,7 +63,7 @@ public:
static constexpr MsgSizeT BUF_STATUS_BUSY = -1; static constexpr MsgSizeT BUF_STATUS_BUSY = -1;
static constexpr MsgSizeT BUF_STATUS_ERROR = -2; static constexpr MsgSizeT BUF_STATUS_ERROR = -2;
UDPHandlerImpl(std::size_t bufListSize = 1000, std::ostream* pErr = 0): UDPHandlerImpl(std::size_t bufListSize = 1000, std::ostream* pErr = nullptr):
_thread("UDPHandlerImpl"), _thread("UDPHandlerImpl"),
_stop(false), _stop(false),
_done(false), _done(false),
@ -76,7 +76,7 @@ public:
{ {
} }
~UDPHandlerImpl() ~UDPHandlerImpl() override
/// Destroys the UDPHandlerImpl. /// Destroys the UDPHandlerImpl.
{ {
stop(); stop();
@ -94,7 +94,7 @@ public:
/// the pointers to the newly created guard/buffer. /// the pointers to the newly created guard/buffer.
/// If mutex lock times out, returns null pointer. /// If mutex lock times out, returns null pointer.
{ {
char* ret = 0; char* ret = nullptr;
if (_mutex.tryLock(10)) if (_mutex.tryLock(10))
{ {
if (_buffers[sock].size() < _bufListSize) // building buffer list if (_buffers[sock].size() < _bufListSize) // building buffer list
@ -145,7 +145,7 @@ public:
_dataReady.set(); _dataReady.set();
} }
void run() void run() override
/// Does the work. /// Does the work.
{ {
while (!_stop) while (!_stop)
@ -336,7 +336,8 @@ private:
using BufMap = std::map<poco_socket_t, BufList>; using BufMap = std::map<poco_socket_t, BufList>;
using BLIt = typename BufList::iterator; using BLIt = typename BufList::iterator;
using BufIt = std::map<poco_socket_t, BLIt>; using BufIt = std::map<poco_socket_t, BLIt>;
using MemPool = Poco::FastMemoryPool<char[S]>; using BufArray = std::array<char, S>;
using MemPool = Poco::FastMemoryPool<BufArray>;
void setStatusImpl(char*& pBuf, MsgSizeT status) void setStatusImpl(char*& pBuf, MsgSizeT status)
{ {

View File

@ -57,7 +57,7 @@ public:
_thread.start(*this); _thread.start(*this);
} }
~UDPServerImpl() ~UDPServerImpl() override
/// Destroys the UDPServer. /// Destroys the UDPServer.
{ {
_stop = true; _stop = true;
@ -79,7 +79,7 @@ public:
return _poller.address(); return _poller.address();
} }
void run() void run() override
/// Does the work. /// Does the work.
{ {
while (!_stop) _poller.poll(); while (!_stop) _poller.poll();

View File

@ -101,10 +101,10 @@ public:
/// for replying to sender and data or error backlog threshold is /// for replying to sender and data or error backlog threshold is
/// exceeded, sender is notified of the current backlog size. /// exceeded, sender is notified of the current backlog size.
{ {
typedef typename UDPHandlerImpl<S>::MsgSizeT RT; using RT = typename UDPHandlerImpl<S>::MsgSizeT;
char* p = 0; char* p = nullptr;
struct sockaddr* pSA = 0; struct sockaddr* pSA = nullptr;
poco_socklen_t* pAL = 0; poco_socklen_t* pAL = nullptr;
poco_socket_t sockfd = sock.impl()->sockfd(); poco_socket_t sockfd = sock.impl()->sockfd();
nextHandler(); nextHandler();
try try
@ -179,10 +179,10 @@ public:
return done; return done;
} }
AtomicCounter::ValueType setError(poco_socket_t sock, char* buf = 0, const std::string& err = "") AtomicCounter::ValueType setError(poco_socket_t sock, char* buf = nullptr, const std::string& err = "")
/// Sets error to the provided buffer buf. If the buffer is null, a new buffer is obtained /// Sets error to the provided buffer buf. If the buffer is null, a new buffer is obtained
/// from handler. /// from handler.
/// If successful, returns the handler's eror backlog size, otherwise returns zero. /// If successful, returns the handler's error backlog size, otherwise returns zero.
{ {
if (!buf) buf = handler().next(sock); if (!buf) buf = handler().next(sock);
if (buf) return handler().setError(buf, err.empty() ? Error::getMessage(Error::last()) : err); if (buf) return handler().setError(buf, err.empty() ? Error::getMessage(Error::last()) : err);

View File

@ -179,7 +179,7 @@ public:
WebSocket(const WebSocket& socket); WebSocket(const WebSocket& socket);
/// Creates a WebSocket from another WebSocket. /// Creates a WebSocket from another WebSocket.
virtual ~WebSocket(); ~WebSocket() override;
/// Destroys the WebSocket. /// Destroys the WebSocket.
WebSocket& operator = (const Socket& socket); WebSocket& operator = (const Socket& socket);

View File

@ -133,7 +133,7 @@ SocketImpl* SocketImpl::acceptConnection(SocketAddress& clientAddr)
if (_sockfd == POCO_INVALID_SOCKET) throw InvalidSocketException(); if (_sockfd == POCO_INVALID_SOCKET) throw InvalidSocketException();
sockaddr_storage buffer; sockaddr_storage buffer;
struct sockaddr* pSA = reinterpret_cast<struct sockaddr*>(&buffer); auto* pSA = reinterpret_cast<struct sockaddr*>(&buffer);
poco_socklen_t saLen = sizeof(buffer); poco_socklen_t saLen = sizeof(buffer);
poco_socket_t sd; poco_socket_t sd;
do do

View File

@ -50,7 +50,7 @@ namespace
start(); start();
} }
void processData(char *buf) void processData(char *buf) override
{ {
if (!addr.empty() && addr != address(buf).toString()) ++errors; if (!addr.empty() && addr != address(buf).toString()) ++errors;
addr = address(buf).toString(); addr = address(buf).toString();
@ -68,7 +68,7 @@ namespace
} }
} }
void processError(char *buf) void processError(char *buf) override
{ {
if (std::string(error(buf)) != "error") ++errors; if (std::string(error(buf)) != "error") ++errors;
std::memset(buf, 0, blockSize()); std::memset(buf, 0, blockSize());
@ -100,7 +100,7 @@ namespace
const char *str = "hello\n"; const char *str = "hello\n";
for (; i < reps; ++i) for (; i < reps; ++i)
{ {
data.push_back(str); data.emplace_back(str);
if (data.size() == 230 || i == reps - 1) if (data.size() == 230 || i == reps - 1)
{ {
data.back().append(1, '\0'); data.back().append(1, '\0');

View File

@ -47,7 +47,7 @@ namespace
{ {
} }
void handleRequest(HTTPServerRequest& request, HTTPServerResponse& response) void handleRequest(HTTPServerRequest& request, HTTPServerResponse& response) override
{ {
try try
{ {
@ -97,7 +97,7 @@ namespace
{ {
} }
Poco::Net::HTTPRequestHandler* createRequestHandler(const HTTPServerRequest& request) Poco::Net::HTTPRequestHandler* createRequestHandler(const HTTPServerRequest& request) override
{ {
return new WebSocketRequestHandler(_bufSize); return new WebSocketRequestHandler(_bufSize);
} }