Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-01-04 09:59: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 
0045 public:
0046    enum EStatusBits { kIsUnix = BIT(16),    // set if unix socket
0047                       kBrokenConn = BIT(17) // set if conn reset by peer or broken
0048                     };
0049    enum EInterest { kRead = 1, kWrite = 2 };
0050    enum EServiceType { kSOCKD, kROOTD };
0051 
0052 protected:
0053    enum ESocketErrors {
0054      kInvalid = -1,
0055      kInvalidStillInList = -2
0056    };
0057    TInetAddress  fAddress;        // remote internet address and port #
0058    UInt_t        fBytesRecv;      // total bytes received over this socket
0059    UInt_t        fBytesSent;      // total bytes sent using this socket
0060    Int_t         fCompress;       // Compression level and algorithm
0061    TInetAddress  fLocalAddress;   // local internet address and port #
0062    Int_t         fRemoteProtocol; // protocol of remote daemon
0063    TSecContext  *fSecContext;     // after a successful Authenticate call
0064                                   // points to related security context
0065    TString       fService;        // name of service (matches remote port #)
0066    EServiceType  fServType;       // remote service type
0067    Int_t         fSocket;         // socket descriptor
0068    Int_t         fTcpWindowSize;  // TCP window size (default 65535);
0069    TString       fUrl;            // needs this for special authentication options
0070    TBits         fBitsInfo;       // bits array to mark TStreamerInfo classes already sent
0071    TList        *fUUIDs;          // list of TProcessIDs already sent through the socket
0072 
0073    TVirtualMutex *fLastUsageMtx;  // Protect last usage setting / reading
0074    TTimeStamp    fLastUsage;      // Time stamp of last usage
0075 
0076    static ULong64_t fgBytesRecv;  // total bytes received by all socket objects
0077    static ULong64_t fgBytesSent;  // total bytes sent by all socket objects
0078 
0079    static Int_t  fgClientProtocol; // client "protocol" version
0080 
0081    TSocket() : fAddress(), fBytesRecv(0), fBytesSent(0), fCompress(ROOT::RCompressionSetting::EAlgorithm::kUseGlobal),
0082                fLocalAddress(), fRemoteProtocol(), fSecContext(nullptr), fService(),
0083                fServType(kSOCKD), fSocket(-1), fTcpWindowSize(0), fUrl(),
0084                fBitsInfo(), fUUIDs(nullptr), fLastUsageMtx(nullptr), fLastUsage() {}
0085 
0086    Bool_t       Authenticate(const char *user);
0087    void         SetDescriptor(Int_t desc) { fSocket = desc; }
0088    void         SendStreamerInfos(const TMessage &mess);
0089    Bool_t       RecvStreamerInfos(TMessage *mess);
0090    void         SendProcessIDs(const TMessage &mess);
0091    Bool_t       RecvProcessIDs(TMessage *mess);
0092    void         MarkBrokenConnection();
0093 
0094 private:
0095    TSocket&      operator=(const TSocket &) = delete;
0096    Option_t     *GetOption() const override { return TObject::GetOption(); }
0097 
0098 public:
0099    TSocket(TInetAddress address, const char *service, Int_t tcpwindowsize = -1);
0100    TSocket(TInetAddress address, Int_t port, Int_t tcpwindowsize = -1);
0101    TSocket(const char *host, const char *service, Int_t tcpwindowsize = -1);
0102    TSocket(const char *host, Int_t port, Int_t tcpwindowsize = -1);
0103    TSocket(const char *sockpath);
0104    TSocket(Int_t descriptor);
0105    TSocket(Int_t descriptor, const char *sockpath);
0106    TSocket(const TSocket &s);
0107    virtual ~TSocket() { Close(); }
0108 
0109    virtual void          Close(Option_t *opt="");
0110    virtual Int_t         GetDescriptor() const { return fSocket; }
0111    TInetAddress          GetInetAddress() const { return fAddress; }
0112    virtual TInetAddress  GetLocalInetAddress();
0113    Int_t                 GetPort() const { return fAddress.GetPort(); }
0114    const char           *GetService() const { return fService; }
0115    Int_t                 GetServType() const { return (Int_t)fServType; }
0116    virtual Int_t         GetLocalPort();
0117    UInt_t                GetBytesSent() const { return fBytesSent; }
0118    UInt_t                GetBytesRecv() const { return fBytesRecv; }
0119    Int_t                 GetCompressionAlgorithm() const;
0120    Int_t                 GetCompressionLevel() const;
0121    Int_t                 GetCompressionSettings() const;
0122    Int_t                 GetErrorCode() const;
0123    virtual Int_t         GetOption(ESockOptions opt, Int_t &val);
0124    Int_t                 GetRemoteProtocol() const { return fRemoteProtocol; }
0125    TSecContext          *GetSecContext() const { return fSecContext; }
0126    Int_t                 GetTcpWindowSize() const { return fTcpWindowSize; }
0127    TTimeStamp            GetLastUsage() { R__LOCKGUARD2(fLastUsageMtx); return fLastUsage; }
0128    const char           *GetUrl() const { return fUrl.Data(); }
0129    virtual Bool_t        IsAuthenticated() const { return fSecContext ? kTRUE : kFALSE; }
0130    virtual Bool_t        IsValid() const { return fSocket < 0 ? kFALSE : kTRUE; }
0131    virtual Int_t         Recv(TMessage *&mess);
0132    virtual Int_t         Recv(Int_t &status, Int_t &kind);
0133    virtual Int_t         Recv(char *mess, Int_t max);
0134    virtual Int_t         Recv(char *mess, Int_t max, Int_t &kind);
0135    virtual Int_t         RecvRaw(void *buffer, Int_t length, ESendRecvOptions opt = kDefault);
0136    virtual Int_t         Reconnect() { return -1; }
0137    virtual Int_t         Select(Int_t interest = kRead, Long_t timeout = -1);
0138    virtual Int_t         Send(const TMessage &mess);
0139    virtual Int_t         Send(Int_t kind);
0140    virtual Int_t         Send(Int_t status, Int_t kind);
0141    virtual Int_t         Send(const char *mess, Int_t kind = kMESS_STRING);
0142    virtual Int_t         SendObject(const TObject *obj, Int_t kind = kMESS_OBJECT);
0143    virtual Int_t         SendRaw(const void *buffer, Int_t length,
0144                                  ESendRecvOptions opt = kDefault);
0145    void                  SetCompressionAlgorithm(Int_t algorithm = ROOT::RCompressionSetting::EAlgorithm::kUseGlobal);
0146    void                  SetCompressionLevel(Int_t level = ROOT::RCompressionSetting::ELevel::kUseMin);
0147    void                  SetCompressionSettings(Int_t settings = ROOT::RCompressionSetting::EDefaults::kUseCompiledDefault);
0148    virtual Int_t         SetOption(ESockOptions opt, Int_t val);
0149    void                  SetRemoteProtocol(Int_t rproto) { fRemoteProtocol = rproto; }
0150    void                  SetSecContext(TSecContext *ctx) { fSecContext = ctx; }
0151    void                  SetService(const char *service) { fService = service; }
0152    void                  SetServType(Int_t st) { fServType = (EServiceType)st; }
0153    void                  SetUrl(const char *url) { fUrl = url; }
0154 
0155    void                  Touch() { R__LOCKGUARD2(fLastUsageMtx); fLastUsage.Set(); }
0156 
0157    static Int_t          GetClientProtocol();
0158 
0159    static ULong64_t      GetSocketBytesSent();
0160    static ULong64_t      GetSocketBytesRecv();
0161 
0162    static TSocket       *CreateAuthSocket(const char *user, const char *host,
0163                                           Int_t port, Int_t size = 0,
0164                                           Int_t tcpwindowsize = -1, TSocket *s = nullptr, Int_t *err = nullptr);
0165    static TSocket       *CreateAuthSocket(const char *url, Int_t size = 0,
0166                                           Int_t tcpwindowsize = -1, TSocket *s = nullptr, Int_t *err = nullptr);
0167    static void           NetError(const char *where, Int_t error);
0168 
0169    ClassDefOverride(TSocket,0)  //This class implements client sockets
0170 };
0171 
0172 //______________________________________________________________________________
0173 inline Int_t TSocket::GetCompressionAlgorithm() const
0174 {
0175    return (fCompress < 0) ? -1 : fCompress / 100;
0176 }
0177 
0178 //______________________________________________________________________________
0179 inline Int_t TSocket::GetCompressionLevel() const
0180 {
0181    return (fCompress < 0) ? -1 : fCompress % 100;
0182 }
0183 
0184 //______________________________________________________________________________
0185 inline Int_t TSocket::GetCompressionSettings() const
0186 {
0187    return (fCompress < 0) ? -1 : fCompress;
0188 }
0189 
0190 #endif