123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398 |
- // ClientDlg.cpp : implementation file
- //
- #include "stdafx.h"
- #include "Client.h"
- #include "ClientDlg.h"
- #include "afxdialogex.h"
- #include "../../../../Common/Src/WaitFor.h"
- #ifdef _WIN64
- #ifdef _DEBUG
- #pragma comment(lib, "../../../Bin/HPSocket/x64/HPSocket_UD.lib")
- #else
- #pragma comment(lib, "../../../Bin/HPSocket/x64/HPSocket_U.lib")
- #endif
- #else
- #ifdef _DEBUG
- #pragma comment(lib, "../../../Bin/HPSocket/x86/HPSocket_UD.lib")
- #else
- #pragma comment(lib, "../../../Bin/HPSocket/x86/HPSocket_U.lib")
- #endif
- #endif
- // CClientDlg dialog
- #define DEFAULT_ADDRESS _T("127.0.0.1")
- #define DEFAULT_PORT _T("5555")
- CClientDlg::CClientDlg(CWnd* pParent /*=nullptr*/)
- : CDialogEx(CClientDlg::IDD, pParent)
- {
- m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
- }
- void CClientDlg::DoDataExchange(CDataExchange* pDX)
- {
- CDialogEx::DoDataExchange(pDX);
- 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_START, m_Start);
- DDX_Control(pDX, IDC_STOP, m_Stop);
- DDX_Control(pDX, IDC_TEST_TIMES, m_TestTimes);
- DDX_Control(pDX, IDC_SOCKET_COUNT, m_SocketCount);
- DDX_Control(pDX, IDC_DATA_LEN, m_DataLen);
- DDX_Control(pDX, IDC_TEST_TIMES_INTERV, m_TestInterv);
- }
- BEGIN_MESSAGE_MAP(CClientDlg, CDialogEx)
- ON_WM_PAINT()
- ON_WM_QUERYDRAGICON()
- ON_BN_CLICKED(IDC_START, &CClientDlg::OnBnClickedStart)
- ON_BN_CLICKED(IDC_STOP, &CClientDlg::OnBnClickedStop)
- ON_MESSAGE(USER_INFO_MSG, OnUserInfoMsg)
- ON_WM_VKEYTOITEM()
- ON_WM_CLOSE()
- 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_TestTimes.SetCurSel(5);
- m_TestInterv.SetCurSel(1);
- m_SocketCount.SetCurSel(5);
- m_DataLen.SetCurSel(5);
- m_Address.SetWindowText(DEFAULT_ADDRESS);
- m_Port.SetWindowText(DEFAULT_PORT);
- ::SetMainWnd(this);
- ::SetInfoList(&m_Info);
- SetAppState(ST_STOPPED);
- return TRUE; // return TRUE unless you set the focus to a control
- }
- void CClientDlg::OnClose()
- {
- ::SetMainWnd(nullptr);
- __super::OnClose();
- }
- // 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<WPARAM>(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<HCURSOR>(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)
- {
- if(m_enState == state)
- return;
- 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_Port.EnableWindow(m_enState == ST_STOPPED);
- m_TestTimes.EnableWindow(m_enState == ST_STOPPED);
- m_TestInterv.EnableWindow(m_enState == ST_STOPPED);
- m_SocketCount.EnableWindow(m_enState == ST_STOPPED);
- m_DataLen.EnableWindow(m_enState == ST_STOPPED);
- }
- BOOL CClientDlg::CheckParams()
- {
- BOOL isOK = TRUE;
- if(m_strAddress.IsEmpty())
- {
- m_Address.SetFocus();
- isOK = FALSE;
- }
- else if(m_usPort == 0)
- {
- m_Port.SetFocus();
- isOK = FALSE;
- }
- else if(m_iTestTimes <= 0)
- {
- m_TestTimes.SetFocus();
- isOK = FALSE;
- }
- else if(m_iTestInterv < 0)
- {
- m_TestInterv.SetFocus();
- isOK = FALSE;
- }
- else if(m_iSocketCount <= 0)
- {
- m_SocketCount.SetFocus();
- isOK = FALSE;
- }
- else if(m_iDataLen <= 0)
- {
- m_DataLen.SetFocus();
- isOK = FALSE;
- }
- if(!isOK)
- MessageBox(_T("One or more settings invalid, pls check!"), _T("Params Error"), MB_OK);
- return isOK;
- }
- void CClientDlg::OnBnClickedStart()
- {
- CString strAddress;
- CString strPort;
- CString strTestTimes;
- CString strTestInterv;
- CString strSocketCount;
- CString strDataLen;
- m_Address.GetWindowText(strAddress);
- m_Port.GetWindowText(strPort);
- m_TestTimes.GetWindowText(strTestTimes);
- m_TestInterv.GetWindowText(strTestInterv);
- m_SocketCount.GetWindowText(strSocketCount);
- m_DataLen.GetWindowText(strDataLen);
- m_strAddress = strAddress.Trim();
- m_usPort = (USHORT)_ttoi(strPort);
- m_iTestTimes = _ttoi(strTestTimes);
- m_iTestInterv = _ttoi(strTestInterv);
- m_iSocketCount = _ttoi(strSocketCount);
- m_iDataLen = _ttoi(strDataLen);
- if(!CheckParams())
- return;
- SetAppState(ST_STARTING);
- m_dwBeginTickCount = 0;
- m_dwTimeconsuming = 0;
- m_llTotalReceived = 0;
- m_llTotalSent = 0;
- m_llExpectReceived = (LONGLONG)m_iTestTimes * (LONGLONG)m_iSocketCount * (LONGLONG)m_iDataLen;
- m_vtClients.Clear();
- for(int i = 0; i < m_iSocketCount; i++)
- {
- smart_simple_ptr<CTcpClientPtr> pSocket = new CTcpClientPtr(this);
- if((*pSocket)->Start(m_strAddress, m_usPort))
- m_vtClients->push_back(pSocket.release());
- else
- {
- ::LogClientStartFail((*pSocket)->GetLastError(), (*pSocket)->GetLastErrorDesc());
- m_vtClients.Clear();
- SetAppState(ST_STOPPED);
- return;
- }
- }
- ::LogClientStart(m_strAddress, m_usPort);
- DWORD dwSendDelay = 3;
- CString strMsg;
- strMsg.Format(_T(" *** willing to send data after %d seconds ..."), dwSendDelay);
- ::LogMsg(strMsg);
- ::WaitWithMessageLoop(dwSendDelay * 1000);
- m_sendBuffer.Malloc(m_iDataLen, true);
- SetAppState(ST_STARTED);
- ::LogMsg(_T(" *** Go Now !"));
- m_dwBeginTickCount = ::TimeGetTime();
- BOOL bTerminated = FALSE;
- for(int i = 0; i < m_iTestTimes; i++)
- {
- for(int j = 0; j < m_iSocketCount; j++)
- {
- CTcpClientPtr* pSocket = m_vtClients[j];
- if(!(*pSocket)->Send(m_sendBuffer, (int)m_sendBuffer.Size()))
- {
- ::LogClientSendFail(i + 1, j + 1, ::SYS_GetLastError(), ::HP_GetSocketErrorDesc(SE_DATA_SEND));
- bTerminated = TRUE;
- break;
- }
- }
- if(bTerminated)
- break;
- if(m_iTestInterv > 0 && i + 1 < m_iTestTimes)
- ::WaitWithMessageLoop(m_iTestInterv);
- }
- m_sendBuffer.Free();
- }
- void CClientDlg::OnBnClickedStop()
- {
- SetAppState(ST_STOPPING);
- for(size_t i = 0; i < m_vtClients.Size(); i++)
- {
- CTcpClientPtr* pSocket = m_vtClients[i];
- if((*pSocket)->Stop())
- ::LogClientStopping((DWORD)i + 1);
- //else
- // ASSERT(FALSE);
- }
- ::WaitWithMessageLoop(100L);
- CString strMsg;
- strMsg.Format( _T(" *** Summary: expect - %lld, send - %lld, recv - %lld"),
- m_llExpectReceived, m_llTotalSent, m_llTotalReceived);
- ::LogMsg(strMsg);
- if(m_llExpectReceived == m_llTotalSent && m_llTotalSent == m_llTotalReceived)
- strMsg.Format(_T(" *** Success: time consuming - %u millisecond !"), m_dwTimeconsuming);
- else
- strMsg.Format(_T(" *** Fail: manual terminated ? (or data lost)"));
- ::LogMsg(strMsg);
- SetAppState(ST_STOPPED);
- }
- 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::OnPrepareConnect(IClient* pClient, SOCKET socket)
- {
- return HR_OK;
- }
- EnHandleResult CClientDlg::OnSend(IClient* pClient, const BYTE* pData, int iLength)
- {
- #ifdef _DEBUG2
- ::PostOnSend(pClient->GetConnectionID(), pData, iLength);
- #endif
- #if (_WIN32_WINNT <= _WIN32_WINNT_WS03)
- ::InterlockedExchangeAdd((volatile LONG*)&m_llTotalSent, iLength);
- #else
- ::InterlockedExchangeAdd64(&m_llTotalSent, iLength);
- #endif
- return HR_OK;
- }
- EnHandleResult CClientDlg::OnReceive(IClient* pClient, const BYTE* pData, int iLength)
- {
- #ifdef _DEBUG2
- ::PostOnReceive(pClient->GetConnectionID(), pData, iLength);
- #endif
- #if (_WIN32_WINNT <= _WIN32_WINNT_WS03)
- ::InterlockedExchangeAdd((volatile LONG*)&m_llTotalReceived, iLength);
- #else
- ::InterlockedExchangeAdd64(&m_llTotalReceived, iLength);
- #endif
- if(m_llTotalReceived == m_llExpectReceived)
- {
- m_dwTimeconsuming = ::GetTimeGap32(m_dwBeginTickCount);
- ::PostTimeConsuming(m_dwTimeconsuming);
- }
- ASSERT(m_llTotalReceived <= m_llExpectReceived);
- return HR_OK;
- }
- EnHandleResult CClientDlg::OnClose(IClient* pClient, EnSocketOperation enOperation, int iErrorCode)
- {
- iErrorCode == SE_OK ? ::PostOnClose(pClient->GetConnectionID()) :
- ::PostOnError(pClient->GetConnectionID(), enOperation, iErrorCode) ;
- return HR_OK;
- }
- EnHandleResult CClientDlg::OnConnect(IClient* pClient)
- {
- ::LogOnConnect2(pClient->GetConnectionID());
- return HR_OK;
- }
|