⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 qclientdoc.cpp

📁 一款抢答器 界面友好 功能完善 支持断开重连 可在娱乐竞赛中使用此为 源代码
💻 CPP
字号:
// QClientDoc.cpp : CQClientDoc 类的实现
//

#include "stdafx.h"
#include "QClient.h"

#include "QClientDoc.h"
#include "SetupDlg.h"
#include "QClientView.h"


#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CQClientDoc

IMPLEMENT_DYNCREATE(CQClientDoc, CDocument)

BEGIN_MESSAGE_MAP(CQClientDoc, CDocument)
END_MESSAGE_MAP()


// CQClientDoc 构造/析构

CQClientDoc::CQClientDoc()
{
	m_pMsg = new CMsg();
	m_uMode=0;
	m_uIndex=0;
	m_strGroupName=_T("");
	m_pSocket=NULL;
	// TODO: 在此添加一次性构造代码

}

CQClientDoc::~CQClientDoc()
{
	this->StopConnect ();
    if(m_pSocket!=NULL)
	{
		m_pSocket->Close ();
	}
	delete m_pMsg;
	delete m_pSocket;
	m_pSocket=NULL;
}

BOOL CQClientDoc::OnNewDocument()
{
	if (!CDocument::OnNewDocument())
		return FALSE;
	CSetupDlg Dlg;
	while(TRUE)
	{
		if(IDOK!=Dlg.DoModal ())
		return FALSE;
		m_uIndex=Dlg.uIndex ;
		m_strGroupName=Dlg.sUserName;
		if(ConnectSocket(Dlg.uIndex ,Dlg.sUserName ,Dlg.dwIpAddr ,Dlg.uPort ))
			return TRUE;
		if(AfxMessageBox(_T("是否连接其他地址"),MB_YESNO)==IDNO)
			return FALSE;
	}

	

	return TRUE;
}




// CQClientDoc 序列化

void CQClientDoc::Serialize(CArchive& ar)
{
	if (ar.IsStoring())
	{
		// TODO: 在此添加存储代码
	}
	else
	{
		// TODO: 在此添加加载代码
	}
}


// CQClientDoc 诊断

#ifdef _DEBUG
void CQClientDoc::AssertValid() const
{
	CDocument::AssertValid();
}

void CQClientDoc::Dump(CDumpContext& dc) const
{
	CDocument::Dump(dc);
}
#endif //_DEBUG
BOOL CQClientDoc::ConnectSocket(UINT uIndex, LPCTSTR sUserName, DWORD dwIpAddr, UINT uPort)
{
	CString sIpAddr;
	unsigned char * pIp;
	pIp=(unsigned char*)&dwIpAddr;
	sIpAddr.Format (_T("%u.%u.%u.%u"),*(pIp+3),*(pIp+2),*(pIp+1),*pIp);
	m_pSocket=new CClientSocket(this);
	if(!m_pSocket->Create ())
	{
		delete m_pSocket;
		m_pSocket=NULL;
		AfxMessageBox(_T("创建套接字失败!"));
		return FALSE;
	}
	while (!m_pSocket->Connect (sIpAddr,uPort))
	{
		if(AfxMessageBox(_T("连接服务器失败,是否重连?"),MB_YESNO)==IDNO)
		{
			delete m_pSocket;
			m_pSocket=NULL;
			return FALSE;
		}

	}
	m_pFile=new CSocketFile(m_pSocket);
	m_pArchiveIn=new CArchive(m_pFile,CArchive::load );
	m_pArchiveOut= new CArchive(m_pFile,CArchive::store );

	CMsg msg;
	msg.m_uMode =2;
	msg.m_uIndex =m_uIndex;
	SendMsg(&msg);
	
	return TRUE;


}
void CQClientDoc::ProcessPendingRead ()
{
	
	ReceiveMsg(m_pMsg);
		
	/*if(this->m_uMode ==0)
	{
		if(m_pMsg!=NULL)
		{
			AfxMessageBox(_T("恭喜你! 获取抢答机会!"));

		}
	}*/
	if(m_pMsg->m_uMode ==6)
	{
		m_uMode=0;
		POSITION pos=GetFirstViewPosition();
		if(pos!=NULL)
		{
			CView *pView=GetNextView(pos);
			CQClientView *pClientView=DYNAMIC_DOWNCAST(CQClientView,pView);
			if(pClientView!=NULL)
				pClientView->m_text .SetWindowTextW(_T(">>抢答环节<<"));
			pClientView->Invalidate ();
				
		}
		

	}
	else if(m_pMsg->m_uMode ==7)
	{
		m_uMode=1;
		POSITION pos=GetFirstViewPosition();
		if(pos!=NULL)
		{
			CView *pView=GetNextView(pos);
			CQClientView *pClientView=DYNAMIC_DOWNCAST(CQClientView,pView);
			if(pClientView!=NULL)
				pClientView->m_text .SetWindowTextW(_T(">>必答环节<<"));
			pClientView->Invalidate ();
				
		}
	}
}
void CQClientDoc::ReceiveMsg(CMsg * msg)
{
	
	TRY
	{
		msg->Serialize (*m_pArchiveIn);
	}
	CATCH (CFileException,e)
	{
		AfxMessageBox(_T("接受数据错误!"),MB_YESNO);
		delete msg;
		msg=NULL;
	}
	END_CATCH

    
	


		

}
void CQClientDoc::SendMsg(CMsg *msg)
{
	if(m_pArchiveOut!=NULL)
	{
		TRY
		{
	     msg->Serialize(*m_pArchiveOut);
		 m_pArchiveOut->Flush() ;
		}
		CATCH(CFileException,e)
		{
			AfxMessageBox(_T("发送数据错误!"),MB_YESNO);
		}
		END_CATCH
    }
}
void CQClientDoc::StopConnect ()
{
	if(m_pSocket!=NULL)
	{
	CMsg msg;
	msg.m_uIndex =m_uIndex ;
	msg.m_uMode=3;
	msg.m_strAnswer =_T("") ;
	msg.m_strUserName=m_strGroupName ;

	SendMsg (&msg);
	}
}

// CQClientDoc 命令

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -