-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathConnectionManager.h
More file actions
194 lines (172 loc) · 3.83 KB
/
ConnectionManager.h
File metadata and controls
194 lines (172 loc) · 3.83 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
#ifndef CONNECTIONMANAGER_H
#define CONNECTIONMANAGER_H
#include <list>
#include <iostream>
#include "ThreadLib.h"
#include "SocketLibTypes.h"
#include"SocketLibErrors.h"
#include "Connection.h"
#include "SocketSet.h"
namespace SocketLib
{
template<typename protocol>
class Connection;
template<typename protocol,typename defaulthandler>
class ConnectionManager
{
public:
typedef std::list<Connection<protocol>> clist;
typedef typename std::list<Connection<protocol>>::iterator clistitr;
ConnectionManager(int p_maxdatarate = 1024,
int p_sendtimeout = 60, int p_maxbuffered = 8192);
~ConnectionManager();
void NewConnection(DataSocket& p_socket);
inline int AvailableConnections() const
{
return MAX - (int)m_connections.size();
}
inline int TotalConnections() const
{
return (int)m_connections.size();
}
void CloseConnections();
void Listen();
void Send();
inline void Manage()
{
Listen();
Send();
CloseConnections();
}
protected:
void Close(clistitr p_itr);
clist m_connections;
int m_maxdatarate;
int m_sendtimeout;
int m_maxbuffered;
SocketSet m_set;
};
template<typename protocol, typename defaulthandler>
ConnectionManager<protocol, defaulthandler>::ConnectionManager(int p_maxdatarate,
int p_sendtimeout, int p_maxbuffered):
m_maxdatarate(p_maxdatarate),m_sendtimeout(p_sendtimeout),
m_maxbuffered(p_maxbuffered)
{
}
template<typename protocol, typename defaulthandler>
ConnectionManager<protocol, defaulthandler>::~ConnectionManager()
{
clistitr itr;
for (itr = m_connections.begin(); itr != m_connections.end(); ++itr)
{
itr->CloseSocket();
}
}
template<typename protocol, typename defaulthandler>
void ConnectionManager<protocol,defaulthandler>::
NewConnection(DataSocket& p_socket)
{
Connection<protocol> conn(p_socket);
if (AvailableConnections() == 0)
{
defaulthandler::NoRoom(conn);
conn.CloseSocket();
}
else
{
m_connections.push_back(conn);
Connection<protocol>& c = *m_connections.rbegin();
c.SetBlocking(false);
m_set.AddSocket(c);
// may be better? -- for "new"
c.AddHandler(new defaulthandler(c));
}
}
template<typename protocol, typename defaulthandler>
void ConnectionManager<protocol, defaulthandler>::Close(clistitr p_itr)
{
m_set.RemoveSocket(*p_itr);
p_itr->CloseSocket();
m_connections.erase(p_itr);
}
template<typename protocol, typename defaulthandler>
void ConnectionManager<protocol, defaulthandler>::Listen()
{
int socks = 0;
if (TotalConnections() > 0)
{
socks = m_set.Poll();
}
// any activity?
if (socks > 0)
{
clistitr itr = m_connections.begin();
clistitr c;
while (itr != m_connections.end())
{
c = itr++;
if (m_set.HasActivity(*c))
{
try
{
c->Receive();
if (c->GetCurrentDataRate() > m_maxdatarate)
{
c->Close();
c->Handler()->Flooded();
Close(c);
}
}
catch (...)
{
c->Close();
c->Handler()->Hungup();
Close(c);
}
}
}
}
}
template<typename protocol, typename defaulthandler>
void ConnectionManager<protocol, defaulthandler>::Send()
{
clistitr itr = m_connections.begin();
clistitr c;
while (itr != m_connections.end())
{
c = itr++;
try
{
c->SendBuffer();
if (c->GetBufferedBytes() > m_maxbuffered ||
c->GetLastSendTime() > m_sendtimeout)
{
c->Close();
c->Handler()->Hungup();
Close(c);
}
}
catch (...)
{
c->Close();
c->Handler()->Hungup();
Close(c);
}
}
}
template<typename protocol, typename defaulthandler>
void ConnectionManager<protocol, defaulthandler>::CloseConnections()
{
clistitr itr = m_connections.begin();
clistitr c;
while (itr != m_connections.end())
{
c = itr++;
if(c->Closed())
{
Close(c);
}
}
}
}
#endif // !CONNECTIONMANAGER_H