AutoSendToClientThread.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. #include "stdafx.h"
  2. #include "AutoSendToClientThread.h"
  3. #include "Misc.h"
  4. #include "Options.h"
  5. #include "CP_Main.h"
  6. #include "client.h"
  7. CAutoSendToClientThread::CAutoSendToClientThread(void)
  8. {
  9. m_waitTimeout = 30000;
  10. for(int eventEnum = 0; eventEnum < ECAUTOSENDTOCLIENTTHREADEVENTS_COUNT; eventEnum++)
  11. {
  12. AddEvent(eventEnum);
  13. }
  14. }
  15. CAutoSendToClientThread::~CAutoSendToClientThread(void)
  16. {
  17. }
  18. void CAutoSendToClientThread::FireSendToClient(CClipList *pClipList)
  19. {
  20. Start();
  21. ATL::CCritSecLock csLock(m_cs.m_sect);
  22. if(m_threadRunning)
  23. {
  24. Log(_T("Adding clip to send to client in thread"));
  25. POSITION pos = pClipList->GetHeadPosition();
  26. while(pos)
  27. {
  28. CClip *pClip = pClipList->GetNext(pos);
  29. m_saveClips.AddTail(pClip);
  30. }
  31. pClipList->RemoveAll();
  32. FireEvent(SEND_TO_CLIENTS);
  33. }
  34. else
  35. {
  36. Log(_T("Error creating thread to send to clients"));
  37. }
  38. }
  39. void CAutoSendToClientThread::OnTimeOut(void *param)
  40. {
  41. Stop(-1);
  42. }
  43. void CAutoSendToClientThread::OnEvent(int eventId, void *param)
  44. {
  45. switch((eCAutoSendToClientThreadEvents)eventId)
  46. {
  47. case SEND_TO_CLIENTS:
  48. OnSendToClient();
  49. break;
  50. }
  51. }
  52. void CAutoSendToClientThread::OnSendToClient()
  53. {
  54. CClipList *pLocalClips = new CClipList();
  55. //Save the clips locally
  56. {
  57. ATL::CCritSecLock csLock(m_cs.m_sect);
  58. POSITION pos;
  59. CClip* pClip;
  60. pos = m_saveClips.GetHeadPosition();
  61. while(pos)
  62. {
  63. pClip = m_saveClips.GetNext(pos);
  64. pLocalClips->AddTail(pClip);
  65. }
  66. //pLocalClips now own, the clips
  67. m_saveClips.RemoveAll();
  68. }
  69. SendToClient(pLocalClips);
  70. delete pLocalClips;
  71. pLocalClips = NULL;
  72. }
  73. bool CAutoSendToClientThread::SendToClient(CClipList *pClipList)
  74. {
  75. LogSendRecieveInfo("@@@@@@@@@@@@@@@ - START OF SendClientThread - @@@@@@@@@@@@@@@");
  76. if(pClipList == NULL)
  77. {
  78. LogSendRecieveInfo("ERROR if(pClipList == NULL)");
  79. return FALSE;
  80. }
  81. long lCount = pClipList->GetCount();
  82. LogSendRecieveInfo(StrF(_T("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(_T("ERROR opening connection to %s"), g_Opt.m_SendClients[nClient].csIP));
  92. if(g_Opt.m_SendClients[nClient].bShownFirstError == FALSE)
  93. {
  94. CString cs;
  95. cs.Format(_T("Error opening connection to %s"), g_Opt.m_SendClients[nClient].csIP);
  96. ::SendMessage(theApp.m_MainhWnd, WM_SEND_RECIEVE_ERROR, (WPARAM)cs.GetBuffer(cs.GetLength()), 0);
  97. cs.ReleaseBuffer();
  98. g_Opt.m_SendClients[nClient].bShownFirstError = TRUE;
  99. }
  100. continue;
  101. }
  102. //We were connected successfully show an error next time we can't connect
  103. g_Opt.m_SendClients[nClient].bShownFirstError = FALSE;
  104. CClip* pClip;
  105. POSITION pos;
  106. pos = pClipList->GetHeadPosition();
  107. while(pos)
  108. {
  109. pClip = pClipList->GetNext(pos);
  110. if(pClip == NULL)
  111. {
  112. ASSERT(FALSE);
  113. LogSendRecieveInfo("Error in GetNext");
  114. break;
  115. }
  116. LogSendRecieveInfo(StrF(_T("Sending clip to %s"), g_Opt.m_SendClients[nClient].csIP));
  117. if(client.SendItem(pClip) == FALSE)
  118. {
  119. CString cs;
  120. cs.Format(_T("Error sending clip to %s"), g_Opt.m_SendClients[nClient].csIP);
  121. ::SendMessage(theApp.m_MainhWnd, WM_SEND_RECIEVE_ERROR, (WPARAM)cs.GetBuffer(cs.GetLength()), 0);
  122. cs.ReleaseBuffer();
  123. break;
  124. }
  125. }
  126. client.CloseConnection();
  127. }
  128. }
  129. LogSendRecieveInfo("@@@@@@@@@@@@@@@ - END OF SendClientThread - @@@@@@@@@@@@@@@");
  130. return TRUE;
  131. }