SendSocket.cpp 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. // SendSocket.cpp: implementation of the CSendSocket class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #include "stdafx.h"
  5. #include "cp_main.h"
  6. #include "SendSocket.h"
  7. #include "shared/TextConvert.h"
  8. #ifdef _DEBUG
  9. #undef THIS_FILE
  10. static char THIS_FILE[]=__FILE__;
  11. #define new DEBUG_NEW
  12. #endif
  13. //////////////////////////////////////////////////////////////////////
  14. // Construction/Destruction
  15. //////////////////////////////////////////////////////////////////////
  16. CSendSocket::CSendSocket()
  17. {
  18. m_pEncryptor = new CEncryption;
  19. m_Connection = NULL;
  20. }
  21. CSendSocket::~CSendSocket()
  22. {
  23. delete m_pEncryptor;
  24. m_pEncryptor = NULL;
  25. }
  26. BOOL CSendSocket::SendCSendData(CSendInfo &data, MyEnums::eSendType type)
  27. {
  28. data.m_Type = type;
  29. return SendExactSize((char *)&data, sizeof(CSendInfo), true);
  30. }
  31. BOOL CSendSocket::SendExactSize(char *pData, long lLength, bool bEncrypt)
  32. {
  33. BOOL bRet = FALSE;
  34. if(!m_pEncryptor && bEncrypt)
  35. {
  36. ASSERT(!"Encryption not initialized");
  37. LogSendRecieveInfo("SendExactSize::Encryption not initialized");
  38. return bRet;
  39. }
  40. LogSendRecieveInfo(StrF(_T("START SendExactSize Total %d"), lLength));
  41. UCHAR* pOutput = (UCHAR*)pData;
  42. int nLenOutput = lLength;
  43. long lBytesRead = 0;
  44. if(bEncrypt == false || m_pEncryptor->Encrypt((UCHAR*)pData, lLength, g_Opt.m_csPassword, pOutput, nLenOutput))
  45. {
  46. long lExpected = nLenOutput;
  47. while(lBytesRead < lExpected)
  48. {
  49. long lSize = send(m_Connection, (char*)pOutput + lBytesRead, lExpected - lBytesRead, 0);
  50. if(lSize == SOCKET_ERROR || lSize == 0)
  51. {
  52. LogSendRecieveInfo(StrF(_T("lSize == SOCKET_ERROR, %d"), WSAGetLastError()));
  53. bRet = FALSE;
  54. break;
  55. }
  56. lBytesRead += lSize;
  57. }
  58. if(lBytesRead == lExpected)
  59. bRet = TRUE;
  60. if(pOutput != (UCHAR*)pData)
  61. m_pEncryptor->FreeBuffer(pOutput);
  62. }
  63. else
  64. {
  65. LogSendRecieveInfo("SendExactSize::Failed to encrypt data");
  66. }
  67. // LogSendRecieveInfo(StrF(_T("END SendExactSize Total %d"), lBytesRead));
  68. return bRet;
  69. }