This commit is contained in:
parent
b17b50b751
commit
ca63fd413b
@ -11,7 +11,6 @@ add_executable(Server main.cpp
|
||||
ServiceLogic.h ServiceLogic.inl ServiceLogic.cpp
|
||||
ServiceManager.h
|
||||
SystemUsage.h SystemUsage.cpp
|
||||
UdpServer.h UdpServer.cpp
|
||||
Live2dBackend.h Live2dBackend.cpp
|
||||
WeChatContext/CorporationContext.h WeChatContext/CorporationContext.cpp
|
||||
WeChatContext/WeChatContext.h WeChatContext/WeChatContext.cpp
|
||||
|
@ -1,82 +0,0 @@
|
||||
#include "UdpServer.h"
|
||||
#include "BoostLog.h"
|
||||
#include "boost/endian.hpp"
|
||||
#include <random>
|
||||
#include <sstream>
|
||||
|
||||
UdpServer::UdpServer(boost::asio::io_context &io_context)
|
||||
: m_socket(io_context, boost::asio::ip::udp::endpoint(boost::asio::ip::udp::v4(), 8088)), m_reveiveBuffer(512),
|
||||
m_timer(io_context) {
|
||||
|
||||
// start recieve.
|
||||
m_socket.async_receive_from(
|
||||
boost::asio::buffer(m_reveiveBuffer), m_remotePoint,
|
||||
std::bind(&UdpServer::reveiveHandler, this, std::placeholders::_1, std::placeholders::_2));
|
||||
|
||||
sendData();
|
||||
|
||||
boost::system::error_code error;
|
||||
auto address = boost::asio::ip::make_address("127.0.0.1", error);
|
||||
if (error) {
|
||||
LOG(error) << error.message();
|
||||
} else {
|
||||
m_remotePoint = boost::asio::ip::udp::endpoint(address, 1234);
|
||||
}
|
||||
}
|
||||
|
||||
void UdpServer::reveiveHandler(const boost::system::error_code &error, std::size_t bytes_transferred) {
|
||||
LOG(info) << "Received byte size: " << bytes_transferred
|
||||
<< ",remote ip address: " << m_remotePoint.address().to_string() << std::endl;
|
||||
if (error) {
|
||||
LOG(error) << error.message();
|
||||
return;
|
||||
}
|
||||
|
||||
handleReceivedBuffer(m_reveiveBuffer);
|
||||
|
||||
// continue recieve next datagram.
|
||||
m_socket.async_receive_from(
|
||||
boost::asio::buffer(m_reveiveBuffer), m_remotePoint,
|
||||
std::bind(&UdpServer::reveiveHandler, this, std::placeholders::_1, std::placeholders::_2));
|
||||
}
|
||||
|
||||
void UdpServer::sendHandler(const boost::system::error_code &error, std::size_t bytes_transferred) {
|
||||
if (error) {
|
||||
LOG(error) << error.message();
|
||||
}
|
||||
}
|
||||
|
||||
void UdpServer::handleReceivedBuffer(std::vector<char> &data) {
|
||||
}
|
||||
|
||||
void UdpServer::sendData() {
|
||||
using namespace std::chrono_literals;
|
||||
static uint32_t index = 0;
|
||||
m_timer.expires_after(1ms);
|
||||
m_timer.async_wait([this](const boost::system::error_code &error) {
|
||||
if (error) {
|
||||
LOG(error) << error.message();
|
||||
return;
|
||||
}
|
||||
std::random_device rd; // 将用于获得随机数引擎的种子
|
||||
std::mt19937 gen(rd()); // 以 rd() 播种的标准 mersenne_twister_engine
|
||||
std::uniform_int_distribution<uint32_t> distribution(std::numeric_limits<uint32_t>::min(),
|
||||
std::numeric_limits<uint32_t>::max());
|
||||
|
||||
std::array<char, 408> data;
|
||||
data[0] = 0xAA;
|
||||
data[1] = 0xFF;
|
||||
data[2] = 0x55;
|
||||
data[3] = 0x00;
|
||||
auto bigEndian = reinterpret_cast<uint32_t *>(&data[4]);
|
||||
*bigEndian++ = boost::endian::native_to_big(index++);
|
||||
for (size_t i = 0; i < 100; i++) {
|
||||
*bigEndian++ = boost::endian::native_to_big(distribution(gen));
|
||||
}
|
||||
|
||||
m_socket.async_send_to(boost::asio::buffer(data), m_remotePoint,
|
||||
std::bind(&UdpServer::sendHandler, this, std::placeholders::_1, std::placeholders::_2));
|
||||
|
||||
sendData();
|
||||
});
|
||||
}
|
@ -1,36 +0,0 @@
|
||||
#ifndef UDPSERVER_H
|
||||
#define UDPSERVER_H
|
||||
|
||||
#include <boost/asio.hpp>
|
||||
|
||||
class UdpServer {
|
||||
public:
|
||||
UdpServer(boost::asio::io_context &io_context);
|
||||
|
||||
protected:
|
||||
/**
|
||||
* @brief reveiveHandler
|
||||
* @param error Result of operation.
|
||||
* @param bytes_transferred Number of bytes received.
|
||||
*/
|
||||
void reveiveHandler(const boost::system::error_code &error, std::size_t bytes_transferred);
|
||||
|
||||
/**
|
||||
* @brief sendHandler
|
||||
* @param error Result of operation.
|
||||
* @param bytes_transferred Number of bytes sent.
|
||||
*/
|
||||
void sendHandler(const boost::system::error_code &error, std::size_t bytes_transferred);
|
||||
|
||||
void handleReceivedBuffer(std::vector<char> &data);
|
||||
|
||||
void sendData();
|
||||
|
||||
private:
|
||||
boost::asio::ip::udp::socket m_socket;
|
||||
boost::asio::ip::udp::endpoint m_remotePoint; //客户端端点
|
||||
std::vector<char> m_reveiveBuffer;
|
||||
boost::asio::steady_timer m_timer;
|
||||
};
|
||||
|
||||
#endif // UDPSERVER_H
|
@ -7,7 +7,6 @@
|
||||
#include "MediaServer.h"
|
||||
#include "ProxyListener.h"
|
||||
#include "ServiceManager.h"
|
||||
#include "UdpServer.h"
|
||||
#include "WeChatContext/CorporationContext.h"
|
||||
#include "WeChatContext/WeChatContext.h"
|
||||
#include "WebApplication/Application.h"
|
||||
@ -94,7 +93,6 @@ int main(int argc, char const *argv[]) {
|
||||
application->ioContext().stop();
|
||||
});
|
||||
|
||||
auto udpServer = std::make_shared<UdpServer>(application->ioContext());
|
||||
auto mediaServer = std::make_shared<MediaServer>(554, false);
|
||||
auto webApp = Singleton<WebToolkit::Server>::instance<Construct>(application->getWtPort(), application->getApplicationRoot(), application->getDocumentRoot());
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user