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

📄 recvdata.cpp

📁 仿QQ的聊天程序vc++/mfc来学习的简易聊天室代码功能较全
💻 CPP
📖 第 1 页 / 共 3 页
字号:
						CNoticeMsg msg5;
						if(pUsers[FriendIndex].State==1)
							msg5.index=SOMEONE_ONLINE;
						else
							msg5.index=SOMEONE_ONHIDE;
						msg5.MyId=pUsers[FriendIndex].Id;
						msg5.IP=pUsers[FriendIndex].IP;
						msg5.Port=pUsers[FriendIndex].Port;
						msg5.tarIP=pData->tarIP;
						msg5.nPort=pData->nPort;
						pSocket->SendData(&msg5);
					}
					else
					{
						CString mysql;							
						
						mysql.Format("insert into offmsg(SenderId,RecvId,mIndex,RecvTime,Msg) values(%d,%d,%d,%d,'%s')",
							msg4.MyId,msg4.FriendId,msg4.index,msg4.Time,msg4.Msg);
						try
						{
							pDatabase->ExecuteSQL(mysql);
						}
						catch(CDBException e)
						{
#ifdef _DEBUG
							pView->ShowMessage(e.m_strError);
#endif
						}
					}
				}
				else
				{
					// this friend is already in your friends
					msg3.Value=3;
					if(!pSocket->SendData(&msg3))
						goto proc_end;
				}					
			}			

			CString str;
			str.Format("ID: %d want add ID: %d as friend!",msg.MyId,msg.FriendId);
#ifdef _DEBUG			
			pView->ShowMessage(str);			
