📄 explorerthread.h
字号:
#ifndef EXPLORERPARA
#define EXPLORERPARA
//
#include "SnmpTools.h"
#include "DataSupport.h"
#include <afxtempl.h>
#define WM_DRAWTAIL (WM_USER+114)
#define WM_ENDEXPLORER (WM_USER+165)
struct ExplorerParament
{
DistributeDate *PData;
ULONG OriginalIp;
char CommunityName[64];
int RecursionDepth;
CWnd * MessageReceiver;
//下面两个参数是为了扩展应用的,以便可以以任意一个节点为起点开始新的探测!
int CurrentDepth;//当前深度,也就是当前所处的列数
RouterEnty *Father;
};
struct RecursionFunStruct
{
RouterEnty *Father;
ULONG DestIp;
int CurrentDepth;
ExplorerParament* TheParament;
};
//static CCriticalSection Section;
//SnmpTool * PSnmptool;
DWORD WINAPI RecursionFun(LPVOID lpParam )
{
RecursionFunStruct *Param=(RecursionFunStruct *)lpParam;
int CurrentDepth=Param->CurrentDepth;
ExplorerParament& TheParament=*(Param->TheParament);
if(CurrentDepth==TheParament.RecursionDepth)
{
return 0;
}
else
{
RouterEnty *Father=Param->Father;
ULONG DestIp=Param->DestIp;
delete Param;
SnmpTool TheTool(DestIp,TheParament.CommunityName);
bool Stop=false;
if(Father==NULL)//this is the head!
{
ULONG IpTable[64];
memset(IpTable,0,64*4);
int Length=64;
CList<ULONG> IpList;
if(TheTool.GetIpTable(IpTable,Length))//get the host ip table successfully!
{
for(int i=0;i<Length;i++)
{
IpList.AddTail(IpTable[i]);
}
}
else//fail to get ip table
{
IpList.AddHead(DestIp);
Stop=true;
}
RouterEnty* This= TheParament.PData->NewRouterEnty(IpList);
memcpy(This->ButtonEnty.ValidCommunityName,TheParament.CommunityName,64);
This->ButtonEnty.ValidIp=DestIp;
TheParament.MessageReceiver->SendMessage(WM_DRAWTAIL,CurrentDepth);
if(Stop)
{
return 0;
}
ULONG NextHopTable[64];
memset(NextHopTable,0,64*4);
Length=64;
if(TheTool.GetNextHopTable(NextHopTable,Length))//get next hop table success!
{
HANDLE *ThreadHandle=new HANDLE[Length];
for(int i=0;i<Length;i++)
{
RecursionFunStruct *Param=new RecursionFunStruct;
Param->CurrentDepth=CurrentDepth+1;
Param->DestIp=NextHopTable[i];
Param->TheParament=&TheParament;
Param->Father=This;
DWORD dwThreadId;
ThreadHandle[i]=CreateThread(NULL, 0,RecursionFun, Param,0,&dwThreadId);
}
WaitForMultipleObjects(Length,ThreadHandle,TRUE,INFINITE);
delete []ThreadHandle;
return 1;
}
else
{
return 0 ;
}
}//end of this node is the head
else//this node is not the head!!
{
ULONG IpTable[64];
memset(IpTable,0,64*4);
int Length=64;
CList<ULONG> IpList;
static CCriticalSection Section;
if(TheTool.GetIpTable(IpTable,Length))//get the host ip table successfully!
{
for(int i=0;i<Length;i++)
{
IpList.AddTail(IpTable[i]);
}
}
else//fail to get ip table
{
Stop=true;
IpList.AddHead(DestIp);
}
Section.Lock();
RouterEnty* This= TheParament.PData->AddChild(Father,IpList);
if(This==NULL)//scroll back
{
Section.Unlock();
return 0;
}
memcpy(This->ButtonEnty.ValidCommunityName,TheParament.CommunityName,64);
This->ButtonEnty.ValidIp=DestIp;
TheParament.MessageReceiver->SendMessage(WM_DRAWTAIL,CurrentDepth);
Section.Unlock();
if(Stop)
{
return 0;
}
ULONG NextHopTable[64];
memset(NextHopTable,0,64*4);
Length=64;
if(TheTool.GetNextHopTable(NextHopTable,Length))//get next hop table success!
{
HANDLE *ThreadHandle=new HANDLE[Length];
for(int i=0;i<Length;i++)
{
RecursionFunStruct * Param=new RecursionFunStruct;
Param->CurrentDepth=CurrentDepth+1;
Param->DestIp=NextHopTable[i];
Param->TheParament=&TheParament;
Param->Father=This;
DWORD dwThreadId;
ThreadHandle[i]=CreateThread(NULL, 0,RecursionFun, Param,0,&dwThreadId);
}
WaitForMultipleObjects(Length,ThreadHandle,TRUE,INFINITE);
delete []ThreadHandle;
return 1;
}
else
{
return 0;
}
}//end of this node is not the head!!
}
}
UINT ExplorerThreadFunc(LPVOID PParament)
{
static CCriticalSection Section;
ExplorerParament TheParament;
Section.Lock();
memcpy(&TheParament,PParament,sizeof(TheParament));
ExplorerParament *P=(ExplorerParament *)PParament;
RecursionFunStruct *Param=new RecursionFunStruct;
Param->CurrentDepth=TheParament.CurrentDepth;
Param->DestIp=TheParament.OriginalIp;
Param->TheParament=&TheParament;
Param->Father=TheParament.Father;
RecursionFun(Param);
TheParament.MessageReceiver->SendMessage(WM_ENDEXPLORER);
delete ((ExplorerParament *)PParament);
Section.Unlock();
return 1;
}
UINT ReExplorerThreadFunc(LPVOID PParament)
{
static CCriticalSection Section;
ExplorerParament TheParament;
Section.Lock();
memcpy(&TheParament,PParament,sizeof(TheParament));
ExplorerParament *P=(ExplorerParament *)PParament;
SnmpTool TheTool(TheParament.OriginalIp,TheParament.CommunityName);
ULONG IpTable[64];
memset(IpTable,0,64*4);
int Length=64;
POSITION p=TheParament.Father->RouterIP.GetHeadPosition();
if(TheTool.GetIpTable(IpTable,Length))//get the host ip table successfully!
{
for(int i=0;i<Length;i++)
{
if(IpTable[i]!=TheParament.OriginalIp)
{
TheParament.Father->RouterIP.AddTail(IpTable[i]);
TheParament.Father->ButtonEnty.IPList.AddTail(IpTable[i]);
}
}
memcpy(TheParament.Father->ButtonEnty.ValidCommunityName,TheParament.CommunityName,64);
TheParament.Father->ButtonEnty.ValidIp=TheParament.OriginalIp;
TheParament.MessageReceiver->SendMessage(WM_ENDEXPLORER,2,(LPARAM)(&(TheParament.Father->ButtonEnty)));//发送消息通知UI线程完成更改Tip的操作
}
else
{
TheParament.MessageReceiver->SendMessage(WM_ENDEXPLORER,-1);
delete ((ExplorerParament *)PParament);
Section.Unlock();
return 1;
}
ULONG NextHopTable[64];
memset(NextHopTable,0,64*4);
Length=64;
if(TheTool.GetNextHopTable(NextHopTable,Length))//get next hop table success!
{
HANDLE *ThreadHandle=new HANDLE[Length];
for(int i=0;i<Length;i++)
{
RecursionFunStruct *Param=new RecursionFunStruct;
Param->CurrentDepth=TheParament.CurrentDepth+1;
Param->DestIp=NextHopTable[i];
Param->TheParament=&TheParament;
Param->Father=TheParament.Father;
DWORD dwThreadId;
ThreadHandle[i]=CreateThread(NULL, 0,RecursionFun, Param,0,&dwThreadId);
}
WaitForMultipleObjects(Length,ThreadHandle,TRUE,INFINITE);
return 1;
}
else
{
return 0;
}
TheParament.MessageReceiver->SendMessage(WM_ENDEXPLORER);
delete ((ExplorerParament *)PParament);
Section.Unlock();
return 1;
}
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -