2024-10-22 19:04:25 +08:00
|
|
|
#include "udp_scan.hpp"
|
|
|
|
|
#include <boost/thread/thread.hpp>
|
|
|
|
|
#include <boost/lexical_cast.hpp>
|
2024-10-23 09:22:06 +08:00
|
|
|
#include "common/common_func.hpp"
|
|
|
|
|
#include "common/global.hpp"
|
2024-10-19 16:02:41 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
UdpSys::UdpSys():
|
|
|
|
|
mIoSev(), udpSock(mIoSev, boost::asio::ip::udp::endpoint(boost::asio::ip::udp::v4(), 7303))
|
|
|
|
|
{
|
|
|
|
|
print_light_green("UdpQt Init\n");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void UdpSys::ClearConnect()
|
|
|
|
|
{
|
|
|
|
|
remoteIp.clear();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void UdpSys::StartConnectSysUdp()
|
|
|
|
|
{
|
|
|
|
|
print_light_green("start connect QT\n");
|
|
|
|
|
if (udpSock.is_open()) {
|
|
|
|
|
boost::asio::socket_base::linger option1(true, 0);
|
|
|
|
|
udpSock.set_option(option1);
|
|
|
|
|
boost::asio::socket_base::send_buffer_size option4(125000);
|
|
|
|
|
udpSock.set_option(option4);
|
|
|
|
|
remoteEndPoint = boost::asio::ip::udp::endpoint(boost::asio::ip::udp::v4(), 9998);
|
|
|
|
|
remoteEndPoint.address(boost::asio::ip::address::from_string("127.0.0.1"));
|
2024-10-22 19:04:25 +08:00
|
|
|
recvUdpData();
|
2024-10-19 16:02:41 +08:00
|
|
|
mIoSev.run();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void UdpSys::handle_send_to(const boost::system::error_code& ec,
|
|
|
|
|
size_t trans)
|
|
|
|
|
{
|
|
|
|
|
if (ec) {
|
|
|
|
|
print_error("send the udp to sys error! \n");
|
|
|
|
|
} else {
|
|
|
|
|
print_info("send the udp to sys ok! \n");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void UdpSys::SendUdpToSingle(std::string pData)
|
|
|
|
|
{
|
|
|
|
|
boost::mutex::scoped_lock lock(udpMutex);
|
|
|
|
|
int len = pData.length();
|
|
|
|
|
if (len <= 0)
|
|
|
|
|
return ;
|
|
|
|
|
char *mi = new char[len + 3];
|
|
|
|
|
memset(mi, 0, len + 3);
|
|
|
|
|
strcpy(mi, pData.c_str());
|
|
|
|
|
strcat(mi, "\r\n");
|
|
|
|
|
print_brown("sys send single : %s \n", pData.c_str());
|
|
|
|
|
udpSock.async_send_to(boost::asio::buffer(mi,len + 3), remoteEndPoint,
|
|
|
|
|
boost::bind(&UdpSys::handle_send_to, this,
|
|
|
|
|
boost::asio::placeholders::error,
|
|
|
|
|
boost::asio::placeholders::bytes_transferred));
|
|
|
|
|
delete [] mi;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void UdpSys::recvUdpData()
|
|
|
|
|
{
|
|
|
|
|
udpSock.async_receive_from(boost::asio::buffer(m_buffer), senderEndPoint,
|
|
|
|
|
boost::bind(&UdpSys::HandleRead, this,
|
|
|
|
|
boost::asio::placeholders::error,
|
|
|
|
|
boost::asio::placeholders::bytes_transferred));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void UdpSys::HandleRead(const boost::system::error_code& pEc,
|
|
|
|
|
std::size_t pBytesTransferred)
|
|
|
|
|
{
|
|
|
|
|
std::string ip = senderEndPoint.address().to_string();
|
|
|
|
|
if (pEc) {
|
|
|
|
|
std::cerr << pEc.value() <<std::endl;
|
|
|
|
|
std::cerr << pEc.category().name() << std::endl;
|
|
|
|
|
std::cerr << pEc.message() << std::endl;
|
|
|
|
|
} else if (pBytesTransferred == 0) {
|
|
|
|
|
std::cout << "rev has rec!" << std::endl;
|
|
|
|
|
if (pBytesTransferred == 0) {
|
|
|
|
|
std::cerr << "[ Read 0 Bytes ][ " << ip << " Quit ! ]" << std::endl;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
std::string read_cmd(m_buffer.data(), pBytesTransferred);
|
|
|
|
|
print_light_purple("%s", read_cmd.c_str());
|
|
|
|
|
if(read_cmd.length()>0){
|
|
|
|
|
AnalysisDataSys(read_cmd);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
recvUdpData();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void UdpSys::AnalysisDataSys(std::string cmd)
|
|
|
|
|
{
|
|
|
|
|
printf("%s", cmd.c_str());
|
|
|
|
|
Json::Reader recvReader;
|
|
|
|
|
Json::Value JsonVal;
|
|
|
|
|
if(recvReader.parse(cmd,JsonVal)) {
|
|
|
|
|
int cmdType = atoi(JsonVal["cmd"].asString().c_str());
|
|
|
|
|
switch (cmdType) {
|
|
|
|
|
case 3:{ //配置服务器
|
|
|
|
|
std::string strServerIp = JsonVal["localServerIpAddress"].asString();
|
|
|
|
|
int localServerPort = JsonVal["localServerPort"].asInt();
|
|
|
|
|
|
|
|
|
|
std::string strDataWatchAddedBy = "system";
|
|
|
|
|
if (!JsonVal["dataNodeGatewayAddedBy"].isNull()) {
|
|
|
|
|
strDataWatchAddedBy = JsonVal["dataNodeGatewayAddedBy"].asString();
|
|
|
|
|
WriteStr2Config(SYSTEMINFOFILE, "SystemInfo", "dataNodeGatewayAddedBy", strDataWatchAddedBy);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
print_info("strServerIp : %s \n", strServerIp.c_str());
|
|
|
|
|
if (strServerIp.compare(GlobalConfig::ServerIP) != 0 || localServerPort != GlobalConfig::ServerPort)
|
|
|
|
|
{
|
|
|
|
|
GlobalConfig::ServerIP = strServerIp;
|
|
|
|
|
GlobalConfig::ServerPort = localServerPort;
|
|
|
|
|
|
|
|
|
|
WriteStr2Config(SERVERCONFIG, "Server", "localServerIpAddress", GlobalConfig::ServerIP);
|
|
|
|
|
WriteStr2Config(SERVERCONFIG, "Server", "localServerPort", to_string(GlobalConfig::ServerPort));
|
|
|
|
|
|
|
|
|
|
std::string fileserver = ReadStrByOpt(SERVERCONFIG, "FileServer", "FileServerIpAddress");
|
|
|
|
|
if (0 == fileserver.compare("0.0.0.0") || 0 == fileserver.length()) {
|
|
|
|
|
WriteStr2Config(SERVERCONFIG, "FileServer", "FileServerIpAddress", GlobalConfig::ServerIP);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
JsonVal["status"] = "ACK";
|
|
|
|
|
Json::FastWriter fw;
|
|
|
|
|
std::string str = fw.write(JsonVal);
|
|
|
|
|
print_info("send info : %s \n", str.c_str());
|
|
|
|
|
boost::asio::ip::udp::endpoint remoteEP(boost::asio::ip::address::from_string(senderEndPoint.address().to_string()),
|
|
|
|
|
MULTICAST_PORT_SEND);
|
|
|
|
|
udpSock.async_send_to(boost::asio::buffer(str),remoteEP,
|
|
|
|
|
boost::bind(&UdpSys::handle_send_to,this,boost::asio::placeholders::error,
|
2024-10-22 19:04:25 +08:00
|
|
|
boost::asio::placeholders::bytes_transferred));
|
2024-10-19 16:02:41 +08:00
|
|
|
} else {
|
2024-10-22 19:04:25 +08:00
|
|
|
//TODO: add log here
|
2024-10-19 16:02:41 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case 4:{
|
|
|
|
|
Json::FastWriter fw;
|
|
|
|
|
std::string status = JsonVal["status"].asString();
|
|
|
|
|
if(status.compare("REQ") == 0)
|
|
|
|
|
{
|
|
|
|
|
JsonVal["dataNodeGatewayNo"] = GlobalConfig::MacAddr_G.c_str();
|
|
|
|
|
JsonVal["localServerIpAddress"] = GlobalConfig::ServerIP;
|
|
|
|
|
JsonVal["status"] = "ACK";
|
|
|
|
|
std::string data = fw.write(JsonVal);
|
|
|
|
|
boost::asio::ip::udp::endpoint remoteEP(boost::asio::ip::address::from_string(senderEndPoint.address().to_string()),
|
|
|
|
|
MULTICAST_PORT_SEND);
|
|
|
|
|
udpSock.async_send_to(boost::asio::buffer(data),remoteEP,
|
|
|
|
|
boost::bind(&UdpSys::handle_send_to,this,boost::asio::placeholders::error,
|
|
|
|
|
boost::asio::placeholders::bytes_transferred));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}else
|
|
|
|
|
{
|
|
|
|
|
print_error("json parse failed");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
UdpSys::~UdpSys()
|
|
|
|
|
{
|
|
|
|
|
if (udpSock.is_open())
|
|
|
|
|
udpSock.close();
|
|
|
|
|
}
|