Upstream tarball 9574
[amule.git] / src / ServerConnect.h
blob8940d8cf8069e9010e6a032b8be683ce71ee5bff
1 //
2 // This file is part of the aMule Project.
3 //
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 )
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.
20 //
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
28 //
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
39 class CServerList;
40 class CServerSocket;
41 class CServer;
42 class CPacket;
43 class CServerUDPSocket;
45 #define CS_FATALERROR -5
46 #define CS_DISCONNECTED -4
47 #define CS_SERVERDEAD -3
48 #define CS_ERROR -2
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 {
59 public:
60 CServerConnect(CServerList* in_serverlist, amuleIPV4Address &address);
61 ~CServerConnect();
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);
75 // Creteil Begin
76 bool IsUDPSocketAvailable() const { return serverudpsocket != NULL; }
77 // Creteil End
79 bool SendUDPPacket(CPacket* packet,CServer* host, bool delpacket, bool rawpacket = false, uint16 port_offset = 4);
80 bool Disconnect();
81 bool IsConnecting() { return connecting; }
82 bool IsConnected() { return connected; }
83 uint32 GetClientID() { return clientid; }
84 CServer*GetCurrentServer();
85 uint32 clientid;
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;
97 /**
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);
107 private:
108 bool connecting;
109 bool singleconnecting;
110 bool connected;
111 int8 max_simcons;
112 bool m_bTryObfuscated;
113 CServerSocket* connectedsocket;
114 CServerList* used_list;
115 CServerUDPSocket* serverudpsocket;
117 // list of currently opened sockets
118 typedef std::list<CServerSocket*> SocketsList;
119 SocketsList m_lstOpenSockets;
120 CTimer m_idRetryTimer;
122 ServerSocketMap connectionattemps;
125 #endif // SERVERCONNECT_H
126 // File_checked_for_headers