Server.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. // Server.cpp: implementation of the CServer class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #include "stdafx.h"
  5. #include "cp_main.h"
  6. #include "Server.h"
  7. #ifdef _DEBUG
  8. #undef THIS_FILE
  9. static char THIS_FILE[]=__FILE__;
  10. #define new DEBUG_NEW
  11. #endif
  12. //////////////////////////////////////////////////////////////////////
  13. // Construction/Destruction
  14. //////////////////////////////////////////////////////////////////////
  15. UINT MTServerThread(LPVOID pParam)
  16. {
  17. static bool bRunning = false;
  18. if(bRunning)
  19. return 0;
  20. bRunning = true;
  21. LogSendRecieveInfo("Start of ServerThread");
  22. theApp.m_bExitServerThread = false;
  23. WSADATA wsaData;
  24. sockaddr_in local;
  25. int wsaret = WSAStartup(0x101,&wsaData);
  26. if(wsaret!=0)
  27. {
  28. LogSendRecieveInfo("ERROR - int wsaret = WSAStartup(0x101,&wsaData);");
  29. return 0;
  30. }
  31. local.sin_family = AF_INET;
  32. local.sin_addr.s_addr = INADDR_ANY;
  33. local.sin_port = htons((u_short)g_Opt.m_lPort);
  34. theApp.m_sSocket = socket(AF_INET, SOCK_STREAM, 0);
  35. if(theApp.m_sSocket == INVALID_SOCKET)
  36. {
  37. LogSendRecieveInfo("ERROR - theApp.m_sSocket = socket(AF_INET, SOCK_STREAM, 0);");
  38. return 0;
  39. }
  40. if(bind(theApp.m_sSocket,(sockaddr*)&local,sizeof(local))!=0)
  41. {
  42. LogSendRecieveInfo("ERROR - if(bind(theApp.m_sSocket,(sockaddr*)&local,sizeof(local))!=0)");
  43. return 0;
  44. }
  45. if(listen(theApp.m_sSocket,10)!=0)
  46. {
  47. LogSendRecieveInfo("ERROR - if(listen(theApp.m_sSocket,10)!=0)");
  48. return 0;
  49. }
  50. SOCKET socket;
  51. sockaddr_in from;
  52. int fromlen = sizeof(from);
  53. while(true)
  54. {
  55. if(theApp.m_bAppExiting || theApp.m_bExitServerThread)
  56. break;
  57. socket = accept(theApp.m_sSocket, (struct sockaddr*)&from,&fromlen);
  58. AfxBeginThread(ClientThread,(LPVOID)socket);
  59. }
  60. LogSendRecieveInfo("End of Server Thread");
  61. bRunning = false;
  62. theApp.m_sSocket = INVALID_SOCKET;
  63. return 0;
  64. }
  65. BOOL RecieveExactSize(SOCKET sock, char *pData, long lSize)
  66. {
  67. long lReceiveCount = 0;
  68. long lWanted = lSize;
  69. long lOffset = 0;
  70. while(lWanted > 0)
  71. {
  72. lReceiveCount = recv(sock, pData + lOffset, lWanted, 0);
  73. if(lReceiveCount == SOCKET_ERROR)
  74. {
  75. LogSendRecieveInfo("********ERROR if(lReceiveCount == SOCKET_ERROR)*******");
  76. return FALSE;
  77. }
  78. else if(lReceiveCount == 0)
  79. {
  80. LogSendRecieveInfo("********ERROR lRecieveCount == 0");
  81. return FALSE;
  82. }
  83. LogSendRecieveInfo(StrF("------Bytes Read %d Total Recieved %d", lReceiveCount, lOffset));
  84. lWanted -= lReceiveCount;
  85. lOffset += lReceiveCount;
  86. }
  87. LogSendRecieveInfo(StrF("------END RecieveExactSize Recieved %d", lOffset));
  88. return TRUE;
  89. }
  90. BOOL RecieveCSendInfo(SOCKET sock, CSendInfo *pInfo)
  91. {
  92. BOOL bRet = RecieveExactSize(sock, (char*)pInfo, sizeof(CSendInfo));
  93. if(bRet)
  94. {
  95. bRet = pInfo->m_nSize == sizeof(CSendInfo);
  96. }
  97. return bRet;
  98. }
  99. UINT ClientThread(LPVOID pParam)
  100. {
  101. LogSendRecieveInfo("*********************Start of ClientThread*********************");
  102. SOCKET socket = (SOCKET)pParam;
  103. CClipList *pClipList = NULL;
  104. CClip *pClip = NULL;
  105. CClipFormat cf;
  106. CSendInfo info;
  107. bool bBreak = false;
  108. BOOL bSetToClipBoard = FALSE;
  109. while(true)
  110. {
  111. if(RecieveCSendInfo(socket, &info) == FALSE)
  112. break;
  113. switch(info.m_Type)
  114. {
  115. case MyEnums::START:
  116. {
  117. if(pClip != NULL)
  118. {
  119. delete pClip;
  120. pClip = NULL;
  121. }
  122. pClip = new CClip;
  123. CString cs;
  124. cs.Format("%s\n(%s)(%s)", info.m_cDesc, info.m_cComputerName, info.m_cIP);
  125. if(pClip)
  126. {
  127. pClip->m_Desc = cs;
  128. }
  129. bSetToClipBoard = FALSE;
  130. if(g_Opt.m_csIPListToPutOnClipboard.Find(info.m_cIP) >= 0)
  131. bSetToClipBoard = TRUE;
  132. if(g_Opt.m_csIPListToPutOnClipboard.Find(info.m_cComputerName) >= 0)
  133. bSetToClipBoard = TRUE;
  134. info.m_cDesc[20] = 0;
  135. LogSendRecieveInfo(StrF("::START %s %s %s", info.m_cDesc, info.m_cComputerName, info.m_cIP));
  136. }
  137. break;
  138. case MyEnums::DATA_START:
  139. {
  140. LogSendRecieveInfo("::DATA_START -- START");
  141. cf.m_hgData = NewGlobal(info.m_lParameter1);
  142. cf.m_cfType = GetFormatID(info.m_cDesc);
  143. LogSendRecieveInfo(StrF("::--------DATA_START Total Size = %d type = %s", info.m_lParameter1, info.m_cDesc));
  144. if(pClip)
  145. pClip->m_lTotalCopySize += info.m_lParameter1;
  146. LogSendRecieveInfo("::--------Before RecieveExactSize");
  147. LPVOID pvData = GlobalLock(cf.m_hgData);
  148. //Recieve the clip data
  149. if(RecieveExactSize(socket, (char*)pvData, info.m_lParameter1) == FALSE)
  150. bBreak = true;
  151. GlobalUnlock(cf.m_hgData);
  152. LogSendRecieveInfo("::--------After RecieveExactSize");
  153. LogSendRecieveInfo("::DATA_START -- END");
  154. }
  155. break;
  156. case MyEnums::DATA_END:
  157. {
  158. LogSendRecieveInfo("::DATA_END");
  159. if(pClip)
  160. {
  161. pClip->m_Formats.Add(cf);
  162. cf.m_hgData = 0; // now owned by pClip
  163. }
  164. }
  165. break;
  166. case MyEnums::END:
  167. {
  168. LogSendRecieveInfo("::END");
  169. if(pClipList == NULL)
  170. pClipList = new CClipList;
  171. if(pClipList)
  172. {
  173. pClipList->AddTail(pClip);
  174. pClip = NULL;
  175. }
  176. else
  177. LogSendRecieveInfo("::ERROR pClipList was NULL");
  178. }
  179. break;
  180. case MyEnums::EXIT:
  181. {
  182. LogSendRecieveInfo("::EXIT");
  183. if(pClipList && pClipList->GetCount() > 0)
  184. {
  185. theApp.m_lClipsRecieved += pClipList->GetCount();
  186. //Post a message pClipList will be freed by the reciever
  187. ::PostMessage(theApp.m_MainhWnd, WM_ADD_TO_DATABASE_FROM_SOCKET, (WPARAM)pClipList, bSetToClipBoard);
  188. pClipList = NULL;
  189. }
  190. else
  191. LogSendRecieveInfo("::ERROR pClipList was NULL or Count was 0");
  192. bBreak = true;
  193. }
  194. break;
  195. }
  196. if(bBreak || theApp.m_bAppExiting)
  197. break;
  198. }
  199. if(pClipList)
  200. {
  201. delete pClipList;
  202. pClipList = NULL;
  203. LogSendRecieveInfo("::ERROR pClipList was not NULL something is wrong");
  204. }
  205. if(pClip)
  206. {
  207. delete pClip;
  208. pClip = NULL;
  209. LogSendRecieveInfo("::ERROR pClip was not NULL something is wrong");
  210. }
  211. closesocket(socket);
  212. LogSendRecieveInfo("*********************End of ClientThread*********************");
  213. return 0;
  214. }