📄 hnetmgr.cpp
字号:
#include "stdafx.h"#include "resource.h"#include "HNetMgr.h"#include "HNet.h"#include "hoopstest.h"#include "hoopstestDoc.h"#include "hoopstestView.h"#include "UserPasswordDlg.h"#include "HUtilityLocaleString.h"#include <process.h>#ifdef _DEBUG#define new DEBUG_NEW#undef THIS_FILEstatic char THIS_FILE[] = __FILE__;#endifBEGIN_MESSAGE_MAP(HNetMgr, CWnd) ON_WM_TIMER()END_MESSAGE_MAP()HNetMgr::HNetMgr(){ m_pHNet = 0; m_nTimer = 0; Init();}HNetMgr::~HNetMgr(){ if( m_pHNet ) { delete m_pHNet; m_pHNet = 0; } // kill the timer if( m_nTimer != 0 ) { KillTimer(m_nTimer); m_nTimer = 0; }}void HNetMgr::Init(){ // This is formula to receive windows messages (esp. the timer messages) // in a non-window class (this one) - just register some class and create a window of 0 dimensions CString strWndClass = AfxRegisterWndClass(0); if( !Create(strWndClass, _T("Net"), WS_CHILD, CRect(0, 0, 0, 0), this, 0) ) { assert(0); } // first let's create a HNet object m_pHNet = new HNet(); // pass a thread to HNet UseThread(); // register callback handler for connection status m_pHNet->SetStatusNoticeFunction(hnet_status_notice_function, (void*)this); // register callback handler for error notifications m_pHNet->SetErrorNoticeFunction(hnet_error_notice_function, (void*)this); // register callback handler for proxy authentication notifications m_pHNet->SetUserPasswordFunction(hnet_user_password_function, (void*)this);}static void hnet_thread(void* hnet_object){ HNet* pHNet = (HNet*) hnet_object;// pHNet->UseThread(); return;}void HNetMgr::UseThread(){ _beginthread(hnet_thread, 0, (void*) m_pHNet);}void HNetMgr::ReleaseThread(){// m_pHNet->ReleaseThread();}void HNetMgr::hnet_status_notice_function(unsigned int status, void * user_data){ if(status == HNET_STATUS_LINK_DOWN) { AfxMessageBox(_T("Connection to server down")); } if(status == HNET_STATUS_LINK_UP) { AfxMessageBox(_T("Connection to server up")); } if(status == HNET_STATUS_UNCONNECTED) { AfxMessageBox(_T("Server unconnected")); }}void HNetMgr::hnet_error_notice_function( unsigned int error, const char * session_name, const char * client_name, void * user_data){ switch (error) { case HNET_ERROR_NOTICE_PASSWORD_SERVER_ACCESS: AfxMessageBox(_T("Server access password Error")); break; case HNET_ERROR_NOTICE_PASSWORD_SERVER_ADMIN: AfxMessageBox(_T("Server admin password Error")); break; case HNET_ERROR_NOTICE_PASSWORD_SESSION: AfxMessageBox(_T("Session password Error")); break; case HNET_ERROR_NOTICE_PASSWORD_CLIENT: AfxMessageBox(_T("Client password Error")); break; case HNET_ERROR_NOTICE_BAD_SESSION: AfxMessageBox(_T("Bad session Error")); break; case HNET_ERROR_NOTICE_BAD_CLIENT: AfxMessageBox(_T("Bad client Error")); break; default: AfxMessageBox(_T("Unknown Error")); }}void HNetMgr::hnet_user_password_function(const char * realm, void * opaque_data, void * user_data){ HNetMgr * self = (HNetMgr*) user_data; CUserPasswordDlg prompt(realm); if(prompt.DoModal() == IDOK) self->ReportUserPassword( H_ASCII_TEXT(prompt.m_csUser), H_ASCII_TEXT(prompt.m_csPassword), opaque_data);}void HNetMgr::enumerate_session_helper(const char * name, void * user_data){ ((CListBox *)user_data)->AddString(H_TEXT(name));}bool HNetMgr::ConnectToServer( const char * csServerAddress, const char * csServerPort, const char * csServerAccessPassword){ assert(m_pHNet); bool bResult = m_pHNet->ConnectToServer( csServerAddress, csServerPort, csServerAccessPassword); if( !bResult ) return false; // install the timer which will periodically pump the message to // the HNet server if( (m_pHNet->IsConnected()) && (m_nTimer == 0 ) ) { int timer_id = 1; do { m_nTimer = CWnd::SetTimer( timer_id, 50, 0 ); timer_id++; } while( m_nTimer == 0 && timer_id < 50 ); } return true;}void HNetMgr::DisconnectFromServer(){ // disconnect from the HNetServer m_pHNet->DisconnectFromServer(); // kill the timer if( m_nTimer != 0 ) { KillTimer(m_nTimer); m_nTimer = 0; }}bool HNetMgr::CreateSession( const char * csSessionName, const char * csSessionPassword, const char * csAdminPassword){ assert(m_pHNet); return m_pHNet->CreateSession( csSessionName, csSessionPassword, csAdminPassword);}HNetClient * HNetMgr::CreateSessionAndClient( const char * session_name, const char * session_password, const char * admin_password, const char * client_name ){ assert(m_pHNet); HNetClient* pHNetClient = m_pHNet->CreateSessionAndClient( session_name, session_password, admin_password, client_name ); if( !pHNetClient ) return pHNetClient; create_new_client_doc(pHNetClient); return pHNetClient;}HNetClient * HNetMgr::JoinSession( const char * session_name, const char * session_password, const char * client_name){ assert(m_pHNet); HNetClient* pHNetClient = m_pHNet->JoinSession( session_name, session_password, client_name ); if( !pHNetClient ) return pHNetClient; create_new_client_doc(pHNetClient); return pHNetClient;}bool HNetMgr::GetSessionsList( CListBox* plistSessions){ assert(m_pHNet); m_pHNet->EnumerateServerSessions(enumerate_session_helper, (void*) plistSessions); return true;}bool HNetMgr::DeleteSession( const char * session_name, const char * session_password, const char * admin_password){ assert(m_pHNet); return m_pHNet->DeleteSession( session_name, session_password, admin_password );}void HNetMgr::QuitSession(HNetClient* pHNetClient){ assert(m_pHNet); m_pHNet->QuitSession( pHNetClient );}bool HNetMgr::ReportUserPassword(const char * user, const char * password, void * opaque_data){ assert(m_pHNet); return m_pHNet->ReportUserPassword(user, password, opaque_data);}void HNetMgr::OnTimer(UINT nIDEvent) { if (nIDEvent == m_nTimer) { m_pHNet->Run(); }}BOOL HNetMgr::PreCreateWindow(CREATESTRUCT& cs){ // TODO: Modify the Window class or styles here by modifying // the CREATESTRUCT cs cs.style = WS_OVERLAPPEDWINDOW; cs.cx = 0; cs.cy = 0; return CWnd::PreCreateWindow(cs);}void HNetMgr::create_new_client_doc(HNetClient* pHNetClient){ // open a new document CWinApp* pTheApp = AfxGetApp(); POSITION pos = pTheApp->GetFirstDocTemplatePosition(); CDocTemplate* pDocTempl = pTheApp->GetNextDocTemplate(pos); ChoopstestDoc* pDoc = (ChoopstestDoc*) pDocTempl->OpenDocumentFile(0); assert(pDoc); POSITION pos_view = pDoc->GetFirstViewPosition(); ChoopstestView* pFirstView = (ChoopstestView*) pDoc->GetNextView(pos_view); assert(pFirstView); pFirstView->CreateHNetClient(pHNetClient);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -