Fix bad #define
[amule.git] / src / ListenSocket.cpp
blobe7bb4a58794f2a47434e29d17ae3f4eb23b68892
1 //
2 // This file is part of the aMule Project.
3 //
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 )
6 //
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
9 // respective authors.
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 //-----------------------------------------------------------------------------
38 // CListenSocket
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
53 bListening = false;
54 shutdown = false;
55 m_OpenSocketsInterval = 0;
56 m_nPendingConnections = 0;
57 totalconnectionchecks = 0;
58 averageconnections = 0.0;
59 // Set the listen socket event handler -- The handler is written in amule.cpp
60 if (IsOk()) {
61 SetEventHandler(*theApp, ID_LISTENSOCKET_EVENT);
62 SetNotify(wxSOCKET_CONNECTION_FLAG);
63 Notify(true);
65 AddLogLineNS(_("ListenSocket: Ok."));
66 } else {
67 AddLogLineCS(_("ERROR: Could not listen to TCP port.") );
72 CListenSocket::~CListenSocket()
74 shutdown = true;
75 Discard();
76 Close();
78 #ifdef __DEBUG__
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());
83 #endif
85 KillAllSockets();
89 bool CListenSocket::StartListening()
91 // 0.42e
92 bListening = true;
94 return true;
97 void CListenSocket::ReStartListening()
99 // 0.42e
100 bListening = true;
101 if (m_nPendingConnections) {
102 m_nPendingConnections--;
103 OnAccept(0);
107 void CListenSocket::StopListening()
109 // 0.42e
110 bListening = false;
111 theStats::AddMaxConnectionLimitReached();
114 void CListenSocket::OnAccept(int nErrorCode)
116 // 0.42e
117 if (!nErrorCode) {
118 m_nPendingConnections++;
119 if (m_nPendingConnections < 1) {
120 wxFAIL;
121 m_nPendingConnections = 1;
123 if (TooManySockets(true) && !theApp->serverconnect->IsConnecting()) {
124 StopListening();
125 return;
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.
130 ReStartListening();
132 // Deal with the pending connections, there might be more than one, due to
133 // the StopListening() call above.
134 while (m_nPendingConnections) {
135 m_nPendingConnections--;
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();
141 } else {
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();
168 } else {
169 cur_socket->CheckTimeOut();
174 if ((GetOpenSockets()+5 < thePrefs::GetMaxConnections() || theApp->serverconnect->IsConnecting()) && !bListening) {
175 ReStartListening();
179 void CListenSocket::RecalculateStats()
181 // 0.42e
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]++;
188 break;
189 case ES_NOTCONNECTED:
190 m_ConnectionStates[1]++;
191 break;
192 case ES_CONNECTED:
193 m_ConnectionStates[2]++;
194 break;
199 void CListenSocket::AddSocket(CClientTCPSocket* toadd)
201 wxASSERT(toadd);
202 socket_list.insert(toadd);
203 theStats::AddActiveConnection();
206 void CListenSocket::RemoveSocket(CClientTCPSocket* todel)
208 wxASSERT(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();
222 } else {
223 cur_socket->Safe_Delete();
224 cur_socket->Destroy();
229 bool CListenSocket::TooManySockets(bool bIgnoreInterval)
231 if (GetOpenSockets() > thePrefs::GetMaxConnections()
232 || (!bIgnoreInterval && m_OpenSocketsInterval > (thePrefs::GetMaxConperFive() * GetMaxConperFiveModifier()))) {
233 return true;
234 } else {
235 return false;
239 bool CListenSocket::IsValidSocket(CClientTCPSocket* totest)
241 // 0.42e
242 return socket_list.find(totest) != socket_list.end();
246 void CListenSocket::UpdateConnectionsStatus()
248 // 0.42e xcept for the khaos stats
249 if( theApp->IsConnected() ) {
250 totalconnectionchecks++;
251 float percent;
252 percent = (float)(totalconnectionchecks-1)/(float)totalconnectionchecks;
253 if( percent > .99f ) {
254 percent = .99f;
256 averageconnections = (averageconnections*percent) + (float)GetOpenSockets()*(1.0f-percent);
261 float CListenSocket::GetMaxConperFiveModifier()
263 float SpikeSize = GetOpenSockets() - averageconnections;
264 if ( SpikeSize < 1 ) {
265 return 1;
268 float SpikeTolerance = 2.5f*thePrefs::GetMaxConperFive();
269 if ( SpikeSize > SpikeTolerance ) {
270 return 0;
273 return 1.0f - (SpikeSize/SpikeTolerance);
275 // File_checked_for_headers