1
0
mirror of https://github.com/VCMP-SqMod/SqMod.git synced 2025-02-07 21:37:14 +01:00
SqMod/vendor/POCO/Net/testsuite/src/UDPEchoServer.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

84 lines
1.4 KiB
C++

//
// UDPEchoServer.cpp
//
// Copyright (c) 2005-2006, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#include "UDPEchoServer.h"
#include "Poco/Net/SocketAddress.h"
#include "Poco/Timespan.h"
#include <iostream>
using Poco::Net::Socket;
using Poco::Net::DatagramSocket;
using Poco::Net::SocketAddress;
using Poco::Net::IPAddress;
UDPEchoServer::UDPEchoServer():
_thread("UDPEchoServer"),
_stop(false)
{
_socket.bind(SocketAddress(), true);
_thread.start(*this);
_ready.wait();
}
UDPEchoServer::UDPEchoServer(const SocketAddress& sa):
_thread("UDPEchoServer"),
_stop(false)
{
_socket.bind(sa, true);
_thread.start(*this);
_ready.wait();
}
UDPEchoServer::~UDPEchoServer()
{
_stop = true;
_thread.join();
}
Poco::UInt16 UDPEchoServer::port() const
{
return _socket.address().port();
}
void UDPEchoServer::run()
{
Poco::Timespan span(250000);
while (!_stop)
{
_ready.set();
if (_socket.poll(span, Socket::SELECT_READ))
{
try
{
char buffer[256];
SocketAddress sender;
int n = _socket.receiveFrom(buffer, sizeof(buffer), sender);
n = _socket.sendTo(buffer, n, sender);
}
catch (Poco::Exception& exc)
{
std::cerr << "UDPEchoServer: " << exc.displayText() << std::endl;
}
}
}
}
SocketAddress UDPEchoServer::address() const
{
return _socket.address();
}