📄 recvdata.cpp
字号:
recordset.Close();
}
CString str;
in_addr tIP;
tIP.S_un.S_addr=pData->tarIP;
str.Format("ID: %d 上线(正常登陆),IP: %s,Port: %d",pData->MyId,inet_ntoa(tIP),pData->nPort);
#ifdef _DEBUG
pView->ShowMessage(str);
#endif
}
else
{
CString str;
str.Format("ID: %d A Invalid ID",pData->MyId);
#ifdef _DEBUG
pView->ShowMessage(str);
#endif
}
break;
}
case OFFLINE:
{
int pIndex=pData->MyId-UserIdRadix;
if(pIndex>=0&&pIndex<pApp->m_nTotalUserNumber) // true means it esxit
{
if(pUsers[pIndex].State)
{
pUsers[pIndex].State=0;
::InterlockedDecrement((LPLONG)&pApp->m_nNumberOnline);
//找出把我当作好友的人,并发OFFLINE消息
CRecordset recordset(pDatabase);
CString mysql;
mysql.Format("select MyId from Friends where FriendId=%d",pUsers[pIndex].Id);
recordset.Open(AFX_DB_USE_DEFAULT_TYPE,mysql);
CDBVariant value;
CData msg;
msg.index=SOMEONE_OFFLINE;
msg.MyId=pData->MyId;
while(!recordset.IsEOF())
{
recordset.GetFieldValue(short(0),value);
int pi=value.m_lVal-UserIdRadix;
if(pUsers[pi].State)
{
msg.tarIP=pUsers[pi].IP;
msg.nPort=pUsers[pi].Port;
pSocket->SendData(&msg);
}
recordset.MoveNext();
}
recordset.Close();
}
CString str;
str.Format("ID: %d 下线",pData->MyId);
#ifdef _DEBUG
pView->ShowMessage(str);
#endif
}
else
{
CString str;
str.Format("ID: %d A Invalid ID",pData->MyId);
#ifdef _DEBUG
pView->ShowMessage(str);
#endif
}
break;
}
case SEND_MSG_TO_FRIEND:
case REFUSE_AS_FRIEND:
case ACCEPT_AS_FRIEND:
case FRIEND_IDENTITY_VALIDATE:
{
// 用户给朋友发的离线消息
CMsg1 msg;
CopyDataBuf(&msg,pData);
if(!msg.LoadFromBuf())
{
#ifdef _DEBUG
pView->ShowMessage("msg.LoadFromBuf error!");
#endif
break;
}
int pIndex=msg.FriendId-UserIdRadix;
int mIndex=msg.MyId-UserIdRadix;
if(pIndex>=0&&pIndex<pApp->m_nTotalUserNumber&&mIndex>=0&&mIndex<pApp->m_nTotalUserNumber) //It's fit user
{
if(index==ACCEPT_AS_FRIEND)
{
CRecordset rs(pDatabase);
CString sql1;
sql1.Format("select friendid from friends where myid=%d and friendid=%d",msg.FriendId,msg.MyId);
rs.Open(AFX_DB_USE_DEFAULT_TYPE,sql1);
int count=rs.GetRecordCount();
rs.Close();
if(count==0)
{
CString mysql;
mysql.Format("insert into Friends(Myid,FriendId) values(%d,%d)",msg.FriendId,msg.MyId);
pDatabase->ExecuteSQL(mysql);
}
else break;
}
// Is frined online?
BOOL SendOK=FALSE;
if(pUsers[pIndex].State)
{
msg.tarIP=pUsers[pIndex].IP;
msg.nPort=pUsers[pIndex].Port;
if(pSocket->SendData(&msg))
SendOK=TRUE;
}
// Save it to database
if(!SendOK)
{
CString mysql;
CTime t1=CTime::GetCurrentTime();
mysql.Format("insert into offmsg(SenderId,RecvId,mIndex,RecvTime,Msg) values(%d,%d,%d,%d,'%s')",
msg.MyId,msg.FriendId,msg.index,t1,msg.Msg);
try
{
pDatabase->ExecuteSQL(mysql);
}
catch(CDBException e)
{
#ifdef _DEBUG
pView->ShowMessage(e.m_strError);
#endif
}
}
CString str;
str.Format("ID: %d To ID: %d 用户给朋友发的离线消息",msg.MyId,msg.FriendId);
#ifdef _DEBUG
pView->ShowMessage(str);
#endif
}
else
{
#ifdef _DEBUG
pView->ShowMessage("A Invalid Id");
#endif
}
break;
}
case MULTI_SEND_MSG:
{
// 用户给多个朋友发的离线消息
CMsg2 msg;
CopyDataBuf(&msg,pData);
if(!msg.LoadFromBuf())
{
TRACE0("msg.LoadFromBuf error!\n");
break;
}
int mIndex=msg.MyId-UserIdRadix;
if(mIndex>=0&&mIndex<pApp->m_nTotalUserNumber) //It's fit user
{
for(int i=0;i<msg.aFriendId.GetSize();i++)
{
int pIndex=msg.aFriendId.GetAt(i)-UserIdRadix;
if(pIndex<0||pIndex>=pApp->m_nTotalUserNumber)continue;
// Is This frined online?
BOOL SendOK=FALSE;
if(pUsers[pIndex].State)
{
CMsg1 msg1;
msg1.index=SEND_MSG_TO_FRIEND;
msg1.Msg=msg.Msg;
msg1.MyId=msg.MyId;
msg1.Time=msg.Time;
msg1.FriendId=msg.aFriendId.GetAt(i);
msg1.tarIP=pUsers[pIndex].IP;
msg1.nPort=pUsers[pIndex].Port;
if(pSocket->SendData(&msg1))
SendOK=TRUE;
}
if(!SendOK)
{
// Save it to database
CString mysql;
CTime t1=CTime::GetCurrentTime();
mysql.Format("insert into offmsg(SenderId,RecvId,mIndex,RecvTime,Msg) values(%d,%d,%d,%d,'%s')",
msg.MyId,msg.aFriendId.GetAt(i),SEND_MSG_TO_FRIEND,t1,msg.Msg);
try
{
pDatabase->ExecuteSQL(mysql);
}
catch(CDBException e)
{
#ifdef _DEBUG
pView->ShowMessage(e.m_strError);
#endif
}
}
}
CString str;
str.Format("ID: %d 用户给多个朋友发的离线消息",msg.MyId);
#ifdef _DEBUG
pView->ShowMessage(str);
#endif
}
break;
}
case SEND_BROADCAST:
{
// 发送广播消息
CMsg4 msg;
CopyDataBuf(&msg,pData);
if(!msg.LoadFromBuf())
{
TRACE0("msg.LoadFromBuf error!\n");
break;
}
int mIndex=msg.MyId-UserIdRadix;
if(mIndex>=0&&mIndex<pApp->m_nTotalUserNumber)
{
if(msg.BroadcastPwd==pApp->m_strBroadcastPwd)
{
CString mysql;
CTime t1=CTime::GetCurrentTime();
mysql.Format("insert into broadcast(SenderId,SendTime,Msg) values(%d,%d,'%s')",msg.MyId,t1,msg.Msg);
pDatabase->ExecuteSQL(mysql);
int CurMsgId=0;
CRecordset rs(pDatabase);
rs.Open(AFX_DB_USE_DEFAULT_TYPE,"select last(MsgId) from broadcast");
CDBVariant v1;
rs.GetFieldValue(short(0),v1);
rs.Close();
CurMsgId=v1.m_lVal;
// Check for everyone ,send broadcast
CMsg1 msg1;
msg1.index=SYSTEM_BROADCAST;
msg1.MyId=msg.MyId;
msg1.Msg=msg.Msg;
msg1.Time=t1;
for(int i=0;i<pApp->m_nTotalUserNumber;i++)
{
if(pUsers[i].Id==0||pUsers[i].Id==msg.MyId)
continue;
BOOL SendOK=FALSE;
if(pUsers[i].State)
{
msg1.tarIP=pUsers[i].IP;
msg1.nPort=pUsers[i].Port;
if(pSocket->SendData(&msg1))
SendOK=TRUE;
}
if(!SendOK)
{
//Save it to Database
CString mysql;
mysql.Format("insert into OffBroadcast(RecvId,MsgId) values(%d,%d)",i+1+UserIdRadix,CurMsgId);
pDatabase->ExecuteSQL(mysql);
}
}
CString str;
str.Format("ID: %d 发送广播消息",msg.MyId);
#ifdef _DEBUG
pView->ShowMessage(str);
#endif
}
}
break;
}
case FRIEND_DETAIL:
{
// 好友详细信息
CMsg1 msg;
CopyDataBuf(&msg,pData);
if(!msg.LoadFromBuf())
{
TRACE0("msg.LoadFromBuf error!\n");
break;
}
TRACE1("Request Friend Detail %d\n",msg.FriendId);
int mIndex=msg.FriendId-UserIdRadix;
if(mIndex>=0&&mIndex<pApp->m_nTotalUserNumber&&pUsers[mIndex].Id)
{
CMsgPerson msg1;
msg1.index=RE_FRIEND_DETAIL;
msg1.wIndex=msg.wIndex;
msg1.tarIP=msg.tarIP;
msg1.nPort=msg.nPort;
msg1.MyId=pUsers[mIndex].Id;
msg1.Name=pUsers[mIndex].Name;
msg1.PhotoId=pUsers[mIndex].PhotoId;
msg1.City=pUsers[mIndex].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();
if(pSocket->SendData(&msg1))
{
CString str;
str.Format("ID: %d FriendID: %d 好友详细信息",msg.MyId,msg.FriendId);
#ifdef _DEBUG
pView->ShowMessage(str);
#endif
}
}
break;
}
case ADD_AS_FRIEND:
{
// 加为好友请求
CMsg1 msg;
CopyDataBuf(&msg,pData);
if(!msg.LoadFromBuf())
{
TRACE0("msg.LoadFromBuf error!\n");
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;
// find the friendid user
CMsg3 msg3;
msg3.tarIP=msg.tarIP;
msg3.nPort=msg.nPort;
msg3.index=RE_ADD_AS_FRIEND;
msg3.wIndex=msg.wIndex;
if(pUsers[FriendIndex].CanbeAdd!=1)
{
// 拒绝任何人加入 or 需要身份验证
msg3.Value=pUsers[FriendIndex].CanbeAdd;
if(!pSocket->SendData(&msg3))
break;
}
else
{
// add it be my friend
CRecordset rs(pDatabase);
CString sql1;
sql1.Format("select friendid from friends where myid=%d and friendid=%d",msg.MyId,msg.FriendId);
rs.Open(AFX_DB_USE_DEFAULT_TYPE,sql1);
int count=rs.GetRecordCount();
rs.Close();
if(msg.MyId!=msg.FriendId&&count==0)
{
CString mysql;
mysql.Format("insert into Friends(Myid,FriendId) values(%d,%d)",msg.MyId,msg.FriendId);
pDatabase->ExecuteSQL(mysql);
msg3.Value=1;
//成功加入
pSocket->SendData(&msg3);
//通知被加入的人
CMsg1 msg4;
msg4.tarIP=pUsers[FriendIndex].IP;
msg4.nPort=pUsers[FriendIndex].Port;
msg4.index=BE_ADDED_AS_FRIEND;
msg4.MyId=msg.MyId;
msg4.FriendId=msg.FriendId;
msg4.Msg.Format("%d 把你加为好友",msg.MyId);
msg4.Time=CTime::GetCurrentTime();
if(pUsers[FriendIndex].State&&pSocket->SendData(&msg4))
{
Sleep(500);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -