forked from sabrogden/Ditto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AutoSendToClientThread.cpp
164 lines (132 loc) · 3.59 KB
/
AutoSendToClientThread.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#include "stdafx.h"
#include "AutoSendToClientThread.h"
#include "Misc.h"
#include "Options.h"
#include "CP_Main.h"
#include "client.h"
CAutoSendToClientThread::CAutoSendToClientThread(void)
{
m_waitTimeout = 30000;
m_threadName = "CAutoSendToClientThread";
for(int eventEnum = 0; eventEnum < ECAUTOSENDTOCLIENTTHREADEVENTS_COUNT; eventEnum++)
{
AddEvent(eventEnum);
}
}
CAutoSendToClientThread::~CAutoSendToClientThread(void)
{
}
void CAutoSendToClientThread::FireSendToClient(CClipList *pClipList)
{
Start();
ATL::CCritSecLock csLock(m_cs.m_sect);
if(m_threadRunning)
{
Log(_T("Adding clip to send to client in thread"));
POSITION pos = pClipList->GetHeadPosition();
while(pos)
{
CClip *pClip = pClipList->GetNext(pos);
m_saveClips.AddTail(pClip);
}
pClipList->RemoveAll();
FireEvent(SEND_TO_CLIENTS);
}
else
{
Log(_T("Error creating thread to send to clients"));
}
}
void CAutoSendToClientThread::OnTimeOut(void *param)
{
Stop(-1);
}
void CAutoSendToClientThread::OnEvent(int eventId, void *param)
{
switch((eCAutoSendToClientThreadEvents)eventId)
{
case SEND_TO_CLIENTS:
OnSendToClient();
break;
}
}
void CAutoSendToClientThread::OnSendToClient()
{
CClipList *pLocalClips = new CClipList();
//Save the clips locally
{
ATL::CCritSecLock csLock(m_cs.m_sect);
POSITION pos;
CClip* pClip;
pos = m_saveClips.GetHeadPosition();
while(pos)
{
pClip = m_saveClips.GetNext(pos);
pLocalClips->AddTail(pClip);
}
//pLocalClips now own, the clips
m_saveClips.RemoveAll();
}
SendToClient(pLocalClips);
delete pLocalClips;
pLocalClips = NULL;
}
bool CAutoSendToClientThread::SendToClient(CClipList *pClipList)
{
LogSendRecieveInfo("@@@@@@@@@@@@@@@ - START OF SendClientThread - @@@@@@@@@@@@@@@");
if(pClipList == NULL)
{
LogSendRecieveInfo("ERROR if(pClipList == NULL)");
return FALSE;
}
INT_PTR lCount = pClipList->GetCount();
LogSendRecieveInfo(StrF(_T("Start of Send ClientThread Count - %d"), lCount));
for(int nClient = 0; nClient < MAX_SEND_CLIENTS; nClient++)
{
if(g_Opt.m_SendClients[nClient].bSendAll &&
g_Opt.m_SendClients[nClient].csIP.GetLength() > 0)
{
CClient client;
if(client.OpenConnection(g_Opt.m_SendClients[nClient].csIP) == FALSE)
{
LogSendRecieveInfo(StrF(_T("ERROR opening connection to %s"), g_Opt.m_SendClients[nClient].csIP));
if(g_Opt.m_SendClients[nClient].bShownFirstError == FALSE)
{
CString cs;
cs.Format(_T("Error opening connection to %s"), g_Opt.m_SendClients[nClient].csIP);
::SendMessage(theApp.m_MainhWnd, WM_SEND_RECIEVE_ERROR, (WPARAM)cs.GetBuffer(cs.GetLength()), 0);
cs.ReleaseBuffer();
g_Opt.m_SendClients[nClient].bShownFirstError = TRUE;
}
continue;
}
//We were connected successfully show an error next time we can't connect
g_Opt.m_SendClients[nClient].bShownFirstError = FALSE;
CClip* pClip;
POSITION pos;
pos = pClipList->GetHeadPosition();
while(pos)
{
pClip = pClipList->GetNext(pos);
if(pClip == NULL)
{
ASSERT(FALSE);
LogSendRecieveInfo("Error in GetNext");
break;
}
LogSendRecieveInfo(StrF(_T("Sending clip to %s"), g_Opt.m_SendClients[nClient].csIP));
if(client.SendItem(pClip, false) == FALSE)
{
CString cs;
cs.Format(_T("Error sending clip to %s"), g_Opt.m_SendClients[nClient].csIP);
::SendMessage(theApp.m_MainhWnd, WM_SEND_RECIEVE_ERROR, (WPARAM)cs.GetBuffer(cs.GetLength()), 0);
cs.ReleaseBuffer();
break;
}
}
client.CloseConnection();
}
}
LogSendRecieveInfo("@@@@@@@@@@@@@@@ - END OF SendClientThread - @@@@@@@@@@@@@@@");
return TRUE;
}