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