Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-09-02 09:24:38

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