-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathCPHandler.cpp
More file actions
211 lines (166 loc) · 5.43 KB
/
CPHandler.cpp
File metadata and controls
211 lines (166 loc) · 5.43 KB
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/*
Copyright [2010] [Richard Bross]
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// CPHandler.cpp: implementation of the CPHandler class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "CPHandler.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
// Not much to do here
CPHandler::CPHandler()
{
iSize = iLiveSize = 0;
bThreadRunning = TRUE;
}
// This object should not be deleted until all CPConnection objects
// have been removed by calls to RemoveConnection.
// If that has occurred, then there should not be a thread running,
// because the thread will end when the last object has been removed.
CPHandler::~CPHandler()
{
bThreadRunning = FALSE;
WaitForThreadCompletion();
}
// Add a connection to the queued.
// These are added to cQueuedConnections.
// At the beginning of the next I/O cycle, they are added to cLiveConnections.
BOOL CPHandler::AddConnection(CPConnection *pConnection, SOCKET sNewSocket)
{
CONNECTION_ITERATOR IterSocket;
unsigned char *ucHS;
int nReturn;
// Check pointer
if (!pConnection)
return(FALSE);
// If not connected, forget it
if (pConnection->eConnected != CPConnection::CONNECT_SUCCESS
&& pConnection->eConnected != CPConnection::CONNECT_PENDING)
return(FALSE);
cCritQueuedMap.Lock();
// Assign the socket
if (sNewSocket != INVALID_SOCKET)
{
pConnection->sSocket = sNewSocket;
pConnection->SetBlockingMode(FALSE);
};
// Timestamp.
pConnection->tConnected = time(NULL);
// See if it's already there
IterSocket = cQueuedConnections.find(pConnection->cSignature);
// Found?
if (IterSocket != cQueuedConnections.end())
{ // This socket is already in the map
cCritQueuedMap.Unlock();
return(TRUE); // Although technically an error, for calling it twice, it IS in the map.
}
else
{ // Add
cQueuedConnections.insert(CONNECTION_MAP::value_type(pConnection->cSignature, pConnection));
pConnection->SetHandler(this);
if (pConnection->GetHandshake(&ucHS, nReturn))
pConnection->SendDataOut((char *) ucHS, 4096, nReturn);
iSize++;
};
cCritQueuedMap.Unlock();
return(TRUE);
};
// Remove a connection from cLiveConnections.
// This will wait until the next I/O cycle is complete.
// This is usually only called from a CPConnection
// destructor, since it has to wait to be removed before
// being deleted.
BOOL CPHandler::RemoveConnection(CPConnection *pConnection)
{
CONNECTION_ITERATOR IterSocket;
CONNECTION_ITERATOR IterSocket2;
// If it's not connected, forget it.
if (!pConnection)
return(FALSE);
// First see if it's in the queue map
cCritQueuedMap.Lock();
// See if it's already there
IterSocket = cQueuedConnections.find(pConnection->cSignature);
// Found?
if (IterSocket != cQueuedConnections.end())
{
IterSocket2 = IterSocket;
IterSocket2++;
cQueuedConnections.erase(IterSocket);
IterSocket = IterSocket2;
pConnection->SetHandler(NULL);
iSize--;
}
cCritQueuedMap.Unlock();
cCritMap.Lock();
// See if it's already there
IterSocket = cLiveConnections.find(pConnection->cSignature);
// Found?
if (IterSocket != cLiveConnections.end())
{
IterSocket2 = IterSocket;
IterSocket2++;
cLiveConnections.erase(IterSocket);
IterSocket = IterSocket2;
pConnection->SetHandler(NULL);
iSize--;
}
else
{ // Not found, which was the point, I guess.
}
cCritMap.Unlock();
// Call the sockets Disconnected member.
CallDisconnected(pConnection);
return(TRUE);
};
// Called in the case of an unrecoverable error
void CPHandler::DisconnectAll()
{
CPConnection *pConnection;
CONNECTION_ITERATOR IterSocket;
// Iterate through map
for (IterSocket = cLiveConnections.begin(); IterSocket != cLiveConnections.end(); IterSocket++)
{
pConnection = IterSocket->second;
// Defensive coding, this shouldn't happen.
pConnection->CloseSocket();
pConnection->Disconnected();
};
cLiveConnections.clear();
};
// Get the last size (not necesssarily current)
int CPHandler::GetLastSize()
{
return(iSize);
};
// Get the current size (will wait for critical section)
int CPHandler::GetCurrentSize()
{
return(iLiveSize);
};
// Call the cPConnection->HandleConnection
// This is here so derived classes can choose NOT to call it
// in case the handler is chained (like the CPClientHandler)
int CPHandler::CallHandleConnection(CPConnection *pConnection)
{
if (pConnection)
return(pConnection->HandleConnection());
else
return(0);
};
// If we want to call the sockets Disconnected member
void CPHandler::CallDisconnected(CPConnection *pConnection)
{
pConnection->Disconnected();
};