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

📄 meetingdoc.cpp

📁 聊天的
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// MeetingDoc.cpp : implementation of the CMeetingDoc class
//

#include "stdafx.h"
#include "Meeting.h"

#include "MeetingDoc.h"
#include "meetingview.h"
#include "MainFrm.h"
#include "Shape.h"
#include "ConnectDlg.h"
#include "CreateDlg.h"
#include "DrawSetDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CMeetingDoc


IMPLEMENT_DYNCREATE(CMeetingDoc, CDocument)

BEGIN_MESSAGE_MAP(CMeetingDoc, CDocument)
	//{{AFX_MSG_MAP(CMeetingDoc)
	ON_COMMAND(ID_DRAW_HAND, OnDrawHand)
	ON_UPDATE_COMMAND_UI(ID_DRAW_HAND, OnUpdateDrawHand)
	ON_COMMAND(ID_DRAW_LINE, OnDrawLine)
	ON_UPDATE_COMMAND_UI(ID_DRAW_LINE, OnUpdateDrawLine)
	ON_COMMAND(ID_DRAW_RECTANGLE, OnDrawRectangle)
	ON_UPDATE_COMMAND_UI(ID_DRAW_RECTANGLE, OnUpdateDrawRectangle)
	ON_COMMAND(ID_DRAW_PICK, OnDrawPick)
	ON_UPDATE_COMMAND_UI(ID_DRAW_PICK, OnUpdateDrawPick)
	ON_COMMAND(ID_DRAW_CIRCLE, OnDrawCircle)
	ON_UPDATE_COMMAND_UI(ID_DRAW_CIRCLE, OnUpdateDrawCircle)
	ON_COMMAND(ID_NET_CREATE, OnNetCreate)
	ON_UPDATE_COMMAND_UI(ID_NET_CREATE, OnUpdateNetCreate)
	ON_COMMAND(ID_NET_CONNECT, OnNetConnect)
	ON_UPDATE_COMMAND_UI(ID_NET_CONNECT, OnUpdateNetConnect)
	ON_COMMAND(ID_NET_HANDUP, OnNetHandup)
	ON_UPDATE_COMMAND_UI(ID_NET_HANDUP, OnUpdateNetHandup)
	ON_COMMAND(ID_NET_BREAK, OnNetBreak)
	ON_UPDATE_COMMAND_UI(ID_NET_BREAK, OnUpdateNetBreak)
	ON_COMMAND(ID_DRAW_CLEAR, OnDrawClear)
	ON_COMMAND(ID_DRAW_SETUP, OnDrawSetup)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMeetingDoc construction/destruction

CMeetingDoc::CMeetingDoc()
:m_nPenWidth(1)
,m_bFill(false)
,m_socketListen(NULL)
,m_socketClient(NULL)
,m_pView(NULL)
,m_pTalkDlg(NULL)
,m_bIsServer(NULL)
,m_bIsClient(NULL)
{
	m_colorPen=RGB(0,0,255);
	m_penCur.CreatePen(PS_SOLID,m_nPenWidth,m_colorPen);
	m_colorFill=RGB(0,0,125);
	m_brushCur.CreateSolidBrush(m_colorFill);
	m_shapeCur=HAND;
	
	//初始化类私有数据
	DataBlock = 0;
	MaxDataBlock = MAXWAVEBLOCK;
	CurrentBlock = 0;
}

CMeetingDoc::~CMeetingDoc()
{
	while(!m_shapeList.IsEmpty())
		delete m_shapeList.RemoveHead();
	ClearAll();
}

BOOL CMeetingDoc::OnNewDocument()
{
	if (!CDocument::OnNewDocument())
		return FALSE;

	// TODO: add reinitialization code here
	// (SDI documents will reuse this document)

	return TRUE;
}



/////////////////////////////////////////////////////////////////////////////
// CMeetingDoc serialization

void CMeetingDoc::Serialize(CArchive& ar)
{
	if (ar.IsStoring())
	{
		// TODO: add storing code here
	}
	else
	{
		// TODO: add loading code here
	}
}

/////////////////////////////////////////////////////////////////////////////
// CMeetingDoc diagnostics

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

