// @(#)root/net:$Id$
// Author: Marcelo Sousa   26/10/2011

/*************************************************************************
 * Copyright (C) 1995-2011, Rene Brun and Fons Rademakers.               *
 * All rights reserved.                                                  *
 *                                                                       *
 * For the licensing terms see $ROOTSYS/LICENSE.                         *
 * For the list of contributors see $ROOTSYS/README/CREDITS.             *
 *************************************************************************/

#ifndef ROOT_TUDPSocket
#define ROOT_TUDPSocket


//////////////////////////////////////////////////////////////////////////
//                                                                      //
// TUDPSocket                                                           //
//                                                                      //
// This class implements udp client sockets. A socket is an endpoint    //
// for communication between two machines.                              //
// The actual work is done via the TSystem class (either TUnixSystem,   //
// or TWinNTSystem).                                                    //
//                                                                      //
//////////////////////////////////////////////////////////////////////////

#ifndef ROOT_TNamed
#include "TNamed.h"
#endif
#ifndef ROOT_TBits
#include "TBits.h"
#endif
#ifndef ROOT_TInetAddress
#include "TInetAddress.h"
#endif
#ifndef ROOT_MessageTypes
#include "MessageTypes.h"
#endif
#ifndef ROOT_TVirtualAuth
#include "TVirtualAuth.h"
#endif
#ifndef ROOT_TSecContext
#include "TSecContext.h"
#endif
#ifndef ROOT_TTimeStamp
#include "TTimeStamp.h"
#endif
#ifndef ROOT_TVirtualMutex
#include "TVirtualMutex.h"
#endif
#ifndef ROOT_TSocket
#include "TSocket.h"
#endif

class TUDPSocket : public TNamed {

friend class TServerSocket;

public:
   enum EStatusBits { kIsUnix = BIT(16),    // set if unix socket
                      kBrokenConn = BIT(17) // set if conn reset by peer or broken
                    };
   enum EInterest { kRead = 1, kWrite = 2 };
   enum EServiceType { kSOCKD, kROOTD, kPROOFD };

protected:
   TInetAddress  fAddress;        // remote internet address and port #
   UInt_t        fBytesRecv;      // total bytes received over this socket
   UInt_t        fBytesSent;      // total bytes sent using this socket
   Int_t         fCompress;       // Compression level and algorithm
   TInetAddress  fLocalAddress;   // local internet address and port #
   Int_t         fRemoteProtocol; // protocol of remote daemon
   TSecContext  *fSecContext;     // after a successful Authenticate call
                                  // points to related security context
   TString       fService;        // name of service (matches remote port #)
   EServiceType  fServType;       // remote service type
   Int_t         fSocket;         // socket descriptor
   TString       fUrl;            // needs this for special authentication options
   TBits         fBitsInfo;       // bits array to mark TStreamerInfo classes already sent
   TList        *fUUIDs;          // list of TProcessIDs already sent through the socket

   TVirtualMutex *fLastUsageMtx;   // Protect last usage setting / reading
   TTimeStamp    fLastUsage;      // Time stamp of last usage

   static ULong64_t fgBytesRecv;  // total bytes received by all socket objects
   static ULong64_t fgBytesSent;  // total bytes sent by all socket objects

   TUDPSocket() : fAddress(), fBytesRecv(0), fBytesSent(0), fCompress(0),
                  fLocalAddress(), fRemoteProtocol(), fSecContext(0), fService(),
                  fServType(kSOCKD), fSocket(-1), fUrl(),
                  fBitsInfo(), fUUIDs(0), fLastUsageMtx(0), fLastUsage() { }

   void         SetDescriptor(Int_t desc) { fSocket = desc; }
   void         SendStreamerInfos(const TMessage &mess);
   Bool_t       RecvStreamerInfos(TMessage *mess);
   void         SendProcessIDs(const TMessage &mess);
   Bool_t       RecvProcessIDs(TMessage *mess);

private:
   TUDPSocket&   operator=(const TUDPSocket &);  // not implemented
   Option_t     *GetOption() const { return TObject::GetOption(); }

public:
   TUDPSocket(TInetAddress address, const char *service);
   TUDPSocket(TInetAddress address, Int_t port);
   TUDPSocket(const char *host, const char *service);
   TUDPSocket(const char *host, Int_t port);
   TUDPSocket(const char *sockpath);

   TUDPSocket(Int_t descriptor);
   TUDPSocket(Int_t descriptor, const char *sockpath);
   TUDPSocket(const TUDPSocket &s);

   virtual ~TUDPSocket() { Close(); }

