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

📄 net.cpp

📁 超强的图形界面聊天软件.rar
💻 CPP
字号:
// Net.cpp : implementation file
//

#include "stdafx.h"
#include "pictalk.h"
#include "Net.h"
#include "UserInfo.h"
#include "pictalkDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CNet
CArray<mpack,mpack> m_Willcon;
CArray<SOCKET,SOCKET> m_UNusedCon;
CArray<CClientSocket *,CClientSocket *> m_UsedCon;
CArray<CClientSocket *,CClientSocket *> m_wUsedCon;
CNet::CNet(CPictalkDlg * pPictalkDlg)
{
	m_pPictalkDlg=pPictalkDlg;
}
CNet::CNet(CPictalkDlg * pPictalkDlg,UINT PortAddress)
{
	m_UNusedCon.RemoveAll();
	m_UsedCon.RemoveAll();

	m_pPictalkDlg=pPictalkDlg;
	m_bMain=TRUE;
	BuildListen(PortAddress);
	m_bReady=TRUE;
}
CNet::CNet(CPictalkDlg * pPictalkDlg,LPCTSTR ServerName, UINT PortAddress)
{
	m_bReady=FALSE;
	m_bMain=FALSE;
	m_UNusedCon.RemoveAll();
	m_UsedCon.RemoveAll();
	CClientSocket * pSocket;

	m_pPictalkDlg=pPictalkDlg;
	m_bMain=FALSE;
	if(!(pSocket=ConnectServer(ServerName,PortAddress)))
	{
		pSocket=NULL;
		return;
	}
	pSocket->Init();
	m_UsedCon.Add(pSocket);
}
CNet::~CNet()
{
	int i;
	for(i=0;i<m_UsedCon.GetSize();i++)
		delete m_UsedCon[i];
}

/////////////////////////////////////////////////////////////////////////////
// CNet diagnostics

#ifdef _DEBUG

#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CNet serialization

/////////////////////////////////////////////////////////////////////////////
// CNet commands

