150 lines
4.4 KiB
C++
150 lines
4.4 KiB
C++
#include <unistd.h>
|
|
#include <fstream>
|
|
#include <iostream>
|
|
#include <boost/bind.hpp>
|
|
#include <boost/thread/thread.hpp>
|
|
#include "SH_SearchDev.hpp"
|
|
|
|
|
|
static const char* MULTICAST_HOST_NAME1 = "224.0.0.1"; //组播接收平台搜索信号地址
|
|
static const int MULTICAST_PORT_RECV1 = 7301; //组播接收端口
|
|
static const int MULTICAST_PORT_SEND = 7302; //根据接收组播udp发送端口
|
|
|
|
|
|
SearchDev::SearchDev(boost::asio::io_service& ioservice):mSockRecv_1(ioservice),
|
|
mListenEP1(ip::udp::v4(),MULTICAST_PORT_RECV1),
|
|
mSendEndpoint1(ip::address::from_string(MULTICAST_HOST_NAME1),MULTICAST_PORT_SEND)
|
|
{
|
|
Init();
|
|
}
|
|
|
|
SearchDev::~SearchDev()
|
|
{
|
|
Stop();
|
|
}
|
|
|
|
void SearchDev::Init()
|
|
{
|
|
// GwRouteInit();
|
|
//设置组播接收地址1
|
|
try {
|
|
mSockRecv_1.open(mListenEP1.protocol());
|
|
mSockRecv_1.set_option(ip::udp::socket::reuse_address(true));
|
|
mSockRecv_1.bind(mListenEP1);
|
|
mSockRecv_1.set_option(ip::multicast::join_group(ip::address_v4::from_string(MULTICAST_HOST_NAME1)));
|
|
mSockRecv_1.set_option(boost::asio::ip::multicast::enable_loopback(false));
|
|
} catch(boost::system::system_error& e) {
|
|
std::cout << e.what() << std::endl;
|
|
}
|
|
}
|
|
|
|
|
|
void SearchDev::GwRouteInit() {
|
|
|
|
// system("route > scan_test");
|
|
// std::fstream fileOut;
|
|
// int find = 0;
|
|
// fileOut.open("scan_test",std::ios::in | std::ios::app);
|
|
// if (fileOut.is_open()) {//文件打开
|
|
// while (!fileOut.eof()) {
|
|
// std::string tmpStr("");
|
|
// getline(fileOut,tmpStr);
|
|
// //fileOut >> tmpStr;
|
|
// print_info("%s\n",tmpStr.c_str());
|
|
// if (strstr(tmpStr.c_str(), "224.0.0.0")) {
|
|
// find = 1;
|
|
// }
|
|
// }
|
|
// }
|
|
// fileOut.close();
|
|
|
|
// if (!find) {
|
|
// char cmd[128]="route add -net 224.0.0.0 netmask 224.0.0.0 dev ";
|
|
// strcat(cmd,GlobalConfig::KEthname.c_str());
|
|
// print_info("%s\n",cmd);
|
|
// system(cmd);
|
|
// }
|
|
// system("rm scan_test");
|
|
}
|
|
|
|
void SearchDev::HandleSend_1(const char *pMsg,const boost::system::error_code &pEc)
|
|
{
|
|
if (pEc) {
|
|
print_info("send udp error 7302\n");
|
|
} else {
|
|
print_info("send udp ok 7302\n");
|
|
}
|
|
}
|
|
|
|
|
|
|
|
void SearchDev::MultiCastRecv() {
|
|
MultiCastRecv_1();
|
|
}
|
|
|
|
void SearchDev::MultiCastRecv_1()
|
|
{
|
|
memset(mRecvBuf1,0,BUF_LENGTH);
|
|
|
|
mSockRecv_1.async_receive_from(boost::asio::buffer(mRecvBuf1,BUF_LENGTH),mRecvEP1,
|
|
boost::bind(&SearchDev::HandleRecv_1,this,boost::asio::placeholders::error,
|
|
boost::asio::placeholders::bytes_transferred));
|
|
}
|
|
|
|
void SearchDev::HandleRecv_1(const boost::system::error_code &pEc,size_t pBytesRecv)
|
|
{
|
|
if (!pEc) {
|
|
print_info("mRecvBuf1 : %s\n", mRecvBuf1);
|
|
std::string data = std::string(mRecvBuf1);
|
|
Json::Value jsData;
|
|
Json::Reader recvReader;
|
|
Json::Value JsonVal;
|
|
Json::FastWriter fw;
|
|
if (recvReader.parse(data, jsData)) {
|
|
int cmdType = atoi(jsData["cmd"].asString().c_str());
|
|
switch (cmdType) {
|
|
case 4:{
|
|
std::string status = jsData["status"].asString();
|
|
if(status.compare("REQ") == 0) {
|
|
jsData["dataWatchNo"] = GlobalConfig::MacAddr_G.c_str();
|
|
jsData["localServerIpAddress"] = GlobalConfig::IpAddr_G;
|
|
jsData["status"] = "ACK";
|
|
jsData["DeviceType"] = "WirelessGateWay";
|
|
std::string strData = fw.write(jsData);
|
|
print_info("send info %s ip: %s\n", strData.c_str(), mRecvEP1.address().to_string().c_str());
|
|
ip::udp::endpoint remoteEP(ip::address::from_string(mRecvEP1.address().to_string()),
|
|
MULTICAST_PORT_SEND);
|
|
mSockRecv_1.async_send_to(boost::asio::buffer(strData),remoteEP,
|
|
boost::bind(&SearchDev::HandleSend_1,this,"SockRecv_1_1",boost::asio::placeholders::error));
|
|
}
|
|
}
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}else{
|
|
print_error("parse error\n");
|
|
}
|
|
MultiCastRecv_1();
|
|
} else {
|
|
}
|
|
}
|
|
|
|
void SearchDev::Run()
|
|
{
|
|
// mIoSev.run();
|
|
}
|
|
|
|
void SearchDev::Stop()
|
|
{
|
|
// if (!mIoSev.stopped())
|
|
// mIoSev.stop();
|
|
}
|
|
|
|
void SearchDev::Restart()
|
|
{
|
|
Init();
|
|
}
|
|
|
|
|