Logo ROOT   6.07/09
Reference Guide
TUDPSocket.h
Go to the documentation of this file.
1 // @(#)root/net:$Id$
2 // Author: Marcelo Sousa 26/10/2011
3 
4 /*************************************************************************
5  * Copyright (C) 1995-2011, 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_TUDPSocket
13 #define ROOT_TUDPSocket
14 
15 
16 //////////////////////////////////////////////////////////////////////////
17 // //
18 // TUDPSocket //
19 // //
20 // This class implements udp client sockets. A socket is an endpoint //
21 // for 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 #ifndef ROOT_TSocket
52 #include "TSocket.h"
53 #endif
54 
55 class TUDPSocket : public TNamed {
56 
57 friend class TServerSocket;
58 
59 public:
60  enum EStatusBits { kIsUnix = BIT(16), // set if unix socket
61  kBrokenConn = BIT(17) // set if conn reset by peer or broken
62  };
63  enum EInterest { kRead = 1, kWrite = 2 };
65 
66 protected:
67  TInetAddress fAddress; // remote internet address and port #
68  UInt_t fBytesRecv; // total bytes received over this socket
69  UInt_t fBytesSent; // total bytes sent using this socket
70  Int_t fCompress; // Compression level and algorithm
71  TInetAddress fLocalAddress; // local internet address and port #
72  Int_t fRemoteProtocol; // protocol of remote daemon
73  TSecContext *fSecContext; // after a successful Authenticate call
74  // points to related security context
75  TString fService; // name of service (matches remote port #)
76  EServiceType fServType; // remote service type
77  Int_t fSocket; // socket descriptor
78  TString fUrl; // needs this for special authentication options
79  TBits fBitsInfo; // bits array to mark TStreamerInfo classes already sent
80  TList *fUUIDs; // list of TProcessIDs already sent through the socket
81 
82  TVirtualMutex *fLastUsageMtx; // Protect last usage setting / reading
83  TTimeStamp fLastUsage; // Time stamp of last usage
84 
85  static ULong64_t fgBytesRecv; // total bytes received by all socket objects
86  static ULong64_t fgBytesSent; // total bytes sent by all socket objects
87 
88  TUDPSocket() : fAddress(), fBytesRecv(0), fBytesSent(0), fCompress(0),
89  fLocalAddress(), fRemoteProtocol(), fSecContext(0), fService(),
90  fServType(kSOCKD), fSocket(-1), fUrl(),
91  fBitsInfo(), fUUIDs(0), fLastUsageMtx(0), fLastUsage() { }
92 
93  void SetDescriptor(Int_t desc) { fSocket = desc; }
94  void SendStreamerInfos(const TMessage &mess);
96  void SendProcessIDs(const TMessage &mess);
98 
99 private:
100  TUDPSocket& operator=(const TUDPSocket &); // not implemented
101  Option_t *GetOption() const { return TObject::GetOption(); }
102 
103 public:
104  TUDPSocket(TInetAddress address, const char *service);
105  TUDPSocket(TInetAddress address, Int_t port);
106  TUDPSocket(const char *host, const char *service);
107  TUDPSocket(const char *host, Int_t port);
108  TUDPSocket(const char *sockpath);
109 
110  TUDPSocket(Int_t descriptor);
111  TUDPSocket(Int_t descriptor, const char *sockpath);
112  TUDPSocket(const TUDPSocket &s);
113 
114  virtual ~TUDPSocket() { Close(); }
115 
116  virtual void Close(Option_t *opt="");
117  virtual Int_t GetDescriptor() const { return fSocket; }
120  Int_t GetPort() const { return fAddress.GetPort(); }
121  const char *GetService() const { return fService; }
122  Int_t GetServType() const { return (Int_t)fServType; }
123  virtual Int_t GetLocalPort();
124  UInt_t GetBytesSent() const { return fBytesSent; }
125  UInt_t GetBytesRecv() const { return fBytesRecv; }
127  Int_t GetCompressionLevel() const;
129  Int_t GetErrorCode() const;
130  virtual Int_t GetOption(ESockOptions opt, Int_t &val);
132  TSecContext *GetSecContext() const { return fSecContext; }
133 
134  TTimeStamp GetLastUsage() { R__LOCKGUARD2(fLastUsageMtx); return fLastUsage; }
135  const char *GetUrl() const { return fUrl; }
136 
137  virtual Bool_t IsValid() const { return fSocket < 0 ? kFALSE : kTRUE; }
138  virtual Int_t Recv(TMessage *&mess);
139  virtual Int_t Recv(Int_t &status, Int_t &kind);
140  virtual Int_t Recv(char *mess, Int_t max);
141  virtual Int_t Recv(char *mess, Int_t max, Int_t &kind);
142  virtual Int_t RecvRaw(void *buffer, Int_t length, ESendRecvOptions opt = kDefault);
143  virtual Int_t Reconnect() { return -1; }
144  virtual Int_t Select(Int_t interest = kRead, Long_t timeout = -1);
145  virtual Int_t Send(const TMessage &mess);
146  virtual Int_t Send(Int_t kind);
147  virtual Int_t Send(Int_t status, Int_t kind);
148  virtual Int_t Send(const char *mess, Int_t kind = kMESS_STRING);
149  virtual Int_t SendObject(const TObject *obj, Int_t kind = kMESS_OBJECT);
150  virtual Int_t SendRaw(const void *buffer, Int_t length,
151  ESendRecvOptions opt = kDefault);
152  void SetCompressionAlgorithm(Int_t algorithm=0);
153  void SetCompressionLevel(Int_t level=1);
154  void SetCompressionSettings(Int_t settings=1);
155  virtual Int_t SetOption(ESockOptions opt, Int_t val);
156  void SetRemoteProtocol(Int_t rproto) { fRemoteProtocol = rproto; }
157  void SetSecContext(TSecContext *ctx) { fSecContext = ctx; }
158  void SetService(const char *service) { fService = service; }
159  void SetServType(Int_t st) { fServType = (EServiceType)st; }
160  void SetUrl(const char *url) { fUrl = url; }
161 
162  void Touch() { R__LOCKGUARD2(fLastUsageMtx); fLastUsage.Set(); }
163 
164  static ULong64_t GetSocketBytesSent();
165  static ULong64_t GetSocketBytesRecv();
166 
167  static void NetError(const char *where, Int_t error);
168 
169  ClassDef(TUDPSocket,0) //This class implements UDP client sockets
170 };
171 
172 //______________________________________________________________________________
174 {
175  return (fCompress < 0) ? -1 : fCompress / 100;
176 }
177 
178 //______________________________________________________________________________
180 {
181  return (fCompress < 0) ? -1 : fCompress % 100;
182 }
183 
184 //______________________________________________________________________________
186 {
187  return (fCompress < 0) ? -1 : fCompress;
188 }
189 
190 #endif
void SetCompressionSettings(Int_t settings=1)
Used to specify the compression level and algorithm: settings = 100 * algorithm + level...
virtual Int_t SendRaw(const void *buffer, Int_t length, ESendRecvOptions opt=kDefault)
Send a raw buffer of specified length.
Definition: TUDPSocket.cxx:591
virtual void Close(Option_t *opt="")
Close the socket.
Definition: TUDPSocket.cxx:359
TSecContext * GetSecContext() const
Definition: TUDPSocket.h:132
Bool_t RecvStreamerInfos(TMessage *mess)
Receive a message containing streamer infos.
Definition: TUDPSocket.cxx:903
void Touch()
Definition: TUDPSocket.h:162
Int_t fCompress
Definition: TUDPSocket.h:70
virtual TInetAddress GetLocalInetAddress()
Return internet address of local host to which the socket is bound.
Definition: TUDPSocket.cxx:378
TList * fUUIDs
Definition: TUDPSocket.h:80
const char Option_t
Definition: RtypesCore.h:62
ESockOptions
Definition: TSocket.h:52
void SendProcessIDs(const TMessage &mess)
Check if TProcessIDs must be sent.
Definition: TUDPSocket.cxx:656
Int_t GetPort() const
Definition: TInetAddress.h:77
This class represents an Internet Protocol (IP) address.
Definition: TInetAddress.h:40
This class implements a mutex interface.
Definition: TVirtualMutex.h:34
Bool_t RecvProcessIDs(TMessage *mess)
Receive a message containing process ids.
Definition: TUDPSocket.cxx:950
Basic string class.
Definition: TString.h:137
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
const Bool_t kFALSE
Definition: Rtypes.h:92
const char * GetService() const
Definition: TUDPSocket.h:121
TString fService
Definition: TUDPSocket.h:75
void SetDescriptor(Int_t desc)
Definition: TUDPSocket.h:93
ESendRecvOptions
Definition: TSocket.h:65
Int_t fSocket
Definition: TUDPSocket.h:77
Option_t * GetOption() const
Definition: TUDPSocket.h:101
virtual Int_t SetOption(ESockOptions opt, Int_t val)
Set socket options.
Definition: TUDPSocket.cxx:988
void SetCompressionLevel(Int_t level=1)
See comments for function SetCompressionSettings.
static ULong64_t fgBytesRecv
Definition: TUDPSocket.h:85
#define ClassDef(name, id)
Definition: Rtypes.h:254
The TNamed class is the base class for all named ROOT classes.
Definition: TNamed.h:33
virtual Int_t Send(const TMessage &mess)
Send a TMessage object.
Definition: TUDPSocket.cxx:491
void Set()
Set Date/Time to current time as reported by the system.
Definition: TTimeStamp.cxx:556
virtual Option_t * GetOption() const
Definition: TObject.h:115
Int_t GetPort() const
Definition: TUDPSocket.h:120
virtual Int_t SendObject(const TObject *obj, Int_t kind=kMESS_OBJECT)
Send an object.
Definition: TUDPSocket.cxx:571
void SetServType(Int_t st)
Definition: TUDPSocket.h:159
Int_t GetCompressionAlgorithm() const
Definition: TUDPSocket.h:173
TString fUrl
Definition: TUDPSocket.h:78
virtual Bool_t IsValid() const
Definition: TUDPSocket.h:137
A doubly linked list.
Definition: TList.h:47
void SetUrl(const char *url)
Definition: TUDPSocket.h:160
Int_t GetServType() const
Definition: TUDPSocket.h:122
TInetAddress fAddress
Definition: TUDPSocket.h:67
void SetRemoteProtocol(Int_t rproto)
Definition: TUDPSocket.h:156
UInt_t GetBytesSent() const
Definition: TUDPSocket.h:124
static ULong64_t fgBytesSent
Definition: TUDPSocket.h:86
void SetSecContext(TSecContext *ctx)
Definition: TUDPSocket.h:157
virtual Int_t GetLocalPort()
Return the local port # to which the socket is bound.
Definition: TUDPSocket.cxx:392
TTimeStamp GetLastUsage()
Definition: TUDPSocket.h:134
unsigned int UInt_t
Definition: RtypesCore.h:42
TSecContext * fSecContext
Definition: TUDPSocket.h:73
void SetService(const char *service)
Definition: TUDPSocket.h:158
UInt_t fBytesRecv
Definition: TUDPSocket.h:68
#define R__LOCKGUARD2(mutex)
TUDPSocket & operator=(const TUDPSocket &)
static ULong64_t GetSocketBytesRecv()
Get total number of bytes received via all sockets.
void SetCompressionAlgorithm(Int_t algorithm=0)
See comments for function SetCompressionSettings.
long Long_t
Definition: RtypesCore.h:50
virtual Int_t Recv(TMessage *&mess)
Receive a TMessage object.
Definition: TUDPSocket.cxx:789
UInt_t fBytesSent
Definition: TUDPSocket.h:69
TBits fBitsInfo
Definition: TUDPSocket.h:79
TInetAddress fLocalAddress
Definition: TUDPSocket.h:71
virtual Int_t Select(Int_t interest=kRead, Long_t timeout=-1)
Waits for this socket to change status.
Definition: TUDPSocket.cxx:412
EStatusBits
Definition: TObject.h:62
unsigned long long ULong64_t
Definition: RtypesCore.h:70
The TTimeStamp encapsulates seconds and ns since EPOCH.
Definition: TTimeStamp.h:76
#define BIT(n)
Definition: Rtypes.h:120
Int_t GetCompressionLevel() const
Definition: TUDPSocket.h:179
Int_t GetErrorCode() const
Returns error code.
Mother of all ROOT objects.
Definition: TObject.h:44
Container of bits.
Definition: TBits.h:33
static void NetError(const char *where, Int_t error)
Print error string depending on error code.
virtual ~TUDPSocket()
Definition: TUDPSocket.h:114
Int_t GetCompressionSettings() const
Definition: TUDPSocket.h:185
EServiceType fServType
Definition: TUDPSocket.h:76
Int_t fRemoteProtocol
Definition: TUDPSocket.h:72
virtual Int_t Reconnect()
Definition: TUDPSocket.h:143
TVirtualMutex * fLastUsageMtx
Definition: TUDPSocket.h:82
void SendStreamerInfos(const TMessage &mess)
Check if TStreamerInfo must be sent.
Definition: TUDPSocket.cxx:621
static ULong64_t GetSocketBytesSent()
Get total number of bytes sent via all sockets.
const Bool_t kTRUE
Definition: Rtypes.h:91
virtual Int_t RecvRaw(void *buffer, Int_t length, ESendRecvOptions opt=kDefault)
Receive a raw buffer of specified length bytes.
Definition: TUDPSocket.cxx:872
UInt_t GetBytesRecv() const
Definition: TUDPSocket.h:125
Int_t GetRemoteProtocol() const
Definition: TUDPSocket.h:131
TTimeStamp fLastUsage
Definition: TUDPSocket.h:83
TInetAddress GetInetAddress() const
Definition: TUDPSocket.h:118
virtual Int_t GetDescriptor() const
Definition: TUDPSocket.h:117
const char * GetUrl() const
Definition: TUDPSocket.h:135