ClientSocket.cpp
上传用户:guangzhiyw
上传日期:2007-01-09
资源大小:495k
文件大小:4k
源码类别:

ICQ/即时通讯

开发平台:

Visual C++

  1. // ClientSocket.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "Client.h"
  5. #include "ClientSocket.h"
  6. #ifdef _DEBUG
  7. #define new DEBUG_NEW
  8. #undef THIS_FILE
  9. static char THIS_FILE[] = __FILE__;
  10. #endif
  11. /////////////////////////////////////////////////////////////////////////////
  12. // CClientSocket
  13. CClientSocket::CClientSocket()
  14. {
  15. strcpy(m_szResponseMsg,ResponseMsg);
  16. m_szrLength=strlen(m_szResponseMsg);
  17. memset(m_arBuf,0,sizeof(m_arBuf));
  18. memset(m_apRecvWnd,0,sizeof(m_apRecvWnd));
  19. m_bBusy=FALSE;
  20. m_nTotalSend=0;
  21. m_nCurRecvWnd=1;
  22. }
  23. CClientSocket::~CClientSocket()
  24. {
  25. }
  26. // Do not edit the following lines, which are needed by ClassWizard.
  27. #if 0
  28. BEGIN_MESSAGE_MAP(CClientSocket, CAsyncSocket)
  29. //{{AFX_MSG_MAP(CClientSocket)
  30. //}}AFX_MSG_MAP
  31. END_MESSAGE_MAP()
  32. #endif // 0
  33. /////////////////////////////////////////////////////////////////////////////
  34. // CClientSocket member functions
  35. BOOL CClientSocket::Create(int TimeOut)
  36. {
  37. m_nTimeOut=TimeOut;
  38. return CAsyncSocket::Create(0,SOCK_DGRAM,FD_READ);
  39. }
  40. BOOL CClientSocket::SendData(CData *pData,BOOL &bCancel,CWnd* pRecvWnd,int FailReDoTimes)
  41. {
  42. ASSERT(pData!=NULL);
  43. ASSERT(FailReDoTimes>=1);
  44. if(pRecvWnd!=NULL)
  45. {
  46. for(int i=1;i<m_nCurRecvWnd;i++)
  47. {
  48. if(m_apRecvWnd[i]==pRecvWnd)break;
  49. }
  50. if(i==m_nCurRecvWnd)
  51. {
  52. m_apRecvWnd[i]=pRecvWnd;
  53. pData->wIndex=i;
  54. m_nCurRecvWnd++;
  55. if(m_nCurRecvWnd==RecvWndNumber)m_nCurRecvWnd=1;
  56. }
  57. else
  58. {
  59. pData->wIndex=i;
  60. }
  61. }
  62. else
  63. {
  64. pData->wIndex=0;
  65. }
  66. int times=0,iCurIndex=0;
  67. BOOL bSendSuccess=FALSE;
  68. m_nTotalSend++;
  69. iCurIndex=m_nTotalSend%CheckBufLength;
  70. m_arBuf[iCurIndex]=0;
  71. pData->This=iCurIndex;
  72. pData->PackToBuf();
  73. CString strIP;
  74. in_addr tIP;
  75. tIP.S_un.S_addr=pData->tarIP;
  76. strIP=inet_ntoa(tIP);
  77. while(times<FailReDoTimes&&!bSendSuccess&&!bCancel)
  78. {
  79. if(!m_bBusy)
  80. {
  81. m_bBusy=TRUE;
  82. DWORD tBegin=GetTickCount();
  83. SendTo(pData->szBuf,pData->num,pData->nPort,strIP);
  84. m_bBusy=FALSE;
  85. MSG msg;
  86. while(!m_arBuf[iCurIndex]&&GetTickCount()-tBegin<m_nTimeOut&&!bCancel)
  87. {
  88. if(::PeekMessage(&msg,NULL,0,0,PM_REMOVE))
  89. {
  90. ::TranslateMessage(&msg);
  91. ::DispatchMessage(&msg);
  92. }
  93. }
  94. bSendSuccess=m_arBuf[iCurIndex];
  95. TRACE3("Send Data index:%d This:%d iCurI:%dn",times,pData->This,iCurIndex);
  96. times++;
  97. }
  98. }
  99. return bSendSuccess;
  100. }
  101. void CClientSocket::OnReceive(int nErrorCode) 
  102. {
  103. char buff[DataBufLength];
  104. int nRead;
  105. CString sIP;
  106. UINT nPort;
  107. nRead = ReceiveFrom(buff,DataBufLength,sIP,nPort);
  108. if (nRead != SOCKET_ERROR && nRead != 0 )
  109. {
  110. if(StrNSame(buff,m_szResponseMsg,nRead-4,m_szrLength))
  111. {
  112. DWORD Index=*((DWORD*)(buff+nRead-4));
  113. if(Index<=CheckBufLength)
  114. {
  115. m_arBuf[Index]=1;
  116. return;
  117. }
  118. }
  119. CData* pData=new CData;
  120. StrNCopy(pData->szBuf,buff,nRead);
  121. pData->num=nRead;
  122. pData->nPort=nPort;
  123. pData->tarIP=inet_addr(sIP);
  124. if(pData->LoadFromBuf())
  125. {
  126. StrNCopy(m_szResponseMsg+m_szrLength,(char*)&pData->This,sizeof(DWORD));
  127. SendTo(m_szResponseMsg,m_szrLength+sizeof(DWORD),nPort,sIP);
  128. if(pData->wIndex>=0&&pData->wIndex<RecvWndNumber&&m_apRecvWnd[pData->wIndex]!=NULL)
  129. {
  130. m_apRecvWnd[pData->wIndex]->PostMessage(WM_RECIEVE_MSG,(WPARAM)pData,0);
  131. }
  132. TRACE2("Recv a Data index: %d This: %dn",pData->index,pData->This);
  133. }
  134. }
  135. }
  136. void CClientSocket::SendDataDirect(CData *pData)
  137. {
  138. ASSERT(pData!=NULL);
  139. m_nTotalSend++;
  140. pData->This=m_nTotalSend%CheckBufLength;
  141. pData->PackToBuf();
  142. CString strIP;
  143. in_addr tIP;
  144. tIP.S_un.S_addr=pData->tarIP;
  145. strIP=inet_ntoa(tIP);
  146. SendTo(pData->szBuf,pData->num,pData->nPort,strIP);
  147. }