Client.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. // Client.cpp: implementation of the CClient class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #include "stdafx.h"
  5. #include "cp_main.h"
  6. #include "Client.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. BOOL SendToFriend(CSendToFriendInfo &Info)
  16. {
  17. LogSendRecieveInfo("@@@@@@@@@@@@@@@ - START OF Send To Friend - @@@@@@@@@@@@@@@");
  18. if(Info.m_lPos > -1 && Info.m_lPos < MAX_SEND_CLIENTS)
  19. {
  20. Info.m_csIP = g_Opt.m_SendClients[Info.m_lPos].csIP;
  21. }
  22. else
  23. {
  24. Info.m_csErrorText = StrF("ERROR getting ip position - %d", Info.m_lPos);
  25. LogSendRecieveInfo(Info.m_csErrorText);
  26. return FALSE;
  27. }
  28. LogSendRecieveInfo(StrF("Sending clip to %s", Info.m_csIP));
  29. CClient client;
  30. if(client.OpenConnection(Info.m_csIP) == FALSE)
  31. {
  32. Info.m_csErrorText = StrF("ERROR opening connection to %s", Info.m_csIP);
  33. LogSendRecieveInfo(Info.m_csErrorText);
  34. return FALSE;
  35. }
  36. long lCount = Info.m_pClipList->GetCount();
  37. int i = -1;
  38. CClip* pClip;
  39. POSITION pos;
  40. pos = Info.m_pClipList->GetHeadPosition();
  41. while(pos)
  42. {
  43. pClip = Info.m_pClipList->GetNext(pos);
  44. if(pClip == NULL)
  45. {
  46. ASSERT(FALSE);
  47. continue;
  48. }
  49. i++;
  50. if(Info.m_pPopup)
  51. {
  52. Info.m_pPopup->SendToolTipText(StrF("Sending %d of %d", i+1, lCount));
  53. }
  54. MSG msg;
  55. while(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
  56. {
  57. TranslateMessage(&msg);
  58. DispatchMessage(&msg);
  59. }
  60. LogSendRecieveInfo(StrF("Sending %d of %d clip to %s", i+1, lCount, Info.m_csIP));
  61. if(client.SendItem(pClip) == FALSE)
  62. {
  63. Info.m_csErrorText = "ERROR SendItem Failed";
  64. LogSendRecieveInfo(Info.m_csErrorText);
  65. return FALSE;
  66. }
  67. }
  68. LogSendRecieveInfo("@@@@@@@@@@@@@@@ - END OF Send To Friend - @@@@@@@@@@@@@@@");
  69. return TRUE;
  70. }
  71. UINT SendClientThread(LPVOID pParam)
  72. {
  73. EnterCriticalSection(&theApp.m_CriticalSection);
  74. LogSendRecieveInfo("@@@@@@@@@@@@@@@ - START OF SendClientThread - @@@@@@@@@@@@@@@");
  75. CClipList *pClipList = (CClipList*)pParam;
  76. if(pClipList == NULL)
  77. {
  78. LogSendRecieveInfo("ERROR if(pClipList == NULL)");
  79. return FALSE;
  80. }
  81. long lCount = pClipList->GetCount();
  82. LogSendRecieveInfo(StrF("Start of Send ClientThread Count - %d", lCount));
  83. for(int nClient = 0; nClient < MAX_SEND_CLIENTS; nClient++)
  84. {
  85. if(g_Opt.m_SendClients[nClient].bSendAll &&
  86. g_Opt.m_SendClients[nClient].csIP.GetLength() > 0)
  87. {
  88. CClient client;
  89. if(client.OpenConnection(g_Opt.m_SendClients[nClient].csIP) == FALSE)
  90. {
  91. LogSendRecieveInfo(StrF("ERROR opening connection to %s", g_Opt.m_SendClients[nClient].csIP));
  92. CString cs;
  93. cs.Format("Error opening connection to %s",g_Opt.m_SendClients[nClient].csIP);
  94. ::SendMessage(theApp.m_MainhWnd, WM_SEND_RECIEVE_ERROR, (WPARAM)cs.GetBuffer(cs.GetLength()), 0);
  95. cs.ReleaseBuffer();
  96. continue;
  97. }
  98. CClip* pClip;
  99. POSITION pos;
  100. pos = pClipList->GetHeadPosition();
  101. while(pos)
  102. {
  103. pClip = pClipList->GetNext(pos);
  104. if(pClip == NULL)
  105. {
  106. ASSERT(FALSE);
  107. LogSendRecieveInfo("Error in GetNext");
  108. break;
  109. }
  110. LogSendRecieveInfo(StrF("Sending clip to %s", g_Opt.m_SendClients[nClient].csIP));
  111. if(client.SendItem(pClip) == FALSE)
  112. {
  113. CString cs;
  114. cs.Format("Error sending clip to %s",g_Opt.m_SendClients[nClient].csIP);
  115. ::SendMessage(theApp.m_MainhWnd, WM_SEND_RECIEVE_ERROR, (WPARAM)cs.GetBuffer(cs.GetLength()), 0);
  116. cs.ReleaseBuffer();
  117. break;
  118. }
  119. }
  120. client.CloseConnection();
  121. }
  122. }
  123. delete pClipList;
  124. pClipList = NULL;
  125. LogSendRecieveInfo("@@@@@@@@@@@@@@@ - END OF SendClientThread - @@@@@@@@@@@@@@@");
  126. LeaveCriticalSection(&theApp.m_CriticalSection);
  127. return TRUE;
  128. }
  129. CClient::CClient()
  130. {
  131. m_Connection = NULL;
  132. }
  133. CClient::~CClient()
  134. {
  135. CloseConnection();
  136. }
  137. BOOL CClient::CloseConnection()
  138. {
  139. if(m_Connection != NULL && m_Connection != 0)
  140. {
  141. SendInfo Info;
  142. SendData(&Info, MyEnums::EXIT);
  143. closesocket(m_Connection);
  144. WSACleanup();
  145. m_Connection = NULL;
  146. }
  147. return TRUE;
  148. }
  149. BOOL CClient::OpenConnection(const char* servername)
  150. {
  151. WSADATA wsaData;
  152. struct hostent *hp;
  153. unsigned int addr;
  154. struct sockaddr_in server;
  155. int wsaret=WSAStartup(0x101,&wsaData);
  156. if(wsaret)
  157. {
  158. LogSendRecieveInfo("ERROR - WSAStartup(0x101,&wsaData)");
  159. return FALSE;
  160. }
  161. m_Connection = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
  162. if(m_Connection == INVALID_SOCKET)
  163. {
  164. LogSendRecieveInfo("ERROR - socket(AF_INET,SOCK_STREAM,IPPROTO_TCP)");
  165. m_Connection = NULL;
  166. return FALSE;
  167. }
  168. if(inet_addr(servername)==INADDR_NONE)
  169. {
  170. hp=gethostbyname(servername);
  171. }
  172. else
  173. {
  174. addr=inet_addr(servername);
  175. hp=gethostbyaddr((char*)&addr,sizeof(addr),AF_INET);
  176. }
  177. if(hp==NULL)
  178. {
  179. LogSendRecieveInfo("ERROR - if(hp==NULL)");
  180. closesocket(m_Connection);
  181. m_Connection = NULL;
  182. return FALSE;
  183. }
  184. server.sin_addr.s_addr=*((unsigned long*)hp->h_addr);
  185. server.sin_family=AF_INET;
  186. server.sin_port=htons(20248);
  187. if(connect(m_Connection,(struct sockaddr*)&server,sizeof(server)))
  188. {
  189. LogSendRecieveInfo("ERROR if(connect(m_Connection,(struct sockaddr*)&server,sizeof(server)))");
  190. closesocket(m_Connection);
  191. m_Connection = NULL;
  192. return FALSE;
  193. }
  194. return TRUE;
  195. }
  196. BOOL CClient::SendItem(CClip *pClip)
  197. {
  198. SendInfo Info;
  199. strncpy(Info.m_cComputerName, GetComputerName(), sizeof(Info.m_cComputerName));
  200. strncpy(Info.m_cIP, GetIPAddress(), sizeof(Info.m_cIP));
  201. strncpy(Info.m_cText, pClip->m_Desc, sizeof(Info.m_cText));
  202. Info.m_cText[sizeof(Info.m_cText)-1] = 0;
  203. Info.m_cComputerName[sizeof(Info.m_cComputerName)-1] = 0;
  204. Info.m_cIP[sizeof(Info.m_cIP)-1] = 0;
  205. if(SendData(&Info, MyEnums::START) == FALSE)
  206. return FALSE;
  207. CClipFormat* pCF;
  208. int nCount = pClip->m_Formats.GetSize();
  209. DWORD dwSize;
  210. DWORD dwDataSent;
  211. //For each data type
  212. for( int i=0; i < nCount; i++ )
  213. {
  214. pCF = &pClip->m_Formats.GetData()[i];
  215. Info.m_lParameter1 = dwSize = GlobalSize(pCF->m_hgData);
  216. strncpy(Info.m_cText, GetFormatName(pCF->m_cfType), sizeof(Info.m_cText));
  217. Info.m_cText[sizeof(Info.m_cText)-1] = 0;
  218. if(SendData(&Info, MyEnums::DATA_START) == FALSE)
  219. return FALSE;
  220. dwDataSent = 0;
  221. while(dwDataSent < dwSize)
  222. {
  223. LPVOID pvData = GlobalLock(pCF->m_hgData);
  224. if(dwSize - dwDataSent < MAX_DATA_SIZE)
  225. Info.m_lParameter1 = dwSize - dwDataSent;
  226. else
  227. Info.m_lParameter1 = MAX_DATA_SIZE;
  228. memcpy(Info.m_cText, ((char*)pvData) + dwDataSent, Info.m_lParameter1);
  229. GlobalUnlock(pCF->m_hgData);
  230. if(SendData(&Info, MyEnums::DATA) == FALSE)
  231. return FALSE;
  232. dwDataSent += Info.m_lParameter1;
  233. }
  234. if(SendData(&Info, MyEnums::DATA_END) == FALSE)
  235. return FALSE;
  236. }
  237. if(SendData(&Info, MyEnums::END) == FALSE)
  238. return FALSE;
  239. return TRUE;
  240. }
  241. BOOL CClient::SendData(SendInfo *pInfo, MyEnums::eSendType type)
  242. {
  243. pInfo->m_Type = type;
  244. long lSize = send(m_Connection, (char *)pInfo, sizeof(SendInfo), 0);
  245. if(lSize != sizeof(SendInfo))
  246. {
  247. ASSERT(FALSE);
  248. }
  249. if(lSize == SOCKET_ERROR)
  250. {
  251. LogSendRecieveInfo(StrF("lSize == SOCKET_ERROR, %d", WSAGetLastError()));
  252. return FALSE;
  253. }
  254. return TRUE;
  255. }