FileSend.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. // FileSend.cpp: implementation of the CFileSend class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #include "stdafx.h"
  5. #include "cp_main.h"
  6. #include "FileSend.h"
  7. #include "Server.h"
  8. #include "TextConvert.h"
  9. #include <shlwapi.h>
  10. #ifdef _DEBUG
  11. #undef THIS_FILE
  12. static char THIS_FILE[]=__FILE__;
  13. #define new DEBUG_NEW
  14. #endif
  15. //using namespace nsPath;
  16. CFileSend::CFileSend()
  17. {
  18. }
  19. CFileSend::~CFileSend()
  20. {
  21. }
  22. BOOL CFileSend::SendClientFiles(SOCKET sock, CClipList *pClipList)
  23. {
  24. if(!pClipList || pClipList->GetCount() <= 0)
  25. {
  26. LogSendRecieveInfo("::ERROR SendClientFiles called either pClipList was null or empty");
  27. return FALSE;
  28. }
  29. m_Send.SetSocket(sock);
  30. CSendInfo Info;
  31. BOOL bRet = FALSE;
  32. CStringArray CopyFiles;
  33. CClipFormat *pFormat = GetCF_HDROP_Data(pClipList);
  34. if(pFormat)
  35. {
  36. HDROP drop = (HDROP)GlobalLock(pFormat->m_hgData);
  37. int nNumFiles = DragQueryFile(drop, -1, NULL, 0);
  38. TCHAR file[MAX_PATH];
  39. for(int nFile = 0; nFile < nNumFiles; nFile++)
  40. {
  41. if(DragQueryFile(drop, nFile, file, sizeof(file)) > 0)
  42. {
  43. if(PathIsDirectory(file) == FALSE)
  44. {
  45. CopyFiles.Add(file);
  46. }
  47. }
  48. }
  49. GlobalUnlock(pFormat->m_hgData);
  50. }
  51. Info.m_lParameter1 = CopyFiles.GetSize();
  52. if(Info.m_lParameter1 > 0)
  53. {
  54. if(m_Send.SendCSendData(Info, MyEnums::START))
  55. {
  56. for(int nFile = 0; nFile < Info.m_lParameter1; nFile++)
  57. {
  58. SendFile(CopyFiles[nFile]);
  59. }
  60. if(m_Send.SendCSendData(Info, MyEnums::END))
  61. bRet = TRUE;
  62. }
  63. }
  64. return bRet;
  65. }
  66. CClipFormat* CFileSend::GetCF_HDROP_Data(CClipList *pClipList)
  67. {
  68. CClip* pClip;
  69. CClipFormat* pCF;
  70. POSITION pos;
  71. pos = pClipList->GetHeadPosition();
  72. while(pos)
  73. {
  74. pClip = pClipList->GetNext(pos);
  75. if(pClip)
  76. {
  77. pCF = pClip->m_Formats.FindFormat(CF_HDROP);
  78. if(pCF)
  79. return pCF;
  80. }
  81. }
  82. return NULL;
  83. }
  84. BOOL CFileSend::SendFile(CString csFile)
  85. {
  86. CFile file;
  87. BOOL bRet = FALSE;
  88. CSendInfo Info;
  89. char *pBuffer = new char[CHUNK_WRITE_SIZE];
  90. if(pBuffer == NULL)
  91. {
  92. LogSendRecieveInfo("Error creating buffer to send file over in");
  93. return FALSE;
  94. }
  95. try
  96. {
  97. CFileException ex;
  98. if(file.Open(csFile, CFile::modeRead|CFile::typeBinary, &ex))
  99. {
  100. CStringA dest;
  101. if(CTextConvert::ConvertToUTF8(csFile, dest))
  102. {
  103. strncpy(Info.m_cDesc, dest, sizeof(Info.m_cDesc));
  104. Info.m_cDesc[sizeof(Info.m_cDesc)-1] = 0;
  105. }
  106. Info.m_lParameter1 = (long)file.GetLength();
  107. if(m_Send.SendCSendData(Info, MyEnums::DATA_START))
  108. {
  109. long lReadBytes = 0;
  110. BOOL bError = FALSE;
  111. do
  112. {
  113. lReadBytes = file.Read(pBuffer, CHUNK_WRITE_SIZE);
  114. if(m_Send.SendExactSize(pBuffer, lReadBytes, false) == FALSE)
  115. {
  116. LogSendRecieveInfo("Error sending SendExactSize in SendFile");
  117. bError = TRUE;
  118. break;
  119. }
  120. }while(lReadBytes >= CHUNK_WRITE_SIZE);
  121. if(bError == FALSE)
  122. {
  123. if(m_Send.SendCSendData(Info, MyEnums::DATA_END))
  124. bRet = TRUE;
  125. }
  126. }
  127. }
  128. else
  129. {
  130. TCHAR szError[100];
  131. ex.GetErrorMessage(szError, 100);
  132. LogSendRecieveInfo(StrF(_T("Error opening file in Send file, error: %s"), szError));
  133. }
  134. }
  135. catch(CFileException *e)
  136. {
  137. TCHAR szError[100];
  138. e->GetErrorMessage(szError, 100);
  139. LogSendRecieveInfo(StrF(_T("Exception - Error in Send file, error: %s"), szError));
  140. }
  141. delete []pBuffer;
  142. pBuffer = NULL;
  143. return bRet;
  144. }