UpdateServerDlg.cpp
Upload User: dzyhzl
Upload Date: 2019-04-29
Package Size: 56270k
Code Size: 8k
Development Platform:

C/C++

  1. // UpdateServerDlg.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include <objbase.h>
  5. #include <initguid.h>
  6. #include "UpdateServer.h"
  7. #include "UpdateServerDlg.h"
  8. #include "Library.h"
  9. #include "protocol.h"
  10. #ifdef _DEBUG
  11. #define new DEBUG_NEW
  12. #undef THIS_FILE
  13. static char THIS_FILE[] = __FILE__;
  14. #endif
  15. OnlineGameLib::Win32::CLibrary g_theHeavenLib( "heaven.dll" );
  16. /////////////////////////////////////////////////////////////////////////////
  17. // CAboutDlg dialog used for App About
  18. class CAboutDlg : public CDialog
  19. {
  20. public:
  21. CAboutDlg();
  22. // Dialog Data
  23. //{{AFX_DATA(CAboutDlg)
  24. enum { IDD = IDD_ABOUTBOX };
  25. //}}AFX_DATA
  26. // ClassWizard generated virtual function overrides
  27. //{{AFX_VIRTUAL(CAboutDlg)
  28. protected:
  29. virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
  30. //}}AFX_VIRTUAL
  31. // Implementation
  32. protected:
  33. //{{AFX_MSG(CAboutDlg)
  34. //}}AFX_MSG
  35. DECLARE_MESSAGE_MAP()
  36. };
  37. CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
  38. {
  39. //{{AFX_DATA_INIT(CAboutDlg)
  40. //}}AFX_DATA_INIT
  41. }
  42. void CAboutDlg::DoDataExchange(CDataExchange* pDX)
  43. {
  44. CDialog::DoDataExchange(pDX);
  45. //{{AFX_DATA_MAP(CAboutDlg)
  46. //}}AFX_DATA_MAP
  47. }
  48. BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
  49. //{{AFX_MSG_MAP(CAboutDlg)
  50. // No message handlers
  51. //}}AFX_MSG_MAP
  52. END_MESSAGE_MAP()
  53. /////////////////////////////////////////////////////////////////////////////
  54. // CUpdateServerDlg dialog
  55. CUpdateServerDlg::CUpdateServerDlg(CWnd* pParent /*=NULL*/)
  56. : CDialog(CUpdateServerDlg::IDD, pParent)
  57. {
  58. //{{AFX_DATA_INIT(CUpdateServerDlg)
  59. m_nServerPort = 7653;
  60. //}}AFX_DATA_INIT
  61. m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  62. m_pServer = NULL;
  63. }
  64. void CUpdateServerDlg::DoDataExchange(CDataExchange* pDX)
  65. {
  66. CDialog::DoDataExchange(pDX);
  67. //{{AFX_DATA_MAP(CUpdateServerDlg)
  68. DDX_Control(pDX, IDC_LIST_USEDNOD, m_ctlUsedNode);
  69. DDX_Control(pDX, IDC_LIST_FREENODE, m_ctlFreeNode);
  70. DDX_Text(pDX, IDC_EDIT_SERVERPORT, m_nServerPort);
  71. //}}AFX_DATA_MAP
  72. }
  73. BEGIN_MESSAGE_MAP(CUpdateServerDlg, CDialog)
  74. //{{AFX_MSG_MAP(CUpdateServerDlg)
  75. ON_WM_SYSCOMMAND()
  76. ON_WM_PAINT()
  77. ON_WM_QUERYDRAGICON()
  78. ON_BN_CLICKED(IDC_BTN_CREATESERVICE, OnBtnCreateservice)
  79. ON_BN_CLICKED(IDC_BTN_INSERT, OnBtnInsert)
  80. ON_BN_CLICKED(IDC_BTN_REMOVE, OnBtnRemove)
  81. //}}AFX_MSG_MAP
  82. END_MESSAGE_MAP()
  83. /////////////////////////////////////////////////////////////////////////////
  84. // CUpdateServerDlg message handlers
  85. BOOL CUpdateServerDlg::OnInitDialog()
  86. {
  87. CDialog::OnInitDialog();
  88. // Add "About..." menu item to system menu.
  89. // IDM_ABOUTBOX must be in the system command range.
  90. ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
  91. ASSERT(IDM_ABOUTBOX < 0xF000);
  92. CMenu* pSysMenu = GetSystemMenu(FALSE);
  93. if (pSysMenu != NULL)
  94. {
  95. CString strAboutMenu;
  96. strAboutMenu.LoadString(IDS_ABOUTBOX);
  97. if (!strAboutMenu.IsEmpty())
  98. {
  99. pSysMenu->AppendMenu(MF_SEPARATOR);
  100. pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
  101. }
  102. }
  103. SetIcon(m_hIcon, TRUE); // Set big icon
  104. SetIcon(m_hIcon, FALSE); // Set small icon
  105. // TODO: Add extra initialization here
  106. return TRUE;  // return TRUE  unless you set the focus to a control
  107. }
  108. void CUpdateServerDlg::OnSysCommand(UINT nID, LPARAM lParam)
  109. {
  110. if ((nID & 0xFFF0) == IDM_ABOUTBOX)
  111. {
  112. CAboutDlg dlgAbout;
  113. dlgAbout.DoModal();
  114. }
  115. else
  116. {
  117. CDialog::OnSysCommand(nID, lParam);
  118. }
  119. }
  120. // If you add a minimize button to your dialog, you will need the code below
  121. //  to draw the icon.  For MFC applications using the document/view model,
  122. //  this is automatically done for you by the framework.
  123. void CUpdateServerDlg::OnPaint() 
  124. {
  125. if (IsIconic())
  126. {
  127. CPaintDC dc(this); // device context for painting
  128. SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
  129. // Center icon in client rectangle
  130. int cxIcon = GetSystemMetrics(SM_CXICON);
  131. int cyIcon = GetSystemMetrics(SM_CYICON);
  132. CRect rect;
  133. GetClientRect(&rect);
  134. int x = (rect.Width() - cxIcon + 1) / 2;
  135. int y = (rect.Height() - cyIcon + 1) / 2;
  136. // Draw the icon
  137. dc.DrawIcon(x, y, m_hIcon);
  138. }
  139. else
  140. {
  141. CDialog::OnPaint();
  142. }
  143. }
  144. HCURSOR CUpdateServerDlg::OnQueryDragIcon()
  145. {
  146. return (HCURSOR) m_hIcon;
  147. }
  148. void CUpdateServerDlg::OnBtnCreateservice() 
  149. {
  150. UpdateData( TRUE );
  151. if ( !m_nServerPort )
  152. {
  153. AfxMessageBox( "Please input a valid port" );
  154. return;
  155. }
  156. /*
  157.  * We open the heaven to wait for the rainbow
  158.  */
  159. pfnCreateServerInterface pServerFactroyFun = 
  160. ( pfnCreateServerInterface )( g_theHeavenLib.GetProcAddress( _T( "CreateInterface" ) ) );
  161. IServerFactory *pServerFactory = NULL;
  162. if ( pServerFactroyFun && 
  163. SUCCEEDED( pServerFactroyFun( IID_IServerFactory, reinterpret_cast< void ** >( &pServerFactory ) ) ) )
  164. {
  165. pServerFactory->SetEnvironment( 20, 0, 20, 1024 );
  166. /*
  167.  * For gameserver
  168.  */
  169. pServerFactory->CreateServerInterface( IID_IIOCPServer, reinterpret_cast< void ** >( &m_pServer ) );
  170. pServerFactory->Release();
  171. }
  172. if ( m_pServer )
  173. {
  174. m_pServer->Startup();
  175. m_pServer->RegisterMsgFilter( reinterpret_cast< void * >( this ), EventNotify );
  176. if ( SUCCEEDED( m_pServer->OpenService( INADDR_ANY, m_nServerPort ) ) )
  177. {
  178. ::EnableWindow( GetDlgItem( IDC_EDIT_SERVERPORT )->GetSafeHwnd(), FALSE );
  179. ::EnableWindow( GetDlgItem( IDC_BTN_CREATESERVICE )->GetSafeHwnd(), FALSE );
  180. }
  181. }
  182. }
  183. void CUpdateServerDlg::OnOK() 
  184. {
  185. CString csInfo;
  186. int nLen;
  187. ::EnableWindow( GetDlgItem( IDC_LIST_USEDNOD )->GetSafeHwnd(), FALSE );
  188. for ( int i=0; i < m_ctlUsedNode.GetCount(); i++ )
  189. {
  190. nLen = m_ctlUsedNode.GetTextLen( i );
  191. m_ctlUsedNode.GetText( i, csInfo.GetBuffer( nLen ) );
  192. csInfo.ReleaseBuffer();
  193. const char *pInfo = csInfo.GetBuffer( 0 );
  194. stdMap::iterator it;
  195. if ( m_theClient.end() != ( it = m_theClient.find( pInfo ) ) )
  196. {
  197. UINT nID = (* it).second;
  198. tagWork work;
  199. work.cProtocol = enumWork;
  200. work.nAction = 0;
  201. if ( m_pServer )
  202. {
  203. m_pServer->SendData( nID, &work, sizeof( tagWork ) );
  204. }
  205. }
  206. }
  207. }
  208. void CUpdateServerDlg::OnCancel() 
  209. {
  210. CDialog::OnCancel();
  211. }
  212. void CUpdateServerDlg::OnBtnInsert() 
  213. {
  214. m_ctlFreeNode.GetCurSel();
  215. int nIndex = m_ctlFreeNode.GetCurSel();
  216. int nCount = m_ctlFreeNode.GetCount();
  217. if ( ( nIndex != LB_ERR ) && ( nCount > 0 ))
  218. {
  219. CString csInfo;
  220. int nLen = 0;
  221. nLen = m_ctlFreeNode.GetTextLen( nLen );
  222. m_ctlFreeNode.GetText( nIndex, csInfo.GetBuffer( nLen ) );
  223. csInfo.ReleaseBuffer();
  224. m_ctlFreeNode.DeleteString( nIndex );
  225. m_ctlUsedNode.AddString( csInfo.GetBuffer( 0 ) );
  226. }
  227. }
  228. void CUpdateServerDlg::OnBtnRemove() 
  229. {
  230. m_ctlUsedNode.GetCurSel();
  231. int nIndex = m_ctlUsedNode.GetCurSel();
  232. int nCount = m_ctlUsedNode.GetCount();
  233. if ( ( nIndex != LB_ERR ) && ( nCount > 0 ))
  234. {
  235. CString csInfo;
  236. int nLen = 0;
  237. nLen = m_ctlUsedNode.GetTextLen( nLen );
  238. m_ctlUsedNode.GetText( nIndex, csInfo.GetBuffer( nLen ) );
  239. csInfo.ReleaseBuffer();
  240. m_ctlUsedNode.DeleteString( nIndex );
  241. m_ctlFreeNode.AddString( csInfo.GetBuffer( 0 ) );
  242. }
  243. }
  244. void __stdcall CUpdateServerDlg::EventNotify( LPVOID lpParam,
  245. const unsigned long &ulnID,
  246. const unsigned long &ulnEventType )
  247. {
  248. CUpdateServerDlg *pThis = reinterpret_cast< CUpdateServerDlg * >( lpParam );
  249. ASSERT( pThis );
  250. try
  251. {
  252. pThis->_EventNotify( ulnID, ulnEventType );
  253. }
  254. catch(...)
  255. {
  256. }
  257. }
  258. void CUpdateServerDlg::_EventNotify( const unsigned long &ulnID,
  259. const unsigned long &ulnEventType )
  260. {
  261. const char *pInfo = m_pServer->GetClientInfo( ulnID );
  262. if ( !pInfo )
  263. {
  264. return;
  265. }
  266. switch ( ulnEventType )
  267. {
  268. case enumClientConnectCreate:
  269. m_ctlFreeNode.AddString( pInfo );
  270. m_theClient[pInfo] = ulnID;
  271. break;
  272. case enumClientConnectClose:
  273. {
  274. int nIndex = 0;
  275. if ( LB_ERR != ( nIndex = m_ctlFreeNode.FindString( 0, pInfo ) ) )
  276. {
  277. m_ctlFreeNode.DeleteString( nIndex );
  278. }
  279. if ( LB_ERR != ( nIndex = m_ctlUsedNode.FindString( 0, pInfo ) ) )
  280. {
  281. m_ctlUsedNode.DeleteString( nIndex );
  282. }
  283. m_theClient.erase( pInfo );
  284. }
  285. break;
  286. }
  287. }