Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:12:25

0001 // @(#)root/net:$Id$
0002 // Author: Fons Rademakers   18/12/96
0003 
0004 /*************************************************************************
0005  * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers.               *
0006  * All rights reserved.                                                  *
0007  *                                                                       *
0008  * For the licensing terms see $ROOTSYS/LICENSE.                         *
0009  * For the list of contributors see $ROOTSYS/README/CREDITS.             *
0010  *************************************************************************/
0011 
0012 #ifndef ROOT_TSocket
0013 #define ROOT_TSocket
0014 
0015 
0016 //////////////////////////////////////////////////////////////////////////
0017 //                                                                      //
0018 // TSocket                                                              //
0019 //                                                                      //
0020 // This class implements client sockets. A socket is an endpoint for    //
0021 // communication between two machines.                                  //
0022 // The actual work is done via the TSystem class (either TUnixSystem,   //
0023 // or TWinNTSystem).                                                    //
0024 //                                                                      //
0025 //////////////////////////////////////////////////////////////////////////
0026 
0027 #include "TSystem.h"
0028 #include "Compression.h"
0029 #include "TNamed.h"
0030 #include "TBits.h"
0031 #include "TInetAddress.h"
0032 #include "MessageTypes.h"
0033 #include "TVirtualAuth.h"
0034 #include "TSecContext.h"
0035 #include "TTimeStamp.h"
0036 #include "TVirtualMutex.h"
0037 
0038 class TMessage;
0039 class THostAuth;
0040 
0041 class TSocket : public TNamed {
0042 
0043 friend class TServerSocket;
0044 friend class TProofServ;   // to be able to call SetDescriptor(), RecvHostAuth()
0045 friend class TSlave;       // to be able to call SendHostAuth()
0046 
0047 public:
0048    enum EStatusBits { kIsUnix = BIT(16),    // set if unix socket
0049                       kBrokenConn = BIT(17) // set if conn reset by peer or broken
0050                     };
0051    enum EInterest { kRead = 1, kWrite = 2 };
0052    enum EServiceType { kSOCKD, kROOTD, kPROOFD };
0053 
0054 protected:
0055    enum ESocketErrors {
0056      kInvalid = -1,
0057      kInvalidStillInList = -2
0058    };
0059    TInetAddress  fAddress;        // remote internet address and port #
0060    UInt_t        fBytesRecv;      // total bytes received over this socket
0061    UInt_t        fBytesSent;      // total bytes sent using this socket
0062    Int_t         fCompress;       // Compression level and algorithm
0063    TInetAddress  fLocalAddress;   // local internet address and port #
0064    Int_t         fRemoteProtocol; // protocol of remote daemon
0065    TSecContext  *fSecContext;     // after a successful Authenticate call
0066                                   // points to related security context
0067    TString       fService;        // name of service (matches remote port #)
0068    EServiceType  fServType;       // remote service type
0069    Int_t         fSocket;         // socket descriptor
0070    Int_t         fTcpWindowSize;  // TCP window size (default 65535);
0071    TString       fUrl;            // needs this for special authentication options
0072    TBits         fBitsInfo;       // bits array to mark TStreamerInfo classes already sent
0073    TList        *fUUIDs;          // list of TProcessIDs already sent through the socket
0074 
0075    TVirtualMutex *fLastUsageMtx;   // Protect last usage setting / reading
0076    TTimeStamp    fLastUsage;      // Time stamp of last usage
0077 
0078    static ULong64_t fgBytesRecv;  // total bytes received by all socket objects
0079    static ULong64_t fgBytesSent;  // total bytes sent by all socket objects
0080 
0081    static Int_t  fgClientProtocol; // client "protocol" version
0082 
0083    TSocket() : fAddress(), fBytesRecv(0), fBytesSent(0), fCompress(ROOT::RCompressionSetting::EAlgorithm::kUseGlobal),
0084                fLocalAddress(), fRemoteProtocol(), fSecContext(nullptr), fService(),
0085                fServType(kSOCKD), fSocket(-1), fTcpWindowSize(0), fUrl(),
0086                fBitsInfo(), fUUIDs(nullptr), fLastUsageMtx(nullptr), fLastUsage() {}
0087 
0088    Bool_t       Authenticate(const char *user);
0089    void         SetDescriptor(Int_t desc) { fSocket = desc; }
0090    void         SendStreamerInfos(const TMessage &mess);
0091    Bool_t       RecvStreamerInfos(TMessage *mess);
0092    void         SendProcessIDs(const TMessage &mess);
0093    Bool_t       RecvProcessIDs(TMessage *mess);
0094    void         MarkBrokenConnection();
0095 
0096 private:
0097    TSocket&      operator=(const TSocket &) = delete;
0098    Option_t     *GetOption() const override { return TObject::GetOption(); }
0099 
0100 public:
0101    TSocket(TInetAddress address, const char *service, Int_t tcpwindowsize = -1);
0102    TSocket(TInetAddress address, Int_t port, Int_t tcpwindowsize = -1);
0103    TSocket(const char *host, const char *service, Int_t tcpwindowsize = -1);
0104    TSocket(const char *host, Int_t port, Int_t tcpwindowsize = -1);
0105    TSocket(const char *sockpath);
0106    TSocket(Int_t descriptor);
0107    TSocket(Int_t descriptor, const char *sockpath);
0108    TSocket(const TSocket &s);
0109    virtual ~TSocket() { Close(); }
0110 
0111    virtual void          Close(Option_t *opt="");
0112    virtual Int_t         GetDescriptor() const { return fSocket; }
0113    TInetAddress          GetInetAddress() const { return fAddress; }
0114    virtual TInetAddress  GetLocalInetAddress();
0115    Int_t                 GetPort() const { return fAddress.GetPort(); }
0116    const char           *GetService() const { return fService; }
0117    Int_t                 GetServType() const { return (Int_t)fServType; }
0118    virtual Int_t         GetLocalPort();
0119    UInt_t                GetBytesSent() const { return fBytesSent; }
0120    UInt_t                GetBytesRecv() const { return fBytesRecv; }
0121    Int_t                 GetCompressionAlgorithm() const;
0122    Int_t                 GetCompressionLevel() const;
0123    Int_t                 GetCompressionSettings() const;
0124    Int_t                 GetErrorCode() const;
0125    virtual Int_t         GetOption(ESockOptions opt, Int_t &val);
0126    Int_t                 GetRemoteProtocol() const { return fRemoteProtocol; }
0127    TSecContext          *GetSecContext() const { return fSecContext; }
0128    Int_t                 GetTcpWindowSize() const { return fTcpWindowSize; }
0129    TTimeStamp            GetLastUsage() { R__LOCKGUARD2(fLastUsageMtx); return fLastUsage; }
0130    const char           *GetUrl() const { return fUrl.Data(); }
0131    virtual Bool_t        IsAuthenticated() const { return fSecContext ? kTRUE : kFALSE; }
0132    virtual Bool_t        IsValid() const { return fSocket < 0 ? kFALSE : kTRUE; }
0133    virtual Int_t         Recv(TMessage *&mess);
0134    virtual Int_t         Recv(Int_t &status, Int_t &kind);
0135    virtual Int_t         Recv(char *mess, Int_t max);
0136    virtual Int_t         Recv(char *mess, Int_t max, Int_t &kind);
0137    virtual Int_t         RecvRaw(void *buffer, Int_t length, ESendRecvOptions opt = kDefault);
0138    virtual Int_t         Reconnect() { return -1; }
0139    virtual Int_t         Select(Int_t interest = kRead, Long_t timeout = -1);
0140    virtual Int_t         Send(const TMessage &mess);
0141    virtual Int_t         Send(Int_t kind);
0142    virtual Int_t         Send(Int_t status, Int_t kind);
0143    virtual Int_t         Send(const char *mess, Int_t kind = kMESS_STRING);
0144    virtual Int_t         SendObject(const TObject *obj, Int_t kind = kMESS_OBJECT);
0145    virtual Int_t         SendRaw(const void *buffer, Int_t length,
0146                                  ESendRecvOptions opt = kDefault);
0147    void                  SetCompressionAlgorithm(Int_t algorithm = ROOT::RCompressionSetting::EAlgorithm::kUseGlobal);
0148    void                  SetCompressionLevel(Int_t level = ROOT::RCompressionSetting::ELevel::kUseMin);
0149    void                  SetCompressionSettings(Int_t settings = ROOT::RCompressionSetting::EDefaults::kUseCompiledDefault);
0150    virtual Int_t         SetOption(ESockOptions opt, Int_t val);
0151    void                  SetRemoteProtocol(Int_t rproto) { fRemoteProtocol = rproto; }
0152    void                  SetSecContext(TSecContext *ctx) { fSecContext = ctx; }
0153    void                  SetService(const char *service) { fService = service; }
0154    void                  SetServType(Int_t st) { fServType = (EServiceType)st; }
0155    void                  SetUrl(const char *url) { fUrl = url; }
0156 
0157    void                  Touch() { R__LOCKGUARD2(fLastUsageMtx); fLastUsage.Set(); }
0158 
0159    static Int_t          GetClientProtocol();
0160 
0161    static ULong64_t      GetSocketBytesSent();
0162    static ULong64_t      GetSocketBytesRecv();
0163 
0164    static TSocket       *CreateAuthSocket(const char *user, const char *host,
0165                                           Int_t port, Int_t size = 0,
0166                                           Int_t tcpwindowsize = -1, TSocket *s = nullptr, Int_t *err = nullptr);
0167    static TSocket       *CreateAuthSocket(const char *url, Int_t size = 0,
0168                                           Int_t tcpwindowsize = -1, TSocket *s = nullptr, Int_t *err = nullptr);
0169    static void           NetError(const char *where, Int_t error);
0170 
0171    ClassDefOverride(TSocket,0)  //This class implements client sockets
0172 };
0173 
0174 //______________________________________________________________________________
0175 inline Int_t TSocket::GetCompressionAlgorithm() const
0176 {
0177    return (fCompress < 0) ? -1 : fCompress / 100;
0178 }
0179 
0180 //______________________________________________________________________________
0181 inline Int_t TSocket::GetCompressionLevel() const
0182 {
0183    return (fCompress < 0) ? -1 : fCompress % 100;
0184 }
0185 
0186 //______________________________________________________________________________
0187 inline Int_t TSocket::GetCompressionSettings() const
0188 {
0189    return (fCompress < 0) ? -1 : fCompress;
0190 }
0191 
0192 #endif