FXNetworkService.h

Go to the documentation of this file.
00001 /********************************************************************************
00002 *                                                                               *
00003 *                                Network Services                               *
00004 *                                                                               *
00005 *********************************************************************************
00006 *        Copyright (C) 2009 by Niall Douglas.   All Rights Reserved.            *
00007 *       NOTE THAT I DO NOT PERMIT ANY OF MY CODE TO BE PROMOTED TO THE GPL      *
00008 *********************************************************************************
00009 * This code is free software; you can redistribute it and/or modify it under    *
00010 * the terms of the GNU Library General Public License v2.1 as published by the  *
00011 * Free Software Foundation EXCEPT that clause 3 does not apply ie; you may not  *
00012 * "upgrade" this code to the GPL without my prior written permission.           *
00013 * Please consult the file "License_Addendum2.txt" accompanying this file.       *
00014 *                                                                               *
00015 * This code is distributed in the hope that it will be useful,                  *
00016 * but WITHOUT ANY WARRANTY; without even the implied warranty of                *
00017 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.                          *
00018 *********************************************************************************
00019 * $Id:                                                                          *
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 } // namespace
00143 
00144 #endif

(C) 2002-2009 Niall Douglas. Some parts (C) to assorted authors.
Generated on Fri Nov 20 18:31:23 2009 for TnFOX by doxygen v1.4.7