📄 fullserverwithrmtmonitordemodlg.cpp
字号:
// fullServerWithRmtMonitorDemoDlg.cpp : 实现文件
//
#include "stdafx.h"
#include "fullServerWithRmtMonitorDemo.h"
#include "fullServerWithRmtMonitorDemoDlg.h"
#include ".\fullserverwithrmtmonitordemodlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// 用于应用程序“关于”菜单项的 CAboutDlg 对话框
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// 对话框数据
enum { IDD = IDD_ABOUTBOX };
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持
// 实现
protected:
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
END_MESSAGE_MAP()
// CfullServerWithRmtMonitorDemoDlg 对话框
CfullServerWithRmtMonitorDemoDlg::CfullServerWithRmtMonitorDemoDlg(CWnd* pParent /*=NULL*/)
: CDialog(CfullServerWithRmtMonitorDemoDlg::IDD, pParent)
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CfullServerWithRmtMonitorDemoDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
DDX_Control(pDX, IDC_MSG, m_tabMsgList);
}
BEGIN_MESSAGE_MAP(CfullServerWithRmtMonitorDemoDlg, CDialog)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
//}}AFX_MSG_MAP
ON_NOTIFY(TCN_SELCHANGE, IDC_MSG, OnTcnSelchangeMsg)
ON_BN_CLICKED(IDC_START_SVR, OnBnClickedStartSvr)
ON_BN_CLICKED(IDC_STOP_SVR, OnBnClickedStopSvr)
ON_BN_CLICKED(IDC_PAUSE_SVR, OnBnClickedPauseSvr)
ON_BN_CLICKED(IDC_SVR_STATE, OnBnClickedSvrState)
ON_BN_CLICKED(IDC_EXIT_NOW, OnBnClickedExitNow)
ON_BN_CLICKED(IDC_RESMUE_SVR, OnBnClickedResmueSvr)
END_MESSAGE_MAP()
// CfullServerWithRmtMonitorDemoDlg 消息处理程序
BOOL CfullServerWithRmtMonitorDemoDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// 将\“关于...\”菜单项添加到系统菜单中。
// IDM_ABOUTBOX 必须在系统命令范围内。
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// 设置此对话框的图标。当应用程序主窗口不是对话框时,框架将自动
// 执行此操作
SetIcon(m_hIcon, TRUE); // 设置大图标
SetIcon(m_hIcon, FALSE); // 设置小图标
// TODO: 在此添加额外的初始化代码
m_tabMsgList.InsertItem(0,"所有信息");
m_tabMsgList.InsertItem(1,"错误信息");
CRect rcW;
m_tabMsgList.GetWindowRect(rcW);
rcW.top+= 25;
rcW.bottom-=5;
rcW.left+=5;
rcW.right-=5;
ScreenToClient(rcW);
m_wndShowAll.CreateChildWnd(0,this,"all",rcW,10090+1,4,90,80,40);
m_conAll.SetBufferWnd(&m_wndShowAll);
m_wndShowError.CreateChildWnd(0,this,"all",rcW,10090+2,4,90,80,40);
m_conError.SetBufferWnd(&m_wndShowError);
SetBufferWndShow();
m_outputDemo.SetOutputWnd(&m_conAll, &m_conError);
return TRUE; // 除非设置了控件的焦点,否则返回 TRUE
}
void CfullServerWithRmtMonitorDemoDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// 如果向对话框添加最小化按钮,则需要下面的代码
// 来绘制该图标。对于使用文档/视图模型的 MFC 应用程序,
// 这将由框架自动完成。
void CfullServerWithRmtMonitorDemoDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // 用于绘制的设备上下文
SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
// 使图标在工作矩形中居中
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// 绘制图标
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
//当用户拖动最小化窗口时系统调用此函数取得光标显示。
HCURSOR CfullServerWithRmtMonitorDemoDlg::OnQueryDragIcon()
{
return static_cast<HCURSOR>(m_hIcon);
}
void CfullServerWithRmtMonitorDemoDlg::SetBufferWndShow(void)
{
int iSel = m_tabMsgList.GetCurSel();
if(iSel ==0)
{
m_wndShowAll.ShowWindow(SW_SHOW);
m_wndShowError.ShowWindow(SW_HIDE);
}
else if(iSel ==1)
{
m_wndShowAll.ShowWindow(SW_HIDE);
m_wndShowError.ShowWindow(SW_SHOW);
}
}
void CfullServerWithRmtMonitorDemoDlg::OnTcnSelchangeMsg(NMHDR *pNMHDR, LRESULT *pResult)
{
int iSel = m_tabMsgList.GetCurSel();
TRACE("cur sel=%d\n",iSel);
*pResult = 0;
SetBufferWndShow();
}
DWORD __stdcall CfullServerWithRmtMonitorDemoDlg::StartServersThread(LPVOID pObj)
{
CfullServerWithRmtMonitorDemoDlg* pThis = (CfullServerWithRmtMonitorDemoDlg*)pObj;
pThis->EnableButtons();
pThis->m_outputDemo.PutLine(COutputDemo::Message,"开始设置参数");
pThis->m_svrDemo.InitServerName("TCP_Svr_with_Rmt_Monitor_DEMO");
pThis->m_svrDemo.InitOutput(&pThis->m_outputDemo);
pThis->m_svrDemo.InitSocketServerNetPara(NULL,13601);
pThis->m_svrDemo.InitMonitorServerNetPara(NULL,13602);
pThis->m_svrDemo.InitSocketServerCallback(TCPSocketServerCallBack);
pThis->m_svrDemo.InitMonitorServerCallback(RmtCallBack);
pThis->m_outputDemo.PutLine(COutputDemo::Message,"设置参数完成");
pThis->m_outputDemo.PutLine(COutputDemo::Message,"开始启动服务");
BOOL fOK = pThis->m_svrDemo.StartServers();
pThis->m_outputDemo.PutLine(COutputDemo::Message,(fOK)?"启动服务成功":"启动服务失败");
pThis->m_outputDemo.PutLine(COutputDemo::Message,"-*-*-*-*-*-*-*-*-*-*-*-*-");
pThis->m_outputDemo.SetRmtMonitor(pThis->m_svrDemo.GetRemoteMonitorManager());
pThis->EnableButtons(1,1,1,1,1);
return 0;
}
DWORD __stdcall CfullServerWithRmtMonitorDemoDlg::StopServersThread(LPVOID pObj)
{
CfullServerWithRmtMonitorDemoDlg* pThis = (CfullServerWithRmtMonitorDemoDlg*)pObj;
pThis->EnableButtons();
BOOL fOK = pThis->m_svrDemo.StopServers();
pThis->m_outputDemo.PutLine(COutputDemo::Message,(fOK)?"停止服务成功":"停止服务失败");
pThis->m_outputDemo.PutLine(COutputDemo::Message,"-*-*-*-*-*-*-*-*-*-*-*-*-");
pThis->EnableButtons(1,1,1,1,1);
return 0;
}
void CfullServerWithRmtMonitorDemoDlg::EnableButtons(BOOL fRun,BOOL fStop,BOOL fPause,BOOL fState,BOOL fQuit)
{
GetDlgItem(IDC_START_SVR)->EnableWindow(fRun);
GetDlgItem(IDC_STOP_SVR)->EnableWindow(fStop);
GetDlgItem(IDC_PAUSE_SVR)->EnableWindow(fPause);
GetDlgItem(IDC_SVR_STATE)->EnableWindow(fState);
GetDlgItem(IDC_EXIT_NOW)->EnableWindow(fQuit);
}
void CfullServerWithRmtMonitorDemoDlg::OnBnClickedStartSvr()
{
DWORD dwS = m_svrDemo.GetServerState();
if(dwS ==0)
{
DWORD dwID;
HANDLE hT = CreateThread(NULL,0,StartServersThread,(LPVOID)this,0,&dwID);
if(hT)
{
CloseHandle(hT);
}
}
else
{
m_outputDemo.PutLine(COutputDemo::GeneralError,"服务已经在运行");
}
}
void CfullServerWithRmtMonitorDemoDlg::OnBnClickedStopSvr()
{
DWORD dwS = m_svrDemo.GetServerState();
if(dwS ==1)
{
m_outputDemo.SetRmtMonitor(NULL);
DWORD dwID;
HANDLE hT = CreateThread(NULL,0,StopServersThread,(LPVOID)this,0,&dwID);
if(hT)
CloseHandle(hT);
}
else
{
m_outputDemo.PutLine(COutputDemo::GeneralError,"服务没有在运行");
}
}
void CfullServerWithRmtMonitorDemoDlg::OnBnClickedPauseSvr()
{
DWORD dwS = m_svrDemo.GetServerState();
if(dwS ==1)
{
if(m_svrDemo.PauseTCPSocketServer())
m_outputDemo.PutLine(COutputDemo::Success,"TCPSocketServer已经暂停");
else
m_outputDemo.PutLine(COutputDemo::GeneralError,"TCPSocketServer无法暂停,可能是TCPSocketServer服务未启动");
}
else
{
m_outputDemo.PutLine(COutputDemo::GeneralError,"服务没有在运行");
}
}
void CfullServerWithRmtMonitorDemoDlg::OnBnClickedResmueSvr()
{
DWORD dwS = m_svrDemo.GetServerState();
if(dwS ==1)
{
if(m_svrDemo.PauseTCPSocketServer())
m_outputDemo.PutLine(COutputDemo::Success,"TCPSocketServer已经恢复");
else
m_outputDemo.PutLine(COutputDemo::GeneralError,"TCPSocketServer无法恢复,可能是TCPSocketServer服务未启动");
}
else
{
m_outputDemo.PutLine(COutputDemo::GeneralError,"服务没有在运行");
}
}
void CfullServerWithRmtMonitorDemoDlg::OnBnClickedSvrState()
{
DWORD dwS = m_svrDemo.GetServerState();
CString szOut;
if(dwS==1)
{
char szTState[20]="";
enum CServerStatus::ParentThreadStatus eState = m_svrDemo.GetTCPSocketServerStatus();
CServerStatus::GetParentThreadStatusString(eState,szTState);
szOut.Format("服务正在运行,TCPSocketServer状态:%s",szTState);
}
else
{
szOut.Format("服务未运行");
}
m_outputDemo.PutLine(COutputDemo::Message,"-*-*-*-*-*-*-*-*-*-*-*-*-");
m_outputDemo.PutLine(COutputDemo::Message,szOut);
m_outputDemo.PutLine(COutputDemo::Message,"-*-*-*-*-*-*-*-*-*-*-*-*-");
}
void CfullServerWithRmtMonitorDemoDlg::OnBnClickedExitNow()
{
DWORD dwS = m_svrDemo.GetServerState();
if(dwS ==1)
{
m_outputDemo.PutLine(COutputDemo::Message,"-*-*-*-*-*-*-*-*-*-*-*-*-");
m_outputDemo.PutLine(COutputDemo::GeneralWarn,"服务在运行时不允许退出,此时退出将会导致资源无法释放,请先停止服务器运行");
m_outputDemo.PutLine(COutputDemo::Message,"-*-*-*-*-*-*-*-*-*-*-*-*-");
}
else
{
CDialog::OnOK();
}
}
void CfullServerWithRmtMonitorDemoDlg::OnOK()
{
// TODO: 在此添加专用代码和/或调用基类
return;
// CDialog::OnOK();
}
void CfullServerWithRmtMonitorDemoDlg::OnCancel()
{
// TODO: 在此添加专用代码和/或调用基类
OnBnClickedExitNow();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -