Planeshift

clients.h

Go to the documentation of this file.
00001 /*
00002  * clients.h - Author: Keith Fulton
00003  *
00004  * Copyright (C) 2001 Atomic Blue ([email protected], http://www.atomicblue.org)
00005  *
00006  *
00007  * This program is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU General Public License
00009  * as published by the Free Software Foundation (version 2 of the License)
00010  * This program is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  * GNU General Public License for more details.
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00017  *
00018  */
00019 #ifndef __CLIENTS_H__
00020 #define __CLIENTS_H__
00021 
00022 #include <csutil/hash.h>
00023 #include <csutil/threading/thread.h>
00024 
00025 #include "client.h"
00026 
00027 class ClientIterator;
00028 class iResultRow;
00029 
00030 class SockAddress
00031 {
00032 public:
00033     SockAddress(const SOCKADDR_IN &sock);
00034     bool operator< (const SockAddress &other) const;
00035 private:
00036     // Define a few operators as privat so that we know
00037     // that they are not used.
00038     bool operator> (const SockAddress &other) const;
00039     bool operator= (const SockAddress &other) const;
00040     bool operator== (const SockAddress &other) const;
00041 
00042 #ifdef INCLUDE_IPV6_SUPPORT
00043     SOCKADDR_IN addr;
00044 #else
00045     uint32_t port;
00046     uint32_t addr;
00047 #endif
00048 };
00049 
00050 template<> class csHashComputer<SockAddress> :
00051     public csHashComputerStruct<SockAddress> {};
00052 
00058 class ClientConnectionSet
00059 {
00060 public:
00061     typedef csHash<Client*, SockAddress> AddressHash;
00062 protected:
00063     friend class ClientIterator;
00064 
00065     AddressHash addrHash;
00066     csHash<Client*> hash;
00067     csPDelArray<Client> toDelete;
00068     CS::Threading::RecursiveMutex mutex;
00069 
00070 public:
00071     ClientConnectionSet();
00072     ~ClientConnectionSet();
00073 
00074     bool Initialize();
00075 
00076     Client* Add(LPSOCKADDR_IN addr);
00077 
00078     // Delete all clients marked to be deleted
00079     void SweepDelete();
00080 
00081     // Mark this client as ready to be deleted
00082     void MarkDelete(Client* client);
00083 
00085     size_t Count(void) const;
00086 
00088     size_t CountReadyPlayers() const;
00089 
00091     Client* FindAny(uint32_t id);
00093     Client* Find(uint32_t id);
00095     Client* Find(const char* name);
00097     Client* FindPlayer(PID playerID);
00099     Client* FindAccount(AccountID accountID, uint32_t excludeClient = 0);
00101     Client* Find(LPSOCKADDR_IN addr);
00102 
00103     csRef<NetPacketQueueRefCount> FindQueueAny(uint32_t id);
00104 };
00105 
00106 class ClientIterator : public ClientConnectionSet::AddressHash::GlobalIterator
00107 {
00108 public:
00109     ClientIterator(ClientConnectionSet &clients);
00110     ~ClientIterator();
00111 
00112 private:
00113 
00115     CS::Threading::RecursiveMutex &mutex;
00116 };
00117 
00118 
00119 #endif