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 "TSecContext.h"
34#include "TTimeStamp.h"
35#include "TVirtualMutex.h"
36
37class TMessage;
38class TSocket;
39
40namespace ROOT::Deprecated {
42 static Bool_t IsAuthenticated(const TSocket &s);
43 static void SetSecContext(TSocket &s, TSecContext *ctx);
44 static TSecContext *GetSecContext(const TSocket &s);
45
46 static TSocket *CreateAuthSocket(const char *user, const char *host,
47 Int_t port, Int_t size = 0,
48 Int_t tcpwindowsize = -1, TSocket *s = nullptr, Int_t *err = nullptr);
49 static TSocket *CreateAuthSocket(const char *url, Int_t size = 0,
50 Int_t tcpwindowsize = -1, TSocket *s = nullptr, Int_t *err = nullptr);
51};
52} // namespace ROOT::Deprecated
53
54class TSocket : public TNamed {
55
56friend class TServerSocket;
58
59public:
60 enum EStatusBits { kIsUnix = BIT(16), // set if unix socket
61 kBrokenConn = BIT(17) // set if conn reset by peer or broken
62 };
63 enum EInterest { kRead = 1, kWrite = 2 };
65
66protected:
68// clang++ <v20 (-Wshadow) complains about shadowing TSystem.h global enum EFpeMask. Let's silence warning:
69#if defined(__clang__) && __clang_major__ < 20
70#pragma clang diagnostic push
71#pragma clang diagnostic ignored "-Wshadow"
72#endif
74#if defined(__clang__) && __clang_major__ < 20
75#pragma clang diagnostic pop
76#endif
78 };
79 TInetAddress fAddress; // remote internet address and port #
80 UInt_t fBytesRecv; // total bytes received over this socket
81 UInt_t fBytesSent; // total bytes sent using this socket
82 Int_t fCompress; // Compression level and algorithm
83 TInetAddress fLocalAddress; // local internet address and port #
84 Int_t fRemoteProtocol; // protocol of remote daemon
85 ROOT::Deprecated::TSecContext *fSecContext; // after a successful Authenticate call
86 // points to related security context
87 TString fService; // name of service (matches remote port #)
88 EServiceType fServType; // remote service type
89 Int_t fSocket; // socket descriptor
90 Int_t fTcpWindowSize; // TCP window size (default 65535);
91 TString fUrl; // needs this for special authentication options
92 TBits fBitsInfo; // bits array to mark TStreamerInfo classes already sent
93 TList *fUUIDs; // list of TProcessIDs already sent through the socket
94
95 TVirtualMutex *fLastUsageMtx; // Protect last usage setting / reading
96 TTimeStamp fLastUsage; // Time stamp of last usage
97
98 static ULong64_t fgBytesRecv; // total bytes received by all socket objects
99 static ULong64_t fgBytesSent; // total bytes sent by all socket objects
100
101 static Int_t fgClientProtocol; // client "protocol" version
102
103 TSocket() : fAddress(), fBytesRecv(0), fBytesSent(0), fCompress(ROOT::RCompressionSetting::EAlgorithm::kUseGlobal),
106 fBitsInfo(), fUUIDs(nullptr), fLastUsageMtx(nullptr), fLastUsage() {}
107
108 Bool_t Authenticate(const char *user);
109 void SetDescriptor(Int_t desc) { fSocket = desc; }
110 void SendStreamerInfos(const TMessage &mess);
112 void SendProcessIDs(const TMessage &mess);
115
116private:
117 TSocket& operator=(const TSocket &) = delete;
118 Option_t *GetOption() const override { return TObject::GetOption(); }
119
120public:
121 TSocket(TInetAddress address, const char *service, Int_t tcpwindowsize = -1);
122 TSocket(TInetAddress address, Int_t port, Int_t tcpwindowsize = -1);
123 TSocket(const char *host, const char *service, Int_t tcpwindowsize = -1);
124 TSocket(const char *host, Int_t port, Int_t tcpwindowsize = -1);
125 TSocket(const char *sockpath);
127 TSocket(Int_t descriptor, const char *sockpath);
128 TSocket(const TSocket &s);
129 virtual ~TSocket() { Close(); }
130
131 virtual void Close(Option_t *opt="");
132 virtual Int_t GetDescriptor() const { return fSocket; }
135 Int_t GetPort() const { return fAddress.GetPort(); }
136 const char *GetService() const { return fService; }
137 Int_t GetServType() const { return (Int_t)fServType; }
138 virtual Int_t GetLocalPort();
139 UInt_t GetBytesSent() const { return fBytesSent; }
140 UInt_t GetBytesRecv() const { return fBytesRecv; }
144 Int_t GetErrorCode() const;
145 virtual Int_t GetOption(ESockOptions opt, Int_t &val);
148 R__DEPRECATED(6, 42, "TSocket::GetSecContext is deprecated")
152 const char *GetUrl() const { return fUrl.Data(); }
154 R__DEPRECATED(6, 42, "TSocket::IsAuthenticated is deprecated")
156 virtual Bool_t IsValid() const { return fSocket < 0 ? kFALSE : kTRUE; }
157 virtual Int_t Recv(TMessage *&mess);
158 virtual Int_t Recv(Int_t &status, Int_t &kind);
159 virtual Int_t Recv(char *mess, Int_t max);
160 virtual Int_t Recv(char *mess, Int_t max, Int_t &kind);
161 virtual Int_t RecvRaw(void *buffer, Int_t length, ESendRecvOptions opt = kDefault);
162 virtual Int_t Reconnect() { return -1; }
163 virtual Int_t Select(Int_t interest = kRead, Long_t timeout = -1);
164 virtual Int_t Send(const TMessage &mess);
165 virtual Int_t Send(Int_t kind);
166 virtual Int_t Send(Int_t status, Int_t kind);
167 virtual Int_t Send(const char *mess, Int_t kind = kMESS_STRING);
168 virtual Int_t SendObject(const TObject *obj, Int_t kind = kMESS_OBJECT);
169 virtual Int_t SendRaw(const void *buffer, Int_t length,
174 virtual Int_t SetOption(ESockOptions opt, Int_t val);
177 R__DEPRECATED(6, 42, "TSocket::SetSecContext is deprecated")
179 void SetService(const char *service) { fService = service; }
181 void SetUrl(const char *url) { fUrl = url; }
182
184
185 static Int_t GetClientProtocol();
186
189
190 static TSocket *CreateAuthSocket(const char *user, const char *host,
191 Int_t port, Int_t size = 0,
192 Int_t tcpwindowsize = -1, TSocket *s = nullptr, Int_t *err = nullptr)
193 R__DEPRECATED(6, 42, "Socket authentication is deprecated. See README.AUTH for details.")
195 user, host, port, size, tcpwindowsize, s, err); }
196 static TSocket *CreateAuthSocket(const char *url, Int_t size = 0,
197 Int_t tcpwindowsize = -1, TSocket *s = nullptr, Int_t *err = nullptr)
198 R__DEPRECATED(6, 42, "Socket authentication is deprecated. See README.AUTH for details.")
200
201 static void NetError(const char *where, Int_t error);
202
203 ClassDefOverride(TSocket,0) //This class implements client sockets
204};
205
206//______________________________________________________________________________
208{
209 return (fCompress < 0) ? -1 : fCompress / 100;
210}
211
212//______________________________________________________________________________
214{
215 return (fCompress < 0) ? -1 : fCompress % 100;
216}
217
218//______________________________________________________________________________
220{
221 return (fCompress < 0) ? -1 : fCompress;
222}
223
224#endif
@ kMESS_STRING
@ kMESS_OBJECT
size_t size(const MatrixT &matrix)
retrieve the size of a square matrix
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
This class implements server sockets.
This class implements client sockets.
Definition TSocket.h:54
TInetAddress fAddress
Definition TSocket.h:79
Int_t fCompress
Definition TSocket.h:82
virtual Int_t SetOption(ESockOptions opt, Int_t val)
Set socket options.
Definition TSocket.cxx:1037
Int_t fSocket
Definition TSocket.h:89
Int_t GetErrorCode() const
Returns error code.
Definition TSocket.cxx:1059
void SetServType(Int_t st)
Definition TSocket.h:180
TVirtualMutex * fLastUsageMtx
Definition TSocket.h:95
UInt_t GetBytesRecv() const
Definition TSocket.h:140
void SetRemoteProtocol(Int_t rproto)
Definition TSocket.h:175
void SetCompressionLevel(Int_t level=ROOT::RCompressionSetting::ELevel::kUseMin)
See comments for function SetCompressionSettings.
Definition TSocket.cxx:1084
void SendStreamerInfos(const TMessage &mess)
Check if TStreamerInfo must be sent.
Definition TSocket.cxx:658
ESocketErrors
Definition TSocket.h:67
@ kInvalidStillInList
Definition TSocket.h:77
@ kInvalid
Definition TSocket.h:73
TString fUrl
Definition TSocket.h:91
ROOT::Deprecated::TSecContext * fSecContext
Definition TSocket.h:85
const char * GetService() const
Definition TSocket.h:136
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) R__DEPRECATED(6
void SetCompressionAlgorithm(Int_t algorithm=ROOT::RCompressionSetting::EAlgorithm::kUseGlobal)
See comments for function SetCompressionSettings.
Definition TSocket.cxx:1070
TSocket()
Definition TSocket.h:103
static ULong64_t GetSocketBytesSent()
Get total number of bytes sent via all sockets.
Definition TSocket.cxx:1487
TTimeStamp GetLastUsage()
Definition TSocket.h:151
TString fService
Definition TSocket.h:87
Bool_t RecvStreamerInfos(TMessage *mess)
Receive a message containing streamer infos.
Definition TSocket.cxx:952
EInterest
Definition TSocket.h:63
@ kRead
Definition TSocket.h:63
@ kWrite
Definition TSocket.h:63
virtual Int_t Recv(TMessage *&mess)
Receive a TMessage object.
Definition TSocket.cxx:827
TList * fUUIDs
Definition TSocket.h:93
static Int_t GetClientProtocol()
Static method returning supported client protocol.
Definition TSocket.cxx:1467
TBits fBitsInfo
Definition TSocket.h:92
void SetService(const char *service)
Definition TSocket.h:179
Int_t GetRemoteProtocol() const
Definition TSocket.h:146
UInt_t GetBytesSent() const
Definition TSocket.h:139
Bool_t Authenticate(const char *user)
Authenticated the socket with specified user.
Definition TSocket.cxx:1130
TInetAddress fLocalAddress
Definition TSocket.h:83
Int_t GetTcpWindowSize() const
Definition TSocket.h:150
static ULong64_t fgBytesRecv
Definition TSocket.h:98
EStatusBits
Definition TSocket.h:60
@ kIsUnix
Definition TSocket.h:60
@ kBrokenConn
Definition TSocket.h:61
virtual Bool_t IsAuthenticated() const R__DEPRECATED(6
virtual void Close(Option_t *opt="")
Close the socket.
Definition TSocket.cxx:398
void SetSecContext(ROOT::Deprecated::TSecContext *ctx) R__DEPRECATED(6
void MarkBrokenConnection()
Close the socket and mark as due to a broken connection.
Definition TSocket.cxx:380
void Touch()
Definition TSocket.h:183
Bool_t RecvProcessIDs(TMessage *mess)
Receive a message containing process ids.
Definition TSocket.cxx:999
void SetUrl(const char *url)
Definition TSocket.h:181
TInetAddress GetInetAddress() const
Definition TSocket.h:133
Int_t GetCompressionLevel() const
Definition TSocket.h:213
virtual Int_t RecvRaw(void *buffer, Int_t length, ESendRecvOptions opt=kDefault)
Receive a raw buffer of specified length bytes.
Definition TSocket.cxx:922
static ULong64_t fgBytesSent
Definition TSocket.h:99
virtual Int_t SendRaw(const void *buffer, Int_t length, ESendRecvOptions opt=kDefault)
Send a raw buffer of specified length.
Definition TSocket.cxx:629
TTimeStamp fLastUsage
Definition TSocket.h:96
void SendProcessIDs(const TMessage &mess)
Check if TProcessIDs must be sent.
Definition TSocket.cxx:693
static Int_t fgClientProtocol
Definition TSocket.h:101
virtual TInetAddress GetLocalInetAddress()
Return internet address of local host to which the socket is bound.
Definition TSocket.cxx:418
static TSocket Socket authentication is deprecated See README AUTH for details
Definition TSocket.h:194
virtual Int_t Select(Int_t interest=kRead, Long_t timeout=-1)
Waits for this socket to change status.
Definition TSocket.cxx:452
ROOT::Deprecated::TSecContext * GetSecContext() const R__DEPRECATED(6
virtual Int_t GetLocalPort()
Return the local port # to which the socket is bound.
Definition TSocket.cxx:432
virtual ~TSocket()
Definition TSocket.h:129
void SetDescriptor(Int_t desc)
Definition TSocket.h:109
Int_t fTcpWindowSize
Definition TSocket.h:90
Option_t * GetOption() const override
Definition TSocket.h:118
EServiceType
Definition TSocket.h:64
@ kSOCKD
Definition TSocket.h:64
@ kROOTD
Definition TSocket.h:64
static void NetError(const char *where, Int_t error)
Print error string depending on error code.
Definition TSocket.cxx:1475
virtual Int_t GetDescriptor() const
Definition TSocket.h:132
Int_t GetCompressionSettings() const
Definition TSocket.h:219
Int_t GetPort() const
Definition TSocket.h:135
Int_t GetServType() const
Definition TSocket.h:137
EServiceType fServType
Definition TSocket.h:88
TSocket & operator=(const TSocket &)=delete
virtual Int_t SendObject(const TObject *obj, Int_t kind=kMESS_OBJECT)
Send an object.
Definition TSocket.cxx:609
void SetCompressionSettings(Int_t settings=ROOT::RCompressionSetting::EDefaults::kUseCompiledDefault)
Used to specify the compression level and algorithm: settings = 100 * algorithm + level.
Definition TSocket.cxx:1122
UInt_t fBytesSent
Definition TSocket.h:81
Int_t fRemoteProtocol
Definition TSocket.h:84
UInt_t fBytesRecv
Definition TSocket.h:80
Int_t GetCompressionAlgorithm() const
Definition TSocket.h:207
virtual Bool_t IsValid() const
Definition TSocket.h:156
virtual Int_t Reconnect()
Definition TSocket.h:162
static TSocket * CreateAuthSocket(const char *url, Int_t size=0, Int_t tcpwindowsize=-1, TSocket *s=nullptr, Int_t *err=nullptr) R__DEPRECATED(6
const char * GetUrl() const
Definition TSocket.h:152
virtual Int_t Send(const TMessage &mess)
Send a TMessage object.
Definition TSocket.cxx:531
static ULong64_t GetSocketBytesRecv()
Get total number of bytes received via all sockets.
Definition TSocket.cxx:1495
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.
for(Int_t i=0;i< n;i++)
Definition legend1.C:18
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:1431
static void SetSecContext(TSocket &s, TSecContext *ctx)
Definition TSocket.cxx:47
static Bool_t IsAuthenticated(const TSocket &s)
Definition TSocket.cxx:42
static TSecContext * GetSecContext(const TSocket &s)
Definition TSocket.cxx:52
@ 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