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