2 // This file is part of the aMule Project.
4 // Copyright (c) 2003-2011 aMule Team ( admin@amule.org / http://www.amule.org )
5 // Copyright (c) 2002-2011 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 incoming 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(amuleIPV4Address
&addr
, const CProxyData
*ProxyData
)
48 // wxSOCKET_NOWAIT - means non-blocking i/o
49 // wxSOCKET_REUSEADDR - means we can reuse the socket immediately (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 totalconnectionchecks
= 0;
57 averageconnections
= 0.0;
58 memset(m_ConnectionStates
, 0, 3 * sizeof(m_ConnectionStates
[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
)->IsDestroying());
89 void CListenSocket::OnAccept()
91 m_pending
= theApp
->IsRunning(); // just do nothing if we are shutting down
92 // If the client is still at maxconnections,
93 // this will allow it to go above it ...
94 // But if you don't, you will get a lowID on all servers.
95 while (m_pending
&& (theApp
->serverconnect
->IsConnecting() || !TooManySockets())) {
96 if (!SocketAvailable()) {
99 // Create a new socket to deal with the connection
100 CClientTCPSocket
* newclient
= new CClientTCPSocket();
101 // Accept the connection and give it to the newly created socket
102 if (!AcceptWith(*newclient
, false)) {
103 newclient
->Safe_Delete();
106 if (!newclient
->InitNetworkData()) {
107 // IP or port were not returned correctly
108 // from the accepted address, or filtered.
109 newclient
->Safe_Delete();
115 theStats::AddMaxConnectionLimitReached();
119 void CListenSocket::AddConnection()
121 m_OpenSocketsInterval
++;
124 void CListenSocket::Process()
126 // 042e + Kry changes for Destroy
127 m_OpenSocketsInterval
= 0;
128 SocketSet::iterator it
= socket_list
.begin();
129 while ( it
!= socket_list
.end() ) {
130 CClientTCPSocket
* cur_socket
= *it
++;
131 if (!cur_socket
->IsDestroying()) {
132 cur_socket
->CheckTimeOut();
141 void CListenSocket::RecalculateStats()
144 memset(m_ConnectionStates
, 0, 3 * sizeof(m_ConnectionStates
[0]));
145 for (SocketSet::iterator it
= socket_list
.begin(); it
!= socket_list
.end(); ) {
146 CClientTCPSocket
* cur_socket
= *it
++;
147 switch (cur_socket
->GetConState()) {
148 case ES_DISCONNECTED
:
149 m_ConnectionStates
[0]++;
151 case ES_NOTCONNECTED
:
152 m_ConnectionStates
[1]++;
155 m_ConnectionStates
[2]++;
161 void CListenSocket::AddSocket(CClientTCPSocket
* toadd
)
164 socket_list
.insert(toadd
);
165 theStats::AddActiveConnection();
168 void CListenSocket::RemoveSocket(CClientTCPSocket
* todel
)
171 socket_list
.erase(todel
);
172 theStats::RemoveActiveConnection();
175 void CListenSocket::KillAllSockets()
177 // 0.42e reviewed - they use delete, but our safer is Destroy...
178 // But I bet it would be better to call Safe_Delete on the socket.
179 // Update: no... Safe_Delete MARKS for deletion. We need to delete it.
180 for (SocketSet::iterator it
= socket_list
.begin(); it
!= socket_list
.end(); ) {
181 CClientTCPSocket
* cur_socket
= *it
++;
182 if (cur_socket
->GetClient()) {
183 cur_socket
->Safe_Delete_Client();
185 cur_socket
->Safe_Delete();
186 cur_socket
->Destroy();
191 bool CListenSocket::TooManySockets(bool bIgnoreInterval
)
193 if (GetOpenSockets() > thePrefs::GetMaxConnections()
194 || (!bIgnoreInterval
&& m_OpenSocketsInterval
> (thePrefs::GetMaxConperFive() * GetMaxConperFiveModifier()))) {
201 bool CListenSocket::IsValidSocket(CClientTCPSocket
* totest
)
204 return socket_list
.find(totest
) != socket_list
.end();
208 void CListenSocket::UpdateConnectionsStatus()
210 // 0.42e xcept for the khaos stats
211 if( theApp
->IsConnected() ) {
212 totalconnectionchecks
++;
214 percent
= (float)(totalconnectionchecks
-1)/(float)totalconnectionchecks
;
215 if( percent
> .99f
) {
218 averageconnections
= (averageconnections
*percent
) + (float)GetOpenSockets()*(1.0f
-percent
);
223 float CListenSocket::GetMaxConperFiveModifier()
225 float SpikeSize
= GetOpenSockets() - averageconnections
;
226 if ( SpikeSize
< 1 ) {
230 float SpikeTolerance
= 2.5f
* thePrefs::GetMaxConperFive();
231 if ( SpikeSize
> SpikeTolerance
) {
235 return 1.0f
- (SpikeSize
/SpikeTolerance
);
237 // File_checked_for_headers