Server.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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. if( socket != INVALID_SOCKET )
  59. AfxBeginThread(ClientThread,(LPVOID)socket);
  60. }
  61. LogSendRecieveInfo("End of Server Thread");
  62. bRunning = false;
  63. theApp.m_sSocket = INVALID_SOCKET;
  64. return 0;
  65. }
  66. UINT ClientThread(LPVOID pParam)
  67. {
  68. LogSendRecieveInfo("*********************Start of ClientThread*********************");
  69. CServer Server;
  70. Server.RunThread((SOCKET)pParam);
  71. LogSendRecieveInfo("*********************End of ClientThread*********************");
  72. return 0;
  73. }
  74. CServer::CServer()
  75. {
  76. m_pClipList = NULL;
  77. m_pClip = NULL;
  78. m_bSetToClipBoard = FALSE;
  79. }
  80. CServer::~CServer()
  81. {
  82. closesocket(m_Sock.GetSocket());
  83. }
  84. void CServer::RunThread(SOCKET sock)
  85. {
  86. m_Sock.SetSocket(sock);
  87. CSendInfo info;
  88. bool bBreak = false;
  89. while(true)
  90. {
  91. if(m_Sock.RecieveCSendInfo(&info) == FALSE)
  92. break;
  93. switch(info.m_Type)
  94. {
  95. case MyEnums::START:
  96. OnStart(info);
  97. break;
  98. case MyEnums::DATA_START:
  99. OnDataStart(info);
  100. break;
  101. case MyEnums::DATA_END:
  102. OnDataEnd(info);
  103. break;
  104. case MyEnums::END:
  105. OnEnd(info);
  106. break;
  107. case MyEnums::EXIT:
  108. OnExit(info);
  109. bBreak = true;
  110. break;
  111. case MyEnums::REQUEST_FILES:
  112. OnRequestFiles(info);
  113. break;
  114. default:
  115. LogSendRecieveInfo("::ERROR unknown action type exiting");
  116. bBreak = true;
  117. }
  118. if(bBreak || theApp.m_bAppExiting)
  119. break;
  120. }
  121. if(m_pClipList)
  122. {
  123. LogSendRecieveInfo("::ERROR pClipList was not NULL something is wrong");
  124. delete m_pClipList;
  125. m_pClipList = NULL;
  126. }
  127. if(m_pClip)
  128. {
  129. LogSendRecieveInfo("::ERROR pClip was not NULL something is wrong");
  130. delete m_pClip;
  131. m_pClip = NULL;
  132. }
  133. }
  134. void CServer::OnStart(CSendInfo &info)
  135. {
  136. CTextConvert::ConvertFromUTF8(info.m_cIP, m_csIP);
  137. CTextConvert::ConvertFromUTF8(info.m_cComputerName, m_csComputerName);
  138. CTextConvert::ConvertFromUTF8(info.m_cDesc, m_csDesc);
  139. if(m_pClip != NULL)
  140. {
  141. delete m_pClip;
  142. m_pClip = NULL;
  143. }
  144. m_pClip = new CClip;
  145. CString cs;
  146. cs.Format(_T("%s\n(%s)(%s)"), m_csDesc, m_csComputerName, m_csIP);
  147. if(m_pClip)
  148. {
  149. m_pClip->m_Desc = cs;
  150. }
  151. m_bSetToClipBoard = FALSE;
  152. if(g_Opt.m_csIPListToPutOnClipboard.Find(m_csIP) >= 0)
  153. m_bSetToClipBoard = TRUE;
  154. if(g_Opt.m_csIPListToPutOnClipboard.Find(m_csComputerName) >= 0)
  155. m_bSetToClipBoard = TRUE;
  156. info.m_cDesc[20] = 0;
  157. LogSendRecieveInfo(StrF(_T("::START %s %s %s"), m_csDesc, m_csComputerName, m_csIP));
  158. }
  159. void CServer::OnDataStart(CSendInfo &info)
  160. {
  161. LogSendRecieveInfo("::DATA_START -- START");
  162. CString csFormat;
  163. CTextConvert::ConvertFromUTF8(info.m_cDesc, csFormat);
  164. m_cf.m_cfType = GetFormatID(csFormat);
  165. m_cf.m_hgData = 0;
  166. long lInSize = info.m_lParameter1;
  167. long lOutSize = 0;
  168. LPVOID lpData = m_Sock.ReceiveEncryptedData(lInSize, lOutSize);
  169. if(lpData && lOutSize > 0)
  170. {
  171. m_cf.m_hgData = NewGlobal(lOutSize);
  172. if(m_cf.m_hgData)
  173. {
  174. if(m_pClip)
  175. m_pClip->m_lTotalCopySize += lOutSize;
  176. LPVOID pvData = GlobalLock(m_cf.m_hgData);
  177. if(pvData)
  178. {
  179. memcpy(pvData, lpData, lOutSize);
  180. GlobalUnlock(m_cf.m_hgData);
  181. }
  182. else
  183. {
  184. LogSendRecieveInfo("::DATA_START -- failed to lock hGlobal");
  185. }
  186. }
  187. else
  188. {
  189. LogSendRecieveInfo("::DATA_START -- failed to create new hGlobal");
  190. }
  191. m_Sock.FreeDecryptedData();
  192. }
  193. LogSendRecieveInfo("::DATA_START -- END");
  194. }
  195. void CServer::OnDataEnd(CSendInfo &info)
  196. {
  197. LogSendRecieveInfo("::DATA_END");
  198. if(m_pClip && m_cf.m_hgData)
  199. {
  200. if(m_cf.m_cfType == CF_HDROP)
  201. AddRemoteCF_HDROPFormat();
  202. m_pClip->m_Formats.Add(m_cf);
  203. m_cf.m_hgData = 0; // now owned by pClip
  204. }
  205. else
  206. {
  207. LogSendRecieveInfo("MyEnums::DATA_END Error if(pClip && cf.m_hgData)");
  208. }
  209. }
  210. void CServer::OnEnd(CSendInfo &info)
  211. {
  212. LogSendRecieveInfo("::END");
  213. if(m_pClipList == NULL)
  214. m_pClipList = new CClipList;
  215. if(m_pClipList)
  216. {
  217. m_pClipList->AddTail(m_pClip);
  218. m_pClip = NULL; //clip list now owns the clip
  219. }
  220. else
  221. LogSendRecieveInfo("::ERROR pClipList was NULL");
  222. }
  223. void CServer::OnExit(CSendInfo &info)
  224. {
  225. LogSendRecieveInfo("::EXIT");
  226. if(m_pClipList && m_pClipList->GetCount() > 0)
  227. {
  228. theApp.m_lClipsRecieved += m_pClipList->GetCount();
  229. //Post a message pClipList will be freed by the reciever
  230. ::PostMessage(theApp.m_MainhWnd, WM_ADD_TO_DATABASE_FROM_SOCKET, (WPARAM)m_pClipList, m_bSetToClipBoard);
  231. m_pClipList = NULL;
  232. }
  233. else
  234. LogSendRecieveInfo("::ERROR pClipList was NULL or Count was 0");
  235. }
  236. void CServer::OnRequestFiles(CSendInfo &info)
  237. {
  238. CFileSend Send;
  239. Send.SendClientFiles(m_Sock.GetSocket(), m_pClipList);
  240. delete m_pClipList;
  241. m_pClipList = NULL;
  242. }
  243. void CServer::AddRemoteCF_HDROPFormat()
  244. {
  245. CClipFormat cf;
  246. CDittoCF_HDROP Drop;
  247. CTextConvert Convert;
  248. CStringA dest;
  249. if(CTextConvert::ConvertToUTF8(m_csIP, dest))
  250. {
  251. strncpy(Drop.m_cIP, dest, sizeof(Drop.m_cIP)-1);
  252. }
  253. if(CTextConvert::ConvertToUTF8(m_csComputerName, dest))
  254. {
  255. strncpy(Drop.m_cComputerName, dest, sizeof(Drop.m_cComputerName)-1);
  256. }
  257. cf.m_hgData = NewGlobalP(&Drop, sizeof(Drop));
  258. cf.m_cfType = theApp.m_RemoteCF_HDROP;
  259. m_pClip->m_Formats.Add(cf);
  260. //m_pClip->m_Formats now owns the data
  261. cf.m_hgData = NULL;
  262. }