Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
TSocket.h
Go to the documentation of this file.
1// @(#)root/net:$Id$
2// Author: Fons Rademakers 18/12/96
3
4/*************************************************************************
5 * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers. *
6 * All rights reserved. *
7 * *
8 * For the licensing terms see $ROOTSYS/LICENSE. *
9 * For the list of contributors see $ROOTSYS/README/CREDITS. *
10 *************************************************************************/
11
12#ifndef ROOT_TSocket
13#define ROOT_TSocket
14
15
16//////////////////////////////////////////////////////////////////////////
17// //
18// TSocket //
19// //
20// This class implements client sockets. A socket is an endpoint for //
21// communication between two machines. //
22// The actual work is done via the TSystem class (either TUnixSystem, //
23// or TWinNTSystem). //
24// //
25//////////////////////////////////////////////////////////////////////////
26
27#include "TSystem.h"
28#include "Compression.h"
29#include "TNamed.h"
30#include "TBits.h"
31#include "TInetAddress.h"
32#include "MessageTypes.h"
33#include "TVirtualAuth.h"
34#include "TSecContext.h"
35#include "TTimeStamp.h"
36#include "TVirtualMutex.h"
37
38class TMessage;
39class THostAuth;
40
41class TSocket : public TNamed {
42
43friend class TServerSocket;
44
45public:
46 enum EStatusBits { kIsUnix = BIT(16), // set if unix socket
47 kBrokenConn = BIT(17) // set if conn reset by peer or broken
48 };
49 enum EInterest { kRead = 1, kWrite = 2 };
51
52protected:
54// clang++ <v20 (-Wshadow) complains about shadowing TSystem.h global enum EFpeMask. Let's silence warning:
55#if defined(__clang__) && __clang_major__ < 20
56#pragma clang diagnostic push
57#pragma clang diagnostic ignored "-Wshadow"
58#endif
60#if defined(__clang__) && __clang_major__ < 20
61#pragma clang diagnostic pop
62#endif
64 };
65 TInetAddress fAddress; // remote internet address and port #
66 UInt_t fBytesRecv; // total bytes received over this socket
67 UInt_t fBytesSent; // total bytes sent using this socket
68 Int_t fCompress; // Compression level and algorithm
69 TInetAddress fLocalAddress; // local internet address and port #
70 Int_t fRemoteProtocol; // protocol of remote daemon
71 TSecContext *fSecContext; // after a successful Authenticate call
72 // points to related security context
73 TString fService; // name of service (matches remote port #)
74 EServiceType fServType; // remote service type
75 Int_t fSocket; // socket descriptor
76 Int_t fTcpWindowSize; // TCP window size (default 65535);
77 TString fUrl; // needs this for special authentication options
78 TBits fBitsInfo; // bits array to mark TStreamerInfo classes already sent
79 TList *fUUIDs; // list of TProcessIDs already sent through the socket
80
81 TVirtualMutex *fLastUsageMtx; // Protect last usage setting / reading
82 TTimeStamp fLastUsage; // Time stamp of last usage
83
84 static ULong64_t fgBytesRecv; // total bytes received by all socket objects
85 static ULong64_t fgBytesSent; // total bytes sent by all socket objects
86
87 static Int_t fgClientProtocol; // client "protocol" version
88
89 TSocket() : fAddress(), fBytesRecv(0), fBytesSent(0), fCompress(ROOT::RCompressionSetting::EAlgorithm::kUseGlobal),
92 fBitsInfo(), fUUIDs(nullptr), fLastUsageMtx(nullptr), fLastUsage() {}
93
94 Bool_t Authenticate(const char *user);
95 void SetDescriptor(Int_t desc) { fSocket = desc; }
96 void SendStreamerInfos(const TMessage &mess);
98 void SendProcessIDs(const TMessage &mess);
101
102private:
103 TSocket& operator=(const TSocket &) = delete;
104 Option_t *GetOption() const override { return TObject::GetOption(); }
105
106public:
107 TSocket(TInetAddress address, const char *service, Int_t tcpwindowsize = -1);
108 TSocket(TInetAddress address, Int_t port, Int_t tcpwindowsize = -1);
109 TSocket(const char *host, const char *service, Int_t tcpwindowsize = -1);
110 TSocket(const char *host, Int_t port, Int_t tcpwindowsize = -1);
111 TSocket(const char *sockpath);
113 TSocket(Int_t descriptor, const char *sockpath);
114 TSocket(const TSocket &s);
115 virtual ~TSocket() { Close(); }
116
117 virtual void Close(Option_t *opt="");
118 virtual Int_t GetDescriptor() const { return fSocket; }
121 Int_t GetPort() const { return fAddress.GetPort(); }
122 const char *GetService() const { return fService; }
123 Int_t GetServType() const { return (Int_t)fServType; }
124 virtual Int_t GetLocalPort();
125 UInt_t GetBytesSent() const { return fBytesSent; }
126 UInt_t GetBytesRecv() const { return fBytesRecv; }
130 Int_t GetErrorCode() const;
131 virtual Int_t GetOption(ESockOptions opt, Int_t &val);
136 const char *GetUrl() const { return fUrl.Data(); }
137 virtual Bool_t IsAuthenticated() const { return fSecContext ? kTRUE : kFALSE; }
138 virtual Bool_t IsValid() const { return fSocket < 0 ? kFALSE : kTRUE; }
139 virtual Int_t Recv(TMessage *&mess);
140 virtual Int_t Recv(Int_t &status, Int_t &kind);
141 virtual Int_t Recv(char *mess, Int_t max);
142 virtual Int_t Recv(char *mess, Int_t max, Int_t &kind);
143 virtual Int_t RecvRaw(void *buffer, Int_t length, ESendRecvOptions opt = kDefault);
144 virtual Int_t Reconnect() { return -1; }
145 virtual Int_t Select(Int_t interest = kRead, Long_t timeout = -1);
146 virtual Int_t Send(const TMessage &mess);
147 virtual Int_t Send(Int_t kind);
148 virtual Int_t Send(Int_t status, Int_t kind);
149 virtual Int_t Send(const char *mess, Int_t kind = kMESS_STRING);
150 virtual Int_t SendObject(const TObject *obj, Int_t kind = kMESS_OBJECT);
151 virtual Int_t SendRaw(const void *buffer, Int_t length,
156 virtual Int_t SetOption(ESockOptions opt, Int_t val);
159 void SetService(const char *service) { fService = service; }
161 void SetUrl(const char *url) { fUrl = url; }
162
164
165 static Int_t GetClientProtocol();
166
169
170 static TSocket *CreateAuthSocket(const char *user, const char *host,
171 Int_t port, Int_t size = 0,
172 Int_t tcpwindowsize = -1, TSocket *s = nullptr, Int_t *err = nullptr);
173 static TSocket *CreateAuthSocket(const char *url, Int_t size = 0,
174 Int_t tcpwindowsize = -1, TSocket *s = nullptr, Int_t *err = nullptr);
175 static void NetError(const char *where, Int_t error);
176
177 ClassDefOverride(TSocket,0) //This class implements client sockets
178};
179
180//______________________________________________________________________________
182{
183 return (fCompress < 0) ? -1 : fCompress / 100;
184}
185
186//______________________________________________________________________________
188{
189 return (fCompress < 0) ? -1 : fCompress % 100;
190}
191
192//______________________________________________________________________________
194{
195 return (fCompress < 0) ? -1 : fCompress;
196}
197
198#endif
@ kMESS_STRING
@ kMESS_OBJECT
size_t size(const MatrixT &matrix)
retrieve the size of a square matrix
bool Bool_t
Boolean (0=false, 1=true) (bool)
Definition RtypesCore.h:77
int Int_t
Signed integer 4 bytes (int)
Definition RtypesCore.h:59
long Long_t
Signed long integer 4 bytes (long). Size depends on architecture.
Definition RtypesCore.h:68
constexpr Bool_t kFALSE
Definition RtypesCore.h:108
unsigned long long ULong64_t
Portable unsigned long integer 8 bytes.
Definition RtypesCore.h:84
constexpr Bool_t kTRUE
Definition RtypesCore.h:107
const char Option_t
Option string (const char)
Definition RtypesCore.h:80
#define BIT(n)
Definition Rtypes.h:91
#define ClassDefOverride(name, id)
Definition Rtypes.h:348
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h length
ESockOptions
Definition TSystem.h:229
ESendRecvOptions
Definition TSystem.h:242
@ kDefault
Definition TSystem.h:243
#define R__LOCKGUARD2(mutex)
Container of bits.
Definition TBits.h:26
This class represents an Internet Protocol (IP) address.
Int_t GetPort() const
A doubly linked list.
Definition TList.h:38
The TNamed class is the base class for all named ROOT classes.
Definition TNamed.h:29
Mother of all ROOT objects.
Definition TObject.h:42
virtual Option_t * GetOption() const
Definition TObject.h:147
TInetAddress fAddress
Definition TSocket.h:65
Int_t fCompress
Definition TSocket.h:68
virtual Int_t SetOption(ESockOptions opt, Int_t val)
Set socket options.
Definition TSocket.cxx:1014
Int_t fSocket
Definition TSocket.h:75
Int_t GetErrorCode() const
Returns error code.
Definition TSocket.cxx:1036
void SetServType(Int_t st)
Definition TSocket.h:160
TVirtualMutex * fLastUsageMtx
Definition TSocket.h:81
UInt_t GetBytesRecv() const
Definition TSocket.h:126
void SetRemoteProtocol(Int_t rproto)
Definition TSocket.h:157
void SetCompressionLevel(Int_t level=ROOT::RCompressionSetting::ELevel::kUseMin)
See comments for function SetCompressionSettings.
Definition TSocket.cxx:1061
void SendStreamerInfos(const TMessage &mess)
Check if TStreamerInfo must be sent.
Definition TSocket.cxx:640
ESocketErrors
Definition TSocket.h:53
@ kInvalidStillInList
Definition TSocket.h:63
@ kInvalid
Definition TSocket.h:59
TString fUrl
Definition TSocket.h:77
const char * GetService() const
Definition TSocket.h:122
void SetCompressionAlgorithm(Int_t algorithm=ROOT::RCompressionSetting::EAlgorithm::kUseGlobal)
See comments for function SetCompressionSettings.
Definition TSocket.cxx:1047
TSocket()
Definition TSocket.h:89
static ULong64_t GetSocketBytesSent()
Get total number of bytes sent via all sockets.
Definition TSocket.cxx:1465
TTimeStamp GetLastUsage()
Definition TSocket.h:135
TString fService
Definition TSocket.h:73
Bool_t RecvStreamerInfos(TMessage *mess)
Receive a message containing streamer infos.
Definition TSocket.cxx:929
EInterest
Definition TSocket.h:49
@ kRead
Definition TSocket.h:49
@ kWrite
Definition TSocket.h:49
virtual Int_t Recv(TMessage *&mess)
Receive a TMessage object.
Definition TSocket.cxx:809
TList * fUUIDs
Definition TSocket.h:79
static Int_t GetClientProtocol()
Static method returning supported client protocol.
Definition TSocket.cxx:1445
TBits fBitsInfo
Definition TSocket.h:78
void SetService(const char *service)
Definition TSocket.h:159
Int_t GetRemoteProtocol() const
Definition TSocket.h:132
UInt_t GetBytesSent() const
Definition TSocket.h:125
Bool_t Authenticate(const char *user)
Authenticated the socket with specified user.
Definition TSocket.cxx:1107
TInetAddress fLocalAddress
Definition TSocket.h:69
Int_t GetTcpWindowSize() const
Definition TSocket.h:134
static ULong64_t fgBytesRecv
Definition TSocket.h:84
EStatusBits
Definition TSocket.h:46
@ kIsUnix
Definition TSocket.h:46
@ kBrokenConn
Definition TSocket.h:47
virtual void Close(Option_t *opt="")
Close the socket.
Definition TSocket.cxx:380
void MarkBrokenConnection()
Close the socket and mark as due to a broken connection.
Definition TSocket.cxx:362
void Touch()
Definition TSocket.h:163
Bool_t RecvProcessIDs(TMessage *mess)
Receive a message containing process ids.
Definition TSocket.cxx:976
void SetUrl(const char *url)
Definition TSocket.h:161
TInetAddress GetInetAddress() const
Definition TSocket.h:119
Int_t GetCompressionLevel() const
Definition TSocket.h:187
virtual Int_t RecvRaw(void *buffer, Int_t length, ESendRecvOptions opt=kDefault)
Receive a raw buffer of specified length bytes.
Definition TSocket.cxx:899
TSecContext * GetSecContext() const
Definition TSocket.h:133
static ULong64_t fgBytesSent
Definition TSocket.h:85
virtual Int_t SendRaw(const void *buffer, Int_t length, ESendRecvOptions opt=kDefault)
Send a raw buffer of specified length.
Definition TSocket.cxx:611
TTimeStamp fLastUsage
Definition TSocket.h:82
void SendProcessIDs(const TMessage &mess)
Check if TProcessIDs must be sent.
Definition TSocket.cxx:675
static Int_t fgClientProtocol
Definition TSocket.h:87
virtual TInetAddress GetLocalInetAddress()
Return internet address of local host to which the socket is bound.
Definition TSocket.cxx:400
TSecContext * fSecContext
Definition TSocket.h:71
virtual Int_t Select(Int_t interest=kRead, Long_t timeout=-1)
Waits for this socket to change status.
Definition TSocket.cxx:434
virtual Int_t GetLocalPort()
Return the local port # to which the socket is bound.
Definition TSocket.cxx:414
virtual ~TSocket()
Definition TSocket.h:115
void SetDescriptor(Int_t desc)
Definition TSocket.h:95
Int_t fTcpWindowSize
Definition TSocket.h:76
Option_t * GetOption() const override
Definition TSocket.h:104
EServiceType
Definition TSocket.h:50
@ kSOCKD
Definition TSocket.h:50
@ kROOTD
Definition TSocket.h:50
static TSocket * CreateAuthSocket(const char *user, const char *host, Int_t port, Int_t size=0, Int_t tcpwindowsize=-1, TSocket *s=nullptr, Int_t *err=nullptr)
Creates a socket or a parallel socket and authenticates to the remote server specified in 'url' on re...
Definition TSocket.cxx:1408
static void NetError(const char *where, Int_t error)
Print error string depending on error code.
Definition TSocket.cxx:1453
virtual Int_t GetDescriptor() const
Definition TSocket.h:118
Int_t GetCompressionSettings() const
Definition TSocket.h:193
Int_t GetPort() const
Definition TSocket.h:121
Int_t GetServType() const
Definition TSocket.h:123
EServiceType fServType
Definition TSocket.h:74
TSocket & operator=(const TSocket &)=delete
virtual Int_t SendObject(const TObject *obj, Int_t kind=kMESS_OBJECT)
Send an object.
Definition TSocket.cxx:591
void SetCompressionSettings(Int_t settings=ROOT::RCompressionSetting::EDefaults::kUseCompiledDefault)
Used to specify the compression level and algorithm: settings = 100 * algorithm + level.
Definition TSocket.cxx:1099
void SetSecContext(TSecContext *ctx)
Definition TSocket.h:158
UInt_t fBytesSent
Definition TSocket.h:67
Int_t fRemoteProtocol
Definition TSocket.h:70
UInt_t fBytesRecv
Definition TSocket.h:66
Int_t GetCompressionAlgorithm() const
Definition TSocket.h:181
virtual Bool_t IsValid() const
Definition TSocket.h:138
virtual Int_t Reconnect()
Definition TSocket.h:144
const char * GetUrl() const
Definition TSocket.h:136
virtual Int_t Send(const TMessage &mess)
Send a TMessage object.
Definition TSocket.cxx:513
virtual Bool_t IsAuthenticated() const
Definition TSocket.h:137
static ULong64_t GetSocketBytesRecv()
Get total number of bytes received via all sockets.
Definition TSocket.cxx:1473
Basic string class.
Definition TString.h:138
const char * Data() const
Definition TString.h:384
The TTimeStamp encapsulates seconds and ns since EPOCH.
Definition TTimeStamp.h:45
void Set()
Set Date/Time to current time as reported by the system.
This class implements a mutex interface.
@ kUseGlobal
Use the global compression algorithm.
Definition Compression.h:93
@ kUseCompiledDefault
Use the compile-time default setting.
Definition Compression.h:53
@ kUseMin
Compression level reserved when we are not sure what to use (1 is for the fastest compression)
Definition Compression.h:72