#endif
			break;
		}
	case APPLY_SHOW_ONLINE:
		{
			//查看在线的人
			CMsg3 msg;
			CopyDataBuf(&msg,pData);
			if(!msg.LoadFromBuf())break;
	
			int mIndex=msg.MyId-UserIdRadix;
			if(mIndex<0||mIndex>=pApp->m_nTotalUserNumber||!pUsers[mIndex].Id)break;

			int offsize=msg.Value;
			int count=0;
			int mOff=offsize*PersonNumEveryTime;
			if(mOff>=pApp->m_nTotalUserNumber) break;
			for(int i=0;i<pApp->m_nTotalUserNumber&&count<mOff;i++)
				if(pUsers[i].State)count++;
			if(count==mOff)
			{
				CShowOnlinePeople msg1;
				msg1.tarIP=msg.tarIP;
				msg1.nPort=msg.nPort;
				msg1.index=RECV_SHOW_ONLINE;
				msg1.wIndex=msg.wIndex;
				msg1.MyId=msg.MyId;
				for(int j=0;i<pApp->m_nTotalUserNumber&&j<PersonNumEveryTime;i++)
				{
					if(pUsers[i].State)
					{
						msg1.aPhotoId.Add(pUsers[i].PhotoId);
						msg1.aId.Add(pUsers[i].Id);
						msg1.aName.Add(pUsers[i].Name);
						msg1.aCity.Add(pUsers[i].City);
						j++;
					}
				}
				if(msg1.aId.GetSize()>0)
					pSocket->SendData(&msg1);
			}

#ifdef _DEBUG
			pView->ShowMessage("查看在线的人");			
#endif
			break;
		}
	case FIND_FRIEND_BY_ID:		
		{
			//用号码查找朋友
			CMsg1 msg;
			CopyDataBuf(&msg,pData);
			if(!msg.LoadFromBuf())break;
			
			int mIndex=msg.MyId-UserIdRadix;
			int fIndex=msg.FriendId-UserIdRadix;
			if(mIndex>=0&&mIndex<pApp->m_nTotalUserNumber&&fIndex>=0&&fIndex<pApp->m_nTotalUserNumber&&pUsers[fIndex].Id&&pUsers[mIndex].Id)  //fint it
			{
				//return this peron's detail
				CMsgPerson msg1;
				msg1.index=FOUND_FRIEND_BY_ID;
				msg1.wIndex=msg.wIndex;
				msg1.tarIP=msg.tarIP;
				msg1.nPort=msg.nPort;
				msg1.MyId=pUsers[fIndex].Id;
				msg1.Name=pUsers[fIndex].Name;
				msg1.PhotoId=pUsers[fIndex].PhotoId;
				msg1.City=pUsers[fIndex].City;

				CRecordset recordset(pDatabase);
				CString mysql;
				mysql.Format("select Sex,Age,Country,Province,City,canbeadd from Users where Id=%d",msg.FriendId);
				recordset.Open(AFX_DB_USE_DEFAULT_TYPE,mysql);
				if(recordset.GetRecordCount()==0)break;
				CDBVariant s1;
				recordset.GetFieldValue(short(0),s1);
				msg1.Sex=s1.m_chVal;
				recordset.GetFieldValue(1,msg1.Age);				
				recordset.GetFieldValue(2,msg1.Country);				
				recordset.GetFieldValue(3,msg1.Province);				
				recordset.GetFieldValue(4,msg1.City);								
				recordset.GetFieldValue(5,s1);
				msg1.Canbeadd=s1.m_chVal;

				recordset.Close();
				pSocket->SendData(&msg1);							
			}
			else
			{
				//return not found!
				CData msg1;
				msg1.index=ID_NOT_FOUND_BY_ID;
				msg1.wIndex=msg.wIndex;
				msg1.tarIP=msg.tarIP;
				msg1.nPort=msg.nPort;
				msg1.MyId=msg.MyId;
				pSocket->SendData(&msg1);
			}
#ifdef _DEBUG	
			pView->ShowMessage("用号码查找朋友");			
#endif
			break;
		}
	case FIND_FRIEND_BY_NAME:
		{
			//用姓名查找朋友
			CMsg4 msg;
			CopyDataBuf(&msg,pData);
			if(!msg.LoadFromBuf())break;
	
			int mIndex=msg.MyId-UserIdRadix;
			if(mIndex<0||mIndex>=pApp->m_nTotalUserNumber||!pUsers[mIndex].Id)break;

			CShowOnlinePeople msg1;			
			for(int i=0;i<pApp->m_nTotalUserNumber;i++)
			{
				if(pUsers[i].Name==msg.Msg)
				{
					msg1.aPhotoId.Add(pUsers[i].PhotoId);
					msg1.aId.Add(pUsers[i].Id);
					msg1.aName.Add(pUsers[i].Name);
					msg1.aCity.Add(pUsers[i].City);					
				}
			}
			if(msg1.aId.GetSize()>0)
			{
				msg1.tarIP=msg.tarIP;
				msg1.nPort=msg.nPort;
				msg1.MyId=msg.MyId;
				msg1.index=FOUND_FRIEND_BY_NAME;
				msg1.wIndex=msg.wIndex;
				pSocket->SendData(&msg1);
			}
			else
			{
				CData msg2;
				msg2.tarIP=msg.tarIP;
				msg2.nPort=msg.nPort;
				msg2.MyId=msg.MyId;
				msg2.index=NAME_NOT_FOUND_BY_NAME;
				msg2.wIndex=msg.wIndex;
				pSocket->SendData(&msg2);
			}
					
#ifdef _DEBUG
			pView->ShowMessage("用姓名查找朋友");			
#endif
			break;
		}
	case DELETE_A_FRIEND:
		{
			// 删除一个好友
			CMsg1 msg;
			CopyDataBuf(&msg,pData);
			if(!msg.LoadFromBuf())break;
		
			int MyIndex=msg.MyId-UserIdRadix;
			int FriendIndex=msg.FriendId-UserIdRadix;

			//if it is not valid user.
			if(MyIndex<0||MyIndex>=pApp->m_nTotalUserNumber||FriendIndex<0||FriendIndex>=pApp->m_nTotalUserNumber)
				break;
			if(!pUsers[MyIndex].Id||!pUsers[FriendIndex].Id)break;
			
			// delete the friendid user				
			CString mysql;
			mysql.Format("delete from friends where myid=%d and friendid=%d",msg.MyId,msg.FriendId);
			pDatabase->ExecuteSQL(mysql);			

			CString str;
			str.Format("ID: %d want 删除一个好友ID: %d",msg.MyId,msg.FriendId);
#ifdef _DEBUG
			pView->ShowMessage(str);			
#endif
			break;			
		}
	case DELETE_SELF_IN_FRIEND:
		{
			//选择在某人的好友中删除自己
			CMsg1 msg;
			CopyDataBuf(&msg,pData);
			if(!msg.LoadFromBuf())break;

			int MyIndex=msg.MyId-UserIdRadix;
			int FriendIndex=msg.FriendId-UserIdRadix;

			//if it is not valid user.
			if(MyIndex<0||MyIndex>=pApp->m_nTotalUserNumber||FriendIndex<0||FriendIndex>=pApp->m_nTotalUserNumber)
				break;
			if(!pUsers[MyIndex].Id||!pUsers[FriendIndex].Id)break;

			CString mysql;
			mysql.Format("delete from friends where myid=%d and friendid=%d",msg.FriendId,msg.MyId);
			pDatabase->ExecuteSQL(mysql);

#ifdef _DEBUG
			pView->ShowMessage("选择在某人的好友中删除自己");			
#endif
			break;
		}
	case CHANGE_PERSONAL_INFO:
		{
			CMsgChangePI msg;
			CopyDataBuf(&msg,pData);
			if(!msg.LoadFromBuf())break;
			
			int mIndex=msg.MyId-UserIdRadix;
			if(mIndex<0||mIndex>=pApp->m_nTotalUserNumber||!pUsers[mIndex].Id)break;
			
			if(!msg.Mask)break;
			
			CString strHead,strWhere,str,Mysql;			
			strHead.Format("update users set ");
			strWhere.Format(" where id=%d",msg.MyId);
			
			if(msg.Mask&CMsgChangePI::NAME)
				str.Format("Name='%s',",msg.Name),strHead+=str;
			if(msg.Mask&CMsgChangePI::SEX)
				str.Format("Sex='%d',",msg.Sex),strHead+=str;
			if(msg.Mask&CMsgChangePI::AGE)
				str.Format("Age='%s',",msg.Age),strHead+=str;
			if(msg.Mask&CMsgChangePI::CANBEADD)
				str.Format("Canbeadd=%d,",msg.Canbeadd),strHead+=str;
			if(msg.Mask&CMsgChangePI::COUNTRY)
				str.Format("Country=%d,",msg.Country),strHead+=str;
			if(msg.Mask&CMsgChangePI::PROVINCE)
				str.Format("Province='%s',",msg.Province),strHead+=str;
			if(msg.Mask&CMsgChangePI::CITY)
				str.Format("City='%s',",msg.City),strHead+=str;
			if(msg.Mask&CMsgChangePI::PHOTOID)
				str.Format("Photoid=%d,",msg.PhotoId),strHead+=str;
			
			if(strHead.GetAt(strHead.GetLength()-1)==',')
				Mysql=strHead.Left(strHead.GetLength()-1);
			else Mysql=strHead;
			
			Mysql+=strWhere;
			pDatabase->ExecuteSQL(Mysql);
			
			// change in pUsers array
			if(msg.Mask&CMsgChangePI::NAME)
				pUsers[mIndex].Name=msg.Name;
			if(msg.Mask&CMsgChangePI::CANBEADD)
				pUsers[mIndex].CanbeAdd=msg.Canbeadd;
			if(msg.Mask&CMsgChangePI::CITY)
				pUsers[mIndex].City=msg.City;
			if(msg.Mask&CMsgChangePI::PHOTOID)
				pUsers[mIndex].PhotoId=msg.PhotoId;				
			
#ifdef _DEBUG
			pView->ShowMessage("CHANGE_PERSONAL_INFORMATION");			
#endif
			break;
		}
	case CHANGE_PASSWORD:
		{
			CMsgModifyPwd msg;
			CopyDataBuf(&msg,pData);
			if(!msg.LoadFromBuf())break;
			
			int mIndex=msg.MyId-UserIdRadix;
			if(mIndex>=0&&mIndex<pApp->m_nTotalUserNumber&&pUsers[mIndex].Id)
			{
				CRecordset rs(pDatabase);
				CString mysql;
				mysql.Format("select Password from users where id=%d",msg.MyId);
				rs.Open(AFX_DB_USE_DEFAULT_TYPE,mysql);
				if(rs.GetRecordCount==0)break;
				CString pwd;
				rs.GetFieldValue(short(0),pwd);
				if(msg.OldPwd==pwd)
				{
					CString sql1;
					sql1.Format("update users set Password=%s where id=%d",msg.NewPwd,msg.MyId);
					pDatabase->ExecuteSQL(sql1);
				}
			}		
			
			CString str;
			str.Format("ID: %d 修改个人信息",msg.MyId);
#ifdef _DEBUG
			pView->ShowMessage(str);
#endif
			break;
		}
	case APPLY_ID_LOGIN:
		{
			CMsgPerson msg;
			CopyDataBuf(&msg,pData);
			if(!msg.LoadFromBuf())break;		
			
			CString mysql; 
			int nAppID=0;

			pApp->m_criticalMaxUserId.Lock();
			pApp->m_nTotalUserNumber++;
			pApp->m_nMaxUserId++;
			nAppID=pApp->m_nMaxUserId;
			pApp->m_criticalMaxUserId.Unlock();

			mysql.Format("insert into users(id,photoid,password,name,sex,age,canbeadd,country,province,city) values(%d,%d,'%s','%s',%d,'%s',%d,'%s','%s','%s')",
				nAppID,msg.PhotoId,msg.Password,msg.Name,msg.Sex,msg.Age,msg.Canbeadd,msg.Country,msg.Province,msg.City);
			try
			{
				pDatabase->ExecuteSQL(mysql);
			}
			catch(CDBException e)
			{
				AfxMessageBox(e.m_strError);
			}
			
			CData msg1;
			msg1.index=APPLY_ID_OK;
			msg1.MyId=nAppID;
			msg1.tarIP=msg.tarIP;
			msg1.nPort=msg.nPort;
			pSocket->SendData(&msg1);			
			
			::InterlockedIncrement((LPLONG)&pApp->m_nNumberOnline);

			pUsers[msg1.MyId-UserIdRadix].Id=msg1.MyId;
			pUsers[msg1.MyId-UserIdRadix].Name=msg.Name;
			pUsers[msg1.MyId-UserIdRadix].CanbeAdd=msg.Canbeadd;
			pUsers[msg1.MyId-UserIdRadix].PhotoId=msg.PhotoId;
			pUsers[msg1.MyId-UserIdRadix].City=msg.City;
			pUsers[msg1.MyId-UserIdRadix].IP=msg.tarIP;
			pUsers[msg1.MyId-UserIdRadix].Port=msg.nPort;
			pUsers[msg1.MyId-UserIdRadix].State=1;				
			
			CString str;
			str.Format("用户:%s 申请帐号成功,ID:%d",msg.Name,msg1.MyId);
#ifdef _DEBUG
			pView->ShowMessage(str);			
#endif
			
			break;
		}
	} 
proc_end:
	if(pData)
	{
		delete pData;
		pData=NULL;
	}

	return 0;
}

⌨️ 快捷键说明

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