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