xrootd
XrdNetLink.hh
Go to the documentation of this file.
00001 #ifndef __NET_LINK_H__
00002 #define __NET_LINK_H__
00003 /******************************************************************************/
00004 /*                                                                            */
00005 /*                         X r d N e t L i n k . h h                          */
00006 /*                                                                            */
00007 /* (c) 2004 by the Board of Trustees of the Leland Stanford, Jr., University  */
00008 /*                            All Rights Reserved                             */
00009 /*   Produced by Andrew Hanushevsky for Stanford University under contract    */
00010 /*              DE-AC02-76-SFO0515 with the Department of Energy              */
00011 /******************************************************************************/
00012 
00013 #ifndef WIN32
00014 #include <netinet/in.h>
00015 #include <sys/socket.h>
00016 #include <sys/types.h>
00017 #include <sys/uio.h>
00018 #include <fcntl.h>
00019 #else
00020 #include <Winsock2.h>
00021 #endif
00022 
00023 #include "XrdNet/XrdNetBuffer.hh"
00024 #include "XrdOuc/XrdOucChain.hh"
00025 #include "XrdSys/XrdSysDNS.hh"
00026 #include "XrdSys/XrdSysPthread.hh"
00027 
00028 // Options for SetOpts and Alloc()
00029 //
00030 #define XRDNETLINK_NOBLOCK  0x0001
00031 #define XRDNETLINK_NOCLOSE  0x0002
00032 #define XRDNETLINK_NOSTREAM 0x0004
00033 
00034 // The XrdNetLink class defines the i/o operations on a network link.
00035 //
00036 class XrdNet;
00037 class XrdNetPeer;
00038 class XrdSysError;
00039 class XrdOucStream;
00040 class XrdOucTokenizer;
00041 
00042 class XrdNetLink
00043 {
00044 public:
00045 
00046 XrdOucQSItem<XrdNetLink> LinkLink;
00047 
00048 static XrdNetLink *Alloc(XrdSysError   *erp, XrdNet *Net, XrdNetPeer &Peer,
00049                          XrdNetBufferQ *bq,  int opts=0);
00050 
00051 // Closes() closes the link. Specify defer=1 to postpone deallocating
00052 //          attached objects until the this object is destroyed. You should
00053 //          use defered close for cross-thread unsynchronized closes.
00054 //
00055 int           Close(int defer=0);
00056 
00057 // FDnum() returns the associated file descriptor
00058 //
00059 int           FDnum() {return FD;}
00060 
00061 // The following implement text-oriented read routines. The correspond to
00062 // those implemented by the XrdOucStream object which is the one used.
00063 //
00064 char         *GetLine();
00065 
00066 char         *GetToken(char **rest);
00067 char         *GetToken(void);
00068 void          RetToken(void);
00069 
00070 // isConnected() returns true if this object is connected to an XrdOucStream
00071 //               object and false otherwise.
00072 //
00073 int           isConnected(void) {return (Stream != 0) && (FD >= 0);}
00074 
00075 // LastError() returns the error number associated with the last error
00076 //
00077 int           LastError();
00078 
00079 // Addr() returns the IPV4 address of the endpoint
00080 //
00081 unsigned int Addr() {return XrdSysDNS::IPAddr(&InetAddr);}
00082 
00083 // Moniker() returns the short form of the host name at the endpoint
00084 //
00085 const char  *Moniker() {return Sname;}
00086 
00087 // Name() returns the full host name of the endpoint
00088 //
00089 const char  *Name() {return Lname;}
00090 
00091 // Moniker() returns the short form of the host name at the endpoint
00092 //
00093 const char  *Nick() {return Sname;}
00094 
00095 // OK2Recv() returns true if data can be received within tmo, false otherwise.
00096 //
00097 int           OK2Recv(int mills);
00098 
00099 // Recycle() makes this object available for reuse.
00100 //
00101 void          Recycle();
00102 
00103 // Send() set of methods that accept a char buffer are text oriented send
00104 //        routines. They all add a new-line (\n) character to end the buffer
00105 //        if it does not exist already.
00106 //
00107 int           Send(const char *buff,     // -> Data to send
00108                          int   blen=0,   // Length. If 0, it's compued via strlen()
00109                          int   tmo=-1);  // Millisecond timeout (default is none)
00110 
00111 int           Send(const char *dest,     // -> Hostname to send UDP datagram
00112                    const char *buff,     // Remaining parms as above
00113                          int   blen=0,
00114                           int   tmo=-1);
00115 
00116 int           Send(const struct iovec iov[], // writev() style plist
00117                    int iovcnt,               // Number of elements om iov[]
00118                    int tmo = -1);            // Optional timeout
00119 
00120 int           Send(const char   *dest,       // Hostname to send UDP datagram
00121                    const struct iovec iov[], // Remaining parms as above
00122                    int   iovcnt,
00123                    int   tmo=-1);
00124 
00125 // Send() set of methods that accept a void buffer are byte oriented send
00126 //        routines. These do not inspect the data at all.
00127 //
00128 int           Send(const void *buff,     // -> Data to send
00129                          int   blen=0,   // Length. If 0, it's compued via strlen()
00130                          int   tmo=-1);  // Millisecond timeout (default is none)
00131 
00132 // Recv() receives up to blen bytes. It may receive less than that if
00133 //        additional bytes are not immediately available to receive.
00134 //        it returns the number of bytes read or -1 if an error occurs.
00135 //
00136 int           Recv(char *buff, int blen);
00137 
00138 // Set() sets the maximum number of XrdNetLink objects that may be kept
00139 //       for future re-use.
00140 //
00141 void          Set(int maxl);
00142 
00143 // SetOpts() is used to set socket options, as defined above.
00144 //
00145 void          SetOpts(int opts);
00146 
00147 // Instantiate this object with the pointer to an error object for message
00148 // routing. Additionally, a pointer to a UDP buffer may be provided. This
00149 // buffer must contain a text datagram suitable for tokenization.
00150 //
00151               XrdNetLink(XrdSysError *erp, XrdNetBufferQ *bq) : LinkLink(this)
00152                           {FD = -1; Lname = Sname = 0; recvbuff = sendbuff = 0;
00153                            BuffQ = bq; Stream = 0; Bucket = 0; eDest = erp;
00154                           }
00155              ~XrdNetLink() {Close();}
00156 
00157 private:
00158 
00159 int OK2Send(int timeout=0, const char *dest=0);
00160 int retErr(int ecode, const char *dest=0);
00161 
00162 XrdSysMutex         rdMutex;
00163 XrdSysMutex         wrMutex;
00164 XrdNetBufferQ      *BuffQ;
00165 int                 FD;
00166 int                 noclose;
00167 int                 isReset;
00168 struct sockaddr     InetAddr;
00169 char               *Lname;     // Long  hostname
00170 char               *Sname;     // Short hostname (may be the same as Lname)
00171 XrdNetBuffer       *recvbuff;  // udp receive buffer
00172 XrdNetBuffer       *sendbuff;  // udp send    buffer
00173 XrdOucStream       *Stream;    // tcp tokenizer
00174 XrdOucTokenizer    *Bucket;    // udp tokenizer
00175 XrdSysError        *eDest;
00176 
00177 static XrdSysMutex             LinkList;
00178 static XrdOucStack<XrdNetLink> LinkStack;
00179 static int                     size;
00180 static int                     maxlink;
00181 static int                     numlink;
00182 static int                     devNull;
00183 };
00184 #endif