Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
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 "TSystem.h"
28 #include "Compression.h"
29 #include "TNamed.h"
30 #include "TBits.h"
31 #include "TInetAddress.h"
32 #include "MessageTypes.h"
33 #include "TVirtualAuth.h"
34 #include "TSecContext.h"
35 #include "TTimeStamp.h"
36 #include "TVirtualMutex.h"
37 
38 class TMessage;
39 class THostAuth;
40 
41 class TSocket : public TNamed {
42 
43 friend class TServerSocket;
44 friend class TProofServ; // to be able to call SetDescriptor(), RecvHostAuth()
45 friend class TSlave; // to be able to call SendHostAuth()
46 
47 public:
48  enum EStatusBits { kIsUnix = BIT(16), // set if unix socket
49  kBrokenConn = BIT(17) // set if conn reset by peer or broken
50  };
51  enum EInterest { kRead = 1, kWrite = 2 };
52  enum EServiceType { kSOCKD, kROOTD, kPROOFD };
53 
54 protected:
55  enum ESocketErrors {
56  kInvalid = -1,
57  kInvalidStillInList = -2
58  };
59  TInetAddress fAddress; // remote internet address and port #
60  UInt_t fBytesRecv; // total bytes received over this socket
61  UInt_t fBytesSent; // total bytes sent using this socket
62  Int_t fCompress; // Compression level and algorithm
63  TInetAddress fLocalAddress; // local internet address and port #
64  Int_t fRemoteProtocol; // protocol of remote daemon
65  TSecContext *fSecContext; // after a successful Authenticate call
66  // points to related security context
67  TString fService; // name of service (matches remote port #)
68  EServiceType fServType; // remote service type
69  Int_t fSocket; // socket descriptor
70  Int_t fTcpWindowSize; // TCP window size (default 65535);
71  TString fUrl; // needs this for special authentication options
72  TBits fBitsInfo; // bits array to mark TStreamerInfo classes already sent
73  TList *fUUIDs; // list of TProcessIDs already sent through the socket
74 
75  TVirtualMutex *fLastUsageMtx; // Protect last usage setting / reading
76  TTimeStamp fLastUsage; // Time stamp of last usage
77 
78  static ULong64_t fgBytesRecv; // total bytes received by all socket objects
79  static ULong64_t fgBytesSent; // total bytes sent by all socket objects
80 
81  static Int_t fgClientProtocol; // client "protocol" version
82 
83  TSocket() : fAddress(), fBytesRecv(0), fBytesSent(0), fCompress(ROOT::RCompressionSetting::EAlgorithm::kUseGlobal),
84  fLocalAddress(), fRemoteProtocol(), fSecContext(0), fService(),
85  fServType(kSOCKD), fSocket(-1), fTcpWindowSize(0), fUrl(),
86  fBitsInfo(), fUUIDs(0), fLastUsageMtx(0), fLastUsage() { }
87 
88  Bool_t Authenticate(const char *user);
89  void SetDescriptor(Int_t desc) { fSocket = desc; }
90  void SendStreamerInfos(const TMessage &mess);
91  Bool_t RecvStreamerInfos(TMessage *mess);
92  void SendProcessIDs(const TMessage &mess);
93  Bool_t RecvProcessIDs(TMessage *mess);
94  void MarkBrokenConnection();
95 
96 private:
97  TSocket& operator=(const TSocket &); // not implemented
98  Option_t *GetOption() const { return TObject::GetOption(); }
99 
100 public:
101  TSocket(TInetAddress address, const char *service, Int_t tcpwindowsize = -1);
102  TSocket(TInetAddress address, Int_t port, Int_t tcpwindowsize = -1);
103  TSocket(const char *host, const char *service, Int_t tcpwindowsize = -1);
104  TSocket(const char *host, Int_t port, Int_t tcpwindowsize = -1);
105  TSocket(const char *sockpath);
106  TSocket(Int_t descriptor);
107  TSocket(Int_t descriptor, const char *sockpath);
108  TSocket(const TSocket &s);
109  virtual ~TSocket() { Close(); }
110 
111  virtual void Close(Option_t *opt="");
112  virtual Int_t GetDescriptor() const { return fSocket; }
113  TInetAddress GetInetAddress() const { return fAddress; }
114  virtual TInetAddress GetLocalInetAddress();
115  Int_t GetPort() const { return fAddress.GetPort(); }
116  const char *GetService() const { return fService; }
117  Int_t GetServType() const { return (Int_t)fServType; }
118  virtual Int_t GetLocalPort();
119  UInt_t GetBytesSent() const { return fBytesSent; }
120  UInt_t GetBytesRecv() const { return fBytesRecv; }
121  Int_t GetCompressionAlgorithm() const;
122  Int_t GetCompressionLevel() const;
123  Int_t GetCompressionSettings() const;
124  Int_t GetErrorCode() const;
125  virtual Int_t GetOption(ESockOptions opt, Int_t &val);
126  Int_t GetRemoteProtocol() const { return fRemoteProtocol; }
127  TSecContext *GetSecContext() const { return fSecContext; }
128  Int_t GetTcpWindowSize() const { return fTcpWindowSize; }
129  TTimeStamp GetLastUsage() { R__LOCKGUARD2(fLastUsageMtx); return fLastUsage; }
130  const char *GetUrl() const { return fUrl; }
131  virtual Bool_t IsAuthenticated() const { return fSecContext ? kTRUE : kFALSE; }
132  virtual Bool_t IsValid() const { return fSocket < 0 ? kFALSE : kTRUE; }
133  virtual Int_t Recv(TMessage *&mess);
134  virtual Int_t Recv(Int_t &status, Int_t &kind);
135  virtual Int_t Recv(char *mess, Int_t max);
136  virtual Int_t Recv(char *mess, Int_t max, Int_t &kind);
137  virtual Int_t RecvRaw(void *buffer, Int_t length, ESendRecvOptions opt = kDefault);
138  virtual Int_t Reconnect() { return -1; }
139  virtual Int_t Select(Int_t interest = kRead, Long_t timeout = -1);
140  virtual Int_t Send(const TMessage &mess);
141  virtual Int_t Send(Int_t kind);
142  virtual Int_t Send(Int_t status, Int_t kind);
143  virtual Int_t Send(const char *mess, Int_t kind = kMESS_STRING);
144  virtual Int_t SendObject(const TObject *obj, Int_t kind = kMESS_OBJECT);
145  virtual Int_t SendRaw(const void *buffer, Int_t length,
146  ESendRecvOptions opt = kDefault);
147  void SetCompressionAlgorithm(Int_t algorithm = ROOT::RCompressionSetting::EAlgorithm::kUseGlobal);
148  void SetCompressionLevel(Int_t level = ROOT::RCompressionSetting::ELevel::kUseMin);
149  void SetCompressionSettings(Int_t settings = ROOT::RCompressionSetting::EDefaults::kUseCompiledDefault);
150  virtual Int_t SetOption(ESockOptions opt, Int_t val);
151  void SetRemoteProtocol(Int_t rproto) { fRemoteProtocol = rproto; }
152  void SetSecContext(TSecContext *ctx) { fSecContext = ctx; }
153  void SetService(const char *service) { fService = service; }
154  void SetServType(Int_t st) { fServType = (EServiceType)st; }
155  void SetUrl(const char *url) { fUrl = url; }
156 
157  void Touch() { R__LOCKGUARD2(fLastUsageMtx); fLastUsage.Set(); }
158 
159  static Int_t GetClientProtocol();
160 
161  static ULong64_t GetSocketBytesSent();
162  static ULong64_t GetSocketBytesRecv();
163 
164  static TSocket *CreateAuthSocket(const char *user, const char *host,
165  Int_t port, Int_t size = 0,
166  Int_t tcpwindowsize = -1, TSocket *s = 0, Int_t *err = 0);
167  static TSocket *CreateAuthSocket(const char *url, Int_t size = 0,
168  Int_t tcpwindowsize = -1, TSocket *s = 0, Int_t *err = 0);
169  static void NetError(const char *where, Int_t error);
170 
171  ClassDef(TSocket,0) //This class implements client sockets
172 };
173 
174 //______________________________________________________________________________
175 inline Int_t TSocket::GetCompressionAlgorithm() const
176 {
177  return (fCompress < 0) ? -1 : fCompress / 100;
178 }
179 
180 //______________________________________________________________________________
181 inline Int_t TSocket::GetCompressionLevel() const
182 {
183  return (fCompress < 0) ? -1 : fCompress % 100;
184 }
185 
186 //______________________________________________________________________________
187 inline Int_t TSocket::GetCompressionSettings() const
188 {
189  return (fCompress < 0) ? -1 : fCompress;
190 }
191 
192 #endif