// // File : TRawSocketAcceptor.hh // // Purpose: Declaration of the class TRawSocketAcceptor // // $Id: TRawSocketAcceptor.hh 8758 2010-09-27 12:16:24Z mathes $ // $Revision: 8758 $ // $Date: 2010-09-27 14:16:24 +0200 (Mon, 27 Sep 2010) $ // /** @file TRawSocketAcceptor.hh * Declaration of the class TRawSocketAcceptor. * @author H.-J. Mathes, FzK */ #ifndef _FdRoot_TRawSocketAcceptor_hh_ #define _FdRoot_TRawSocketAcceptor_hh_ // --- forward declaration(s) class TMonitor; class TServerSocket; class TSocket; namespace FdRoot { /** This class implements a socket server for raw sockets, i.e. socket which * don't implement any specific protocol and thus don't know anything * about the data structures they exchange. */ class TRawSocketAcceptor { public: /** Constructor of class RawSocketAcceptor. * All internal variables are initialized and a new TMonitor * (socket monitor class) object is created. */ TRawSocketAcceptor(); /** Destructor of class RawSocketAcceptor. * The server socket and the TMonitor object are deleted. */ virtual ~TRawSocketAcceptor(); /** Called when a client has connected. */ virtual void Connect() = 0; /** Called when a client has disconnected. */ virtual void Disconnect() = 0; /** Get the current settings of the parameter 'message length'. * @see SetMessageLength() */ UInt_t GetMessageLength() { return fMsgLength; } /** Get the status of the servers last operation. */ Int_t GetStatus() { return fStatus; } /** Called to handle all (user defined) messages types. * Has to return kTRUE if message could be handled. * Otherwise the framework will call its own message handler. */ virtual Bool_t HandleRawMessage(char *,Int_t) = 0; // { cerr << "Message not handled !" << endl; return kFALSE; } /** Create a server socket on the specified port. * Method will return kTRUE if operation was successful, kFALSE otherwise. */ virtual Bool_t Open(Int_t port=4000); /** Run the server which might wait a specified time for a socket message. */ virtual void Run(Long_t millisec=20); /** Send a message via the socket from where a message was recently * received. * * The return value of this method is the return value of * the underlying TSocket::SendRaw(), i.e.: * @li returns the number of bytes sent * @li -1 in case of error. * @li -4 in case of kNoBlock and errno == EWOULDBLOCK * @li -5 (new) in case of invalid or non-existing socket */ virtual Int_t Send(const char*,Int_t); /** Set the maximum number of clients the server can handle. */ void SetMaxClients(UInt_t max_clients) { fMaxClients = max_clients; } /** Set the length of the message to be received via the socket. * The default value for this parameter is 1. */ void SetMessageLength(UInt_t msg_length) { fMsgLength = msg_length; } protected: /** accept() method of the socket server. * * After the accept() has been done, the return code of the operation as * well the newly created socket to the client are checked for errors. In * case of an error the socket to the cleint is deleted, otherwise the * _Connect() method is called. */ void _Accept(); /** Internal method of the TRawSocketAcceptor class. * * This method is called when a client has successfully connected to the * server. The client socket is then added to the TMonitor object for * further watching and if the maximum allowed number of clients is * already reached, the server socket is deactivated (= removed from the * list of sockets watched by the TMonitor. Then the Connect() method is * called giving derived classes the possibility to add own specific * 'connect' specific actions. */ void _Connect(); /** Internal method of the TRawSocketAcceptor class. * * This method is called when a client is normally or abnormally * disconnected. The socket where the disconnection occurred is removed * from the list of watched sockets of the TMonitor. Then the Disconnect() * method is called giving derived classes the possibility to add own * 'disconnect' specific actions. */ void _Disconnect(); /** Standard handler for raw messages. */ Bool_t _HandleRawMessage(char *,Int_t); private: TSocket* fCurrentSocket; // the socket of the current activity or // message Int_t fMaxClients; // maximum number of clients allowed UInt_t fMsgLength; // the length of the message to receive TMonitor* fMonitor; // the socket monitor object Int_t fNumClients; // number of clients currently connected TServerSocket* fServerSocket; // the servers prototype socket Int_t fStatus; // status of the last operation }; } // namespace FdRoot #endif // _FdRoot_TRawSocketAcceptor_hh_