// ServerDlg.cpp : implementation file // #include "stdafx.h" #include "Server.h" #include "ServerDlg.h" #include "afxdialogex.h" // CServerDlg dialog const LPCTSTR CServerDlg::ADDRESS = _T("0.0.0.0"); const USHORT CServerDlg::PORT = 5555; CServerDlg::CServerDlg(CWnd* pParent /*=NULL*/) : CDialogEx(CServerDlg::IDD, pParent), m_Server(this) { m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); } void CServerDlg::DoDataExchange(CDataExchange* pDX) { CDialogEx::DoDataExchange(pDX); DDX_Control(pDX, IDC_INFO, m_Info); DDX_Control(pDX, IDC_START, m_Start); DDX_Control(pDX, IDC_STOP, m_Stop); DDX_Control(pDX, IDC_ADDRESS, m_Address); DDX_Control(pDX, IDC_CONN_ID, m_ConnID); DDX_Control(pDX, IDC_DISCONNECT, m_DisConn); } BEGIN_MESSAGE_MAP(CServerDlg, CDialogEx) ON_WM_PAINT() ON_WM_QUERYDRAGICON() ON_BN_CLICKED(IDC_START, &CServerDlg::OnBnClickedStart) ON_BN_CLICKED(IDC_STOP, &CServerDlg::OnBnClickedStop) ON_MESSAGE(USER_INFO_MSG, OnUserInfoMsg) ON_BN_CLICKED(IDC_DISCONNECT, &CServerDlg::OnBnClickedDisconnect) ON_EN_CHANGE(IDC_CONN_ID, &CServerDlg::OnEnChangeConnId) ON_WM_VKEYTOITEM() END_MESSAGE_MAP() // CServerDlg message handlers BOOL CServerDlg::OnInitDialog() { CDialogEx::OnInitDialog(); // Set the icon for this dialog. The framework does this automatically // when the application's main window is not a dialog SetIcon(m_hIcon, TRUE); // Set big icon SetIcon(m_hIcon, FALSE); // Set small icon // TODO: Add extra initialization here CString strTitle; CString strOriginTitle; GetWindowText(strOriginTitle); strTitle.Format(_T("%s - (%s:%d)"), strOriginTitle, ADDRESS, PORT); SetWindowText(strTitle); ::SetMainWnd(this); ::SetInfoList(&m_Info); SetAppState(ST_STOPPED); return TRUE; // return TRUE unless you set the focus to a control } // If you add a minimize button to your dialog, you will need the code below // to draw the icon. For MFC applications using the document/view model, // this is automatically done for you by the framework. void CServerDlg::OnPaint() { if (IsIconic()) { CPaintDC dc(this); // device context for painting SendMessage(WM_ICONERASEBKGND, reinterpret_cast(dc.GetSafeHdc()), 0); // Center icon in client rectangle 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; // Draw the icon dc.DrawIcon(x, y, m_hIcon); } else { CDialogEx::OnPaint(); } } // The system calls this function to obtain the cursor to display while the user drags // the minimized window. HCURSOR CServerDlg::OnQueryDragIcon() { return static_cast(m_hIcon); } BOOL CServerDlg::PreTranslateMessage(MSG* pMsg) { if ( pMsg->message == WM_KEYDOWN &&( pMsg->wParam == VK_ESCAPE || pMsg->wParam == VK_CANCEL || pMsg->wParam == VK_RETURN )) return TRUE; return CDialog::PreTranslateMessage(pMsg); } void CServerDlg::SetAppState(EnAppState state) { m_enState = state; if(this->GetSafeHwnd() == nullptr) return; m_Start.EnableWindow(m_enState == ST_STOPPED); m_Stop.EnableWindow(m_enState == ST_STARTED); m_Address.EnableWindow(m_enState == ST_STOPPED); m_DisConn.EnableWindow(m_enState == ST_STARTED && m_ConnID.GetWindowTextLength() > 0); } void CServerDlg::OnBnClickedStart() { m_Address.GetWindowText(m_strAddress); m_strAddress.Trim(); SetAppState(ST_STARTING); g_SSL.Cleanup(); if(!g_SSL.Initialize(SSL_SM_SERVER, g_s_iVerifyMode, g_s_lpszPemCertFile, g_s_lpszPemKeyFile, g_s_lpszKeyPasswod, g_s_lpszCAPemCertFileOrPath)) { ::LogServerStartFail(::GetLastError(), _T("initialize SSL env fail")); SetAppState(ST_STOPPED); return; } if(m_Server.Start(ADDRESS, PORT)) { ::LogServerStart(ADDRESS, PORT); SetAppState(ST_STARTED); } else { ::LogServerStartFail(m_Server.GetLastError(), m_Server.GetLastErrorDesc()); SetAppState(ST_STOPPED); } } void CServerDlg::OnBnClickedStop() { SetAppState(ST_STOPPING); if(m_Server.Stop()) { ::LogServerStop(); SetAppState(ST_STOPPED); } else { ASSERT(FALSE); } } void CServerDlg::OnBnClickedDisconnect() { CString strConnID; m_ConnID.GetWindowText(strConnID); CONNID dwConnID = (CONNID)_ttoi(strConnID); if(m_Server.Disconnect(dwConnID)) ::LogDisconnect(dwConnID); else ::LogDisconnectFail(dwConnID); } void CServerDlg::OnEnChangeConnId() { m_DisConn.EnableWindow(m_enState == ST_STARTED && m_ConnID.GetWindowTextLength() > 0); } int CServerDlg::OnVKeyToItem(UINT nKey, CListBox* pListBox, UINT nIndex) { if(nKey == 'C') pListBox->ResetContent(); return __super::OnVKeyToItem(nKey, pListBox, nIndex); } LRESULT CServerDlg::OnUserInfoMsg(WPARAM wp, LPARAM lp) { info_msg* msg = (info_msg*)wp; ::LogInfoMsg(msg); return 0; } EnHandleResult CServerDlg::OnPrepareListen(SOCKET soListen) { TCHAR szAddress[40]; int iAddressLen = sizeof(szAddress) / sizeof(TCHAR); USHORT usPort; m_Server.GetListenAddress(szAddress, iAddressLen, usPort); ::PostOnPrepareListen(szAddress, usPort); return HR_OK; } EnHandleResult CServerDlg::OnAccept(CONNID dwConnID, SOCKET soClient) { BOOL bPass = TRUE; TCHAR szAddress[40]; int iAddressLen = sizeof(szAddress) / sizeof(TCHAR); USHORT usPort; m_Server.GetRemoteAddress(dwConnID, szAddress, iAddressLen, usPort); if(!m_strAddress.IsEmpty()) { if(m_strAddress.CompareNoCase(szAddress) == 0) bPass = FALSE; } ::PostOnAccept(dwConnID, szAddress, usPort, bPass); return bPass ? HR_OK : HR_ERROR; } EnHandleResult CServerDlg::OnHandShake(CONNID dwConnID) { ::PostOnHandShake(dwConnID); return HR_OK; } EnHandleResult CServerDlg::OnSend(CONNID dwConnID, const BYTE* pData, int iLength) { ::PostOnSend(dwConnID, pData, iLength); return HR_OK; } EnHandleResult CServerDlg::OnReceive(CONNID dwConnID, const BYTE* pData, int iLength) { ::PostOnReceive(dwConnID, pData, iLength); return m_Server.Send(dwConnID, pData, iLength) ? HR_OK : HR_ERROR; } EnHandleResult CServerDlg::OnClose(CONNID dwConnID, EnSocketOperation enOperation, int iErrorCode) { iErrorCode == SE_OK ? ::PostOnClose(dwConnID) : ::PostOnError(dwConnID, enOperation, iErrorCode); return HR_OK; } EnHandleResult CServerDlg::OnShutdown() { ::PostOnShutdown(); return HR_OK; }