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

📄 recvdata.cpp

📁 一C2S的通讯程序
💻 CPP
📖 第 1 页 / 共 3 页
字号:

			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.aDepartment.Add(pUsers[i].Department);
						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.Department=pUsers[fIndex].Department;

				CRecordset recordset(pDatabase);
				CString mysql;
				mysql.Format("select Sex,Age,Email,Homepage,Address,Phone,fax,description,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.Email);				
				recordset.GetFieldValue(3,msg1.Homepage);				
				recordset.GetFieldValue(4,msg1.Address);				
				recordset.GetFieldValue(5,msg1.Phone);				
				recordset.GetFieldValue(6,msg1.Fax);				
				recordset.GetFieldValue(7,msg1.Description);				
				recordset.GetFieldValue(8,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.aDepartment.Add(pUsers[i].Department);					
				}
			}
			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::ADDRESS)
				str.Format("Address=%d,",msg.Address),strHead+=str;
			if(msg.Mask&CMsgChangePI::DEPARTMENT)
				str.Format("department='%s',",msg.Department),strHead+=str;
			if(msg.Mask&CMsgChangePI::DESCRIPTION)
				str.Format("Description='%s',",msg.Description),strHead+=str;
			if(msg.Mask&CMsgChangePI::EMAIL)
				str.Format("email='%s',",msg.Email),strHead+=str;
			if(msg.Mask&CMsgChangePI::FAX)
				str.Format("Fax='%s',",msg.Fax),strHead+=str;
			if(msg.Mask&CMsgChangePI::HOMEPAGE)
				str.Format("Homepage='%s',",msg.Homepage),strHead+=str;
			if(msg.Mask&CMsgChangePI::PHONE)
				str.Format("Phone='%s',",msg.Phone),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::DEPARTMENT)
				pUsers[mIndex].Department=msg.Department;
			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,email,homepage,address,phone,fax,department,description) values(%d,%d,'%s','%s',%d,'%s',%d,'%s','%s','%s','%s','%s','%s','%s')",
				nAppID,msg.PhotoId,msg.Password,msg.Name,msg.Sex,msg.Age,msg.Canbeadd,msg.Email,msg.Homepage,msg.Address,msg.Phone,msg.Fax,msg.Department,msg.Description);
			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].Department=msg.Department;
			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;
		}
	case HAVE_ID_LOGIN:
		{
			//使用已有号码进行登陆
			CMsgModifyPwd msg;
			CopyDataBuf(&msg,pData);
			if(!msg.LoadFromBuf())break;
			
			int mIndex=msg.MyId-UserIdRadix;
			
			CMsg3 msg1;
			msg1.index=RE_LOGIN_INFO;
			msg1.wIndex=msg.wIndex;
			msg1.MyId=msg.MyId;
			msg1.tarIP=msg.tarIP;
			msg1.nPort=msg.nPort;
			
			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)
				{
					msg1.Value=1;
					pSocket->SendData(&msg1);
				}
				else
				{
					msg1.Value=2;
					pSocket->SendData(&msg1);
				}
			}
			else
			{
				msg1.Value=0;
				pSocket->SendData(&msg1);
			}
			
			CString str;
			str.Format("ID: %d 使用已有ID进行登陆!",msg.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 + -