   virtual void          Close(Option_t *opt="");
   virtual Int_t         GetDescriptor() const { return fSocket; }
   TInetAddress          GetInetAddress() const { return fAddress; }
   virtual TInetAddress  GetLocalInetAddress();
   Int_t                 GetPort() const { return fAddress.GetPort(); }
   const char           *GetService() const { return fService; }
   Int_t                 GetServType() const { return (Int_t)fServType; }
   virtual Int_t         GetLocalPort();
   UInt_t                GetBytesSent() const { return fBytesSent; }
   UInt_t                GetBytesRecv() const { return fBytesRecv; }
   Int_t                 GetCompressionAlgorithm() const;
   Int_t                 GetCompressionLevel() const;
   Int_t                 GetCompressionSettings() const;
   Int_t                 GetErrorCode() const;
   virtual Int_t         GetOption(ESockOptions opt, Int_t &val);
   Int_t                 GetRemoteProtocol() const { return fRemoteProtocol; }
   TSecContext          *GetSecContext() const { return fSecContext; }

   TTimeStamp            GetLastUsage() { R__LOCKGUARD2(fLastUsageMtx); return fLastUsage; }
   const char           *GetUrl() const { return fUrl; }

   virtual Bool_t        IsValid() const { return fSocket < 0 ? kFALSE : kTRUE; }
   virtual Int_t         Recv(TMessage *&mess);
   virtual Int_t         Recv(Int_t &status, Int_t &kind);
   virtual Int_t         Recv(char *mess, Int_t max);
   virtual Int_t         Recv(char *mess, Int_t max, Int_t &kind);
   virtual Int_t         RecvRaw(void *buffer, Int_t length, ESendRecvOptions opt = kDefault);
   virtual Int_t         Reconnect() { return -1; }
   virtual Int_t         Select(Int_t interest = kRead, Long_t timeout = -1);
   virtual Int_t         Send(const TMessage &mess);
   virtual Int_t         Send(Int_t kind);
   virtual Int_t         Send(Int_t status, Int_t kind);
   virtual Int_t         Send(const char *mess, Int_t kind = kMESS_STRING);
   virtual Int_t         SendObject(const TObject *obj, Int_t kind = kMESS_OBJECT);
   virtual Int_t         SendRaw(const void *buffer, Int_t length,
                                 ESendRecvOptions opt = kDefault);
   void                  SetCompressionAlgorithm(Int_t algorithm=0);
   void                  SetCompressionLevel(Int_t level=1);
   void                  SetCompressionSettings(Int_t settings=1);
   virtual Int_t         SetOption(ESockOptions opt, Int_t val);
   void                  SetRemoteProtocol(Int_t rproto) { fRemoteProtocol = rproto; }
   void                  SetSecContext(TSecContext *ctx) { fSecContext = ctx; }
   void                  SetService(const char *service) { fService = service; }
   void                  SetServType(Int_t st) { fServType = (EServiceType)st; }
   void                  SetUrl(const char *url) { fUrl = url; }

   void                  Touch() { R__LOCKGUARD2(fLastUsageMtx); fLastUsage.Set(); }

   static ULong64_t      GetSocketBytesSent();
   static ULong64_t      GetSocketBytesRecv();

   static void           NetError(const char *where, Int_t error);

   ClassDef(TUDPSocket,0)  //This class implements UDP client sockets
};

//______________________________________________________________________________
inline Int_t TUDPSocket::GetCompressionAlgorithm() const
{
   return (fCompress < 0) ? -1 : fCompress / 100;
}

//______________________________________________________________________________
inline Int_t TUDPSocket::GetCompressionLevel() const
{
   return (fCompress < 0) ? -1 : fCompress % 100;
}

//______________________________________________________________________________
inline Int_t TUDPSocket::GetCompressionSettings() const
{
   return (fCompress < 0) ? -1 : fCompress;
}

