Logo ROOT   6.14/05
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 #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 #include "TSocket.h"
36 
37 class TUDPSocket : public TNamed {
38 
39 friend class TServerSocket;
40 
41 public:
42  enum EStatusBits { kIsUnix = BIT(16), // set if unix socket
43  kBrokenConn = BIT(17) // set if conn reset by peer or broken
44  };
45  enum EInterest { kRead = 1, kWrite = 2 };
47 
48 protected:
49  TInetAddress fAddress; // remote internet address and port #
50  UInt_t fBytesRecv; // total bytes received over this socket
51  UInt_t fBytesSent; // total bytes sent using this socket
52  Int_t fCompress; // Compression level and algorithm
53  TInetAddress fLocalAddress; // local internet address and port #
54  Int_t fRemoteProtocol; // protocol of remote daemon
55  TSecContext *fSecContext; // after a successful Authenticate call
56  // points to related security context
57  TString fService; // name of service (matches remote port #)
58  EServiceType fServType; // remote service type
59  Int_t fSocket; // socket descriptor
60  TString fUrl; // needs this for special authentication options
61  TBits fBitsInfo; // bits array to mark TStreamerInfo classes already sent
62  TList *fUUIDs; // list of TProcessIDs already sent through the socket
63 
64  TVirtualMutex *fLastUsageMtx; // Protect last usage setting / reading
65  TTimeStamp fLastUsage; // Time stamp of last usage
66 
67  static ULong64_t fgBytesRecv; // total bytes received by all socket objects
68  static ULong64_t fgBytesSent; // total bytes sent by all socket objects
69 
70  TUDPSocket() : fAddress(), fBytesRecv(0), fBytesSent(0), fCompress(0),
71  fLocalAddress(), fRemoteProtocol(), fSecContext(0), fService(),
72  fServType(kSOCKD), fSocket(-1), fUrl(),
73  fBitsInfo(), fUUIDs(0), fLastUsageMtx(0), fLastUsage() { }
74 
75  void SetDescriptor(Int_t desc) { fSocket = desc; }
76  void SendStreamerInfos(const TMessage &mess);
78  void SendProcessIDs(const TMessage &mess);
80 
81 private:
82  TUDPSocket& operator=(const TUDPSocket &); // not implemented
83  Option_t *GetOption() const { return TObject::GetOption(); }
84 
85 public:
86  TUDPSocket(TInetAddress address, const char *service);
87  TUDPSocket(TInetAddress address, Int_t port);
88  TUDPSocket(const char *host, const char *service);
89  TUDPSocket(const char *host, Int_t port);
90  TUDPSocket(const char *sockpath);
91 
92  TUDPSocket(Int_t descriptor);
93  TUDPSocket(Int_t descriptor, const char *sockpath);
94  TUDPSocket(const TUDPSocket &s);
95 
96  virtual ~TUDPSocket() { Close(); }
97 
98  virtual void Close(Option_t *opt="");
99  virtual Int_t GetDescriptor() const { return fSocket; }
102  Int_t GetPort() const { return fAddress.GetPort(); }
103  const char *GetService() const { return fService; }
104  Int_t GetServType() const { return (Int_t)fServType; }
105  virtual Int_t GetLocalPort();
106  UInt_t GetBytesSent() const { return fBytesSent; }
107  UInt_t GetBytesRecv() const { return fBytesRecv; }
109  Int_t GetCompressionLevel() const;
111  Int_t GetErrorCode() const;
112  virtual Int_t GetOption(ESockOptions opt, Int_t &val);
114  TSecContext *GetSecContext() const { return fSecContext; }
115 
116  TTimeStamp GetLastUsage() { R__LOCKGUARD2(fLastUsageMtx); return fLastUsage; }
117  const char *GetUrl() const { return fUrl; }
118 
119  virtual Bool_t IsValid() const { return fSocket < 0 ? kFALSE : kTRUE; }
120  virtual Int_t Recv(TMessage *&mess);
121  virtual Int_t Recv(Int_t &status, Int_t &kind);
122  virtual Int_t Recv(char *mess, Int_t max);
123  virtual Int_t Recv(char *mess, Int_t max, Int_t &kind);
124  virtual Int_t RecvRaw(void *buffer, Int_t length, ESendRecvOptions opt = kDefault);
125  virtual Int_t Reconnect() { return -1; }
126  virtual Int_t Select(Int_t interest = kRead, Long_t timeout = -1);
127  virtual Int_t Send(const TMessage &mess);
128  virtual Int_t Send(Int_t kind);
129  virtual Int_t Send(Int_t status, Int_t kind);
130  virtual Int_t Send(const char *mess, Int_t kind = kMESS_STRING);
131  virtual Int_t SendObject(const TObject *obj, Int_t kind = kMESS_OBJECT);
132  virtual Int_t SendRaw(const void *buffer, Int_t length,
133  ESendRecvOptions opt = kDefault);
134  void SetCompressionAlgorithm(Int_t algorithm=0);
135  void SetCompressionLevel(Int_t level=1);
136  void SetCompressionSettings(Int_t settings=1);
137  virtual Int_t SetOption(ESockOptions opt, Int_t val);
138  void SetRemoteProtocol(Int_t rproto) { fRemoteProtocol = rproto; }
139  void SetSecContext(TSecContext *ctx) { fSecContext = ctx; }
140  void SetService(const char *service) { fService = service; }
141  void SetServType(Int_t st) { fServType = (EServiceType)st; }
142  void SetUrl(const char *url) { fUrl = url; }
143 
144  void Touch() { R__LOCKGUARD2(fLastUsageMtx); fLastUsage.Set(); }
145 
146  static ULong64_t GetSocketBytesSent();
147  static ULong64_t GetSocketBytesRecv();
148 
149  static void NetError(const char *where, Int_t error);
150 
151  ClassDef(TUDPSocket,0) //This class implements UDP client sockets
152 };
153 
154 //______________________________________________________________________________
156 {
157  return (fCompress < 0) ? -1 : fCompress / 100;
158 }
159 
160 //______________________________________________________________________________
162 {
163  return (fCompress < 0) ? -1 : fCompress % 100;
164 }
165 
166 //______________________________________________________________________________
168 {
169  return (fCompress < 0) ? -1 : fCompress;
170 }
171 
172 #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
const char * GetService() const
Definition: TUDPSocket.h:103
Bool_t RecvStreamerInfos(TMessage *mess)
Receive a message containing streamer infos.
Definition: TUDPSocket.cxx:903
void Touch()
Definition: TUDPSocket.h:144
Int_t fCompress
Definition: TUDPSocket.h:52
virtual TInetAddress GetLocalInetAddress()
Return internet address of local host to which the socket is bound.
Definition: TUDPSocket.cxx:378
TList * fUUIDs
Definition: TUDPSocket.h:62
const char Option_t
Definition: RtypesCore.h:62
ESockOptions
Definition: TSocket.h:36
void SendProcessIDs(const TMessage &mess)
Check if TProcessIDs must be sent.
Definition: TUDPSocket.cxx:656
#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
Bool_t RecvProcessIDs(TMessage *mess)
Receive a message containing process ids.
Definition: TUDPSocket.cxx:950
Basic string class.
Definition: TString.h:131
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
Int_t GetCompressionLevel() const
Definition: TUDPSocket.h:161
TString fService
Definition: TUDPSocket.h:57
void SetDescriptor(Int_t desc)
Definition: TUDPSocket.h:75
Int_t GetServType() const
Definition: TUDPSocket.h:104
ESendRecvOptions
Definition: TSocket.h:49
Int_t fSocket
Definition: TUDPSocket.h:59
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:67
#define ClassDef(name, id)
Definition: Rtypes.h:320
The TNamed class is the base class for all named ROOT classes.
Definition: TNamed.h:29
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
Int_t GetPort() const
Definition: TInetAddress.h:73
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:141
TString fUrl
Definition: TUDPSocket.h:60
A doubly linked list.
Definition: TList.h:44
void SetUrl(const char *url)
Definition: TUDPSocket.h:142
Option_t * GetOption() const
Definition: TUDPSocket.h:83
TInetAddress fAddress
Definition: TUDPSocket.h:49
void SetRemoteProtocol(Int_t rproto)
Definition: TUDPSocket.h:138
Int_t GetErrorCode() const
Returns error code.
UInt_t GetBytesRecv() const
Definition: TUDPSocket.h:107
Int_t GetCompressionAlgorithm() const
Definition: TUDPSocket.h:155
static ULong64_t fgBytesSent
Definition: TUDPSocket.h:68
void SetSecContext(TSecContext *ctx)
Definition: TUDPSocket.h:139
virtual Int_t GetLocalPort()
Return the local port # to which the socket is bound.
Definition: TUDPSocket.cxx:392
TTimeStamp GetLastUsage()
Definition: TUDPSocket.h:116
unsigned int UInt_t
Definition: RtypesCore.h:42
TSecContext * fSecContext
Definition: TUDPSocket.h:55
void SetService(const char *service)
Definition: TUDPSocket.h:140
UInt_t fBytesRecv
Definition: TUDPSocket.h:50
#define R__LOCKGUARD2(mutex)
const Bool_t kFALSE
Definition: RtypesCore.h:88
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
Int_t GetRemoteProtocol() const
Definition: TUDPSocket.h:113
virtual Int_t Recv(TMessage *&mess)
Receive a TMessage object.
Definition: TUDPSocket.cxx:789
UInt_t fBytesSent
Definition: TUDPSocket.h:51
TBits fBitsInfo
Definition: TUDPSocket.h:61
TInetAddress fLocalAddress
Definition: TUDPSocket.h:53
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:57
unsigned long long ULong64_t
Definition: RtypesCore.h:70
The TTimeStamp encapsulates seconds and ns since EPOCH.
Definition: TTimeStamp.h:71
virtual Bool_t IsValid() const
Definition: TUDPSocket.h:119
virtual Option_t * GetOption() const
Definition: TObject.h:120
static constexpr double s
TInetAddress GetInetAddress() const
Definition: TUDPSocket.h:100
Mother of all ROOT objects.
Definition: TObject.h:37
Container of bits.
Definition: TBits.h:29
static void NetError(const char *where, Int_t error)
Print error string depending on error code.
Int_t GetCompressionSettings() const
Definition: TUDPSocket.h:167
virtual ~TUDPSocket()
Definition: TUDPSocket.h:96
EServiceType fServType
Definition: TUDPSocket.h:58
Int_t fRemoteProtocol
Definition: TUDPSocket.h:54
TSecContext * GetSecContext() const
Definition: TUDPSocket.h:114
Int_t GetPort() const
Definition: TUDPSocket.h:102
virtual Int_t Reconnect()
Definition: TUDPSocket.h:125
UInt_t GetBytesSent() const
Definition: TUDPSocket.h:106
TVirtualMutex * fLastUsageMtx
Definition: TUDPSocket.h:64
void SendStreamerInfos(const TMessage &mess)
Check if TStreamerInfo must be sent.
Definition: TUDPSocket.cxx:621
const char * GetUrl() const
Definition: TUDPSocket.h:117
static ULong64_t GetSocketBytesSent()
Get total number of bytes sent via all sockets.
virtual Int_t RecvRaw(void *buffer, Int_t length, ESendRecvOptions opt=kDefault)
Receive a raw buffer of specified length bytes.
Definition: TUDPSocket.cxx:872
const Bool_t kTRUE
Definition: RtypesCore.h:87
TTimeStamp fLastUsage
Definition: TUDPSocket.h:65
virtual Int_t GetDescriptor() const
Definition: TUDPSocket.h:99