enh(Net): cleanup and simplification of UPDServer code and related files.

This commit is contained in:
Matej Kenda 2024-03-14 16:12:14 +01:00
parent bc29f30e5c
commit 30e2d88f21
7 changed files with 67 additions and 77 deletions

View File

@ -19,8 +19,12 @@
#include "Poco/Net/Net.h"
#include "Poco/Net/DatagramSocket.h"
#include "Poco/Net/Socket.h"
#include "Poco/Net/PollSet.h"
#include "Poco/Net/UDPHandler.h"
#include "Poco/Net/UDPServerParams.h"
#include "Poco/Net/UDPSocketReader.h"
namespace Poco {
@ -74,23 +78,20 @@ public:
void poll()
{
if (_reader.handlerStopped()) return;
if (_reader.handlerStopped())
return;
PollSet::SocketModeMap sm;
PollSet::SocketModeMap::iterator it;
PollSet::SocketModeMap::iterator end;
sm = _pollSet.poll(_timeout);
it = sm.begin();
end = sm.end();
for (; it != end; ++it)
for (auto& se: sm)
{
if (it->second & PollSet::POLL_READ)
if (se.second & PollSet::POLL_READ)
{
DatagramSocket ds(it->first);
DatagramSocket ds(se.first);
_reader.read(ds);
}
else if (it->second & PollSet::POLL_ERROR)
else if (se.second & PollSet::POLL_ERROR)
{
_reader.setError(it->first.impl()->sockfd());
_reader.setError(se.first.impl()->sockfd());
}
}
}

View File

@ -21,7 +21,8 @@
#include "Poco/Net/Net.h"
#include "Poco/Net/DatagramSocket.h"
#include "Poco/Net/UDPHandler.h"
#include "Poco/Net/PollSet.h"
#include "Poco/Net/UDPServerParams.h"
#include "Poco/Net/UDPSocketReader.h"
namespace Poco {

View File

@ -21,7 +21,6 @@
#include "Poco/Net/Net.h"
#include "Poco/Net/SocketAddress.h"
#include "Poco/Net/DatagramSocket.h"
#include "Poco/Timespan.h"
#include "Poco/Runnable.h"
#include "Poco/Thread.h"

View File

@ -19,26 +19,27 @@
#include "Poco/Net/Net.h"
#include "Poco/Net/SocketAddress.h"
#include "Poco/Net/SocketDefs.h"
#include "Poco/RefCountedObject.h"
#include "Poco/AutoPtr.h"
#include "Poco/Runnable.h"
#include "Poco/Thread.h"
#include "Poco/MemoryPool.h"
#include "Poco/Event.h"
#include "Poco/Error.h"
#include "Poco/Mutex.h"
#include "Poco/StringTokenizer.h"
#include <deque>
#include <cstring>
#include <atomic>
#include <map>
namespace Poco {
namespace Net {
typedef int UDPMsgSizeT;
#define POCO_UDP_BUF_SIZE 1472 + sizeof(UDPMsgSizeT) + SocketAddress::MAX_ADDRESS_LENGTH
using UDPMsgSizeT = int;
constexpr std::size_t POCO_UDP_BUF_SIZE = 1472 + sizeof(UDPMsgSizeT) + SocketAddress::MAX_ADDRESS_LENGTH;
template <std::size_t S = POCO_UDP_BUF_SIZE, class TMutex = Poco::FastMutex>
@ -53,15 +54,14 @@ class UDPHandlerImpl: public Runnable, public RefCountedObject
/// the inheriting class can call start() in the constructor.
{
public:
typedef UDPMsgSizeT MsgSizeT;
typedef AutoPtr<UDPHandlerImpl> Ptr;
typedef std::vector<Ptr> List;
typedef typename List::iterator Iterator;
typedef TMutex DFMutex;
using MsgSizeT = UDPMsgSizeT;
using Ptr = AutoPtr<UDPHandlerImpl>;
using List = std::vector<Ptr>;
using DFMutex = TMutex;
static const MsgSizeT BUF_STATUS_IDLE = 0;
static const MsgSizeT BUF_STATUS_BUSY = -1;
static const MsgSizeT BUF_STATUS_ERROR = -2;
static constexpr MsgSizeT BUF_STATUS_IDLE = 0;
static constexpr MsgSizeT BUF_STATUS_BUSY = -1;
static constexpr MsgSizeT BUF_STATUS_ERROR = -2;
UDPHandlerImpl(std::size_t bufListSize = 1000, std::ostream* pErr = 0):
_thread("UDPHandlerImpl"),
@ -101,11 +101,11 @@ public:
{
makeNext(sock, &ret);
}
else if (*reinterpret_cast<MsgSizeT*>(*_bufIt[sock]) != 0) // busy
else if (payloadSize(*_bufIt[sock]) != 0) // busy
{
makeNext(sock, &ret);
}
else if (*reinterpret_cast<MsgSizeT*>(*_bufIt[sock]) == 0) // available
else if (payloadSize(*_bufIt[sock]) == 0) // available
{
setBusy(*_bufIt[sock]);
ret = *_bufIt[sock];
@ -116,11 +116,11 @@ public:
}
else // last resort, full scan
{
BufList::iterator it = _buffers[sock].begin();
BufList::iterator end = _buffers[sock].end();
for (; it != end; ++it)
auto& buffers { _buffers[sock] };
auto it {buffers.begin()};
for (; it != buffers.end(); ++it)
{
if (*reinterpret_cast<MsgSizeT*>(*_bufIt[sock]) == 0) // available
if (payloadSize(*_bufIt[sock]) == 0) // available
{
setBusy(*it);
ret = *it;
@ -132,7 +132,7 @@ public:
break;
}
}
if (it == end) makeNext(sock, &ret);
if (it == buffers.end()) makeNext(sock, &ret);
}
_mutex.unlock();
}
@ -156,23 +156,19 @@ public:
{
if (!_stop)
{
BufMap::iterator it = _buffers.begin();
BufMap::iterator end = _buffers.end();
for (; it != end; ++it)
for (auto& buf: _buffers)
{
BufList::iterator lIt = it->second.begin();
BufList::iterator lEnd = it->second.end();
for (; lIt != lEnd; ++lIt)
for (auto* list: buf.second)
{
if (hasData(*lIt))
if (hasData(list))
{
processData(*lIt);
processData(list);
--_dataBacklog;
setIdle(*lIt);
setIdle(list);
}
else if (isError(*lIt))
else if (isError(list))
{
processError(*lIt);
processError(list);
++_errorBacklog;
}
}
@ -245,7 +241,7 @@ public:
/// Returns true if buffer contains data.
{
typename DFMutex::ScopedLock l(_dfMutex);
return *reinterpret_cast<MsgSizeT*>(pBuf) > 0;
return payloadSize(pBuf) > 0;
}
bool isError(char*& pBuf)
@ -263,7 +259,7 @@ public:
static MsgSizeT payloadSize(char* buf)
{
return *((MsgSizeT*) buf);
return *reinterpret_cast<MsgSizeT*>(buf);
}
static SocketAddress address(char* buf)
@ -297,7 +293,7 @@ public:
}
static char* error(char* buf)
/// Returns pointer to the erro message payload.
/// Returns pointer to the error message payload.
///
/// Total message size is S.
///
@ -336,11 +332,11 @@ public:
}
private:
typedef std::deque<char*> BufList;
typedef std::map<poco_socket_t, BufList> BufMap;
typedef typename BufList::iterator BLIt;
typedef std::map<poco_socket_t, BLIt> BufIt;
typedef Poco::FastMemoryPool<char[S]> MemPool;
using BufList = std::deque<char*>;
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]>;
void setStatusImpl(char*& pBuf, MsgSizeT status)
{
@ -378,7 +374,7 @@ private:
};
typedef UDPHandlerImpl<POCO_UDP_BUF_SIZE> UDPHandler;
using UDPHandler = UDPHandlerImpl<>;
} } // namespace Poco::Net

View File

@ -19,14 +19,10 @@
#include "Poco/Net/Net.h"
#include "Poco/Net/DatagramSocket.h"
#include "Poco/Net/PollSet.h"
#include "Poco/Net/UDPHandler.h"
#include "Poco/Net/UDPServerParams.h"
#include "Poco/Net/UDPSocketReader.h"
#include "Poco/Net/SingleSocketPoller.h"
#include "Poco/Net/MultiSocketPoller.h"
#include <map>
namespace Poco {

View File

@ -20,7 +20,9 @@
#include "Poco/Net/Net.h"
#include "Poco/Net/DatagramSocket.h"
#include "Poco/Net/UDPHandler.h"
#include "Poco/Net/UDPServerParams.h"
#include "Poco/Error.h"
namespace Poco {
namespace Net {
@ -137,7 +139,7 @@ public:
}
else return;
}
catch (Poco::Exception& exc)
catch (const Poco::Exception& exc)
{
AtomicCounter::ValueType errors = setError(sock.impl()->sockfd(), p, exc.displayText());
if (_backlogThreshold > 0 && errors > _backlogThreshold && errors != _errorBacklog[sockfd] && pSA && pAL)
@ -154,27 +156,26 @@ public:
/// Returns true if all handlers are stopped.
{
bool stopped = true;
typename UDPHandlerImpl<S>::List::iterator it = _handlers.begin();
typename UDPHandlerImpl<S>::List::iterator end = _handlers.end();
for (; it != end; ++it) stopped = stopped && (*it)->stopped();
for (auto& h: _handlers)
stopped = stopped && h->stopped();
return stopped;
}
void stopHandler()
/// Stops all handlers.
{
typename UDPHandlerImpl<S>::List::iterator it = _handlers.begin();
typename UDPHandlerImpl<S>::List::iterator end = _handlers.end();
for (; it != end; ++it) (*it)->stop();
for (auto& h: _handlers)
h->stop();
}
bool handlerDone() const
/// Returns true if all handlers are done processing data.
{
bool done = true;
typename UDPHandlerImpl<S>::List::iterator it = _handlers.begin();
typename UDPHandlerImpl<S>::List::iterator end = _handlers.end();
for (; it != end; ++it) done = done && (*it)->done();
for (auto& h: _handlers)
done = done && h->done();
return done;
}
@ -204,9 +205,9 @@ private:
return **_handler;
}
typedef typename UDPHandlerImpl<S>::List HandlerList;
typedef typename UDPHandlerImpl<S>::List::iterator HandlerIterator;
typedef std::map<poco_socket_t, Counter> CounterMap;
using HandlerList = typename UDPHandlerImpl<S>::List;
using HandlerIterator = typename HandlerList::iterator;
using CounterMap = std::map<poco_socket_t, Counter>;
HandlerList& _handlers;
HandlerIterator _handler;

View File

@ -124,11 +124,9 @@ namespace
count = 0;
errCount = 0;
Poco::Thread::sleep(10);
Poco::Net::UDPHandler::Iterator it = handlers.begin();
Poco::Net::UDPHandler::Iterator end = handlers.end();
for (; it != end; ++it)
for (const auto& h: handlers)
{
count += dynamic_cast<TestUDPHandler &>(*(*it)).counter.value();
count += dynamic_cast<const TestUDPHandler &>(*h).counter.value();
errCount += count / 10;
}
} while (count < i);
@ -138,11 +136,9 @@ namespace
<< ", received: " << count << std::endl;
return false;
}
Poco::Net::UDPHandler::Iterator it = handlers.begin();
Poco::Net::UDPHandler::Iterator end = handlers.end();
for (; it != end; ++it)
for (const auto& he: handlers)
{
TestUDPHandler &h = dynamic_cast<TestUDPHandler &>(*(*it));
const auto &h = dynamic_cast<const TestUDPHandler &>(*he);
count = h.counter.value();
errCount = h.errCounter.value();
if (errCount < count / 10)