2 // This file is part of the aMule Project.
4 // Copyright (c) 2003-2008 aMule Team ( admin@amule.org / http://www.amule.org )
5 // Copyright (c) 2002-2008 Merkur ( devs@emule-project.net / http://www.emule-project.net )
7 // Any parts of this program derived from the xMule, lMule or eMule project,
8 // or contributed by third-party developers are copyrighted by their
11 // This program is free software; you can redistribute it and/or modify
12 // it under the terms of the GNU General Public License as published by
13 // the Free Software Foundation; either version 2 of the License, or
14 // (at your option) any later version.
16 // This program is distributed in the hope that it will be useful,
17 // but WITHOUT ANY WARRANTY; without even the implied warranty of
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 // GNU General Public License for more details.
21 // You should have received a copy of the GNU General Public License
22 // along with this program; if not, write to the Free Software
23 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
26 #include "ListenSocket.h" // Interface declarations
28 #include <common/EventIDs.h>
30 #include "ClientTCPSocket.h" // Needed for CClientRequestSocket
31 #include "Logger.h" // Needed for AddLogLineM
32 #include "Statistics.h" // Needed for theStats
33 #include "Preferences.h" // Needed for CPreferences
34 #include "amule.h" // Needed for theApp
35 #include "ServerConnect.h" // Needed for CServerConnect
37 //-----------------------------------------------------------------------------
39 //-----------------------------------------------------------------------------
41 // This is the socket that listens to incomming connections in aMule's TCP port
42 // As soon as a connection is detected, it creates a new socket of type
43 // CClientTCPSocket to handle (accept) the connection.
46 CListenSocket::CListenSocket(wxIPaddress
&addr
, const CProxyData
*ProxyData
)
48 // wxSOCKET_NOWAIT - means non-blocking i/o
49 // wxSOCKET_REUSEADDR - means we can reuse the socket imediately (wx-2.5.3)
50 CSocketServerProxy(addr
, wxSOCKET_NOWAIT
|wxSOCKET_REUSEADDR
, ProxyData
)
52 // 0.42e - vars not used by us
55 m_OpenSocketsInterval
= 0;
56 m_nPeningConnections
= 0;
57 totalconnectionchecks
= 0;
58 averageconnections
= 0.0;
59 // Set the listen socket event handler -- The handler is written in amule.cpp
61 SetEventHandler(*theApp
, ID_LISTENSOCKET_EVENT
);
62 SetNotify(wxSOCKET_CONNECTION_FLAG
);
65 AddLogLineNS(_("ListenSocket: Ok."));
67 AddLogLineCS(_("ERROR: Could not listen to TCP port.") );
72 CListenSocket::~CListenSocket()
79 // No new sockets should have been opened by now
80 for (SocketSet::iterator it
= socket_list
.begin(); it
!= socket_list
.end(); it
++) {
81 wxASSERT((*it
)->OnDestroy());
89 bool CListenSocket::StartListening()
97 void CListenSocket::ReStartListening()
101 if (m_nPeningConnections
) {
102 m_nPeningConnections
--;
107 void CListenSocket::StopListening()
111 theStats::AddMaxConnectionLimitReached();
114 void CListenSocket::OnAccept(int nErrorCode
)
118 m_nPeningConnections
++;
119 if (m_nPeningConnections
< 1) {
121 m_nPeningConnections
= 1;
123 if (TooManySockets(true) && !theApp
->serverconnect
->IsConnecting()) {
126 } else if (bListening
== false) {
127 // If the client is still at maxconnections,
128 // this will allow it to go above it ...
129 // But if you don't, you will get a lowID on all servers.
132 // Deal with the pending connections, there might be more than one, due to
133 // the StopListening() call above.
134 while (m_nPeningConnections
) {
135 m_nPeningConnections
--;
136 // Create a new socket to deal with the connection
137 CClientTCPSocket
* newclient
= new CClientTCPSocket();
138 // Accept the connection and give it to the newly created socket
139 if (!AcceptWith(*newclient
, false)) {
140 newclient
->Safe_Delete();
142 wxASSERT(theApp
->IsRunning());
143 if (!newclient
->InitNetworkData()) {
144 // IP or port were not returned correctly
145 // from the accepted address, or filtered.
146 newclient
->Safe_Delete();
153 void CListenSocket::AddConnection()
155 m_OpenSocketsInterval
++;
158 void CListenSocket::Process()
160 // 042e + Kry changes for Destroy
161 m_OpenSocketsInterval
= 0;
162 SocketSet::iterator it
= socket_list
.begin();
163 while ( it
!= socket_list
.end() ) {
164 CClientTCPSocket
* cur_socket
= *it
++;
165 if (!cur_socket
->OnDestroy()) {
166 if (cur_socket
->ForDeletion()) {
167 cur_socket
->Destroy();
169 cur_socket
->CheckTimeOut();
174 if ((GetOpenSockets()+5 < thePrefs::GetMaxConnections() || theApp
->serverconnect
->IsConnecting()) && !bListening
) {
179 void CListenSocket::RecalculateStats()
182 memset(m_ConnectionStates
,0,6);
183 for (SocketSet::iterator it
= socket_list
.begin(); it
!= socket_list
.end(); ) {
184 CClientTCPSocket
* cur_socket
= *it
++;
185 switch (cur_socket
->GetConState()) {
186 case ES_DISCONNECTED
:
187 m_ConnectionStates
[0]++;
189 case ES_NOTCONNECTED
:
190 m_ConnectionStates
[1]++;
193 m_ConnectionStates
[2]++;
199 void CListenSocket::AddSocket(CClientTCPSocket
* toadd
)
202 socket_list
.insert(toadd
);
203 theStats::AddActiveConnection();
206 void CListenSocket::RemoveSocket(CClientTCPSocket
* todel
)
209 socket_list
.erase(todel
);
210 theStats::RemoveActiveConnection();
213 void CListenSocket::KillAllSockets()
215 // 0.42e reviewed - they use delete, but our safer is Destroy...
216 // But I bet it would be better to call Safe_Delete on the socket.
217 // Update: no... Safe_Delete MARKS for deletion. We need to delete it.
218 for (SocketSet::iterator it
= socket_list
.begin(); it
!= socket_list
.end(); ) {
219 CClientTCPSocket
* cur_socket
= *it
++;
220 if (cur_socket
->GetClient()) {
221 cur_socket
->Safe_Delete_Client();
223 cur_socket
->Safe_Delete();
224 cur_socket
->Destroy();
229 bool CListenSocket::TooManySockets(bool bIgnoreInterval
)
231 if (GetOpenSockets() > thePrefs::GetMaxConnections() || (m_OpenSocketsInterval
> (thePrefs::GetMaxConperFive()*GetMaxConperFiveModifier()) && !bIgnoreInterval
)) {
238 bool CListenSocket::IsValidSocket(CClientTCPSocket
* totest
)
241 return socket_list
.find(totest
) != socket_list
.end();
245 void CListenSocket::UpdateConnectionsStatus()
247 // 0.42e xcept for the khaos stats
248 if( theApp
->IsConnected() ) {
249 totalconnectionchecks
++;
251 percent
= (float)(totalconnectionchecks
-1)/(float)totalconnectionchecks
;
252 if( percent
> .99f
) {
255 averageconnections
= (averageconnections
*percent
) + (float)GetOpenSockets()*(1.0f
-percent
);
260 float CListenSocket::GetMaxConperFiveModifier()
262 float SpikeSize
= GetOpenSockets() - averageconnections
;
263 if ( SpikeSize
< 1 ) {
267 float SpikeTolerance
= 2.5f
*thePrefs::GetMaxConperFive();
268 if ( SpikeSize
> SpikeTolerance
) {
272 return 1.0f
- (SpikeSize
/SpikeTolerance
);
274 // File_checked_for_headers