chore: remove UDP server implementation and main example
This commit is contained in:
@@ -1,5 +1,4 @@
|
||||
#include "logger.hpp"
|
||||
|
||||
#include <boost/date_time/posix_time/posix_time_io.hpp>
|
||||
|
||||
void Logger::init(bool toConsole, const std::string_view filePath)
|
||||
|
||||
@@ -1,46 +0,0 @@
|
||||
#include "udp_server_base.hpp"
|
||||
#include "logger.hpp"
|
||||
|
||||
UdpServerBase::UdpServerBase(IoContext &io, unsigned short port)
|
||||
: socket_(io, UdpEndpoint(boost::asio::ip::udp::v4(), port))
|
||||
{
|
||||
}
|
||||
|
||||
void UdpServerBase::start()
|
||||
{
|
||||
doReceive();
|
||||
}
|
||||
|
||||
void UdpServerBase::doReceive()
|
||||
{
|
||||
using namespace boost::asio;
|
||||
|
||||
auto onReceive = [this](ErrorCode ec, std::size_t bytesReceived) {
|
||||
if (!ec && bytesReceived > 0) {
|
||||
std::string_view data(buffer_.data(), bytesReceived);
|
||||
handleRequest(data, remoteEndpoint_);
|
||||
} else if (ec) {
|
||||
Logger::error("UDP receive error: " + ec.message());
|
||||
}
|
||||
doReceive(); // continue listening
|
||||
};
|
||||
|
||||
socket_.async_receive_from(buffer(buffer_), remoteEndpoint_, onReceive);
|
||||
}
|
||||
|
||||
void UdpServerBase::doSend(
|
||||
const std::string &response, const UdpEndpoint &target
|
||||
)
|
||||
{
|
||||
using namespace boost::asio;
|
||||
|
||||
auto bufferToSend = std::make_shared<std::string>(response);
|
||||
|
||||
auto onSend = [bufferToSend](ErrorCode ec, std::size_t /*bytesSent*/) {
|
||||
if (ec) {
|
||||
Logger::error("UDP send error: " + ec.message());
|
||||
}
|
||||
};
|
||||
|
||||
socket_.async_send_to(buffer(*bufferToSend), target, onSend);
|
||||
}
|
||||
@@ -1,23 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include "aliases.hpp"
|
||||
|
||||
class UdpServerBase
|
||||
{
|
||||
public:
|
||||
UdpServerBase(IoContext &io, unsigned short port);
|
||||
virtual ~UdpServerBase() = default;
|
||||
|
||||
void start();
|
||||
|
||||
protected:
|
||||
virtual void
|
||||
handleRequest(const std::string_view data, const UdpEndpoint &sender) = 0;
|
||||
|
||||
void doReceive();
|
||||
void doSend(const std::string &response, const UdpEndpoint &target);
|
||||
|
||||
UdpSocket socket_;
|
||||
std::array<char, 4096> buffer_;
|
||||
UdpEndpoint remoteEndpoint_;
|
||||
};
|
||||
@@ -1,16 +0,0 @@
|
||||
#include <boost/asio.hpp>
|
||||
#include <iostream>
|
||||
|
||||
int main()
|
||||
{
|
||||
std::cout << "Hello, Boost + C++23 + xmake!\n";
|
||||
|
||||
boost::asio::io_context io;
|
||||
boost::asio::steady_timer timer(io, std::chrono::seconds(1));
|
||||
timer.async_wait([](const boost::system::error_code &) {
|
||||
std::cout << "Timer fired after 1 second!\n";
|
||||
});
|
||||
|
||||
io.run();
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user