Logo ROOT   6.14/05
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 "TNamed.h"
28 #include "TBits.h"
29 #include "TInetAddress.h"
30 #include "MessageTypes.h"
31 #include "TVirtualAuth.h"
32 #include "TSecContext.h"
33 #include "TTimeStamp.h"
34 #include "TVirtualMutex.h"
35 
37  kSendBuffer, // size of send buffer
38  kRecvBuffer, // size of receive buffer
39  kOobInline, // OOB message inline
40  kKeepAlive, // keep socket alive
41  kReuseAddr, // allow reuse of local portion of address 5-tuple
42  kNoDelay, // send without delay
43  kNoBlock, // non-blocking I/O
44  kProcessGroup, // socket process group (used for SIGURG and SIGIO)
45  kAtMark, // are we at out-of-band mark (read only)
46  kBytesToRead // get number of bytes to read, FIONREAD (read only)
47 };
48 
50  kDefault, // default option (= 0)
51  kOob, // send or receive out-of-band data
52  kPeek, // peek at incoming message (receive only)
53  kDontBlock // send/recv as much data as possible without blocking
54 };
55 
56 
57 class TMessage;
58 class THostAuth;
59 
60 class TSocket : public TNamed {
61 
62 friend class TServerSocket;
63 friend class TProofServ; // to be able to call SetDescriptor(), RecvHostAuth()
64 friend class TSlave; // to be able to call SendHostAuth()
65 
66 public:
67  enum EStatusBits { kIsUnix = BIT(16), // set if unix socket
68  kBrokenConn = BIT(17) // set if conn reset by peer or broken
69  };
70  enum EInterest { kRead = 1, kWrite = 2 };
72 
73 protected:
75  kInvalid = -1,
77  };
78  TInetAddress fAddress; // remote internet address and port #
79  UInt_t fBytesRecv; // total bytes received over this socket
80  UInt_t fBytesSent; // total bytes sent using this socket
81  Int_t fCompress; // Compression level and algorithm
82  TInetAddress fLocalAddress; // local internet address and port #
83  Int_t fRemoteProtocol; // protocol of remote daemon
84  TSecContext *fSecContext; // after a successful Authenticate call
85  // points to related security context
86  TString fService; // name of service (matches remote port #)
87  EServiceType fServType; // remote service type
88  Int_t fSocket; // socket descriptor
89  Int_t fTcpWindowSize; // TCP window size (default 65535);
90  TString fUrl; // needs this for special authentication options
91  TBits fBitsInfo; // bits array to mark TStreamerInfo classes already sent
92  TList *fUUIDs; // list of TProcessIDs already sent through the socket
93 
94  TVirtualMutex *fLastUsageMtx; // Protect last usage setting / reading
95  TTimeStamp fLastUsage; // Time stamp of last usage
96 
97  static ULong64_t fgBytesRecv; // total bytes received by all socket objects
98  static ULong64_t fgBytesSent; // total bytes sent by all socket objects
99 
100  static Int_t fgClientProtocol; // client "protocol" version
101 
102  TSocket() : fAddress(), fBytesRecv(0), fBytesSent(0), fCompress(0),
103  fLocalAddress(), fRemoteProtocol(), fSecContext(0), fService(),
104  fServType(kSOCKD), fSocket(-1), fTcpWindowSize(0), fUrl(),
105  fBitsInfo(), fUUIDs(0), fLastUsageMtx(0), fLastUsage() { }
106 
107  Bool_t Authenticate(const char *user);
108  void SetDescriptor(Int_t desc) { fSocket = desc; }
109  void SendStreamerInfos(const TMessage &mess);
111  void SendProcessIDs(const TMessage &mess);
113  void MarkBrokenConnection();
114 
115 private:
116  TSocket& operator=(const TSocket &); // not implemented
117  Option_t *GetOption() const { return TObject::GetOption(); }
118 
119 public:
120  TSocket(TInetAddress address, const char *service, Int_t tcpwindowsize = -1);
121  TSocket(TInetAddress address, Int_t port, Int_t tcpwindowsize = -1);
122  TSocket(const char *host, const char *service, Int_t tcpwindowsize = -1);
123  TSocket(const char *host, Int_t port, Int_t tcpwindowsize = -1);
124  TSocket(const char *sockpath);
125  TSocket(Int_t descriptor);
126  TSocket(Int_t descriptor, const char *sockpath);
127  TSocket(const TSocket &s);
128  virtual ~TSocket() { Close(); }
129 
130  virtual void Close(Option_t *opt="");
131  virtual Int_t GetDescriptor() const { return fSocket; }
134  Int_t GetPort() const { return fAddress.GetPort(); }
135  const char *GetService() const { return fService; }
136  Int_t GetServType() const { return (Int_t)fServType; }
137  virtual Int_t GetLocalPort();
138  UInt_t GetBytesSent() const { return fBytesSent; }
139  UInt_t GetBytesRecv() const { return fBytesRecv; }
141  Int_t GetCompressionLevel() const;
143  Int_t GetErrorCode() const;
144  virtual Int_t GetOption(ESockOptions opt, Int_t &val);
146  TSecContext *GetSecContext() const { return fSecContext; }
148  TTimeStamp GetLastUsage() { R__LOCKGUARD2(fLastUsageMtx); return fLastUsage; }
149  const char *GetUrl() const { return fUrl; }
150  virtual Bool_t IsAuthenticated() const { return fSecContext ? kTRUE : kFALSE; }
151  virtual Bool_t IsValid() const { return fSocket < 0 ? kFALSE : kTRUE; }
152  virtual Int_t Recv(TMessage *&mess);
153  virtual Int_t Recv(Int_t &status, Int_t &kind);
154  virtual Int_t Recv(char *mess, Int_t max);
155  virtual Int_t Recv(char *mess, Int_t max, Int_t &kind);
156  virtual Int_t RecvRaw(void *buffer, Int_t length, ESendRecvOptions opt = kDefault);
157  virtual Int_t Reconnect() { return -1; }
158  virtual Int_t Select(Int_t interest = kRead, Long_t timeout = -1);
159  virtual Int_t Send(const TMessage &mess);
160  virtual Int_t Send(Int_t kind);
161  virtual Int_t Send(Int_t status, Int_t kind);
162  virtual Int_t Send(const char *mess, Int_t kind = kMESS_STRING);
163  virtual Int_t SendObject(const TObject *obj, Int_t kind = kMESS_OBJECT);
164  virtual Int_t SendRaw(const void *buffer, Int_t length,
165  ESendRecvOptions opt = kDefault);
166  void SetCompressionAlgorithm(Int_t algorithm=0);
167  void SetCompressionLevel(Int_t level=1);
168  void SetCompressionSettings(Int_t settings=1);
169  virtual Int_t SetOption(ESockOptions opt, Int_t val);
170  void SetRemoteProtocol(Int_t rproto) { fRemoteProtocol = rproto; }
171  void SetSecContext(TSecContext *ctx) { fSecContext = ctx; }
172  void SetService(const char *service) { fService = service; }
173  void SetServType(Int_t st) { fServType = (EServiceType)st; }
174  void SetUrl(const char *url) { fUrl = url; }
175 
176  void Touch() { R__LOCKGUARD2(fLastUsageMtx); fLastUsage.Set(); }
177 
178  static Int_t GetClientProtocol();
179 
180  static ULong64_t GetSocketBytesSent();
181  static ULong64_t GetSocketBytesRecv();
182 
183  static TSocket *CreateAuthSocket(const char *user, const char *host,
184  Int_t port, Int_t size = 0,
185  Int_t tcpwindowsize = -1, TSocket *s = 0, Int_t *err = 0);
186  static TSocket *CreateAuthSocket(const char *url, Int_t size = 0,
187  Int_t tcpwindowsize = -1, TSocket *s = 0, Int_t *err = 0);
188  static void NetError(const char *where, Int_t error);
189 
190  ClassDef(TSocket,0) //This class implements client sockets
191 };
192 
193 //______________________________________________________________________________
195 {
196  return (fCompress < 0) ? -1 : fCompress / 100;
197 }
198 
199 //______________________________________________________________________________
201 {
202  return (fCompress < 0) ? -1 : fCompress % 100;
203 }
204 
205 //______________________________________________________________________________
207 {
208  return (fCompress < 0) ? -1 : fCompress;
209 }
210 
211 #endif
Int_t GetServType() const
Definition: TSocket.h:136
Int_t fSocket
Definition: TSocket.h:88
void SetRemoteProtocol(Int_t rproto)
Definition: TSocket.h:170
virtual Bool_t IsValid() const
Definition: TSocket.h:151
Int_t fTcpWindowSize
Definition: TSocket.h:89
Bool_t RecvStreamerInfos(TMessage *mess)
Receive a message containing streamer infos.
Definition: TSocket.cxx:932
Int_t GetCompressionLevel() const
Definition: TSocket.h:200
UInt_t GetBytesRecv() const
Definition: TSocket.h:139
Int_t GetPort() const
Definition: TSocket.h:134
void SetCompressionSettings(Int_t settings=1)
Used to specify the compression level and algorithm: settings = 100 * algorithm + level...
Definition: TSocket.cxx:1103
ESocketErrors
Definition: TSocket.h:74
const char Option_t
Definition: RtypesCore.h:62
virtual Int_t Reconnect()
Definition: TSocket.h:157
ESockOptions
Definition: TSocket.h:36
Bool_t RecvProcessIDs(TMessage *mess)
Receive a message containing process ids.
Definition: TSocket.cxx:979
virtual Int_t Send(const TMessage &mess)
Send a TMessage object.
Definition: TSocket.cxx:527
virtual Int_t SetOption(ESockOptions opt, Int_t val)
Set socket options.
Definition: TSocket.cxx:1017
virtual ~TSocket()
Definition: TSocket.h:128
static Int_t fgClientProtocol
Definition: TSocket.h:100
virtual Int_t Recv(TMessage *&mess)
Receive a TMessage object.
Definition: TSocket.cxx:822
#define BIT(n)
Definition: Rtypes.h:78
This class represents an Internet Protocol (IP) address.
Definition: TInetAddress.h:36
This class implements a mutex interface.
Definition: TVirtualMutex.h:34
static ULong64_t GetSocketBytesRecv()
Get total number of bytes received via all sockets.
Definition: TSocket.cxx:1522
Basic string class.
Definition: TString.h:131
void SendStreamerInfos(const TMessage &mess)
Check if TStreamerInfo must be sent.
Definition: TSocket.cxx:654
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
virtual Int_t SendObject(const TObject *obj, Int_t kind=kMESS_OBJECT)
Send an object.
Definition: TSocket.cxx:605
void SetSecContext(TSecContext *ctx)
Definition: TSocket.h:171
Option_t * GetOption() const
Definition: TSocket.h:117
TInetAddress fAddress
Definition: TSocket.h:78
TInetAddress fLocalAddress
Definition: TSocket.h:82
ESendRecvOptions
Definition: TSocket.h:49
void SetServType(Int_t st)
Definition: TSocket.h:173
TTimeStamp GetLastUsage()
Definition: TSocket.h:148
EServiceType
Definition: TSocket.h:71
virtual Int_t SendRaw(const void *buffer, Int_t length, ESendRecvOptions opt=kDefault)
Send a raw buffer of specified length.
Definition: TSocket.cxx:625
TVirtualMutex * fLastUsageMtx
Definition: TSocket.h:94
#define ClassDef(name, id)
Definition: Rtypes.h:320
The TNamed class is the base class for all named ROOT classes.
Definition: TNamed.h:29
TString fUrl
Definition: TSocket.h:90
TBits fBitsInfo
Definition: TSocket.h:91
EInterest
Definition: TSocket.h:70
const char * GetUrl() const
Definition: TSocket.h:149
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 &#39;url&#39; on re...
Definition: TSocket.cxx:1457
void Set()
Set Date/Time to current time as reported by the system.
Definition: TTimeStamp.cxx:556
Int_t GetPort() const
Definition: TInetAddress.h:73
TString fService
Definition: TSocket.h:86
Int_t GetRemoteProtocol() const
Definition: TSocket.h:145
void SetCompressionLevel(Int_t level=1)
See comments for function SetCompressionSettings.
Definition: TSocket.cxx:1065
virtual Int_t GetLocalPort()
Return the local port # to which the socket is bound.
Definition: TSocket.cxx:428
A doubly linked list.
Definition: TList.h:44
void SetDescriptor(Int_t desc)
Definition: TSocket.h:108
virtual void Close(Option_t *opt="")
Close the socket.
Definition: TSocket.cxx:394
virtual Int_t Select(Int_t interest=kRead, Long_t timeout=-1)
Waits for this socket to change status.
Definition: TSocket.cxx:448
UInt_t GetBytesSent() const
Definition: TSocket.h:138
Class providing the PROOF server.
Definition: TProofServ.h:66
static void NetError(const char *where, Int_t error)
Print error string depending on error code.
Definition: TSocket.cxx:1502
static ULong64_t GetSocketBytesSent()
Get total number of bytes sent via all sockets.
Definition: TSocket.cxx:1514
Int_t GetCompressionAlgorithm() const
Definition: TSocket.h:194
unsigned int UInt_t
Definition: RtypesCore.h:42
UInt_t fBytesSent
Definition: TSocket.h:80
TSocket()
Definition: TSocket.h:102
Definition: TSocket.h:52
void SetCompressionAlgorithm(Int_t algorithm=0)
See comments for function SetCompressionSettings.
Definition: TSocket.cxx:1050
void MarkBrokenConnection()
Close the socket and mark as due to a broken connection.
Definition: TSocket.cxx:376
void Touch()
Definition: TSocket.h:176
#define R__LOCKGUARD2(mutex)
const Bool_t kFALSE
Definition: RtypesCore.h:88
virtual TInetAddress GetLocalInetAddress()
Return internet address of local host to which the socket is bound.
Definition: TSocket.cxx:414
long Long_t
Definition: RtypesCore.h:50
Int_t fCompress
Definition: TSocket.h:81
TList * fUUIDs
Definition: TSocket.h:92
EStatusBits
Definition: TObject.h:57
TSocket & operator=(const TSocket &)
unsigned long long ULong64_t
Definition: RtypesCore.h:70
The TTimeStamp encapsulates seconds and ns since EPOCH.
Definition: TTimeStamp.h:71
TTimeStamp fLastUsage
Definition: TSocket.h:95
virtual Option_t * GetOption() const
Definition: TObject.h:120
static constexpr double s
virtual Bool_t IsAuthenticated() const
Definition: TSocket.h:150
Definition: TSocket.h:51
EServiceType fServType
Definition: TSocket.h:87
Mother of all ROOT objects.
Definition: TObject.h:37
static ULong64_t fgBytesSent
Definition: TSocket.h:98
Container of bits.
Definition: TBits.h:29
Int_t GetErrorCode() const
Returns error code.
Definition: TSocket.cxx:1039
TSecContext * GetSecContext() const
Definition: TSocket.h:146
void SendProcessIDs(const TMessage &mess)
Check if TProcessIDs must be sent.
Definition: TSocket.cxx:689
virtual Int_t GetDescriptor() const
Definition: TSocket.h:131
static ULong64_t fgBytesRecv
Definition: TSocket.h:97
static Int_t GetClientProtocol()
Static method returning supported client protocol.
Definition: TSocket.cxx:1494
TInetAddress GetInetAddress() const
Definition: TSocket.h:132
Int_t fRemoteProtocol
Definition: TSocket.h:83
void SetUrl(const char *url)
Definition: TSocket.h:174
Int_t GetCompressionSettings() const
Definition: TSocket.h:206
Int_t GetTcpWindowSize() const
Definition: TSocket.h:147
void SetService(const char *service)
Definition: TSocket.h:172
Class describing a PROOF worker server.
Definition: TSlave.h:46
virtual Int_t RecvRaw(void *buffer, Int_t length, ESendRecvOptions opt=kDefault)
Receive a raw buffer of specified length bytes.
Definition: TSocket.cxx:902
const Bool_t kTRUE
Definition: RtypesCore.h:87
const char * GetService() const
Definition: TSocket.h:135
TSecContext * fSecContext
Definition: TSocket.h:84
Bool_t Authenticate(const char *user)
Authenticated the socket with specified user.
Definition: TSocket.cxx:1111
UInt_t fBytesRecv
Definition: TSocket.h:79