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
27 // Client to Server communication
30 #ifndef SERVERCONNECT_H
31 #define SERVERCONNECT_H
34 #include "amuleIPV4Address.h" // Needed for amuleIPV4Address
35 #include "Timer.h" // Needed for CTimer
37 #include <map> // Needed for std::map
43 class CServerUDPSocket
;
45 #define CS_FATALERROR -5
46 #define CS_DISCONNECTED -4
47 #define CS_SERVERDEAD -3
49 #define CS_SERVERFULL -1
50 #define CS_NOTCONNECTED 0
51 #define CS_CONNECTING 1
52 #define CS_CONNECTED 2
53 #define CS_WAITFORLOGIN 3
54 #define CS_RETRYCONNECTTIME 30 // seconds
56 typedef std::map
<uint32
, CServerSocket
*> ServerSocketMap
;
58 class CServerConnect
{
60 CServerConnect(CServerList
* in_serverlist
, amuleIPV4Address
&address
);
63 void ConnectionFailed(CServerSocket
* sender
);
64 void ConnectionEstablished(CServerSocket
* sender
);
66 void ConnectToAnyServer(bool prioSort
= true, bool bNoCrypt
= false);
67 void ConnectToServer(CServer
* toconnect
, bool multiconnect
= false, bool bNoCrypt
= false);
68 void StopConnectionTry();
69 void CheckForTimeout();
71 // safe socket closure and destruction
72 void DestroySocket(CServerSocket
* pSck
);
73 bool SendPacket(CPacket
* packet
,bool delpacket
= true, CServerSocket
* to
= 0);
76 bool IsUDPSocketAvailable() const { return serverudpsocket
!= NULL
; }
79 bool SendUDPPacket(CPacket
* packet
,CServer
* host
, bool delpacket
, bool rawpacket
= false, uint16 port_offset
= 4);
81 bool IsConnecting() { return connecting
; }
82 bool IsConnected() { return connected
; }
83 uint32
GetClientID() { return clientid
; }
84 CServer
*GetCurrentServer();
86 uint8 pendingConnects
;
87 bool IsLowID() { return ::IsLowID(clientid
); }
88 void SetClientID(uint32 newid
);
89 bool IsLocalServer(uint32 dwIP
, uint16 nPort
);
90 void TryAnotherConnectionrequest();
91 bool IsSingleConnect() { return singleconnecting
; }
92 void KeepConnectionAlive();
94 bool AwaitingTestFromIP(uint32 ip
);
95 bool IsConnectedObfuscated() const;
98 * Called when a socket has been DNS resolved.
100 * @param socket The socket object requesting DNS resolution.
101 * @param ip The found IP, or zero on error.
103 * Note that 'socket' may or may not refer to an valid object,
104 * and should be checked before being used.
106 void OnServerHostnameResolved(void* socket
, uint32 ip
);
109 bool singleconnecting
;
112 bool m_bTryObfuscated
;
113 bool m_recurseTryAnotherConnectionrequest
;
114 CServerSocket
* connectedsocket
;
115 CServerList
* used_list
;
116 CServerUDPSocket
* serverudpsocket
;
118 // list of currently opened sockets
119 typedef std::list
<CServerSocket
*> SocketsList
;
120 SocketsList m_lstOpenSockets
;
121 CTimer m_idRetryTimer
;
123 ServerSocketMap connectionattemps
;
126 #endif // SERVERCONNECT_H
127 // File_checked_for_headers