Logo ROOT   6.10/09
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:
74  TInetAddress fAddress; // remote internet address and port #
75  UInt_t fBytesRecv; // total bytes received over this socket
76  UInt_t fBytesSent; // total bytes sent using this socket
77  Int_t fCompress; // Compression level and algorithm
78  TInetAddress fLocalAddress; // local internet address and port #
79  Int_t fRemoteProtocol; // protocol of remote daemon
80  TSecContext *fSecContext; // after a successful Authenticate call
81  // points to related security context
82  TString fService; // name of service (matches remote port #)
83  EServiceType fServType; // remote service type
84  Int_t fSocket; // socket descriptor
85  Int_t fTcpWindowSize; // TCP window size (default 65535);
86  TString fUrl; // needs this for special authentication options
87  TBits fBitsInfo; // bits array to mark TStreamerInfo classes already sent
88  TList *fUUIDs; // list of TProcessIDs already sent through the socket
89 
90  TVirtualMutex *fLastUsageMtx; // Protect last usage setting / reading
91  TTimeStamp fLastUsage; // Time stamp of last usage
92 
93  static ULong64_t fgBytesRecv; // total bytes received by all socket objects
94  static ULong64_t fgBytesSent; // total bytes sent by all socket objects
95 
96  static Int_t fgClientProtocol; // client "protocol" version
97 
98  TSocket() : fAddress(), fBytesRecv(0), fBytesSent(0), fCompress(0),
99  fLocalAddress(), fRemoteProtocol(), fSecContext(0), fService(),
100  fServType(kSOCKD), fSocket(-1), fTcpWindowSize(0), fUrl(),
101  fBitsInfo(), fUUIDs(0), fLastUsageMtx(0), fLastUsage() { }
102 
103  Bool_t Authenticate(const char *user);
104  void SetDescriptor(Int_t desc) { fSocket = desc; }
105  void SendStreamerInfos(const TMessage &mess);
107  void SendProcessIDs(const TMessage &mess);
109 
110 private:
111  TSocket& operator=(const TSocket &); // not implemented
112  Option_t *GetOption() const { return TObject::GetOption(); }
113 
114 public:
115  TSocket(TInetAddress address, const char *service, Int_t tcpwindowsize = -1);
116  TSocket(TInetAddress address, Int_t port, Int_t tcpwindowsize = -1);
117  TSocket(const char *host, const char *service, Int_t tcpwindowsize = -1);
118  TSocket(const char *host, Int_t port, Int_t tcpwindowsize = -1);
119  TSocket(const char *sockpath);
120  TSocket(Int_t descriptor);
121  TSocket(Int_t descriptor, const char *sockpath);
122  TSocket(const TSocket &s);
123  virtual ~TSocket() { Close(); }
124 
125  virtual void Close(Option_t *opt="");
126  virtual Int_t GetDescriptor() const { return fSocket; }
129  Int_t GetPort() const { return fAddress.GetPort(); }
130  const char *GetService() const { return fService; }
131  Int_t GetServType() const { return (Int_t)fServType; }
132  virtual Int_t GetLocalPort();
133  UInt_t GetBytesSent() const { return fBytesSent; }
134  UInt_t GetBytesRecv() const { return fBytesRecv; }
136  Int_t GetCompressionLevel() const;
138  Int_t GetErrorCode() const;
139  virtual Int_t GetOption(ESockOptions opt, Int_t &val);
141  TSecContext *GetSecContext() const { return fSecContext; }
143  TTimeStamp GetLastUsage() { R__LOCKGUARD2(fLastUsageMtx); return fLastUsage; }
144  const char *GetUrl() const { return fUrl; }
145  virtual Bool_t IsAuthenticated() const { return fSecContext ? kTRUE : kFALSE; }
146  virtual Bool_t IsValid() const { return fSocket < 0 ? kFALSE : kTRUE; }
147  virtual Int_t Recv(TMessage *&mess);
148  virtual Int_t Recv(Int_t &status, Int_t &kind);
149  virtual Int_t Recv(char *mess, Int_t max);
150  virtual Int_t Recv(char *mess, Int_t max, Int_t &kind);
151  virtual Int_t RecvRaw(void *buffer, Int_t length, ESendRecvOptions opt = kDefault);
152  virtual Int_t Reconnect() { return -1; }
153  virtual Int_t Select(Int_t interest = kRead, Long_t timeout = -1);
154  virtual Int_t Send(const TMessage &mess);
155  virtual Int_t Send(Int_t kind);
156  virtual Int_t Send(Int_t status, Int_t kind);
157  virtual Int_t Send(const char *mess, Int_t kind = kMESS_STRING);
158  virtual Int_t SendObject(const TObject *obj, Int_t kind = kMESS_OBJECT);
159  virtual Int_t SendRaw(const void *buffer, Int_t length,
160  ESendRecvOptions opt = kDefault);
161  void SetCompressionAlgorithm(Int_t algorithm=0);
162  void SetCompressionLevel(Int_t level=1);
163  void SetCompressionSettings(Int_t settings=1);
164  virtual Int_t SetOption(ESockOptions opt, Int_t val);
165  void SetRemoteProtocol(Int_t rproto) { fRemoteProtocol = rproto; }
166  void SetSecContext(TSecContext *ctx) { fSecContext = ctx; }
167  void SetService(const char *service) { fService = service; }
168  void SetServType(Int_t st) { fServType = (EServiceType)st; }
169  void SetUrl(const char *url) { fUrl = url; }
170 
171  void Touch() { R__LOCKGUARD2(fLastUsageMtx); fLastUsage.Set(); }
172 
173  static Int_t GetClientProtocol();
174 
175  static ULong64_t GetSocketBytesSent();
176  static ULong64_t GetSocketBytesRecv();
177 
178  static TSocket *CreateAuthSocket(const char *user, const char *host,
179  Int_t port, Int_t size = 0,
180  Int_t tcpwindowsize = -1, TSocket *s = 0, Int_t *err = 0);
181  static TSocket *CreateAuthSocket(const char *url, Int_t size = 0,
182  Int_t tcpwindowsize = -1, TSocket *s = 0, Int_t *err = 0);
183  static void NetError(const char *where, Int_t error);
184 
185  ClassDef(TSocket,0) //This class implements client sockets
186 };
187 
188 //______________________________________________________________________________
190 {
191  return (fCompress < 0) ? -1 : fCompress / 100;
192 }
193 
194 //______________________________________________________________________________
196 {
197  return (fCompress < 0) ? -1 : fCompress % 100;
198 }
199 
200 //______________________________________________________________________________
202 {
203  return (fCompress < 0) ? -1 : fCompress;
204 }
205 
206 #endif
Int_t GetServType() const
Definition: TSocket.h:131
Int_t fSocket
Definition: TSocket.h:84
void SetRemoteProtocol(Int_t rproto)
Definition: TSocket.h:165
virtual Bool_t IsValid() const
Definition: TSocket.h:146
Int_t fTcpWindowSize
Definition: TSocket.h:85
Bool_t RecvStreamerInfos(TMessage *mess)
Receive a message containing streamer infos.
Definition: TSocket.cxx:932
Int_t GetCompressionLevel() const
Definition: TSocket.h:195
UInt_t GetBytesRecv() const
Definition: TSocket.h:134
Int_t GetPort() const
Definition: TSocket.h:129
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:152
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:520
virtual Int_t SetOption(ESockOptions opt, Int_t val)
Set socket options.
Definition: TSocket.cxx:1017
virtual ~TSocket()
Definition: TSocket.h:123
static Int_t fgClientProtocol
Definition: TSocket.h:96
virtual Int_t Recv(TMessage *&mess)
Receive a TMessage object.
Definition: TSocket.cxx:818
#define BIT(n)
Definition: Rtypes.h:75
This class represents an Internet Protocol (IP) address.
Definition: TInetAddress.h:36
This class implements a mutex interface.
Definition: TVirtualMutex.h:32
static ULong64_t GetSocketBytesRecv()
Get total number of bytes received via all sockets.
Definition: TSocket.cxx:1522
Basic string class.
Definition: TString.h:129
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
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:166
Option_t * GetOption() const
Definition: TSocket.h:112
TInetAddress fAddress
Definition: TSocket.h:74
TInetAddress fLocalAddress
Definition: TSocket.h:78
ESendRecvOptions
Definition: TSocket.h:49
void SetServType(Int_t st)
Definition: TSocket.h:168
TTimeStamp GetLastUsage()
Definition: TSocket.h:143
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:620
TVirtualMutex * fLastUsageMtx
Definition: TSocket.h:90
#define ClassDef(name, id)
Definition: Rtypes.h:297
The TNamed class is the base class for all named ROOT classes.
Definition: TNamed.h:29
TString fUrl
Definition: TSocket.h:86
TBits fBitsInfo
Definition: TSocket.h:87
EInterest
Definition: TSocket.h:70
const char * GetUrl() const
Definition: TSocket.h:144
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:82
Int_t GetRemoteProtocol() const
Definition: TSocket.h:140
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:43
void SetDescriptor(Int_t desc)
Definition: TSocket.h:104
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:133
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:189
unsigned int UInt_t
Definition: RtypesCore.h:42
UInt_t fBytesSent
Definition: TSocket.h:76
TSocket()
Definition: TSocket.h:98
Definition: TSocket.h:52
void SetCompressionAlgorithm(Int_t algorithm=0)
See comments for function SetCompressionSettings.
Definition: TSocket.cxx:1050
void Touch()
Definition: TSocket.h:171
#define R__LOCKGUARD2(mutex)
const Bool_t kFALSE
Definition: RtypesCore.h:92
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:77
TList * fUUIDs
Definition: TSocket.h:88
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:91
virtual Option_t * GetOption() const
Definition: TObject.h:110
virtual Bool_t IsAuthenticated() const
Definition: TSocket.h:145
Definition: TSocket.h:51
EServiceType fServType
Definition: TSocket.h:83
Mother of all ROOT objects.
Definition: TObject.h:37
static ULong64_t fgBytesSent
Definition: TSocket.h:94
Container of bits.
Definition: TBits.h:29
Int_t GetErrorCode() const
Returns error code.
Definition: TSocket.cxx:1039
TSecContext * GetSecContext() const
Definition: TSocket.h:141
void SendProcessIDs(const TMessage &mess)
Check if TProcessIDs must be sent.
Definition: TSocket.cxx:685
virtual Int_t GetDescriptor() const
Definition: TSocket.h:126
static ULong64_t fgBytesRecv
Definition: TSocket.h:93
static Int_t GetClientProtocol()
Static method returning supported client protocol.
Definition: TSocket.cxx:1494
TInetAddress GetInetAddress() const
Definition: TSocket.h:127
Int_t fRemoteProtocol
Definition: TSocket.h:79
void SetUrl(const char *url)
Definition: TSocket.h:169
Int_t GetCompressionSettings() const
Definition: TSocket.h:201
Int_t GetTcpWindowSize() const
Definition: TSocket.h:142
void SetService(const char *service)
Definition: TSocket.h:167
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:901
const Bool_t kTRUE
Definition: RtypesCore.h:91
const char * GetService() const
Definition: TSocket.h:130
TSecContext * fSecContext
Definition: TSocket.h:80
Bool_t Authenticate(const char *user)
Authenticated the socket with specified user.
Definition: TSocket.cxx:1111
UInt_t fBytesRecv
Definition: TSocket.h:75