AutoSendToClientThread.cpp 3.6 KB

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