📄 ~chatclientdoc.~cpp
字号:
// ChatClientDoc.cpp : implementation of the CChatClientDoc class
//
#include "stdafx.h"
#include "ChatClient.h"
#include "ChatClientView.h"
#include "math.h"
#include "ChatClientDoc.h"
#include "CntrItem.h"
#include "Packet.h"
//#include "DASOCKET.H"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CChatClientDoc
IMPLEMENT_DYNCREATE(CChatClientDoc, CRichEditDoc)
BEGIN_MESSAGE_MAP(CChatClientDoc, CRichEditDoc)
//{{AFX_MSG_MAP(CChatClientDoc)
ON_COMMAND(ID_MENUITEM_SELROOM, OnMenuitemSelroom)
ON_COMMAND(ID_MENUITEM_LOGIN, OnMenuitemReLogin)
//}}AFX_MSG_MAP
// Enable default OLE container implementation
ON_UPDATE_COMMAND_UI(ID_OLE_EDIT_LINKS, CRichEditDoc::OnUpdateEditLinksMenu)
ON_COMMAND(ID_OLE_EDIT_LINKS, CRichEditDoc::OnEditLinks)
ON_UPDATE_COMMAND_UI_RANGE(ID_OLE_VERB_FIRST, ID_OLE_VERB_LAST, CRichEditDoc::OnUpdateObjectVerbMenu)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CChatClientDoc construction/destruction
CChatClientDoc::CChatClientDoc()
{
// TODO: add one-time construction code here
m_pSocket=new CMySocket();
m_pSocket->SetParent(this);
m_pSocket->Create(0,SOCK_DGRAM);
logDlg.m_pSocket=m_pSocket;
LogDo=1;
isAdm='0';
CString roomname=""; //进入房间名称,若未进入为空
hasLog=0;
hasgotoRoom=0;
if(logDlg.DoModal()!=IDOK)
{
exit(0);
};
ServerIP=logDlg.m_LogIP;
ServerPort=logDlg.m_logPort;
m_sUserName=logDlg.m_username;
}
CChatClientDoc::~CChatClientDoc()
{
}
BOOL CChatClientDoc::OnNewDocument()
{
if (!CRichEditDoc::OnNewDocument())
return FALSE;
// TODO: add reinitialization code here
// (SDI documents will reuse this document)
return TRUE;
}
CRichEditCntrItem* CChatClientDoc::CreateClientItem(REOBJECT* preo) const
{
// cast away constness of this
return new CChatClientCntrItem(preo, (CChatClientDoc*) this);
}
/////////////////////////////////////////////////////////////////////////////
// CChatClientDoc serialization
void CChatClientDoc::Serialize(CArchive& ar)
{
if (ar.IsStoring())
{
// TODO: add storing code here
}
else
{
// TODO: add loading code here
}
// Calling the base class CRichEditDoc enables serialization
// of the container document's COleClientItem objects.
// TODO: set CRichEditDoc::m_bRTF = FALSE if you are serializing as text
CRichEditDoc::Serialize(ar);
}
/////////////////////////////////////////////////////////////////////////////
// CChatClientDoc diagnostics
#ifdef _DEBUG
void CChatClientDoc::AssertValid() const
{
CRichEditDoc::AssertValid();
}
void CChatClientDoc::Dump(CDumpContext& dc) const
{
CRichEditDoc::Dump(dc);
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CChatClientDoc commands
void CChatClientDoc::ProcessPendingRead()
{
char buf[255]={""};
m_pSocket->Receive(buf,sizeof(buf),0);
CChatClientView* pView = (CChatClientView * )GetView();
//获得type
CString PacString = buf;
char cMark='@';
int result = PacString.Find(cMark);
if(!PacString.Compare(""))
return ;
CString type;
for(int i=0;i<result;i++)
{
type.Insert(i,PacString[i]);
}
CString pac;
//pView->MessageBox(PacString);
PacString.Delete(0,result+1);
int iType=StrtoInt(type);
switch(iType)
{
case 21: //登陆响应包
FromLogReq(PacString,pView);
break;
case 22: //请求注册响应包
FromRegisterReq(PacString);
break;
case 23: //请求房间信息响应包
ReqfromRoomInfo(PacString);
break;
case 24: //请求进入房间响应包
GotoRoom(PacString);
break;
case 25: //刷新房间客户信息包
RefreshUserList(PacString);
break;
case 26: //刷新对话信息
break;
case 28: //刷新聊天信息
break;
case 27: //对话包
ReceiveTalk(PacString);
break;
case 29: //个人信息包
break;
case 30: //请求踢人响应包
RecTickRequest(PacString);
break;
case 31: //留言包
break;
case 32: //管理员包
SetAdmer(PacString);
break;
default:
break;
//pView->MessageBox();
}
}
int CChatClientDoc::StrtoInt(CString str)
{
int result=0;
int len=str.GetLength();
for(int i=0;i<len;i++)
{
result+=(str[i]-48)*pow(10,len-i-1);
}
return result;
}
CChatClientDoc::SendMessage()
{
CString pac = msg.CMessageToNetPac();
m_pSocket->SendTo(pac,pac.GetLength(),ServerPort,ServerIP,NULL);
}
CChatClientDoc::ReceiveMessage()
{
}
//DEL CChatClientDoc::ConnectSocket(LPCTSTR lpszHandle, LPCTSTR lpszAddress, LPCTSTR m_strImage, UINT nPort)
//DEL {
//DEL m_pSocket->Connect("127.0.0.1",1000);
//DEL }
void CChatClientDoc::FromLogReq(CString cLogPac,CChatClientView * pView)
{
CPacket packet;
char mark='@';
CString cFlag=packet.GetNextCon(cLogPac,mark);
if(cFlag=='0')
{
pView->MessageBox("登陆失败!请重试!");
}
else if(!cFlag.Compare("2"))
{
pView->MessageBox("此用户已经有人上了!");
}
else if(!cFlag.Compare("1"))
{
hasLog=1;
pView->MessageBox("登陆成功!");
CString pac="3@";
ServerPort=logDlg.m_logPort;
ServerIP=logDlg.m_LogIP;
m_pSocket->SendTo(pac,pac.GetLength(),ServerPort,ServerIP,NULL);
}
else
{
pView->MessageBox("不能登陆!");
}
}
void CChatClientDoc::DisplayMsg(LPCTSTR lpszText)
{
CChatClientView* pView = (CChatClientView*)GetView();
if(pView != NULL)
pView->TextOut(lpszText, RGB(128,0,0));
}
void CChatClientDoc::DisplayRecMsg(int type, CString from, CString to,
BOOL sec, CString str, COLORREF clr)
{
CChatClientView* pView = (CChatClientView*)GetView();
if(type == -7){
pView->TextOut(_T("系统:聊天室掌门人以一招'佛山无影脚',将"),RGB(0,0,0));
if(from == m_sUserName){
pView->TextOut(_T("你"),RGB(0,0,255));
}
else
pView->TextOut(from,RGB(0,0,255));
pView->TextOut(_T("踢出门外\r\n"),RGB(0,0,0));
}
if(type == -5){
return;
}
if(type == -3){
pView->TextOut(_T("系统:服务器已关闭!"),RGB(0,0,0));
return;
}
if(type == -1 ){
pView->TextOut(from, RGB(0,0,255));
pView->TextOut(_TEXT("风尘仆仆地推门而入\r\n"),RGB(255,0,0));
}
if(type == -2){
pView->TextOut(from, RGB(0,0,255));
pView->TextOut(_TEXT("静静地离开了,孤单的背影显得格外潇洒\r\n"),RGB(255,0,0));
}
if(type >= 0){
if(from == m_sUserName || to == m_sUserName || sec == FALSE || to =="所有人")
talk(type, from, to, str, clr);
}
}
void CChatClientDoc::talk(CString type, CString form, CString to,
CString str, COLORREF clr)
{
CChatClientView* pView = (CChatClientView*)GetView();
pView->TextOut((LPCTSTR)form, RGB(0,0,255));
pView->TextOut(" 对 ",RGB(0,0,255));
//pView->TextOut((LPCTSTR)first, RGB(0,0,0));
pView->TextOut((LPCTSTR)to, RGB(0,0,255));
pView->TextOut((LPCTSTR)type, RGB(0,0,255));
pView->TextOut("地说:", RGB(0,0,255));
//pView->TextOut((LPCTSTR)second, RGB(0,0,0));
pView->TextOut((LPCTSTR)str,clr);
pView->TextOut((LPCTSTR)"\r\n",clr);
}
void CChatClientDoc::ReqfromRoomInfo(CString PacString)
{
if(LogDo==1)
{
logDlg.EndDialog(IDOK);
LogDo=0;
}
roomSelDlg.username=logDlg.m_username;
roomSelDlg.IP=logDlg.m_LogIP;
roomSelDlg.port=logDlg.m_logPort;
roomSelDlg.m_pSocket=m_pSocket;
roomSelDlg.CSetRoomInfo(PacString);
roomSelDlg.DoModal();
}
void CChatClientDoc::GotoRoom(CString pac)
{
CChatClientView* pView = (CChatClientView*)GetView();
CPacket packet;
if(!pac.Compare(""))
return ;
char cMark='@';
CString flag=packet.GetNextCon(pac,cMark);
if(!flag.Compare("0"))
{
CString course=packet.GetNextCon(pac,cMark);
if(!course.Compare("1"))
pView->MessageBox("很抱歉,你已被此房间管理员踢出!");
else if(!course.Compare("2"))
pView->MessageBox("由于你还未登陆,不能够进入房间!");
else if(!course.Compare("3"))
pView->MessageBox("对不起,你的用户号正在使用!");
else if(!course.Compare("4"))
pView->MessageBox("人员已满!请选择别的房间!");
}
else
{
isAdm=packet.GetNextCon(pac,cMark)[0];
roomSelDlg.EndDialog(IDOK);
hasgotoRoom=1;
roomname=roomSelDlg.roomname;
if(pView!=NULL)
{
pView->SetWindowText("");
pView->CanTick();
InitialSelectedRoom();
}
}
}
void CChatClientDoc::ReceiveTalk(CString pac)
{
//发送着+方式+接收者+内容+颜色
char mark='@';
CPacket packet;
CString sender;
CString type;
CString receiver;
CString str;
int color;
CChatClientView* pView = (CChatClientView*)GetView();
//
//pView->MessageBox(pac);
CString flag=packet.GetNextCon(pac,mark);
if(flag.Compare("0"))
{
if(!flag.Compare("-1"))
{
CString loger=packet.GetNextCon(pac,mark);
pView->TextOut("系统公告:欢迎"+loger+"进入本聊天室!\n",RGB(0,255,0));
}
else if(!flag.Compare("-2")) //退出
{
CString loger=packet.GetNextCon(pac,mark);
pView->TextOut("系统公告:"+loger+"已退出本聊天室!\n",RGB(0,255,0));
if(!m_sUserName.Compare(loger))
{
if(isAdm=='1')
{
isAdm='0';
pView->CanTick();
}
CString pac="3@";
m_pSocket->SendTo(pac,pac.GetLength(),ServerPort,ServerIP,NULL);
}
}
else if(!flag.Compare("-3")) //踢人
{
//CString course=packet.GetNextCon(pac,mark);
CString loger=packet.GetNextCon(pac,mark);
CString course="1";
DisplayTickCourse(loger,course);
if(!m_sUserName.Compare(loger))
{
if(isAdm=='1')
{
isAdm='0';
pView->CanTick();
}
pView->MessageBox("你已被管理员踢出!");
CString pac="3@";
m_pSocket->SendTo(pac,pac.GetLength(),ServerPort,ServerIP,NULL);
}
}
}
else
{
receiver=packet.GetNextCon(pac,mark);
type=packet.GetNextCon(pac,mark);
sender=packet.GetNextCon(pac,mark);
str=packet.GetNextCon(pac,mark);
CString scolor=packet.GetNextCon(pac,mark);
color=StrtoInt(scolor);
//pView->MessageBox(pac);
talk(type, sender, receiver, str, color);
}
}
void CChatClientDoc::RefreshUserList(CString list)
{
CChatClientView* pView = (CChatClientView*)GetView();
CPacket packet;
char mark='@';
pView->RefreshUerNum(packet.GetNextCon(list,mark));
CString name;
name=packet.GetNextCon(list,mark);
int map=0;
map=StrtoInt(packet.GetNextCon(list,mark));
int i=0;
while(name.Compare(""))
{
pView->AddUserList(i,name,map);
name=packet.GetNextCon(list,mark);
map=StrtoInt(packet.GetNextCon(list,mark));
i++;
}
}
void CChatClientDoc::ExitRoom()
{
hasgotoRoom=0;
CString pac = "7@";
pac+=m_sUserName;
pac+="@";
m_pSocket->SendTo(pac,pac.GetLength(),ServerPort,ServerIP,NULL);
}
/********执行踢用户按钮**********/
void CChatClientDoc::TickUser(CString name, int course)
{
CString pac="9@";
pac+=m_sUserName; //用户名
pac+="@";
pac+=name; //被踢用户
pac+="@";
CString s;
s.Format("%d",course);
pac+=s; //原因
pac+="@";
m_pSocket->SendTo(pac,pac.GetLength(),ServerPort,ServerIP,NULL);
}
void CChatClientDoc::DisplayTickCourse(CString name,CString course)
{
CChatClientView* pView = (CChatClientView*)GetView();
if(!course.Compare("1"))
{
pView->TextOut(name+"已被踢出本聊天室!\n",RGB(0,0,255));
}
else if(course.Compare("2"))
{
}
}
void CChatClientDoc::SetAdmer(CString adm)
{
CChatClientView* pView = (CChatClientView*)GetView();
isAdm='1';
pView->CanTick();
return ;
}
/******** 选择房间菜单*********/
void CChatClientDoc::OnMenuitemSelroom()
{
// TODO: Add your command handler code here
CChatClientView* pView = (CChatClientView*)GetView();
if(hasLog)
{
CString pac="3@";
m_pSocket->SendTo(pac,pac.GetLength(),ServerPort,ServerIP,NULL);
}
else
{
pView->MessageBox("你首先要登陆!");
}
}
/*********** 初始化选择的房间 *********/
void CChatClientDoc::InitialSelectedRoom()
{
if(!roomname.Compare(""))
return ;
else
{
CChatClientView* pView = (CChatClientView*)GetView();
pView ->InitialSelectedRoom(m_sUserName,roomname);
}
}
void CChatClientDoc::RecTickRequest(CString pac)
{
CChatClientView* pView = (CChatClientView*)GetView();
if(!pac.Compare("1"))
pView->MessageBox("成功踢出用户!");
else
pView->MessageBox("不能踢出用户!");
}
void CChatClientDoc::FromRegisterReq(CString pac)
{
CChatClientView* pView = (CChatClientView*)GetView();
if(!pac.Compare("1"))
{
pView->MessageBox("成功注册!");
logDlg.RegisterStatedo();
}
else
{
pView->MessageBox("已有用户名,注册失败!");
}
}
CChatClientDoc::QuitChatRoom()
{
/*if(hasgotoRoom)
{
ExitRoom();
}*/
if(hasLog)
{
CString pac="11@";
pac+=m_sUserName; //用户名
pac+="@";
m_pSocket->SendTo(pac,pac.GetLength(),ServerPort,ServerIP,NULL);
}
}
void CChatClientDoc::OnMenuitemReLogin()
{
// TODO: Add your command handler code here
logDlg.m_pSocket=m_pSocket;
LogDo=1;
isAdm='0';
CString roomname=""; //进入房间名称,若未进入为空
hasLog=0;
QuitChatRoom();
if(logDlg.DoModal()!=IDOK)
{
exit(0);
};
ServerIP=logDlg.m_LogIP;
ServerPort=logDlg.m_logPort;
m_sUserName=logDlg.m_username;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -