TSocket.h

Go to the documentation of this file.
00001 // @(#)root/net:$Id: TSocket.h 35826 2010-09-28 09:38:08Z rdm $
00002 // Author: Fons Rademakers   18/12/96
00003 
00004 /*************************************************************************
00005  * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers.               *
00006  * All rights reserved.                                                  *
00007  *                                                                       *
00008  * For the licensing terms see $ROOTSYS/LICENSE.                         *
00009  * For the list of contributors see $ROOTSYS/README/CREDITS.             *
00010  *************************************************************************/
00011 
00012 #ifndef ROOT_TSocket
00013 #define ROOT_TSocket
00014 
00015 
00016 //////////////////////////////////////////////////////////////////////////
00017 //                                                                      //
00018 // TSocket                                                              //
00019 //                                                                      //
00020 // This class implements client sockets. A socket is an endpoint for    //
00021 // communication between two machines.                                  //
00022 // The actual work is done via the TSystem class (either TUnixSystem,   //
00023 // or TWinNTSystem).                                                    //
00024 //                                                                      //
00025 //////////////////////////////////////////////////////////////////////////
00026 
00027 #ifndef ROOT_TNamed
00028 #include "TNamed.h"
00029 #endif
00030 #ifndef ROOT_TBits
00031 #include "TBits.h"
00032 #endif
00033 #ifndef ROOT_TInetAddress
00034 #include "TInetAddress.h"
00035 #endif
00036 #ifndef ROOT_MessageTypes
00037 #include "MessageTypes.h"
00038 #endif
00039 #ifndef ROOT_TVirtualAuth
00040 #include "TVirtualAuth.h"
00041 #endif
00042 #ifndef ROOT_TSecContext
00043 #include "TSecContext.h"
00044 #endif
00045 #ifndef ROOT_TTimeStamp
00046 #include "TTimeStamp.h"
00047 #endif
00048 #ifndef ROOT_TVirtualMutex
00049 #include "TVirtualMutex.h"
00050 #endif
00051 
00052 enum ESockOptions {
00053    kSendBuffer,        // size of send buffer
00054    kRecvBuffer,        // size of receive buffer
00055    kOobInline,         // OOB message inline
00056    kKeepAlive,         // keep socket alive
00057    kReuseAddr,         // allow reuse of local portion of address 5-tuple
00058    kNoDelay,           // send without delay
00059    kNoBlock,           // non-blocking I/O
00060    kProcessGroup,      // socket process group (used for SIGURG and SIGIO)
00061    kAtMark,            // are we at out-of-band mark (read only)
00062    kBytesToRead        // get number of bytes to read, FIONREAD (read only)
00063 };
00064 
00065 enum ESendRecvOptions {
00066    kDefault,           // default option (= 0)
00067    kOob,               // send or receive out-of-band data
00068    kPeek,              // peek at incoming message (receive only)
00069    kDontBlock          // send/recv as much data as possible without blocking
00070 };
00071 
00072 
00073 class TMessage;
00074 class THostAuth;
00075 
00076 class TSocket : public TNamed {
00077 
00078 friend class TServerSocket;
00079 friend class TProofServ;   // to be able to call SetDescriptor(), RecvHostAuth()
00080 friend class TSlave;       // to be able to call SendHostAuth()
00081 
00082 public:
00083    enum EStatusBits { kIsUnix = BIT(16) };   // set if unix socket
00084    enum EInterest { kRead = 1, kWrite = 2 };
00085    enum EServiceType { kSOCKD, kROOTD, kPROOFD };
00086 
00087 protected:
00088    TInetAddress  fAddress;        // remote internet address and port #
00089    UInt_t        fBytesRecv;      // total bytes received over this socket
00090    UInt_t        fBytesSent;      // total bytes sent using this socket
00091    Int_t         fCompress;       // compression level from 0 (not compressed)
00092                                   // to 9 (max compression)
00093    TInetAddress  fLocalAddress;   // local internet address and port #
00094    Int_t         fRemoteProtocol; // protocol of remote daemon
00095    TSecContext  *fSecContext;     // after a successful Authenticate call
00096                                   // points to related security context
00097    TString       fService;        // name of service (matches remote port #)
00098    EServiceType  fServType;       // remote service type
00099    Int_t         fSocket;         // socket descriptor
00100    Int_t         fTcpWindowSize;  // TCP window size (default 65535);
00101    TString       fUrl;            // needs this for special authentication options
00102    TBits         fBitsInfo;       // bits array to mark TStreamerInfo classes already sent
00103    TList        *fUUIDs;          // list of TProcessIDs already sent through the socket
00104 
00105    TVirtualMutex *fLastUsageMtx;   // Protect last usage setting / reading
00106    TTimeStamp    fLastUsage;      // Time stamp of last usage
00107 
00108    static ULong64_t fgBytesRecv;  // total bytes received by all socket objects
00109    static ULong64_t fgBytesSent;  // total bytes sent by all socket objects
00110 
00111    static Int_t  fgClientProtocol; // client "protocol" version
00112 
00113    TSocket() : fAddress(), fBytesRecv(0), fBytesSent(0), fCompress(0),
00114                fLocalAddress(), fRemoteProtocol(), fSecContext(0), fService(),
00115                fServType(kSOCKD), fSocket(-1), fTcpWindowSize(0), fUrl(),
00116      fBitsInfo(), fUUIDs(0), fLastUsageMtx(0), fLastUsage() { }
00117 
00118    Bool_t       Authenticate(const char *user);
00119    void         SetDescriptor(Int_t desc) { fSocket = desc; }
00120    void         SendStreamerInfos(const TMessage &mess);
00121    Bool_t       RecvStreamerInfos(TMessage *mess);
00122    void         SendProcessIDs(const TMessage &mess);
00123    Bool_t       RecvProcessIDs(TMessage *mess);
00124 
00125 private:
00126    TSocket&      operator=(const TSocket &);  // not implemented
00127    Option_t     *GetOption() const { return TObject::GetOption(); }
00128 
00129 public:
00130    TSocket(TInetAddress address, const char *service, Int_t tcpwindowsize = -1);
00131    TSocket(TInetAddress address, Int_t port, Int_t tcpwindowsize = -1);
00132    TSocket(const char *host, const char *service, Int_t tcpwindowsize = -1);
00133    TSocket(const char *host, Int_t port, Int_t tcpwindowsize = -1);
00134    TSocket(const char *sockpath);
00135    TSocket(Int_t descriptor);
00136    TSocket(Int_t descriptor, const char *sockpath);
00137    TSocket(const TSocket &s);
00138    virtual ~TSocket() { Close(); }
00139 
00140    virtual void          Close(Option_t *opt="");
00141    virtual Int_t         GetDescriptor() const { return fSocket; }
00142    TInetAddress          GetInetAddress() const { return fAddress; }
00143    virtual TInetAddress  GetLocalInetAddress();
00144    Int_t                 GetPort() const { return fAddress.GetPort(); }
00145    const char           *GetService() const { return fService; }
00146    Int_t                 GetServType() const { return (Int_t)fServType; }
00147    virtual Int_t         GetLocalPort();
00148    UInt_t                GetBytesSent() const { return fBytesSent; }
00149    UInt_t                GetBytesRecv() const { return fBytesRecv; }
00150    Int_t                 GetCompressionLevel() const { return fCompress; }
00151    Int_t                 GetErrorCode() const;
00152    virtual Int_t         GetOption(ESockOptions opt, Int_t &val);
00153    Int_t                 GetRemoteProtocol() const { return fRemoteProtocol; }
00154    TSecContext          *GetSecContext() const { return fSecContext; }
00155    Int_t                 GetTcpWindowSize() const { return fTcpWindowSize; }
00156    TTimeStamp            GetLastUsage() { R__LOCKGUARD2(fLastUsageMtx); return fLastUsage; }
00157    const char           *GetUrl() const { return fUrl; }
00158    virtual Bool_t        IsAuthenticated() const { return fSecContext ? kTRUE : kFALSE; }
00159    virtual Bool_t        IsValid() const { return fSocket < 0 ? kFALSE : kTRUE; }
00160    virtual Int_t         Recv(TMessage *&mess);
00161    virtual Int_t         Recv(Int_t &status, Int_t &kind);
00162    virtual Int_t         Recv(char *mess, Int_t max);
00163    virtual Int_t         Recv(char *mess, Int_t max, Int_t &kind);
00164    virtual Int_t         RecvRaw(void *buffer, Int_t length, ESendRecvOptions opt = kDefault);
00165    virtual Int_t         Reconnect() { return -1; }
00166    virtual Int_t         Select(Int_t interest = kRead, Long_t timeout = -1);
00167    virtual Int_t         Send(const TMessage &mess);
00168    virtual Int_t         Send(Int_t kind);
00169    virtual Int_t         Send(Int_t status, Int_t kind);
00170    virtual Int_t         Send(const char *mess, Int_t kind = kMESS_STRING);
00171    virtual Int_t         SendObject(const TObject *obj, Int_t kind = kMESS_OBJECT);
00172    virtual Int_t         SendRaw(const void *buffer, Int_t length,
00173                                  ESendRecvOptions opt = kDefault);
00174    void                  SetCompressionLevel(Int_t level = 1);
00175    virtual Int_t         SetOption(ESockOptions opt, Int_t val);
00176    void                  SetRemoteProtocol(Int_t rproto) { fRemoteProtocol = rproto; }
00177    void                  SetSecContext(TSecContext *ctx) { fSecContext = ctx; }
00178    void                  SetService(const char *service) { fService = service; }
00179    void                  SetServType(Int_t st) { fServType = (EServiceType)st; }
00180    void                  SetUrl(const char *url) { fUrl = url; }
00181 
00182    void                  Touch() { R__LOCKGUARD2(fLastUsageMtx); fLastUsage.Set(); }
00183 
00184    static Int_t          GetClientProtocol();
00185 
00186    static ULong64_t      GetSocketBytesSent();
00187    static ULong64_t      GetSocketBytesRecv();
00188 
00189    static TSocket       *CreateAuthSocket(const char *user, const char *host,
00190                                           Int_t port, Int_t size = 0,
00191                                           Int_t tcpwindowsize = -1, TSocket *s = 0);
00192    static TSocket       *CreateAuthSocket(const char *url, Int_t size = 0,
00193                                           Int_t tcpwindowsize = -1, TSocket *s = 0);
00194    static void           NetError(const char *where, Int_t error);
00195 
00196    ClassDef(TSocket,0)  //This class implements client sockets
00197 };
00198 
00199 #endif

Generated on Tue Jul 5 14:28:11 2011 for ROOT_528-00b_version by  doxygen 1.5.1