1
0
mirror of https://github.com/VCMP-SqMod/SqMod.git synced 2024-11-08 08:47:17 +01:00
SqMod/vendor/POCO/Net/src/UDPClient.cpp
Sandu Liviu Catalin 4a6bfc086c Major plugin refactor and cleanup.
Switched to POCO library for unified platform/library interface.
Deprecated the external module API. It was creating more problems than solving.
Removed most built-in libraries in favor of system libraries for easier maintenance.
Cleaned and secured code with help from static analyzers.
2021-01-30 08:51:39 +02:00

92 lines
1.5 KiB
C++

//
// UDPClient.cpp
//
// Library: Net
// Package: UDP
// Module: UDPClient
//
// Copyright (c) 2005-2006, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#include "Poco/Net/UDPClient.h"
#include "Poco/ErrorHandler.h"
namespace Poco {
namespace Net {
UDPClient::UDPClient(const std::string& address, Poco::UInt16 port, bool listen):
_address(address, port),
_pThread(0),
_stop(false)
{
_socket.bind(SocketAddress(address, 0), true, true);
_socket.setReuseAddress(true);
_socket.setReusePort(true);
_socket.connect(_address);
_socket.setBlocking(true);
if (listen)
{
_pThread = new Thread;
_pThread->start(*this);
}
}
UDPClient::~UDPClient()
{
_stop = true;
if (_pThread)
{
_pThread->join();
delete _pThread;
}
}
void UDPClient::run()
{
Poco::Timespan span(250000);
while (!_stop)
{
if (_socket.poll(span, Socket::SELECT_READ))
{
try
{
char buffer[sizeof(Poco::UInt32)*2];
int n = _socket.receiveBytes(buffer, sizeof(buffer));
if (n >= sizeof(Poco::Int32)) handleResponse(buffer, n);
}
catch (Exception& exc)
{
ErrorHandler::handle(exc);
}
catch (std::exception& exc)
{
ErrorHandler::handle(exc);
}
catch (...)
{
ErrorHandler::handle();
}
}
}
}
int UDPClient::handleResponse(char* buffer, int length)
{
Poco::Int32 count = *reinterpret_cast<Poco::Int32*>(buffer);
if (count > 0) _dataBacklog = count;
else if (count < 0) _errorBacklog = count;
return count;
}
} } // namespace Poco::Net