#endif
 TUDPSocket.h:1
 TUDPSocket.h:2
 TUDPSocket.h:3
 TUDPSocket.h:4
 TUDPSocket.h:5
 TUDPSocket.h:6
 TUDPSocket.h:7
 TUDPSocket.h:8
 TUDPSocket.h:9
 TUDPSocket.h:10
 TUDPSocket.h:11
 TUDPSocket.h:12
 TUDPSocket.h:13
 TUDPSocket.h:14
 TUDPSocket.h:15
 TUDPSocket.h:16
 TUDPSocket.h:17
 TUDPSocket.h:18
 TUDPSocket.h:19
 TUDPSocket.h:20
 TUDPSocket.h:21
 TUDPSocket.h:22
 TUDPSocket.h:23
 TUDPSocket.h:24
 TUDPSocket.h:25
 TUDPSocket.h:26
 TUDPSocket.h:27
 TUDPSocket.h:28
 TUDPSocket.h:29
 TUDPSocket.h:30
 TUDPSocket.h:31
 TUDPSocket.h:32
 TUDPSocket.h:33
 TUDPSocket.h:34
 TUDPSocket.h:35
 TUDPSocket.h:36
 TUDPSocket.h:37
 TUDPSocket.h:38
 TUDPSocket.h:39
 TUDPSocket.h:40
 TUDPSocket.h:41
 TUDPSocket.h:42
 TUDPSocket.h:43
 TUDPSocket.h:44
 TUDPSocket.h:45
 TUDPSocket.h:46
 TUDPSocket.h:47
 TUDPSocket.h:48
 TUDPSocket.h:49
 TUDPSocket.h:50
 TUDPSocket.h:51
 TUDPSocket.h:52
 TUDPSocket.h:53
 TUDPSocket.h:54
 TUDPSocket.h:55
 TUDPSocket.h:56
 TUDPSocket.h:57
 TUDPSocket.h:58
 TUDPSocket.h:59
 TUDPSocket.h:60
 TUDPSocket.h:61
 TUDPSocket.h:62
 TUDPSocket.h:63
 TUDPSocket.h:64
 TUDPSocket.h:65
 TUDPSocket.h:66
 TUDPSocket.h:67
 TUDPSocket.h:68
 TUDPSocket.h:69
 TUDPSocket.h:70
 TUDPSocket.h:71
 TUDPSocket.h:72
 TUDPSocket.h:73
 TUDPSocket.h:74
 TUDPSocket.h:75
 TUDPSocket.h:76
 TUDPSocket.h:77
 TUDPSocket.h:78
 TUDPSocket.h:79
 TUDPSocket.h:80
 TUDPSocket.h:81
 TUDPSocket.h:82
 TUDPSocket.h:83
 TUDPSocket.h:84
 TUDPSocket.h:85
 TUDPSocket.h:86
 TUDPSocket.h:87
 TUDPSocket.h:88
 TUDPSocket.h:89
 TUDPSocket.h:90
 TUDPSocket.h:91
 TUDPSocket.h:92
 TUDPSocket.h:93
 TUDPSocket.h:94
 TUDPSocket.h:95
 TUDPSocket.h:96
 TUDPSocket.h:97
 TUDPSocket.h:98
 TUDPSocket.h:99
 TUDPSocket.h:100
 TUDPSocket.h:101
 TUDPSocket.h:102
 TUDPSocket.h:103
 TUDPSocket.h:104
 TUDPSocket.h:105
 TUDPSocket.h:106
 TUDPSocket.h:107
 TUDPSocket.h:108
 TUDPSocket.h:109
 TUDPSocket.h:110
 TUDPSocket.h:111
 TUDPSocket.h:112
 TUDPSocket.h:113
 TUDPSocket.h:114
 TUDPSocket.h:115
 TUDPSocket.h:116
 TUDPSocket.h:117
 TUDPSocket.h:118
 TUDPSocket.h:119
 TUDPSocket.h:120
 TUDPSocket.h:121
 TUDPSocket.h:122
 TUDPSocket.h:123
 TUDPSocket.h:124
 TUDPSocket.h:125
 TUDPSocket.h:126
 TUDPSocket.h:127
 TUDPSocket.h:128
 TUDPSocket.h:129
 TUDPSocket.h:130
 TUDPSocket.h:131
 TUDPSocket.h:132
 TUDPSocket.h:133
 TUDPSocket.h:134
 TUDPSocket.h:135
 TUDPSocket.h:136
 TUDPSocket.h:137
 TUDPSocket.h:138
 TUDPSocket.h:139
 TUDPSocket.h:140
 TUDPSocket.h:141
 TUDPSocket.h:142
 TUDPSocket.h:143
 TUDPSocket.h:144
 TUDPSocket.h:145
 TUDPSocket.h:146
 TUDPSocket.h:147
 TUDPSocket.h:148
 TUDPSocket.h:149
 TUDPSocket.h:150
 TUDPSocket.h:151
 TUDPSocket.h:152
 TUDPSocket.h:153
 TUDPSocket.h:154
 TUDPSocket.h:155
 TUDPSocket.h:156
 TUDPSocket.h:157
 TUDPSocket.h:158
 TUDPSocket.h:159
 TUDPSocket.h:160
 TUDPSocket.h:161
 TUDPSocket.h:162
 TUDPSocket.h:163
 TUDPSocket.h:164
 TUDPSocket.h:165
 TUDPSocket.h:166
 TUDPSocket.h:167
 TUDPSocket.h:168
 TUDPSocket.h:169
 TUDPSocket.h:170
 TUDPSocket.h:171
 TUDPSocket.h:172
 TUDPSocket.h:173
 TUDPSocket.h:174
 TUDPSocket.h:175
 TUDPSocket.h:176
 TUDPSocket.h:177
 TUDPSocket.h:178
 TUDPSocket.h:179
 TUDPSocket.h:180
 TUDPSocket.h:181
 TUDPSocket.h:182
 TUDPSocket.h:183
 TUDPSocket.h:184
 TUDPSocket.h:185
 TUDPSocket.h:186
 TUDPSocket.h:187
 TUDPSocket.h:188
 TUDPSocket.h:189
 TUDPSocket.h:190