// ClientDlg.cpp : implementation file // #include "stdafx.h" #include "Client.h" #include "ClientDlg.h" #include "afxdialogex.h" // CClientDlg dialog #define DEFAULT_CONTENT _T("text to be sent") #define DEFAULT_ADDRESS _T("127.0.0.1") #define DEFAULT_PORT _T("5555") CClientDlg::CClientDlg(CWnd* pParent /*=NULL*/) : CDialogEx(CClientDlg::IDD, pParent), m_Client(this) , m_sslInitializer(SSL_SM_CLIENT, g_c_iVerifyMode, g_c_lpszPemCertFile, g_c_lpszPemKeyFile, g_c_lpszKeyPasswod, g_c_lpszCAPemCertFileOrPath) { VERIFY(m_sslInitializer.IsValid()); m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); } void CClientDlg::DoDataExchange(CDataExchange* pDX) { CDialogEx::DoDataExchange(pDX); DDX_Control(pDX, IDC_CONTENT, m_Content); DDX_Control(pDX, IDC_SEND, m_Send); DDX_Control(pDX, IDC_INFO, m_Info); DDX_Control(pDX, IDC_ADDRESS, m_Address); DDX_Control(pDX, IDC_PORT, m_Port); DDX_Control(pDX, IDC_ASYNC, m_Async); DDX_Control(pDX, IDC_START, m_Start); DDX_Control(pDX, IDC_STOP, m_Stop); } BEGIN_MESSAGE_MAP(CClientDlg, CDialogEx) ON_WM_PAINT() ON_WM_QUERYDRAGICON() ON_BN_CLICKED(IDC_SEND, &CClientDlg::OnBnClickedSend) ON_BN_CLICKED(IDC_START, &CClientDlg::OnBnClickedStart) ON_BN_CLICKED(IDC_STOP, &CClientDlg::OnBnClickedStop) ON_MESSAGE(USER_INFO_MSG, OnUserInfoMsg) ON_WM_VKEYTOITEM() END_MESSAGE_MAP() // CClientDlg message handlers BOOL CClientDlg::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 m_Content.SetWindowText(DEFAULT_CONTENT); m_Address.SetWindowText(DEFAULT_ADDRESS); m_Port.SetWindowText(DEFAULT_PORT); m_Async.SetCheck(BST_CHECKED); ::SetMainWnd(this); ::SetInfoList(&m_Info); SetAppState(ST_STOPPED); m_bAsyncConn = FALSE; 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 CClientDlg::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 CClientDlg::OnQueryDragIcon() { return static_cast(m_hIcon); } BOOL CClientDlg::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 CClientDlg::SetAppState(EnAppState state) { m_enState = state; if(this->GetSafeHwnd() == nullptr) return; m_Async.EnableWindow(m_enState == ST_STOPPED); m_Start.EnableWindow(m_enState == ST_STOPPED); m_Stop.EnableWindow(m_enState == ST_STARTED); m_Send.EnableWindow(m_enState == ST_STARTED); m_Address.EnableWindow(m_enState == ST_STOPPED); m_Port.EnableWindow(m_enState == ST_STOPPED); } void CClientDlg::OnBnClickedSend() { static DWORD SEQ = 0; USES_CONVERSION; CString strContent; m_Content.GetWindowText(strContent); LPCSTR name = "ÉËÉñС¹ÖÊÞ"; LPCSTR desc = T2A((LPCTSTR)strContent); int desc_len = (int)strlen(desc) + 1; int body_len = offsetof(TPkgBody, desc) + desc_len; TPkgBody* pBody = (TPkgBody*)_alloca(body_len); memset(pBody, 0, body_len); pBody->age = 23; strcpy(pBody->name, name); strcpy(pBody->desc, desc); if(m_Client->Send((BYTE*)pBody, body_len)) ::LogSend(m_Client->GetConnectionID(), strContent); else ::LogSendFail(m_Client->GetConnectionID(), ::SYS_GetLastError(), ::HP_GetSocketErrorDesc(SE_DATA_SEND)); } void CClientDlg::OnBnClickedStart() { SetAppState(ST_STARTING); CString strAddress; CString strPort; m_Address.GetWindowText(strAddress); m_Port.GetWindowText(strPort); USHORT usPort = (USHORT)_ttoi(strPort); m_bAsyncConn = m_Async.GetCheck(); ::LogClientStarting(strAddress, usPort); m_Client->SetMaxPackSize(0x01FFF); m_Client->SetPackHeaderFlag(0x169); if(m_Client->Start(strAddress, usPort, m_bAsyncConn)) { } else { ::LogClientStartFail(m_Client->GetLastError(), m_Client->GetLastErrorDesc()); SetAppState(ST_STOPPED); } } void CClientDlg::OnBnClickedStop() { SetAppState(ST_STOPPING); if(m_Client->Stop()) ::LogClientStopping(m_Client->GetConnectionID()); else ASSERT(FALSE); } int CClientDlg::OnVKeyToItem(UINT nKey, CListBox* pListBox, UINT nIndex) { if(nKey == 'C') pListBox->ResetContent(); return __super::OnVKeyToItem(nKey, pListBox, nIndex); } LRESULT CClientDlg::OnUserInfoMsg(WPARAM wp, LPARAM lp) { info_msg* msg = (info_msg*)wp; ::LogInfoMsg(msg); return 0; } EnHandleResult CClientDlg::OnConnect(IClient* pClient) { TCHAR szAddress[40]; int iAddressLen = sizeof(szAddress) / sizeof(TCHAR); USHORT usPort; pClient->GetLocalAddress(szAddress, iAddressLen, usPort); ::PostOnConnect(pClient->GetConnectionID(), szAddress, usPort); return HR_OK; } EnHandleResult CClientDlg::OnHandShake(IClient* pClient) { ::PostOnHandShake(pClient->GetConnectionID()); SetAppState(ST_STARTED); return HR_OK; } EnHandleResult CClientDlg::OnSend(IClient* pClient, const BYTE* pData, int iLength) { ::PostOnSend(pClient->GetConnectionID(), pData, iLength); return HR_OK; } EnHandleResult CClientDlg::OnReceive(IClient* pClient, const BYTE* pData, int iLength) { ::PostOnReceive(pClient->GetConnectionID(), pData, iLength); return HR_OK; } EnHandleResult CClientDlg::OnClose(IClient* pClient, EnSocketOperation enOperation, int iErrorCode) { iErrorCode == SE_OK ? ::PostOnClose(pClient->GetConnectionID()) : ::PostOnError(pClient->GetConnectionID(), enOperation, iErrorCode) ; SetAppState(ST_STOPPED); return HR_OK; }