ÿþ// MainDlg.cpp : implementation of the CMainDlg class // ///////////////////////////////////////////////////////////////////////////// #include "stdafx.h" #include "MainDlg.h" #include "Winsock2.h" #include <stdlib.h> #include "Iphlpapi.h" #pragma comment(lib, "Iphlpapi.lib") #pragma comment(lib, "ws2_32.lib") CString g_strPort = _T("8088"); CMainDlg::CMainDlg() : SHostWnd(_T("LAYOUT:XML_MAINWND")) , m_pServerListener(new CHttpServerListenerImpl()) , m_Server(m_pServerListener.get()) { m_bLayoutInited = FALSE; m_pIpcSvrCallbackImpl = new CIpcSvrCallbackImpl(); } CMainDlg::~CMainDlg() { if(m_pIpcSvrCallbackImpl){ delete m_pIpcSvrCallbackImpl; m_pIpcSvrCallbackImpl = NULL; } } int CMainDlg::OnCreate(LPCREATESTRUCT lpCreateStruct) { SetMsgHandled(FALSE); return 0; } BOOL CMainDlg::OnInitDialog(HWND hWnd, LPARAM lParam) { m_bLayoutInited = TRUE; CString strModulePath = GetCurModuleDir(); // \Pbk m_Server->Stop(); // RËYShttp g¡R m_pServerListener->SetListenPath( strModulePath + _T("cdn")); CString strBindAddress = _T("0.0.0.0"); int nNotOccupy = _ttoi(g_strPort); while(CheckPortIsOccupy(nNotOccupy)) { nNotOccupy++; } g_strPort.Format(_T("%d"),nNotOccupy); USHORT usPort = (USHORT)nNotOccupy; if(!m_Server->Start(strBindAddress, usPort)) { SMessageBox(m_hWnd,_T("cdn/T¨R1Y%ÿ"),_T("Ðc:y"),MB_OK); } // RËYS ipc HRESULT hRes = m_pIpcSvrCallbackImpl->Init((ULONG_PTR)m_hWnd); if( hRes != S_OK ) { SMessageBox(m_hWnd,_T("ipc g¡RÑ~š[1Y%ÿ"),_T("Ðc:y"),MB_OK); } // ¾‹n gáO÷S HANDLE hEvent = OpenEvent(EVENT_ALL_ACCESS, FALSE, _T("mbcdnEventInit")); SetEvent(hEvent); //////////////////////// return 0; } //TODO:ˆmo` f\ void CMainDlg::OnClose() { // \Pbk m_Server->Stop(); CSimpleWnd::DestroyWindow(); } void CMainDlg::OnMaximize() { SendMessage(WM_SYSCOMMAND, SC_MAXIMIZE); } void CMainDlg::OnRestore() { SendMessage(WM_SYSCOMMAND, SC_RESTORE); } void CMainDlg::OnMinimize() { SendMessage(WM_SYSCOMMAND, SC_MINIMIZE); } void CMainDlg::OnSize(UINT nType, SOUI::CSize size) { SetMsgHandled(FALSE); if (!m_bLayoutInited) return; SWindow *pBtnMax = FindChildByName(L"btn_max"); SWindow *pBtnRestore = FindChildByName(L"btn_restore"); if(!pBtnMax || !pBtnRestore) return; if (nType == SIZE_MAXIMIZED) { pBtnRestore->SetVisible(TRUE); pBtnMax->SetVisible(FALSE); } else if (nType == SIZE_RESTORED) { pBtnRestore->SetVisible(FALSE); pBtnMax->SetVisible(TRUE); } } bool CMainDlg::CheckPortIsOccupy(DWORD dwPort) { PMIB_TCPTABLE pTcptable; pTcptable = (MIB_TCPTABLE*)malloc(sizeof(MIB_TCPTABLE)); DWORD dwsize=sizeof(MIB_TCPTABLE); if (GetTcpTable(pTcptable, &dwsize, 2) == ERROR_INSUFFICIENT_BUFFER) { free(pTcptable); pTcptable = (PMIB_TCPTABLE)malloc(dwsize); } if (GetTcpTable(pTcptable, &dwsize, 2) == NO_ERROR) { for (int i=0; i<(int)pTcptable->dwNumEntries; i++) { DWORD port = ntohs(pTcptable->table[i].dwLocalPort) ; if (port == dwPort) { return true ; } } } free(pTcptable); return false; } CString CMainDlg::GetCurModuleDir() { TCHAR szCurrentDir[MAX_PATH] = { 0 }; GetModuleFileName(NULL, szCurrentDir, sizeof(szCurrentDir)); LPTSTR lpInsertPos = _tcsrchr(szCurrentDir, _T('\\')); _tcscpy(lpInsertPos + 1, _T("\0")); return szCurrentDir; }