152 lines
		
	
	
		
			3.5 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
			
		
		
	
	
			152 lines
		
	
	
		
			3.5 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
| 
 | |
| #include "prot_cmdline.h"
 | |
| #include "base/mycfg.h"
 | |
| #include "QNetworkConfigurationManager"
 | |
| #include "QTimer"
 | |
| 
 | |
| prot_cmdline_cb cmdline_find(myarray name)
 | |
| {
 | |
|   extern const int __start_cmdldef;
 | |
|   extern const int __stop_cmdldef;
 | |
|   cmdline_def *start = (cmdline_def *)&__start_cmdldef;
 | |
|   cmdline_def *end = (cmdline_def *)&__stop_cmdldef;
 | |
|   cmdline_def *item = 0;
 | |
|   for (item = start; item < end; item++)
 | |
|   {
 | |
|     if (item != nullptr)
 | |
|     {
 | |
|       if (name == item->cmd)
 | |
|         return item->fun;
 | |
|       // qDebug("if dev: %s",item->name);
 | |
|     }
 | |
|   }
 | |
|   qWarning("can not find cmdline named '%s'", name.data());
 | |
|   return nullptr;
 | |
| }
 | |
| 
 | |
| // 获取本机地址
 | |
| static QString get_local_ip()
 | |
| {
 | |
|   QList<QNetworkInterface> network = QNetworkInterface::allInterfaces();
 | |
|   foreach (QNetworkInterface net, network)
 | |
|   {
 | |
|     QString netName = net.humanReadableName();
 | |
|     if (netName == "eth0")
 | |
|     {
 | |
|       QList<QNetworkAddressEntry> list = net.addressEntries(); // 获取IP地址与子掩码等
 | |
|       foreach (QNetworkAddressEntry address, list)
 | |
|       {
 | |
|         if (address.ip().protocol() == QAbstractSocket::IPv4Protocol) // 获取IPv4的地址
 | |
|         {
 | |
|           return address.ip().toString();
 | |
|         }
 | |
|       }
 | |
|     }
 | |
|   }
 | |
|   return QString();
 | |
| }
 | |
| 
 | |
| void command::init_cb()
 | |
| {
 | |
|   mycfg *cfg_ = syscfg();
 | |
|   if (get_local_ip().isEmpty())
 | |
|   {
 | |
|     qDebug("modify local ip addr.");
 | |
|     QString str = "ifconfig eth0 %1";
 | |
|     system(str.arg(cfg_->local_ip).toLocal8Bit().data());
 | |
|     str = "route add default gw %1";
 | |
|     system(str.arg(cfg_->gateway_ip).toLocal8Bit().data());
 | |
|   }
 | |
|   if (socket_ == nullptr)
 | |
|   {
 | |
|     socket_ = new QUdpSocket(this);
 | |
|     socket_->bind(QHostAddress::Any, cfg_->cmd_port);
 | |
|     connect(socket_, &QUdpSocket::readyRead, this, &command::ready_read_cb);
 | |
|     qInfo() << "udp command start.";
 | |
|     qInfo() << "cmd port=" << cfg_->cmd_port;
 | |
|   }
 | |
| }
 | |
| 
 | |
| void command::send(QByteArray data)
 | |
| {
 | |
|   if (socket_ == nullptr)
 | |
|     return;
 | |
|   if (data.back() != '\n')
 | |
|   {
 | |
|     data.append('\n');
 | |
|   }
 | |
|   socket_->writeDatagram(data, host_addr, port);
 | |
| }
 | |
| 
 | |
| static void cmdline_help(QList<myarray> args);
 | |
| void command::ready_read_cb()
 | |
| {
 | |
|   recv_data.resize(socket_->bytesAvailable());
 | |
|   socket_->readDatagram(recv_data.data(), recv_data.size(), &host_addr, &port);
 | |
|   myarray cmd = recv_data.simplified();
 | |
|   QList<myarray> args=cmd.split(' ');
 | |
|   if(args.size()>0){
 | |
|     prot_cmdline_cb fun=cmdline_find(args[0]);
 | |
|     if(fun!=nullptr){
 | |
|       fun(args);
 | |
|     }else{
 | |
|       mystring str;
 | |
|       str=mystring::asprintf("can not find command: %s",cmd.data());
 | |
|       send(str.data());
 | |
|       cmdline_help(args);
 | |
|     }
 | |
|   }
 | |
|   qInfo() << "udp command." << cmd;
 | |
|   recv_data.clear();
 | |
| }
 | |
| 
 | |
| 
 | |
| 
 | |
| static void cmdline_help(QList<myarray> args)
 | |
| {
 | |
|   command *c=command_start();
 | |
|   extern const int __start_cmdldef;
 | |
|   extern const int __stop_cmdldef;
 | |
|   cmdline_def *start = (cmdline_def *)&__start_cmdldef;
 | |
|   cmdline_def *end = (cmdline_def *)&__stop_cmdldef;
 | |
|   cmdline_def *item = 0;
 | |
|   for (item = start; item < end; item++)
 | |
|   {
 | |
|     if (item != nullptr)
 | |
|     {
 | |
|       QByteArray send_data;
 | |
|       send_data.append(item->cmd);
 | |
|       send_data.append(30-send_data.size(),'.');
 | |
|       send_data.append(item->help);
 | |
|       c->send(send_data);
 | |
|     }
 | |
|   }
 | |
| 
 | |
| }
 | |
| 
 | |
| 
 | |
| cmdline_export(help,cmdline_help,print help information.);
 | |
| 
 | |
| 
 | |
| 
 | |
|  
 | |
| 
 | |
| 
 | |
| static command *g_command;
 | |
| static QThread g_thread;
 | |
| command *command_start()
 | |
| {
 | |
|   if (g_command == nullptr)
 | |
|   {
 | |
|     g_command = new command();
 | |
|     g_command->moveToThread(&g_thread);
 | |
|     QTimer::singleShot(0, g_command, &command::init_cb);
 | |
|     g_thread.start();
 | |
|   }
 | |
|   return g_command;
 | |
| }
 | |
| 
 | |
| 
 | |
| 
 | |
| 
 | 
