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

📄 serversocket.cpp

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

#include "stdafx.h"
#include "QAnswer.h"
#include "ServerSocket.h"



// CServerSocket

CServerSocket::CServerSocket(CQAnswerDoc * pDoc)
{
	m_pDoc=pDoc;
	for(POSITION pos=m_pDoc->GetFirstViewPosition ();pos!=NULL;)
	{
		CView * pView=m_pDoc->GetNextView (pos);
		m_pView=DYNAMIC_DOWNCAST(CQAnswerView,pView);
	}
		
		
			
		
}

CServerSocket::~CServerSocket()
{
}


// CServerSocket 成员函数

void CServerSocket::OnReceive(int nErrorCode)
{
	/*CMsg * msg=m_pDoc->m_pMsg  ;

	msg->m_uIndex =0;
	msg->m_strAnswer =_T("");
	msg->m_strUserName =_T("");
	msg->m_uMode =0;*/
	
	
	
	CMsg * msg=new CMsg;

	ReceiveMsg(msg);
	switch (msg->m_uMode )
	{
	case 0:
		if(m_pDoc->m_bQOpen ==TRUE&&m_pDoc->m_bOpen==TRUE&&m_pView->m_uState ==0)
		{
			//m_pDoc->m_msgList .AddTail (msg);
			/*DWORD style=m_pView->m_btnArray [msg.m_uIndex -1].GetStyle() ;
			style &= ~WS_DISABLED;
			m_pView->m_btnArray [msg.m_uIndex -1].SetStyle(style);*/
			m_pView->m_btnArray [msg->m_uIndex -1].ModifyStyle (WS_DISABLED,0);
		//	delete m_pDoc->m_pMsg;
			m_pDoc->m_pMsg=msg;
			
            m_pView->KillTimer (1);
			m_pDoc->m_bQOpen =FALSE;
			this->m_pView ->Invalidate ();

		}
		break;
	case 1:
           /*m_pView->m_btnArray [msg.m_uIndex -1].SetButtonStyle(m_pView->m_btnArray [msg.m_uIndex -1].GetButtonStyle() 
				^ WS_DISABLED);*/
		if(m_pDoc->m_bOpen==TRUE&&m_pView->m_uState ==1)
		{
		m_pView->m_btnArray [msg->m_uIndex -1].ModifyStyle (WS_DISABLED,0);
		m_pDoc->m_msgArray .SetAt ((msg->m_uIndex -1),msg);
		}
		this->m_pView ->Invalidate ();
        //m_pDoc->m_msgList .AddTail (msg);
		/*m_pDoc->m_tempNumber ++;
		if(m_pDoc->m_tempNumber>=m_pDoc->m_uNumber)
			m_pDoc->m_bOpen =FALSE;*/
		break;
	case 2:
		m_pView->m_btnArray [msg->m_uIndex -1].ShowWindow (SW_SHOW);
		this->m_pView ->Invalidate ();
		
		break;
	case 3:
		m_pView->m_btnArray [msg->m_uIndex -1].ModifyStyle (0,WS_DISABLED);
		m_pView->m_btnArray [msg->m_uIndex -1].ShowWindow (SW_HIDE);
		this->m_pView ->Invalidate ();

		for(POSITION pos=m_pDoc->m_connectionList .GetHeadPosition ();pos!=NULL;)
		{
			CServerSocket * pSocket=(CServerSocket *)(m_pDoc->m_connectionList .GetNext (pos));
			if(pSocket==this)
				delete pSocket;
				m_pDoc->m_connectionList .RemoveAt(pos);

		}





		break;
	}
//	this->m_pView ->Invalidate ();
	

	

	// TODO: 在此添加专用代码和/或调用基类

	CSocket::OnReceive(nErrorCode);
}

//void CServerSocket::AssertValid() const
//{
//	CSocket::AssertValid();
//
//	// TODO: 在此添加专用代码和/或调用基类
//}

//void CServerSocket::Dump(CDumpContext& dc) const
//{
//	CSocket::Dump(dc);
//
//	// TODO: 在此添加专用代码和/或调用基类
//}
void CServerSocket::Init()
{
	m_pFile= new CSocketFile(this);
	m_pArchiveIn= new CArchive(m_pFile,CArchive::load );
	m_pArchiveOut=new CArchive(m_pFile,CArchive::store);
}
void CServerSocket::SendMsg(CMsg *pMsg)
{
		if(m_pArchiveOut!=NULL)
	{
		TRY
		{
	     pMsg->Serialize(*m_pArchiveOut);
		 m_pArchiveOut->Flush() ;
		}
		CATCH(CFileException,e)
		{
			AfxMessageBox(_T("发送数据错误!"),MB_YESNO);
		}
		END_CATCH
    }

}
void CServerSocket::ReceiveMsg(CMsg *pMsg)
{
	
	if(m_pArchiveIn!=NULL)
	{
		TRY
		{
	     pMsg->Serialize(*m_pArchiveIn);

		}
		CATCH(CFileException,e)
		{
			AfxMessageBox(_T("接受数据错误!"),MB_YESNO);
		}
		END_CATCH
    }


}

void CServerSocket::OnClose(int nErrorCode)
{
	// TODO: 在此添加专用代码和/或调用基类

	CSocket::OnClose(nErrorCode);
}

⌨️ 快捷键说明

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