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

View File

@ -38,7 +38,7 @@ class MultiSocketPoller
/// state, the reading/error handling actions are delegated to the reader.
{
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),
_timeout(timeout),
_reader(handlers)

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

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

View File

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

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

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

View File

@ -63,7 +63,7 @@ public:
static constexpr MsgSizeT BUF_STATUS_BUSY = -1;
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"),
_stop(false),
_done(false),
@ -76,7 +76,7 @@ public:
{
}
~UDPHandlerImpl()
~UDPHandlerImpl() override
/// Destroys the UDPHandlerImpl.
{
stop();
@ -94,7 +94,7 @@ public:
/// the pointers to the newly created guard/buffer.
/// If mutex lock times out, returns null pointer.
{
char* ret = 0;
char* ret = nullptr;
if (_mutex.tryLock(10))
{
if (_buffers[sock].size() < _bufListSize) // building buffer list
@ -145,7 +145,7 @@ public:
_dataReady.set();
}
void run()
void run() override
/// Does the work.
{
while (!_stop)
@ -336,7 +336,8 @@ private:
using BufMap = std::map<poco_socket_t, BufList>;
using BLIt = typename BufList::iterator;
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)
{

View File

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

View File

@ -101,10 +101,10 @@ public:
/// for replying to sender and data or error backlog threshold is
/// exceeded, sender is notified of the current backlog size.
{
typedef typename UDPHandlerImpl<S>::MsgSizeT RT;
char* p = 0;
struct sockaddr* pSA = 0;
poco_socklen_t* pAL = 0;
using RT = typename UDPHandlerImpl<S>::MsgSizeT;
char* p = nullptr;
struct sockaddr* pSA = nullptr;
poco_socklen_t* pAL = nullptr;
poco_socket_t sockfd = sock.impl()->sockfd();
nextHandler();
try
@ -179,10 +179,10 @@ public:
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
/// 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) return handler().setError(buf, err.empty() ? Error::getMessage(Error::last()) : err);

View File

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

View File

@ -133,7 +133,7 @@ SocketImpl* SocketImpl::acceptConnection(SocketAddress& clientAddr)
if (_sockfd == POCO_INVALID_SOCKET) throw InvalidSocketException();
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_socket_t sd;
do

View File

@ -50,7 +50,7 @@ namespace
start();
}
void processData(char *buf)
void processData(char *buf) override
{
if (!addr.empty() && addr != address(buf).toString()) ++errors;
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;
std::memset(buf, 0, blockSize());
@ -100,7 +100,7 @@ namespace
const char *str = "hello\n";
for (; i < reps; ++i)
{
data.push_back(str);
data.emplace_back(str);
if (data.size() == 230 || i == reps - 1)
{
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
{
@ -97,7 +97,7 @@ namespace
{
}
Poco::Net::HTTPRequestHandler* createRequestHandler(const HTTPServerRequest& request)
Poco::Net::HTTPRequestHandler* createRequestHandler(const HTTPServerRequest& request) override
{
return new WebSocketRequestHandler(_bufSize);
}