void CMeetingDoc::Dump(CDumpContext& dc) const
{
	CDocument::Dump(dc);
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CMeetingDoc commands

void CMeetingDoc::OnDrawHand() 
{
	m_shapeCur=HAND;
	
}

void CMeetingDoc::OnUpdateDrawHand(CCmdUI* pCmdUI) 
{
	pCmdUI->SetCheck(m_shapeCur==HAND);
	
}




void CMeetingDoc::OnDrawPick() 
{
	m_shapeCur=PICK;
	
}

void CMeetingDoc::OnUpdateDrawPick(CCmdUI* pCmdUI) 
{

	pCmdUI->SetCheck(m_shapeCur==PICK);
}

void CMeetingDoc::OnDrawRectangle() 
{

	m_shapeCur=RECTANGLE;
}


void CMeetingDoc::OnUpdateDrawRectangle(CCmdUI* pCmdUI) 
{
	pCmdUI->SetCheck(m_shapeCur==RECTANGLE);


	
}

void CMeetingDoc::OnDrawLine() 
{
	m_shapeCur=LINE;
}

void CMeetingDoc::OnUpdateDrawLine(CCmdUI* pCmdUI) 
{

	pCmdUI->SetCheck(m_shapeCur==LINE);
}

void CMeetingDoc::OnDrawCircle() 
{
	m_shapeCur=CIRCLE;
	
}

void CMeetingDoc::OnUpdateDrawCircle(CCmdUI* pCmdUI) 
{
	pCmdUI->SetCheck(m_shapeCur==CIRCLE);	
}
void CMeetingDoc::DeleteContents() 
{
	while(!m_shapeList.IsEmpty())
	{
		delete m_shapeList.RemoveHead();
	}
	CDocument::DeleteContents();
}

void CMeetingDoc::OnNetCreate() 
{
	CCreateDlg dlg;
	dlg.m_nChannel=0;
	dlg.m_strServerName="啊猫啊狗";
	if(dlg.DoModal()!=IDOK)return;
	if(!dlg.m_strServerName.IsEmpty())
	{
		m_strUserName=dlg.m_strServerName;
		m_nMaxUser=dlg.m_nMaxUser;
		m_bPhone=dlg.m_bPhone;
	}
	else 
	{
		MessageBox(NULL,"请输入聊天主题","提示",MB_OK);
			return;
	}
	

	if(m_socketListen!=NULL)
	{
		m_socketListen->Close();
		delete m_socketListen;
		m_socketListen=NULL;
	}
	m_socketListen=new CListenSocket(this);
	if(!m_socketListen->Create(3333+dlg.m_nChannel,SOCK_STREAM,NULL))
	{
		AfxMessageBox("建立服务器失败,可能该端口已经使用,请先停止该端口的使用,然后重新建立!");
		m_socketListen=NULL;
		return ;
		
	}
	if(!m_socketListen->Listen())
	{
		AfxMessageBox("不能够接受客户端的加入!");
		delete m_socketListen;
		m_socketListen=NULL;
		return ;
	}    
	m_bIsServer=TRUE;
	if(m_bPhone==TRUE)
		StartPhone();

}

void CMeetingDoc::OnUpdateNetCreate(CCmdUI* pCmdUI) 
{

	pCmdUI->Enable(!m_bIsServer&&!m_bIsClient);
}

void CMeetingDoc::OnNetConnect() 
{
	CConnectDlg dlg;
	if(dlg.DoModal()!=IDOK)return;
	m_strUserName=dlg.m_strUserName;
	m_nSex=dlg.m_nSex;
	if(m_socketClient!=NULL)
	{
		m_socketClient->Close();
		delete m_socketClient;
		m_socketClient=NULL;
	}
	m_socketClient=new CClientSocket(this);
	if(!m_socketClient->Create())
	{
		m_socketClient->Close();
		delete m_socketClient;
		m_socketClient=NULL;
		ClosePhone();
		return;
	}
	if(!m_socketClient->Connect(dlg.m_strAddr,3333+dlg.m_nChannel))
	{
		AfxMessageBox("不能联结到服务器!可能服务器不存在!");
		m_socketClient->Close();
		delete m_socketClient;
		m_socketClient=NULL;
		ClosePhone();
		return ;
	}
	m_socketClient->Initialize();
	m_bIsClient=TRUE;

	CShape* pOrder=new COrder(LOGIN,m_strUserName,m_nSex);
	TrashCollect(pOrder);
	m_socketClient->SendShape(pOrder);
	if((m_bPhone=dlg.m_bPhone)==true)StartPhone();


}

void CMeetingDoc::OnUpdateNetConnect(CCmdUI* pCmdUI) 
{
	pCmdUI->Enable(!m_bIsClient&&!m_bIsServer);
	
}

void CMeetingDoc::AcceptClient()
{
	CClientSocket* pSocket=new CClientSocket(this);
	if(!m_socketListen->Accept(*pSocket,NULL,NULL))
	{
		AfxMessageBox("socket accept false!");
		delete pSocket;
		return;
	}
	if(m_listClient.GetCount()>m_nMaxUser)
	{
//		MessageBox(NULL,"已达到服务器设置的最大用户数,不能接受新用户","提示",MB_OK);
		pSocket->Close();
		delete pSocket;
		pSocket=NULL;
		return ;
	}

	pSocket->Initialize();
	m_listClient.AddTail(pSocket);
}
void CMeetingDoc::ReceiveData(CClientSocket* pSocket)
{
	CShape* pShape=NULL;
	POSITION pos,tmp;
	do
	{
		if(pSocket->ReceiveShape(pShape))
		{
			ProcessMsg(pShape,pSocket);
		}
		else
		{
			tmp=pos=m_listClient.GetHeadPosition();
			
			while(pos!=NULL)
			{
				CClientSocket* pClient=(CClientSocket*)m_listClient.GetNext(pos);
				if(pClient==pSocket)
				{
					m_listClient.RemoveAt(tmp);
					pClient->Close();
					delete pClient;
					pClient=NULL;
					return;
				}

			}
		}
		
	}while(!pSocket->m_pArchiveIn->IsBufferEmpty());
}

void CMeetingDoc::OnNetHandup() 
{
		if(m_socketClient!=NULL)
		{
			m_socketClient->Close();
			delete m_socketClient;
			m_socketClient=NULL;
		}
		m_bIsClient=false;	
		CMainFrame* pFrm=(CMainFrame*)AfxGetApp()->GetMainWnd();
		CListCtrl* p=&pFrm->m_wndUserList;
		p->DeleteAllItems();
		
}

void CMeetingDoc::OnUpdateNetHandup(CCmdUI* pCmdUI) 
{
	pCmdUI->Enable(m_bIsClient);
	
}

void CMeetingDoc::OnNetBreak() 
{
	ClosePhone();
	if(m_bIsServer)
	{

		POSITION pos=m_listClient.GetHeadPosition();
		while(pos!=NULL)
		{
			CClientSocket* client=(CClientSocket*)m_listClient.GetNext(pos);
			delete client;
		}
		if(m_socketListen!=NULL)
		{
			m_socketListen->Close();
			delete m_socketListen;
			m_socketListen=NULL;
		}
		m_bIsServer=false;
		CMainFrame* pFrm=(CMainFrame*)AfxGetApp()->GetMainWnd();
		CListCtrl* p=&pFrm->m_wndUserList;
		p->DeleteAllItems();
		
	}
	
}

void CMeetingDoc::OnUpdateNetBreak(CCmdUI* pCmdUI) 
{
	pCmdUI->Enable(m_bIsServer);	
}
void CMeetingDoc::CloseSocket(CClientSocket* pSocket)
{
	if(m_bIsClient)
	{
		if(pSocket==m_socketClient)
			MessageBox(NULL,"服务器已经被目中止","警告",MB_OK);
		m_socketClient->Close();
		delete m_socketClient;
		m_socketClient=NULL;
		m_bIsClient=false;	
		CMainFrame* pFrm=(CMainFrame*)AfxGetApp()->GetMainWnd();
		CListCtrl* p=&pFrm->m_wndUserList;
		p->DeleteAllItems();
		return ;
	}
	if(m_bIsServer)
	{
		POSITION pos,temp;
		temp=pos=m_listClient.GetHeadPosition();
		CShape *pOrder;
		while(pos!=NULL)
		{
			temp=pos;
			CClientSocket* socket=(CClientSocket*)m_listClient.GetNext(pos);
			//消除用户列表中的记录
			if(socket==pSocket)
			{
				pOrder=new COrder(LOGOUT,socket->m_strUserName,0);
				TrashCollect(pOrder);
				LogOut(pOrder);
				m_listClient.RemoveAt(temp);

⌨️ 快捷键说明

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