1
0
mirror of https://github.com/VCMP-SqMod/SqMod.git synced 2025-02-12 15:57:12 +01:00
SqMod/vendor/POCO/Net/testsuite/src/EchoServer.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

83 lines
1.3 KiB
C++

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