Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
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 };
46  enum EServiceType { kSOCKD, kROOTD, kPROOFD };
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);
77  Bool_t RecvStreamerInfos(TMessage *mess);
78  void SendProcessIDs(const TMessage &mess);
79  Bool_t RecvProcessIDs(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; }
100  TInetAddress GetInetAddress() const { return fAddress; }
101  virtual TInetAddress GetLocalInetAddress();
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; }
108  Int_t GetCompressionAlgorithm() const;
109  Int_t GetCompressionLevel() const;
110  Int_t GetCompressionSettings() const;
111  Int_t GetErrorCode() const;
112  virtual Int_t GetOption(ESockOptions opt, Int_t &val);
113  Int_t GetRemoteProtocol() const { return fRemoteProtocol; }
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 = ROOT::RCompressionSetting::EAlgorithm::kUseGlobal);
135  void SetCompressionLevel(Int_t level = ROOT::RCompressionSetting::ELevel::kUseMin);
136  void SetCompressionSettings(Int_t settings = ROOT::RCompressionSetting::EDefaults::kUseCompiledDefault);
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 //______________________________________________________________________________
155 inline Int_t TUDPSocket::GetCompressionAlgorithm() const
156 {
157  return (fCompress < 0) ? -1 : fCompress / 100;
158 }
159 
160 //______________________________________________________________________________
161 inline Int_t TUDPSocket::GetCompressionLevel() const
162 {
163  return (fCompress < 0) ? -1 : fCompress % 100;
164 }
165 
166 //______________________________________________________________________________
167 inline Int_t TUDPSocket::GetCompressionSettings() const
168 {
169  return (fCompress < 0) ? -1 : fCompress;
170 }
171 
172 #endif