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 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
36 #include "updownclient.h" // Needed for CUpDownClient
38 //-----------------------------------------------------------------------------
40 //-----------------------------------------------------------------------------
42 // This is the socket that listens to incomming connections in aMule's TCP port
43 // As soon as a connection is detected, it creates a new socket of type
44 // CClientTCPSocket to handle (accept) the connection.
47 CListenSocket::CListenSocket(wxIPaddress
&addr
, const CProxyData
*ProxyData
)
49 // wxSOCKET_NOWAIT - means non-blocking i/o
50 // wxSOCKET_REUSEADDR - means we can reuse the socket imediately (wx-2.5.3)
51 CSocketServerProxy(addr
, wxSOCKET_NOWAIT
|wxSOCKET_REUSEADDR
, ProxyData
)
53 // 0.42e - vars not used by us
56 m_OpenSocketsInterval
= 0;
57 m_nPeningConnections
= 0;
58 totalconnectionchecks
= 0;
59 averageconnections
= 0.0;
60 // Set the listen socket event handler -- The handler is written in amule.cpp
62 SetEventHandler(*theApp
, ID_LISTENSOCKET_EVENT
);
63 SetNotify(wxSOCKET_CONNECTION_FLAG
);
66 printf("ListenSocket: Ok.\n");
68 AddLogLineM( true, _("Error: Could not listen to TCP port.") );
69 printf("ListenSocket: Could not listen to TCP port.");
74 CListenSocket::~CListenSocket()
81 // No new sockets should have been opened by now
82 for (SocketSet::iterator it
= socket_list
.begin(); it
!= socket_list
.end(); it
++) {
83 wxASSERT((*it
)->OnDestroy());
91 bool CListenSocket::StartListening()
99 void CListenSocket::ReStartListening()
103 if (m_nPeningConnections
) {
104 m_nPeningConnections
--;
109 void CListenSocket::StopListening()
113 theStats::AddMaxConnectionLimitReached();
116 void CListenSocket::OnAccept(int nErrorCode
)
120 m_nPeningConnections
++;
121 if (m_nPeningConnections
< 1) {
123 m_nPeningConnections
= 1;
125 if (TooManySockets(true) && !theApp
->serverconnect
->IsConnecting()) {
128 } else if (bListening
== false) {
129 // If the client is still at maxconnections,
130 // this will allow it to go above it ...
131 // But if you don't, you will get a lowID on all servers.
134 // Deal with the pending connections, there might be more than one, due to
135 // the StopListening() call above.
136 while (m_nPeningConnections
) {
137 m_nPeningConnections
--;
138 // Create a new socket to deal with the connection
139 CClientTCPSocket
* newclient
= new CClientTCPSocket();
140 // Accept the connection and give it to the newly created socket
141 if (!AcceptWith(*newclient
, false)) {
142 newclient
->Safe_Delete();
144 wxASSERT(theApp
->IsRunning());
145 if (!newclient
->InitNetworkData()) {
146 // IP or port were not returned correctly
147 // from the accepted address, or filtered.
148 newclient
->Safe_Delete();
155 void CListenSocket::AddConnection()
157 m_OpenSocketsInterval
++;
160 void CListenSocket::Process()
162 // 042e + Kry changes for Destroy
163 m_OpenSocketsInterval
= 0;
164 SocketSet::iterator it
= socket_list
.begin();
165 while ( it
!= socket_list
.end() ) {
166 CClientTCPSocket
* cur_socket
= *it
++;
167 if (!cur_socket
->OnDestroy()) {
168 if (cur_socket
->ForDeletion()) {
169 cur_socket
->Destroy();
171 cur_socket
->CheckTimeOut();
176 if ((GetOpenSockets()+5 < thePrefs::GetMaxConnections() || theApp
->serverconnect
->IsConnecting()) && !bListening
) {
181 void CListenSocket::RecalculateStats()
184 memset(m_ConnectionStates
,0,6);
185 for (SocketSet::iterator it
= socket_list
.begin(); it
!= socket_list
.end(); ) {
186 CClientTCPSocket
* cur_socket
= *it
++;
187 switch (cur_socket
->GetConState()) {
188 case ES_DISCONNECTED
:
189 m_ConnectionStates
[0]++;
191 case ES_NOTCONNECTED
:
192 m_ConnectionStates
[1]++;
195 m_ConnectionStates
[2]++;
201 void CListenSocket::AddSocket(CClientTCPSocket
* toadd
)
204 socket_list
.insert(toadd
);
205 theStats::AddActiveConnection();
208 void CListenSocket::RemoveSocket(CClientTCPSocket
* todel
)
211 socket_list
.erase(todel
);
212 theStats::RemoveActiveConnection();
215 void CListenSocket::KillAllSockets()
217 // 0.42e reviewed - they use delete, but our safer is Destroy...
218 // But I bet it would be better to call Safe_Delete on the socket.
219 // Update: no... Safe_Delete MARKS for deletion. We need to delete it.
220 for (SocketSet::iterator it
= socket_list
.begin(); it
!= socket_list
.end(); ) {
221 CClientTCPSocket
* cur_socket
= *it
++;
222 if (cur_socket
->GetClient()) {
223 cur_socket
->GetClient()->Safe_Delete();
225 cur_socket
->Safe_Delete();
226 cur_socket
->Destroy();
231 bool CListenSocket::TooManySockets(bool bIgnoreInterval
)
233 if (GetOpenSockets() > thePrefs::GetMaxConnections() || (m_OpenSocketsInterval
> (thePrefs::GetMaxConperFive()*GetMaxConperFiveModifier()) && !bIgnoreInterval
)) {
240 bool CListenSocket::IsValidSocket(CClientTCPSocket
* totest
)
243 return socket_list
.find(totest
) != socket_list
.end();
247 void CListenSocket::UpdateConnectionsStatus()
249 // 0.42e xcept for the khaos stats
250 if( theApp
->IsConnected() ) {
251 totalconnectionchecks
++;
253 percent
= (float)(totalconnectionchecks
-1)/(float)totalconnectionchecks
;
254 if( percent
> .99f
) {
257 averageconnections
= (averageconnections
*percent
) + (float)GetOpenSockets()*(1.0f
-percent
);
262 float CListenSocket::GetMaxConperFiveModifier()
264 float SpikeSize
= GetOpenSockets() - averageconnections
;
265 if ( SpikeSize
< 1 ) {
269 float SpikeTolerance
= 2.5f
*thePrefs::GetMaxConperFive();
270 if ( SpikeSize
> SpikeTolerance
) {
274 return 1.0f
- (SpikeSize
/SpikeTolerance
);
276 // File_checked_for_headers