00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #ifndef FXNETWORKSERVICE_H
00023 #define FXNETWORKSERVICE_H
00024
00025 #include "FXTime.h"
00026 #include "QThread.h"
00027 #include "FXStream.h"
00028 #include "QBlkSocket.h"
00029 #include <qvaluelist.h>
00030
00031 namespace FX {
00032
00041 struct FXNetworkServiceClient
00042 {
00043 FXTime firstSeen;
00044 FXTime lastSeen;
00045 FXTime bannedUntil;
00046 FXuint refusedCount;
00047 QValueList<QThreadPool::handle> threadhs;
00048 void *data;
00049 FXNetworkServiceClient() : data(0) { }
00050 };
00076 struct FXNetworkServicePrivate;
00077 class FXAPIR FXNetworkService : protected QMutex, public QThread
00078 {
00079 FXNetworkServicePrivate *p;
00080 FXNetworkService(const FXNetworkService &);
00081 FXNetworkService &operator=(const FXNetworkService &);
00082 public:
00084 enum Action
00085 {
00086 ACCEPTED=0,
00087 REFUSE,
00088 BAN,
00089 DELETERECORD
00090 };
00092 typedef Generic::Functor<Generic::TL::create<Action, FXNetworkService *, QBlkSocket *>::value> NewClientSpec;
00094 FXNetworkService(QBlkSocket *serversocket, NewClientSpec newclientv, QThreadPool *dispatch=0, FXuint lrucachesize=1000, FXuint maxclients=0, bool acceptExternalClients=true, const char *threadname="Network Service Monitor Thread");
00095 ~FXNetworkService();
00096
00098 QBlkSocket *serverSocket() const throw();
00100 void setServerSocket(QBlkSocket *s);
00102 QThreadPool *dispatchPool() const throw();
00104 void setDispatchPool(QThreadPool *pool);
00106 FXuint maxClients() const throw();
00108 void setMaxClients(FXuint no=0);
00110 FXuint maxClientsPerIP() const throw();
00112 void setMaxClientsPerIP(FXuint no=8);
00114 float maxClientAttemptsPerMinute() const throw();
00116 void setMaxClientAttemptsPerMinute(float max=1.0);
00118 FXTime banPeriod() const;
00120 void setBanPeriod(const FXTime &period=FXTime(FXTime::micsPerHour));
00122 struct IPMask { QHostAddress mask, XOR; IPMask(const QHostAddress &_mask, const QHostAddress &_xor) : mask(_mask), XOR(_xor) { } };
00124 QMemArray<IPMask> bannedIPMasks() const;
00126 void setBannedIPMasks(const QMemArray<IPMask> &list);
00128 QHostAddressDict<FXNetworkServiceClient> IPClientRecords() const;
00130 FXNetworkServiceClient IPClientRecord(const QHostAddress &a) const;
00131 protected:
00133 FXLRUCache< QHostAddressDict<FXNetworkServiceClient> > &rawIPClientRecords();
00135 virtual QThreadPool::handle newClientUpcall(FXNetworkService *service, QBlkSocket *skt, FXNetworkServiceClient &record, Action &action, NewClientSpec &newclientv);
00136
00137 virtual void run();
00138 virtual void *cleanup();
00139 };
00140
00141
00142 }
00143
00144 #endif