Logo ROOT   6.16/01
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 "Compression.h"
28#include "TNamed.h"
29#include "TBits.h"
30#include "TInetAddress.h"
31#include "MessageTypes.h"
32#include "TVirtualAuth.h"
33#include "TSecContext.h"
34#include "TTimeStamp.h"
35#include "TVirtualMutex.h"
36
38 kSendBuffer, // size of send buffer
39 kRecvBuffer, // size of receive buffer
40 kOobInline, // OOB message inline
41 kKeepAlive, // keep socket alive
42 kReuseAddr, // allow reuse of local portion of address 5-tuple
43 kNoDelay, // send without delay
44 kNoBlock, // non-blocking I/O
45 kProcessGroup, // socket process group (used for SIGURG and SIGIO)
46 kAtMark, // are we at out-of-band mark (read only)
47 kBytesToRead // get number of bytes to read, FIONREAD (read only)
48};
49
51 kDefault, // default option (= 0)
52 kOob, // send or receive out-of-band data
53 kPeek, // peek at incoming message (receive only)
54 kDontBlock // send/recv as much data as possible without blocking
55};
56
57
58class TMessage;
59class THostAuth;
60
61class TSocket : public TNamed {
62
63friend class TServerSocket;
64friend class TProofServ; // to be able to call SetDescriptor(), RecvHostAuth()
65friend class TSlave; // to be able to call SendHostAuth()
66
67public:
68 enum EStatusBits { kIsUnix = BIT(16), // set if unix socket
69 kBrokenConn = BIT(17) // set if conn reset by peer or broken
70 };
71 enum EInterest { kRead = 1, kWrite = 2 };
73
74protected:
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 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),
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 &); // not implemented
118 Option_t *GetOption() const { 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);
126 TSocket(Int_t descriptor);
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);
150 const char *GetUrl() const { return fUrl; }
151 virtual Bool_t IsAuthenticated() const { return fSecContext ? kTRUE : kFALSE; }
152 virtual Bool_t IsValid() const { return fSocket < 0 ? kFALSE : kTRUE; }
153 virtual Int_t Recv(TMessage *&mess);
154 virtual Int_t Recv(Int_t &status, Int_t &kind);
155 virtual Int_t Recv(char *mess, Int_t max);
156 virtual Int_t Recv(char *mess, Int_t max, Int_t &kind);
157 virtual Int_t RecvRaw(void *buffer, Int_t length, ESendRecvOptions opt = kDefault);
158 virtual Int_t Reconnect() { return -1; }
159 virtual Int_t Select(Int_t interest = kRead, Long_t timeout = -1);
160 virtual Int_t Send(const TMessage &mess);
161 virtual Int_t Send(Int_t kind);
162 virtual Int_t Send(Int_t status, Int_t kind);
163 virtual Int_t Send(const char *mess, Int_t kind = kMESS_STRING);
164 virtual Int_t SendObject(const TObject *obj, Int_t kind = kMESS_OBJECT);
165 virtual Int_t SendRaw(const void *buffer, Int_t length,
170 virtual Int_t SetOption(ESockOptions opt, Int_t val);
171 void SetRemoteProtocol(Int_t rproto) { fRemoteProtocol = rproto; }
173 void SetService(const char *service) { fService = service; }
175 void SetUrl(const char *url) { fUrl = url; }
176
178
179 static Int_t GetClientProtocol();
180
183
184 static TSocket *CreateAuthSocket(const char *user, const char *host,
185 Int_t port, Int_t size = 0,
186 Int_t tcpwindowsize = -1, TSocket *s = 0, Int_t *err = 0);
187 static TSocket *CreateAuthSocket(const char *url, Int_t size = 0,
188 Int_t tcpwindowsize = -1, TSocket *s = 0, Int_t *err = 0);
189 static void NetError(const char *where, Int_t error);
190
191 ClassDef(TSocket,0) //This class implements client sockets
192};
193
194//______________________________________________________________________________
196{
197 return (fCompress < 0) ? -1 : fCompress / 100;
198}
199
200//______________________________________________________________________________
202{
203 return (fCompress < 0) ? -1 : fCompress % 100;
204}
205
206//______________________________________________________________________________
208{
209 return (fCompress < 0) ? -1 : fCompress;
210}
211
212#endif
@ kMESS_STRING
Definition: MessageTypes.h:34
@ kMESS_OBJECT
Definition: MessageTypes.h:35
int Int_t
Definition: RtypesCore.h:41
unsigned int UInt_t
Definition: RtypesCore.h:42
const Bool_t kFALSE
Definition: RtypesCore.h:88
long Long_t
Definition: RtypesCore.h:50
bool Bool_t
Definition: RtypesCore.h:59
unsigned long long ULong64_t
Definition: RtypesCore.h:70
const Bool_t kTRUE
Definition: RtypesCore.h:87
const char Option_t
Definition: RtypesCore.h:62
#define ClassDef(name, id)
Definition: Rtypes.h:324
#define BIT(n)
Definition: Rtypes.h:82
ESockOptions
Definition: TSocket.h:37
@ kKeepAlive
Definition: TSocket.h:41
@ kBytesToRead
Definition: TSocket.h:47
@ kReuseAddr
Definition: TSocket.h:42
@ kNoBlock
Definition: TSocket.h:44
@ kSendBuffer
Definition: TSocket.h:38
@ kNoDelay
Definition: TSocket.h:43
@ kOobInline
Definition: TSocket.h:40
@ kRecvBuffer
Definition: TSocket.h:39
@ kProcessGroup
Definition: TSocket.h:45
@ kAtMark
Definition: TSocket.h:46
ESendRecvOptions
Definition: TSocket.h:50
@ kDontBlock
Definition: TSocket.h:54
@ kDefault
Definition: TSocket.h:51
@ kPeek
Definition: TSocket.h:53
@ kOob
Definition: TSocket.h:52
#define R__LOCKGUARD2(mutex)
Container of bits.
Definition: TBits.h:27
This class represents an Internet Protocol (IP) address.
Definition: TInetAddress.h:36
Int_t GetPort() const
Definition: TInetAddress.h:73
A doubly linked list.
Definition: TList.h:44
The TNamed class is the base class for all named ROOT classes.
Definition: TNamed.h:29
Mother of all ROOT objects.
Definition: TObject.h:37
virtual Option_t * GetOption() const
Definition: TObject.h:120
EStatusBits
Definition: TObject.h:57
Class providing the PROOF server.
Definition: TProofServ.h:66
Class describing a PROOF worker server.
Definition: TSlave.h:46
TInetAddress fAddress
Definition: TSocket.h:79
Option_t * GetOption() const
Definition: TSocket.h:118
Int_t fCompress
Definition: TSocket.h:82
virtual Int_t SetOption(ESockOptions opt, Int_t val)
Set socket options.
Definition: TSocket.cxx:1012
Int_t fSocket
Definition: TSocket.h:89
Int_t GetErrorCode() const
Returns error code.
Definition: TSocket.cxx:1034
void SetServType(Int_t st)
Definition: TSocket.h:174
TVirtualMutex * fLastUsageMtx
Definition: TSocket.h:95
UInt_t GetBytesRecv() const
Definition: TSocket.h:140
void SetRemoteProtocol(Int_t rproto)
Definition: TSocket.h:171
void SetCompressionLevel(Int_t level=ROOT::RCompressionSetting::ELevel::kUseMin)
See comments for function SetCompressionSettings.
Definition: TSocket.cxx:1059
void SendStreamerInfos(const TMessage &mess)
Check if TStreamerInfo must be sent.
Definition: TSocket.cxx:649
ESocketErrors
Definition: TSocket.h:75
@ kInvalidStillInList
Definition: TSocket.h:77
@ kInvalid
Definition: TSocket.h:76
TString fUrl
Definition: TSocket.h:91
const char * GetService() const
Definition: TSocket.h:136
void SetCompressionAlgorithm(Int_t algorithm=ROOT::RCompressionSetting::EAlgorithm::kUseGlobal)
See comments for function SetCompressionSettings.
Definition: TSocket.cxx:1045
TSocket()
Definition: TSocket.h:103
static ULong64_t GetSocketBytesSent()
Get total number of bytes sent via all sockets.
Definition: TSocket.cxx:1508
TTimeStamp GetLastUsage()
Definition: TSocket.h:149
TString fService
Definition: TSocket.h:87
Bool_t RecvStreamerInfos(TMessage *mess)
Receive a message containing streamer infos.
Definition: TSocket.cxx:927
EInterest
Definition: TSocket.h:71
@ kRead
Definition: TSocket.h:71
@ kWrite
Definition: TSocket.h:71
virtual Int_t Recv(TMessage *&mess)
Receive a TMessage object.
Definition: TSocket.cxx:817
TList * fUUIDs
Definition: TSocket.h:93
static Int_t GetClientProtocol()
Static method returning supported client protocol.
Definition: TSocket.cxx:1488
TBits fBitsInfo
Definition: TSocket.h:92
void SetService(const char *service)
Definition: TSocket.h:173
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:1105
TInetAddress fLocalAddress
Definition: TSocket.h:83
Int_t GetTcpWindowSize() const
Definition: TSocket.h:148
static ULong64_t fgBytesRecv
Definition: TSocket.h:98
@ kIsUnix
Definition: TSocket.h:68
@ kBrokenConn
Definition: TSocket.h:69
virtual void Close(Option_t *opt="")
Close the socket.
Definition: TSocket.cxx:389
void MarkBrokenConnection()
Close the socket and mark as due to a broken connection.
Definition: TSocket.cxx:371
void Touch()
Definition: TSocket.h:177
Bool_t RecvProcessIDs(TMessage *mess)
Receive a message containing process ids.
Definition: TSocket.cxx:974
void SetUrl(const char *url)
Definition: TSocket.h:175
TInetAddress GetInetAddress() const
Definition: TSocket.h:133
Int_t GetCompressionLevel() const
Definition: TSocket.h:201
virtual Int_t RecvRaw(void *buffer, Int_t length, ESendRecvOptions opt=kDefault)
Receive a raw buffer of specified length bytes.
Definition: TSocket.cxx:897
TSecContext * GetSecContext() const
Definition: TSocket.h:147
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:620
TTimeStamp fLastUsage
Definition: TSocket.h:96
void SendProcessIDs(const TMessage &mess)
Check if TProcessIDs must be sent.
Definition: TSocket.cxx:684
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:409
TSecContext * fSecContext
Definition: TSocket.h:85
virtual Int_t Select(Int_t interest=kRead, Long_t timeout=-1)
Waits for this socket to change status.
Definition: TSocket.cxx:443
virtual Int_t GetLocalPort()
Return the local port # to which the socket is bound.
Definition: TSocket.cxx:423
virtual ~TSocket()
Definition: TSocket.h:129
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:1451
void SetDescriptor(Int_t desc)
Definition: TSocket.h:109
Int_t fTcpWindowSize
Definition: TSocket.h:90
TSocket & operator=(const TSocket &)
EServiceType
Definition: TSocket.h:72
@ kSOCKD
Definition: TSocket.h:72
@ kROOTD
Definition: TSocket.h:72
@ kPROOFD
Definition: TSocket.h:72
static void NetError(const char *where, Int_t error)
Print error string depending on error code.
Definition: TSocket.cxx:1496
virtual Int_t GetDescriptor() const
Definition: TSocket.h:132
Int_t GetCompressionSettings() const
Definition: TSocket.h:207
Int_t GetPort() const
Definition: TSocket.h:135
Int_t GetServType() const
Definition: TSocket.h:137
void SetCompressionSettings(Int_t settings=ROOT::RCompressionSetting::EDefaults::kUseGeneralPurpose)
Used to specify the compression level and algorithm: settings = 100 * algorithm + level.
Definition: TSocket.cxx:1097
EServiceType fServType
Definition: TSocket.h:88
virtual Int_t SendObject(const TObject *obj, Int_t kind=kMESS_OBJECT)
Send an object.
Definition: TSocket.cxx:600
void SetSecContext(TSecContext *ctx)
Definition: TSocket.h:172
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:195
virtual Bool_t IsValid() const
Definition: TSocket.h:152
virtual Int_t Reconnect()
Definition: TSocket.h:158
const char * GetUrl() const
Definition: TSocket.h:150
virtual Int_t Send(const TMessage &mess)
Send a TMessage object.
Definition: TSocket.cxx:522
virtual Bool_t IsAuthenticated() const
Definition: TSocket.h:151
static ULong64_t GetSocketBytesRecv()
Get total number of bytes received via all sockets.
Definition: TSocket.cxx:1516
Basic string class.
Definition: TString.h:131
The TTimeStamp encapsulates seconds and ns since EPOCH.
Definition: TTimeStamp.h:71
void Set()
Set Date/Time to current time as reported by the system.
Definition: TTimeStamp.cxx:556
This class implements a mutex interface.
Definition: TVirtualMutex.h:34
Namespace for new ROOT classes and functions.
Definition: StringConv.hxx:21
static constexpr double s
@ kUseGlobal
Use the global compression algorithm.
Definition: Compression.h:81
@ kUseGeneralPurpose
Use the recommended general-purpose setting; moderate read / write speed and compression ratio.
Definition: Compression.h:53