Logo ROOT  
Reference Guide
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 
38 class TMessage;
39 class THostAuth;
40 
41 class TSocket : public TNamed {
42 
43 friend class TServerSocket;
44 friend class TProofServ; // to be able to call SetDescriptor(), RecvHostAuth()
45 friend class TSlave; // to be able to call SendHostAuth()
46 
47 public:
48  enum EStatusBits { kIsUnix = BIT(16), // set if unix socket
49  kBrokenConn = BIT(17) // set if conn reset by peer or broken
50  };
51  enum EInterest { kRead = 1, kWrite = 2 };
53 
54 protected:
56  kInvalid = -1,
58  };
59  TInetAddress fAddress; // remote internet address and port #
60  UInt_t fBytesRecv; // total bytes received over this socket
61  UInt_t fBytesSent; // total bytes sent using this socket
62  Int_t fCompress; // Compression level and algorithm
63  TInetAddress fLocalAddress; // local internet address and port #
64  Int_t fRemoteProtocol; // protocol of remote daemon
65  TSecContext *fSecContext; // after a successful Authenticate call
66  // points to related security context
67  TString fService; // name of service (matches remote port #)
68  EServiceType fServType; // remote service type
69  Int_t fSocket; // socket descriptor
70  Int_t fTcpWindowSize; // TCP window size (default 65535);
71  TString fUrl; // needs this for special authentication options
72  TBits fBitsInfo; // bits array to mark TStreamerInfo classes already sent
73  TList *fUUIDs; // list of TProcessIDs already sent through the socket
74 
75  TVirtualMutex *fLastUsageMtx; // Protect last usage setting / reading
76  TTimeStamp fLastUsage; // Time stamp of last usage
77 
78  static ULong64_t fgBytesRecv; // total bytes received by all socket objects
79  static ULong64_t fgBytesSent; // total bytes sent by all socket objects
80 
81  static Int_t fgClientProtocol; // client "protocol" version
82 
83  TSocket() : fAddress(), fBytesRecv(0), fBytesSent(0), fCompress(ROOT::RCompressionSetting::EAlgorithm::kUseGlobal),
86  fBitsInfo(), fUUIDs(0), fLastUsageMtx(0), fLastUsage() { }
87 
88  Bool_t Authenticate(const char *user);
89  void SetDescriptor(Int_t desc) { fSocket = desc; }
90  void SendStreamerInfos(const TMessage &mess);
92  void SendProcessIDs(const TMessage &mess);
94  void MarkBrokenConnection();
95 
96 private:
97  TSocket& operator=(const TSocket &); // not implemented
98  Option_t *GetOption() const { return TObject::GetOption(); }
99 
100 public:
101  TSocket(TInetAddress address, const char *service, Int_t tcpwindowsize = -1);
102  TSocket(TInetAddress address, Int_t port, Int_t tcpwindowsize = -1);
103  TSocket(const char *host, const char *service, Int_t tcpwindowsize = -1);
104  TSocket(const char *host, Int_t port, Int_t tcpwindowsize = -1);
105  TSocket(const char *sockpath);
106  TSocket(Int_t descriptor);
107  TSocket(Int_t descriptor, const char *sockpath);
108  TSocket(const TSocket &s);
109  virtual ~TSocket() { Close(); }
110 
111  virtual void Close(Option_t *opt="");
112  virtual Int_t GetDescriptor() const { return fSocket; }
115  Int_t GetPort() const { return fAddress.GetPort(); }
116  const char *GetService() const { return fService; }
117  Int_t GetServType() const { return (Int_t)fServType; }
118  virtual Int_t GetLocalPort();
119  UInt_t GetBytesSent() const { return fBytesSent; }
120  UInt_t GetBytesRecv() const { return fBytesRecv; }
122  Int_t GetCompressionLevel() const;
124  Int_t GetErrorCode() const;
125  virtual Int_t GetOption(ESockOptions opt, Int_t &val);
127  TSecContext *GetSecContext() const { return fSecContext; }
130  const char *GetUrl() const { return fUrl; }
131  virtual Bool_t IsAuthenticated() const { return fSecContext ? kTRUE : kFALSE; }
132  virtual Bool_t IsValid() const { return fSocket < 0 ? kFALSE : kTRUE; }
133  virtual Int_t Recv(TMessage *&mess);
134  virtual Int_t Recv(Int_t &status, Int_t &kind);
135  virtual Int_t Recv(char *mess, Int_t max);
136  virtual Int_t Recv(char *mess, Int_t max, Int_t &kind);
137  virtual Int_t RecvRaw(void *buffer, Int_t length, ESendRecvOptions opt = kDefault);
138  virtual Int_t Reconnect() { return -1; }
139  virtual Int_t Select(Int_t interest = kRead, Long_t timeout = -1);
140  virtual Int_t Send(const TMessage &mess);
141  virtual Int_t Send(Int_t kind);
142  virtual Int_t Send(Int_t status, Int_t kind);
143  virtual Int_t Send(const char *mess, Int_t kind = kMESS_STRING);
144  virtual Int_t SendObject(const TObject *obj, Int_t kind = kMESS_OBJECT);
145  virtual Int_t SendRaw(const void *buffer, Int_t length,
146  ESendRecvOptions opt = kDefault);
150  virtual Int_t SetOption(ESockOptions opt, Int_t val);
151  void SetRemoteProtocol(Int_t rproto) { fRemoteProtocol = rproto; }
152  void SetSecContext(TSecContext *ctx) { fSecContext = ctx; }
153  void SetService(const char *service) { fService = service; }
155  void SetUrl(const char *url) { fUrl = url; }
156 
158 
159  static Int_t GetClientProtocol();
160 
161  static ULong64_t GetSocketBytesSent();
162  static ULong64_t GetSocketBytesRecv();
163 
164  static TSocket *CreateAuthSocket(const char *user, const char *host,
165  Int_t port, Int_t size = 0,
166  Int_t tcpwindowsize = -1, TSocket *s = 0, Int_t *err = 0);
167  static TSocket *CreateAuthSocket(const char *url, Int_t size = 0,
168  Int_t tcpwindowsize = -1, TSocket *s = 0, Int_t *err = 0);
169  static void NetError(const char *where, Int_t error);
170 
171  ClassDef(TSocket,0) //This class implements client sockets
172 };
173 
174 //______________________________________________________________________________
176 {
177  return (fCompress < 0) ? -1 : fCompress / 100;
178 }
179 
180 //______________________________________________________________________________
182 {
183  return (fCompress < 0) ? -1 : fCompress % 100;
184 }
185 
186 //______________________________________________________________________________
188 {
189  return (fCompress < 0) ? -1 : fCompress;
190 }
191 
192 #endif
TSocket::GetDescriptor
virtual Int_t GetDescriptor() const
Definition: TSocket.h:112
Compression.h
TSocket::fUrl
TString fUrl
Definition: TSocket.h:71
TSlave
Class describing a PROOF worker server.
Definition: TSlave.h:46
TSocket::RecvRaw
virtual Int_t RecvRaw(void *buffer, Int_t length, ESendRecvOptions opt=kDefault)
Receive a raw buffer of specified length bytes.
Definition: TSocket.cxx:897
kTRUE
const Bool_t kTRUE
Definition: RtypesCore.h:89
TTimeStamp::Set
void Set()
Set Date/Time to current time as reported by the system.
Definition: TTimeStamp.cxx:556
TSocket::EServiceType
EServiceType
Definition: TSocket.h:52
TSocket::SendStreamerInfos
void SendStreamerInfos(const TMessage &mess)
Check if TStreamerInfo must be sent.
Definition: TSocket.cxx:649
TSocket::SetRemoteProtocol
void SetRemoteProtocol(Int_t rproto)
Definition: TSocket.h:151
ROOT::RCompressionSetting::ELevel::kUseMin
@ kUseMin
Compression level reserved when we are not sure what to use (1 is for the fastest compression)
Definition: Compression.h:68
Option_t
const char Option_t
Definition: RtypesCore.h:64
TSocket::SendObject
virtual Int_t SendObject(const TObject *obj, Int_t kind=kMESS_OBJECT)
Send an object.
Definition: TSocket.cxx:600
TSocket::GetClientProtocol
static Int_t GetClientProtocol()
Static method returning supported client protocol.
Definition: TSocket.cxx:1468
TSocket::GetOption
Option_t * GetOption() const
Definition: TSocket.h:98
ROOT::RCompressionSetting::EDefaults::kUseCompiledDefault
@ kUseCompiledDefault
Use the compile-time default setting.
Definition: Compression.h:50
TTimeStamp.h
TSocket::SetCompressionLevel
void SetCompressionLevel(Int_t level=ROOT::RCompressionSetting::ELevel::kUseMin)
See comments for function SetCompressionSettings.
Definition: TSocket.cxx:1059
TSocket::kPROOFD
@ kPROOFD
Definition: TSocket.h:52
TSocket::fBitsInfo
TBits fBitsInfo
Definition: TSocket.h:72
TSocket::SetDescriptor
void SetDescriptor(Int_t desc)
Definition: TSocket.h:89
TSocket::RecvProcessIDs
Bool_t RecvProcessIDs(TMessage *mess)
Receive a message containing process ids.
Definition: TSocket.cxx:974
TSocket::kROOTD
@ kROOTD
Definition: TSocket.h:52
TSocket::GetCompressionAlgorithm
Int_t GetCompressionAlgorithm() const
Definition: TSocket.h:175
TSocket::GetCompressionLevel
Int_t GetCompressionLevel() const
Definition: TSocket.h:181
TNamed.h
TSocket::fSecContext
TSecContext * fSecContext
Definition: TSocket.h:65
TSocket::Select
virtual Int_t Select(Int_t interest=kRead, Long_t timeout=-1)
Waits for this socket to change status.
Definition: TSocket.cxx:443
TGeant4Unit::s
static constexpr double s
Definition: TGeant4SystemOfUnits.h:162
TSocket::GetLocalPort
virtual Int_t GetLocalPort()
Return the local port # to which the socket is bound.
Definition: TSocket.cxx:423
TSocket::SetService
void SetService(const char *service)
Definition: TSocket.h:153
TSocket::fLocalAddress
TInetAddress fLocalAddress
Definition: TSocket.h:63
Int_t
int Int_t
Definition: RtypesCore.h:43
TSocket::SetCompressionAlgorithm
void SetCompressionAlgorithm(Int_t algorithm=ROOT::RCompressionSetting::EAlgorithm::kUseGlobal)
See comments for function SetCompressionSettings.
Definition: TSocket.cxx:1045
TSocket::GetErrorCode
Int_t GetErrorCode() const
Returns error code.
Definition: TSocket.cxx:1034
TVirtualMutex
This class implements a mutex interface.
Definition: TVirtualMutex.h:34
TSecContext.h
MessageTypes.h
TSocket::IsValid
virtual Bool_t IsValid() const
Definition: TSocket.h:132
kMESS_OBJECT
@ kMESS_OBJECT
Definition: MessageTypes.h:35
TSocket::fTcpWindowSize
Int_t fTcpWindowSize
Definition: TSocket.h:70
TSocket::GetService
const char * GetService() const
Definition: TSocket.h:116
TSocket::GetBytesRecv
UInt_t GetBytesRecv() const
Definition: TSocket.h:120
TBits
Container of bits.
Definition: TBits.h:27
TSocket::fCompress
Int_t fCompress
Definition: TSocket.h:62
TString
Basic string class.
Definition: TString.h:131
Bool_t
bool Bool_t
Definition: RtypesCore.h:61
TObject::GetOption
virtual Option_t * GetOption() const
Definition: TObject.h:135
TSocket::SetServType
void SetServType(Int_t st)
Definition: TSocket.h:154
TSocket::Send
virtual Int_t Send(const TMessage &mess)
Send a TMessage object.
Definition: TSocket.cxx:522
TSocket::Reconnect
virtual Int_t Reconnect()
Definition: TSocket.h:138
bool
TSocket::fLastUsageMtx
TVirtualMutex * fLastUsageMtx
Definition: TSocket.h:75
TSocket::fAddress
TInetAddress fAddress
Definition: TSocket.h:59
TSocket::fSocket
Int_t fSocket
Definition: TSocket.h:69
TSocket
Definition: TSocket.h:41
TSocket::kBrokenConn
@ kBrokenConn
Definition: TSocket.h:49
TSocket::fBytesSent
UInt_t fBytesSent
Definition: TSocket.h:61
TSocket::~TSocket
virtual ~TSocket()
Definition: TSocket.h:109
TSocket::SendRaw
virtual Int_t SendRaw(const void *buffer, Int_t length, ESendRecvOptions opt=kDefault)
Send a raw buffer of specified length.
Definition: TSocket.cxx:620
TSocket::fUUIDs
TList * fUUIDs
Definition: TSocket.h:73
TSocket::fgBytesSent
static ULong64_t fgBytesSent
Definition: TSocket.h:79
TSocket::GetRemoteProtocol
Int_t GetRemoteProtocol() const
Definition: TSocket.h:126
TSystem.h
TSocket::GetCompressionSettings
Int_t GetCompressionSettings() const
Definition: TSocket.h:187
TSocket::GetSecContext
TSecContext * GetSecContext() const
Definition: TSocket.h:127
ESendRecvOptions
ESendRecvOptions
Definition: TSystem.h:227
TSocket::GetLocalInetAddress
virtual TInetAddress GetLocalInetAddress()
Return internet address of local host to which the socket is bound.
Definition: TSocket.cxx:409
TNamed
The TNamed class is the base class for all named ROOT classes.
Definition: TNamed.h:29
kFALSE
const Bool_t kFALSE
Definition: RtypesCore.h:90
TSocket::fService
TString fService
Definition: TSocket.h:67
THostAuth
Definition: THostAuth.h:34
Long_t
long Long_t
Definition: RtypesCore.h:52
TSocket::GetPort
Int_t GetPort() const
Definition: TSocket.h:115
TInetAddress.h
BIT
#define BIT(n)
Definition: Rtypes.h:83
TSocket::GetUrl
const char * GetUrl() const
Definition: TSocket.h:130
TSocket::EInterest
EInterest
Definition: TSocket.h:51
TSocket::kWrite
@ kWrite
Definition: TSocket.h:51
TSocket::SetCompressionSettings
void SetCompressionSettings(Int_t settings=ROOT::RCompressionSetting::EDefaults::kUseCompiledDefault)
Used to specify the compression level and algorithm: settings = 100 * algorithm + level.
Definition: TSocket.cxx:1097
ROOT::RCompressionSetting::EAlgorithm::kUseGlobal
@ kUseGlobal
Use the global compression algorithm.
Definition: Compression.h:88
kMESS_STRING
@ kMESS_STRING
Definition: MessageTypes.h:34
TSocket::TSocket
TSocket()
Definition: TSocket.h:83
TVirtualMutex.h
TSocket::SetSecContext
void SetSecContext(TSecContext *ctx)
Definition: TSocket.h:152
TSocket::kInvalid
@ kInvalid
Definition: TSocket.h:56
TSocket::GetBytesSent
UInt_t GetBytesSent() const
Definition: TSocket.h:119
unsigned int
TSecContext
Definition: TSecContext.h:36
TSocket::GetTcpWindowSize
Int_t GetTcpWindowSize() const
Definition: TSocket.h:128
TSocket::operator=
TSocket & operator=(const TSocket &)
TSocket::kSOCKD
@ kSOCKD
Definition: TSocket.h:52
TSocket::GetSocketBytesRecv
static ULong64_t GetSocketBytesRecv()
Get total number of bytes received via all sockets.
Definition: TSocket.cxx:1496
ULong64_t
unsigned long long ULong64_t
Definition: RtypesCore.h:72
R__LOCKGUARD2
#define R__LOCKGUARD2(mutex)
Definition: TVirtualMutex.h:108
TSocket::fgClientProtocol
static Int_t fgClientProtocol
Definition: TSocket.h:81
TProofServ
Class providing the PROOF server.
Definition: TProofServ.h:66
TSocket::fBytesRecv
UInt_t fBytesRecv
Definition: TSocket.h:60
TBits.h
TSocket::MarkBrokenConnection
void MarkBrokenConnection()
Close the socket and mark as due to a broken connection.
Definition: TSocket.cxx:371
TSocket::ESocketErrors
ESocketErrors
Definition: TSocket.h:55
TSocket::GetServType
Int_t GetServType() const
Definition: TSocket.h:117
TSocket::fLastUsage
TTimeStamp fLastUsage
Definition: TSocket.h:76
TObject
Mother of all ROOT objects.
Definition: TObject.h:37
ClassDef
#define ClassDef(name, id)
Definition: Rtypes.h:322
TSocket::IsAuthenticated
virtual Bool_t IsAuthenticated() const
Definition: TSocket.h:131
TSocket::Recv
virtual Int_t Recv(TMessage *&mess)
Receive a TMessage object.
Definition: TSocket.cxx:817
TSocket::GetLastUsage
TTimeStamp GetLastUsage()
Definition: TSocket.h:129
TSocket::fServType
EServiceType fServType
Definition: TSocket.h:68
TSocket::GetInetAddress
TInetAddress GetInetAddress() const
Definition: TSocket.h:113
TSocket::SetUrl
void SetUrl(const char *url)
Definition: TSocket.h:155
TSocket::kIsUnix
@ kIsUnix
Definition: TSocket.h:48
TServerSocket
Definition: TServerSocket.h:42
ESockOptions
ESockOptions
Definition: TSystem.h:214
kDefault
@ kDefault
Definition: TSystem.h:228
TTimeStamp
The TTimeStamp encapsulates seconds and ns since EPOCH.
Definition: TTimeStamp.h:71
TMessage
Definition: TMessage.h:33
TSocket::RecvStreamerInfos
Bool_t RecvStreamerInfos(TMessage *mess)
Receive a message containing streamer infos.
Definition: TSocket.cxx:927
TSocket::fgBytesRecv
static ULong64_t fgBytesRecv
Definition: TSocket.h:78
TSocket::SetOption
virtual Int_t SetOption(ESockOptions opt, Int_t val)
Set socket options.
Definition: TSocket.cxx:1012
TInetAddress
This class represents an Internet Protocol (IP) address.
Definition: TInetAddress.h:36
TSocket::fRemoteProtocol
Int_t fRemoteProtocol
Definition: TSocket.h:64
TSocket::Authenticate
Bool_t Authenticate(const char *user)
Authenticated the socket with specified user.
Definition: TSocket.cxx:1105
TSocket::Close
virtual void Close(Option_t *opt="")
Close the socket.
Definition: TSocket.cxx:389
ROOT
tbb::task_arena is an alias of tbb::interface7::task_arena, which doesn't allow to forward declare tb...
Definition: StringConv.hxx:21
TSocket::Touch
void Touch()
Definition: TSocket.h:157
TSocket::kInvalidStillInList
@ kInvalidStillInList
Definition: TSocket.h:57
TVirtualAuth.h
TObject::EStatusBits
EStatusBits
Definition: TObject.h:57
TSocket::NetError
static void NetError(const char *where, Int_t error)
Print error string depending on error code.
Definition: TSocket.cxx:1476
TList
A doubly linked list.
Definition: TList.h:44
TSocket::SendProcessIDs
void SendProcessIDs(const TMessage &mess)
Check if TProcessIDs must be sent.
Definition: TSocket.cxx:684
TSocket::kRead
@ kRead
Definition: TSocket.h:51
int
TSocket::CreateAuthSocket
static TSocket * CreateAuthSocket(const char *user, const char *host, Int_t port, Int_t size=0, Int_t tcpwindowsize=-1, TSocket *s=0, Int_t *err=0)
Creates a socket or a parallel socket and authenticates to the remote server specified in 'url' on re...
Definition: TSocket.cxx:1431
TSocket::GetSocketBytesSent
static ULong64_t GetSocketBytesSent()
Get total number of bytes sent via all sockets.
Definition: TSocket.cxx:1488
TInetAddress::GetPort
Int_t GetPort() const
Definition: TInetAddress.h:73