123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276 |
- // 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<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 CServerDlg::OnQueryDragIcon()
- {
- return static_cast<HCURSOR>(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_Server.SetWorkerThreadCount(2);
- //m_Server.SetMaxShutdownWaitTime(100000000);
- //m_Server.SetMaxDatagramSize(50);
- //m_Server.SetDetectAttempts(0);
- //m_Server.SetDetectAttempts(0);
- //m_Server.SetDetectInterval(0);
- //m_Server.SetSendPolicy(SP_SAFE);
- m_Address.GetWindowText(m_strAddress);
- m_strAddress.Trim();
- SetAppState(ST_STARTING);
- 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, const SOCKADDR_IN* pSockAddr)
- {
- 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::OnSend(CONNID dwConnID, const BYTE* pData, int iLength)
- {
- //static int t = 0;
- //if(++t % 3 == 0) return HR_ERROR;
- ::PostOnSend(dwConnID, pData, iLength);
- return HR_OK;
- }
- EnHandleResult CServerDlg::OnReceive(CONNID dwConnID, const BYTE* pData, int iLength)
- {
- //static int t = 0;
- //if(++t % 3 == 0) return HR_ERROR;
- ::PostOnReceive(dwConnID, pData, iLength);
- if(m_Server.Send(dwConnID, pData, iLength))
- return HR_OK;
- else
- return 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;
- }
|