📄 frontenddlg.cpp
字号:
char *Msg; for (;;) { Res = FullRead(SockHand, (char *)&Header, sizeof (struct IpcHeader)); if (Res < 0) break; Header.Size = ntohs(Header.Size); Msg = new char [Header.Size]; ::memcpy(Msg, &Header, sizeof (struct IpcHeader)); Res = FullRead(SockHand, Msg + sizeof (struct IpcHeader), Header.Size - sizeof (struct IpcHeader)); if (Res < 0) break; SYSTEMTIME SysTime; FILETIME FileTime; ::GetSystemTime(&SysTime); ::SystemTimeToFileTime(&SysTime, &FileTime); Now = *(unsigned __int64 *)&FileTime; switch (Header.Type) { case MSG_TYPE_IPC_ROUTE: HandleIpcRoute((struct IpcRoute *)Msg); break; case MSG_TYPE_IPC_CONFIG: HandleIpcConfig((struct IpcConfig *)Msg); break; case MSG_TYPE_OLSR_HELLO: HandleOlsrHello((struct OlsrHello *)Msg, 0); break; case MSG_TYPE_OLSR_TC: HandleOlsrTc((struct OlsrTc *)Msg, 0); break; case MSG_TYPE_OLSR_MID: HandleOlsrMid((struct OlsrHeader *)Msg); break; case MSG_TYPE_OLSR_HNA: HandleOlsrHna((struct OlsrHeader *)Msg); break; case MSG_TYPE_OLSR_LQ_HELLO: HandleOlsrHello((struct OlsrHello *)Msg, 1); break; case MSG_TYPE_OLSR_LQ_TC: HandleOlsrTc((struct OlsrTc *)Msg, 1); break; } delete[] Msg; // XXX - nodes are only timed out while messages keep coming in Timeout(); } AfxEndThread(0); return 0;}unsigned int CFrontendDlg::LogThreadFunc(void){ char Buff[1000]; int Len; int Left, Right; CString Line; CString Int; while (::ReadFile(OutRead, Buff, sizeof (Buff) - 1, (unsigned long *)&Len, NULL)) { if (Len == 0) break; Left = 0; for (Right = 0; Right < Len; Right++) { if (Buff[Right] != 13) Buff[Left++] = Buff[Right]; } Len = Left; Left = 0; for (Right = 0; Right < Len; Right++) { if (Buff[Right] == 10) { Buff[Right] = 0; Line += (Buff + Left); if (PipeMode == PIPE_MODE_RUN) m_TabCtrl.m_Dialog1.AddOutputLine(Line); else if (Line.GetLength() > 8 && Line[0] == 'i' && Line[1] == 'f') { Int = Line.Mid(0, 4); Int.MakeUpper(); Interfaces.Add(Int); IsWlan.Add(Line.Mid(6, 1)); Addresses.Add(Line.Mid(8)); } Line.Empty(); Left = Right + 1; } } Buff[Right] = 0; Line += (Buff + Left); } if (PipeMode == PIPE_MODE_RUN) { m_StopButton.EnableWindow(FALSE); m_StartButton.EnableWindow(TRUE); } AfxEndThread(0); return 0;}static unsigned int LogThreadStub(void *Arg){ class CFrontendDlg *This; This = (class CFrontendDlg *)Arg; return This->LogThreadFunc();}static unsigned int NetThreadStub(void *Arg){ class CFrontendDlg *This; This = (class CFrontendDlg *)Arg; return This->NetThreadFunc();}int CFrontendDlg::ExecutePipe(const char *CmdLine, HANDLE *InWrite, HANDLE *OutRead, HANDLE *ShimProc){ SECURITY_ATTRIBUTES SecAttr; HANDLE OutWrite, OutReadTmp; HANDLE ErrWrite; HANDLE InRead, InWriteTmp; HANDLE Proc; STARTUPINFO StartupInfo; PROCESS_INFORMATION ProcessInfo; SecAttr.nLength = sizeof (SECURITY_ATTRIBUTES); SecAttr.lpSecurityDescriptor = NULL; SecAttr.bInheritHandle = TRUE; Proc = ::GetCurrentProcess(); if (!::CreatePipe(&OutReadTmp, &OutWrite, &SecAttr, 0)) { AfxMessageBox("Cannot create stdout pipe."); return -1; } if (!::DuplicateHandle(Proc, OutReadTmp, Proc, OutRead, 0, FALSE, DUPLICATE_SAME_ACCESS)) { AfxMessageBox("Cannot duplicate temporary stdout read handle."); return -1; } if (!::CloseHandle(OutReadTmp)) { AfxMessageBox("Cannot close temporary stdout read handle."); return -1; } if (!::CreatePipe(&InRead, &InWriteTmp, &SecAttr, 0)) { AfxMessageBox("Cannot create stdin pipe."); return -1; } if (!::DuplicateHandle(Proc, InWriteTmp, Proc, InWrite, 0, FALSE, DUPLICATE_SAME_ACCESS)) { AfxMessageBox("Cannot duplicate temporary stdin write handle."); return -1; } if (!::CloseHandle(InWriteTmp)) { AfxMessageBox("Cannot close temporary stdin write handle."); return -1; } if (!::DuplicateHandle(Proc, OutWrite, Proc, &ErrWrite, 0, TRUE, DUPLICATE_SAME_ACCESS)) { AfxMessageBox("Cannot duplicate stdout write handle for stderr."); return -1; } ::memset(&StartupInfo, 0, sizeof (STARTUPINFO)); StartupInfo.cb = sizeof (STARTUPINFO); StartupInfo.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW; StartupInfo.hStdInput = InRead; StartupInfo.hStdOutput = OutWrite; StartupInfo.hStdError = ErrWrite; StartupInfo.wShowWindow = SW_HIDE; if (!::CreateProcess(NULL, (char *)CmdLine, NULL, NULL, TRUE, 0, NULL, NULL, &StartupInfo, &ProcessInfo)) { AfxMessageBox("Cannot create OLSR server process."); return -1; } if (!::CloseHandle(InRead)) { AfxMessageBox("Cannot close stdin read handle."); return -1; } if (!::CloseHandle(OutWrite)) { AfxMessageBox("Cannot close stdout write handle."); return -1; } if (!::CloseHandle(ErrWrite)) { AfxMessageBox("Cannot close stderr write handle."); return -1; } *ShimProc = ProcessInfo.hProcess; return 0;}int CFrontendDlg::GetInterfaces(){ char GuiPath[MAX_PATH]; CString CmdLine; CWinThread *IntThread; ::GetModuleFileName(NULL, GuiPath, MAX_PATH); CmdLine = GuiPath; CmdLine = CmdLine.Mid(0, CmdLine.ReverseFind('\\')) + "\\olsrd.exe -int"; if (ExecutePipe((const char *)CmdLine, &InWrite, &OutRead, &ShimProc) < 0) { AfxMessageBox("Cannot execute '" + CmdLine + "'."); return -1; } PipeMode = PIPE_MODE_INT; IntThread = AfxBeginThread(LogThreadStub, (void *)this); ::WaitForSingleObject((HANDLE)(*IntThread), INFINITE); return 0;}int CFrontendDlg::StartOlsrd(){ WSADATA WsaData; CString CmdLine; char Path[MAX_PATH]; char TempPath[MAX_PATH]; int Try; m_TabCtrl.m_Dialog3.ClearNodeInfo(); m_TabCtrl.m_Dialog4.ClearRoutes(); if (WSAStartup(0x0202, &WsaData)) { AfxMessageBox("Cannot initialize WinSock library."); return -1; } ::GetModuleFileName(NULL, Path, MAX_PATH); CmdLine = Path; CmdLine = CmdLine.Mid(0, CmdLine.ReverseFind('\\')) + "\\Shim.exe"; ::GetTempPath(MAX_PATH - 16, Path); ::GetTempFileName(Path, "GNU", 0, TempPath); StoredTempFile = TempPath; if (m_TabCtrl.m_Dialog2.SaveConfigFile(StoredTempFile, 0) < 0) { AfxMessageBox("Cannot save temporary configuration file '" + StoredTempFile + "'."); return -1; } CmdLine += " -f " + StoredTempFile; if (ExecutePipe((const char *)CmdLine, &InWrite, &OutRead, &ShimProc) < 0) { AfxMessageBox("Cannot execute '" + CmdLine + "'."); return -1; } PipeMode = PIPE_MODE_RUN; LogThread = AfxBeginThread(LogThreadStub, (void *)this); struct sockaddr_in Addr; Addr.sin_family = AF_INET; Addr.sin_port = ::htons(1212); Addr.sin_addr.s_addr = ::inet_addr("127.0.0.1"); SockHand = ::socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if (SockHand < 0) { AfxMessageBox("Cannot create IPC socket."); ::SetEvent(Event); ::WaitForSingleObject((HANDLE)LogThread, INFINITE); LogThread = NULL; return -1; } for (Try = 0; Try < 5; Try++) { if (::connect(SockHand, (struct sockaddr *)&Addr, sizeof (struct sockaddr_in)) >= 0) break; ::Sleep(500); } if (Try == 10) { AfxMessageBox("Cannot connect to IPC port."); ::SetEvent(Event); ::WaitForSingleObject((HANDLE)LogThread, INFINITE); ::closesocket(SockHand); LogThread = NULL; return -1; } NetThread = AfxBeginThread(NetThreadStub, (void *)this); return 0;}int CFrontendDlg::StopOlsrd(){ if (LogThread == NULL && NetThread == NULL) return 0; TrayIcon::getInstance()->setStatus( TrayIcon::OFF, "Off" ); ::SetEvent(Event); ::WaitForSingleObject((HANDLE)LogThread, INFINITE); ::WaitForSingleObject((HANDLE)NetThread, INFINITE); LogThread = NULL; NetThread = NULL; ::DeleteFile(StoredTempFile); return 0;}BOOL CFrontendDlg::OnInitDialog(){ HICON Small, Large; CDialog::OnInitDialog(); Small = (HICON)::LoadImage(AfxGetApp()->m_hInstance, MAKEINTRESOURCE(IDI_ICON1), IMAGE_ICON, GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON), 0); Large = (HICON)::LoadImage(AfxGetApp()->m_hInstance, MAKEINTRESOURCE(IDI_ICON1), IMAGE_ICON, GetSystemMetrics(SM_CXICON), GetSystemMetrics(SM_CYICON), 0); SetIcon(Small, FALSE); SetIcon(Large, TRUE); GetInterfaces(); m_TabCtrl.InsertItem(0, "Settings"); m_TabCtrl.InsertItem(1, "Output"); m_TabCtrl.InsertItem(2, "Nodes"); m_TabCtrl.InsertItem(3, "Routes"); m_TabCtrl.InitTabDialogs(&Interfaces, &Addresses, &IsWlan); m_StopButton.EnableWindow(FALSE); if (!ConfigFile.IsEmpty()) { if (m_TabCtrl.m_Dialog2.OpenConfigFile(ConfigFile) < 0) AfxMessageBox("Cannot open configuration file '" + ConfigFile + "'."); else { OnStartButton(); m_TabCtrl.SetCurSel(1); m_TabCtrl.DisplayTabDialog(); } } return TRUE;}void CFrontendDlg::OnOK(){}void CFrontendDlg::OnCancel(){ OnExitButton();}void CFrontendDlg::OnStartButton() { m_StartButton.EnableWindow(FALSE); m_TabCtrl.m_Dialog1.SetFrozen(1); if (StartOlsrd() < 0) { m_TabCtrl.m_Dialog1.SetFrozen(0); m_TabCtrl.m_Dialog1.AddOutputLine(""); AfxMessageBox("Cannot start OLSR server."); m_StartButton.EnableWindow(TRUE); return; } m_TabCtrl.m_Dialog1.HandleStart(); m_StopButton.EnableWindow(TRUE);}void CFrontendDlg::OnStopButton() { if (StopOlsrd() < 0) return; m_TabCtrl.m_Dialog1.HandleStop(); m_StopButton.EnableWindow(FALSE); m_StartButton.EnableWindow(TRUE);}void CFrontendDlg::OnExitButton(){ delete TrayIcon::getInstance(); if (StopOlsrd() < 0) return; m_TabCtrl.m_Dialog3.ClearNodeInfo(); m_TabCtrl.m_Dialog4.ClearRoutes(); DestroyWindow();}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -