xrootd
XrdLink.hh
Go to the documentation of this file.
00001 #ifndef __XRD_LINK_H__
00002 #define __XRD_LINK_H__
00003 /******************************************************************************/
00004 /*                                                                            */
00005 /*                            X r d 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. See XrdInfo.cc for complete License Terms       */
00009 /*   Produced by Andrew Hanushevsky for Stanford University under contract    */
00010 /*              DE-AC03-76-SFO0515 with the Department of Energy              */
00011 /******************************************************************************/
00012 
00013 #include <sys/socket.h>
00014 #include <sys/types.h>
00015 #include <fcntl.h>
00016 #include <time.h>
00017 
00018 #include "XrdSys/XrdSysPthread.hh"
00019 
00020 #include "Xrd/XrdJob.hh"
00021 #include "Xrd/XrdLinkMatch.hh"
00022 #include "Xrd/XrdProtocol.hh"
00023   
00024 /******************************************************************************/
00025 /*                       X r d L i n k   O p t i o n s                        */
00026 /******************************************************************************/
00027   
00028 #define XRDLINK_RDLOCK  0x0001
00029 #define XRDLINK_NOCLOSE 0x0002
00030 
00031 /******************************************************************************/
00032 /*                      C l a s s   D e f i n i t i o n                       */
00033 /******************************************************************************/
00034   
00035 class XrdInet;
00036 class XrdNetBuffer;
00037 class XrdNetPeer;
00038 class XrdPoll;
00039 class XrdOucTrace;
00040 class XrdScheduler;
00041 class XrdSysError;
00042 
00043 class XrdLink : XrdJob
00044 {
00045 public:
00046 friend class XrdLinkScan;
00047 friend class XrdPoll;
00048 friend class XrdPollPoll;
00049 friend class XrdPollDev;
00050 friend class XrdPollE;
00051 
00052 static XrdLink *Alloc(XrdNetPeer &Peer, int opts=0);
00053 
00054 void          Bind();
00055 void          Bind(pthread_t tid);
00056 
00057 int           Client(char *buff, int blen);
00058 
00059 int           Close(int defer=0);
00060 
00061 void          DoIt();
00062 
00063 int           FDnum() {return FD;}
00064 
00065 static XrdLink *fd2link(int fd)
00066                 {if (fd < 0) fd = -fd; 
00067                  return (fd <= LTLast && LinkBat[fd] ? LinkTab[fd] : 0);
00068                 }
00069 
00070 static XrdLink *fd2link(int fd, unsigned int inst)
00071                 {if (fd < 0) fd = -fd; 
00072                  if (fd <= LTLast && LinkBat[fd] && LinkTab[fd]
00073                  && LinkTab[fd]->Instance == inst) return LinkTab[fd];
00074                  return (XrdLink *)0;
00075                 }
00076 
00077 static XrdLink *Find(int &curr, XrdLinkMatch *who=0);
00078 
00079        int    getIOStats(long long &inbytes, long long &outbytes,
00080                               int  &numstall,     int  &numtardy)
00081                         { inbytes = BytesIn + BytesInTot;
00082                          outbytes = BytesOut+BytesOutTot;
00083                          numstall = stallCnt + stallCntTot;
00084                          numtardy = tardyCnt + tardyCntTot;
00085                          return InUse;
00086                         }
00087 
00088 static int    getName(int &curr, char *bname, int blen, XrdLinkMatch *who=0);
00089 
00090 XrdProtocol  *getProtocol() {return Protocol;} // opmutex must be locked
00091 
00092 void          Hold(int lk) {(lk ? opMutex.Lock() : opMutex.UnLock());}
00093 
00094 char         *ID;      // This is referenced a lot
00095 
00096 static   void Init(XrdSysError *eP, XrdOucTrace *tP, XrdScheduler *sP)
00097                   {XrdLog = eP; XrdTrace = tP; XrdSched = sP;}
00098 
00099 static   void Init(XrdInet *iP) {XrdNetTCP = iP;}
00100 
00101 unsigned int  Inst() {return Instance;}
00102 
00103 int           isFlawed() {return Etext != 0;}
00104 
00105 int           isInstance(unsigned int inst)
00106                         {return FD >= 0 && Instance == inst;}
00107 
00108 const char   *Name(sockaddr *ipaddr=0)
00109                      {if (ipaddr) memcpy(ipaddr, &InetAddr, sizeof(sockaddr));
00110                       return (const char *)Lname;
00111                      }
00112 
00113 const char   *Host(sockaddr *ipaddr=0)
00114                      {if (ipaddr) memcpy(ipaddr, &InetAddr, sizeof(sockaddr));
00115                       return (const char *)HostName;
00116                      }
00117 
00118 int           Peek(char *buff, int blen, int timeout=-1);
00119 
00120 int           Recv(char *buff, int blen);
00121 int           Recv(char *buff, int blen, int timeout);
00122 
00123 int           RecvAll(char *buff, int blen, int timeout=-1);
00124 
00125 int           Send(const char *buff, int blen);
00126 int           Send(const struct iovec *iov, int iocnt, int bytes=0);
00127 
00128 struct sfVec {union {char *buffer;    // ->Data if fdnum < 0
00129                      off_t offset;    // File offset      of data
00130                     };
00131               int   sendsz;           // Length of data at offset
00132               int   fdnum;            // File descriptor for data
00133              };
00134 static const int sfMax = 8;
00135 
00136 static int    sfOK;                   // True if Send(sfVec) enabled
00137 
00138 int           Send(const struct sfVec *sdP, int sdn); // Iff sfOK > 0
00139 
00140 void          Serialize();                              // ASYNC Mode
00141 
00142 int           setEtext(const char *text);
00143 
00144 void          setID(const char *userid, int procid);
00145 
00146 static void   setKWT(int wkSec, int kwSec);
00147 
00148 XrdProtocol  *setProtocol(XrdProtocol *pp);
00149 
00150 void          setRef(int cnt);                          // ASYNC Mode
00151 
00152 static int    Setup(int maxfd, int idlewait);
00153 
00154 static int    Stats(char *buff, int blen, int do_sync=0);
00155 
00156        void   syncStats(int *ctime=0);
00157 
00158        int    Terminate(const XrdLink *owner, int fdnum, unsigned int inst);
00159 
00160 time_t        timeCon() {return conTime;}
00161 
00162 int           UseCnt() {return InUse;}
00163 
00164               XrdLink();
00165              ~XrdLink() {}  // Is never deleted!
00166 
00167 private:
00168 
00169 void   Reset();
00170 int    sendData(const char *Buff, int Blen);
00171 
00172 static XrdSysError  *XrdLog;
00173 static XrdOucTrace  *XrdTrace;
00174 static XrdScheduler *XrdSched;
00175 static XrdInet      *XrdNetTCP;
00176 
00177 static XrdSysMutex   LTMutex;    // For the LinkTab only LTMutex->IOMutex allowed
00178 static XrdLink     **LinkTab;
00179 static char         *LinkBat;
00180 static unsigned int  LinkAlloc;
00181 static int           LTLast;
00182 static const char   *TraceID;
00183 static int           devNull;
00184 static short         killWait;
00185 static short         waitKill;
00186 
00187 // Statistical area (global and local)
00188 //
00189 static long long    LinkBytesIn;
00190 static long long    LinkBytesOut;
00191 static long long    LinkConTime;
00192 static long long    LinkCountTot;
00193 static int          LinkCount;
00194 static int          LinkCountMax;
00195 static int          LinkTimeOuts;
00196 static int          LinkStalls;
00197 static int          LinkSfIntr;
00198        long long        BytesIn;
00199        long long        BytesInTot;
00200        long long        BytesOut;
00201        long long        BytesOutTot;
00202        int              stallCnt;
00203        int              stallCntTot;
00204        int              tardyCnt;
00205        int              tardyCntTot;
00206        int              SfIntr;
00207 static XrdSysMutex  statsMutex;
00208 
00209 // Identification section
00210 //
00211 struct sockaddr     InetAddr;
00212 char                Uname[24];  // Uname and Lname must be adjacent!
00213 char                Lname[232];
00214 char               *HostName;
00215 int                 HNlen;
00216 pthread_t           TID;
00217 
00218 XrdSysMutex         opMutex;
00219 XrdSysMutex         rdMutex;
00220 XrdSysMutex         wrMutex;
00221 XrdSysSemaphore     IOSemaphore;
00222 XrdSysCondVar      *KillcvP;        // Protected by opMutex!
00223 XrdLink            *Next;
00224 XrdNetBuffer       *udpbuff;
00225 XrdProtocol        *Protocol;
00226 XrdProtocol        *ProtoAlt;
00227 XrdPoll            *Poller;
00228 struct pollfd      *PollEnt;
00229 char               *Etext;
00230 int                 FD;
00231 unsigned int        Instance;
00232 time_t              conTime;
00233 int                 InUse;
00234 int                 doPost;
00235 char                LockReads;
00236 char                KeepFD;
00237 char                isEnabled;
00238 char                isIdle;
00239 char                inQ;
00240 char                tBound;
00241 char                KillCnt;        // Protected by opMutex!
00242 static const char   KillMax =   60;
00243 static const char   KillMsk = 0x7f;
00244 static const char   KillXwt = 0x80;
00245 };
00246 #endif