void CNet::SendMsg(mpack &Msgpack, CSocket *pSocket, BOOL bSend)
{
	int i=0;
	for(;i<m_UsedCon.GetSize();i++)
		if(pSocket!=m_UsedCon[i]){
			TRY
			{
				SendMsgs(Msgpack,m_UsedCon[i]);
			}
			CATCH(CFileException, e)
			{
				delete m_UsedCon[i];
				m_UsedCon.RemoveAt(i);
				i--;
			}
			END_CATCH
		};
}
void CNet::SendMsgs(mpack &Msgpack,CClientSocket * pSocket)
{
	Msgpack.ser(*(pSocket->m_pArchiveOut));
	pSocket->m_pArchiveOut->Flush();
}
void CNet::GetMsg(CArchive& ar,mpack &pmpack)
{
	pmpack.ser(ar);
//	if(pmpack.m_MsgID!=MID_ADDUSER)
//		m_pPictalkDlg->CheckMsg(pmpack);
}
void CNet::SendMsg(mpack &Msgpack)
{
	int i=0;
	for(;i<m_UsedCon.GetSize();i++){
		TRY
		{
			SendMsgs(Msgpack,m_UsedCon[i]);
		}
		CATCH(CFileException, e)
		{
			delete m_UsedCon[i];
			m_UsedCon.RemoveAt(i);
			i--;
		}
		END_CATCH
	}
}
void CNet::ConBegin()
{
	UINT PortAddress=701;
	while(!BuildListen(PortAddress++));
	m_pPictalkDlg->m_pUser->m_nPort=PortAddress-1;
	m_pPictalkDlg->Connected();
	m_bReady=TRUE;
/*	if(m_Willcon.GetSize()){
		int i;
		for(i=0;i<m_Willcon.GetSize();i++){
			CClientSocket * pSocket=new CClientSocket();
			pSocket->Create();
			m_wUsedCon.Add(pSocket);
		}
		AfxBeginThread(ConnectThreadProc,m_pPictalkDlg->GetSafeHwnd(),THREAD_PRIORITY_NORMAL);
	}*/
}
void CNet::NewLink(CClientSocket *pSocket)
{
	m_UsedCon.Add(pSocket);
	if(m_bMain){
		mpack Msgpack;
/*		POSITION pos,pos2;
		
		for(pos=m_pPictalkDlg->m_MapUser.GetStartPosition();pos!=NULL;)
		{
			pos2=pos;
			Msgpack.create();
			pos=m_pPictalkDlg->UserInfo(Msgpack,pos);
			SendMsgs(Msgpack,pSocket);
		}*/
		int i;
		UINT uID=1;
		CUserInfo * tempUser;
		for(i=0;i<m_pPictalkDlg->m_MapUser.GetCount();i++){
			while(!m_pPictalkDlg->m_MapUser.Lookup(uID++,tempUser));
			tempUser->MsgCreate(Msgpack);
			Msgpack.create();
			Msgpack.m_MsgID=MID_ADDUSER;
			SendMsgs(Msgpack,pSocket);
		}

		Msgpack.create();
		Msgpack.m_MsgID=MID_USERID;
		Msgpack.m_ID=++m_pPictalkDlg->m_UserCount;
		SendMsgs(Msgpack,pSocket);
	}
}
BOOL CNet::BuildListen(UINT PortAddress)
{
	m_pListeningSocket=new CListeningSocket(this);
	if(m_pListeningSocket->Create(PortAddress))
	{
		if(!m_pListeningSocket->Listen(20))
		{
			AfxMessageBox("端口设置错误!",MB_OK);
			delete m_pListeningSocket;
			m_pListeningSocket=NULL;
			return FALSE;
		}
	}
	return TRUE;
}
CClientSocket * CNet::ConnectServer(LPCTSTR ServerName, UINT PortAddress)
{
	CClientSocket * pSocket=new CClientSocket(this);
	if(!(pSocket->Create()))
	{
		delete pSocket;
		AfxMessageBox("创建Socket失败!",MB_OK);
		return NULL;
	}
	if(!pSocket->Connect(ServerName,PortAddress))
	{
		delete pSocket;
		AfxMessageBox("请求连接服务器失败!",MB_OK);
		return NULL;
	}
	return pSocket;
}
void CNet::FetchMsg(CClientSocket *pSocket)
{
	mpack pmpack;
	TRY
	{
		do
		{
			GetMsg(*(pSocket->m_pArchiveIn),pmpack);
			if(pmpack.m_SenderID==m_pPictalkDlg->m_pUser->m_ID)
				return;
			if(m_bReady)
				if(m_pPictalkDlg->IsUser(pmpack.m_SenderID)){
					if(!m_pPictalkDlg->m_MapUser[pmpack.m_SenderID]->Check(pmpack.m_num))
						return;
				}
				else
					if(pmpack.m_MsgID!=MID_ADDUSER)
						return;
			pSocket->CheckMsg(pmpack);
		}while(!(pSocket->m_pArchiveIn->IsBufferEmpty()));
	}
	CATCH(CFileException, e)
	{
		AfxMessageBox("网络异常",MB_OK);
		m_pPictalkDlg->StopLink();
	}
	END_CATCH
}
UINT ConnectThreadProc(LPVOID pParam)
{
	int i;
	for(i=0;i<m_Willcon.GetSize();i++){
/*		CAsyncSocket * pSocket=new CAsyncSocket();
		if(!(pSocket->Create()))
		{
			delete pSocket;
			continue;
		}*/
		if(!m_wUsedCon[i]->Connect(m_Willcon[i].m_IP,m_Willcon[i].m_nPort))
		{
//			delete pSocket;
			delete m_wUsedCon[i];
			m_wUsedCon.RemoveAt(i);
			continue;
		}
		m_UNusedCon.Add(m_wUsedCon[i]->m_hSocket);
	}
	::PostMessage((HWND)pParam,ID_CONNECTOVER,0,0);
	return 0;
}
void CNet::ConnectOver()
{
	int i;
	for(i=0;i<m_UNusedCon.GetSize();i++){
		CClientSocket * pSocket=new CClientSocket(this);
		if(pSocket->Attach(m_UNusedCon[i])){
			pSocket->Init();
			m_UsedCon.Add(pSocket);
		}
	}
	m_UNusedCon.RemoveAll();
}

⌨️ 快捷键说明

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