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/types.h>
00033 #include <fcntl.h>
00034 #include <time.h>
00035
00036 #include "XrdNet/XrdNetAddr.hh"
00037 #include "XrdOuc/XrdOucSFVec.hh"
00038 #include "XrdSys/XrdSysPthread.hh"
00039
00040 #include "Xrd/XrdJob.hh"
00041 #include "Xrd/XrdLinkMatch.hh"
00042 #include "Xrd/XrdProtocol.hh"
00043
00044
00045
00046
00047
00048 #define XRDLINK_RDLOCK 0x0001
00049 #define XRDLINK_NOCLOSE 0x0002
00050
00051
00052
00053
00054
00055 class XrdInet;
00056 class XrdNetAddr;
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
00076
00077 inline
00078 XrdNetAddrInfo *AddrInfo() {return (XrdNetAddrInfo *)&Addr;}
00079
00080
00090
00091
00092 static XrdLink *Alloc(XrdNetAddr &peer, int opts=0);
00093
00094 void Bind() {}
00095 void Bind(pthread_t tid) { (void)tid; }
00096
00097 int Client(char *buff, int blen);
00098
00099 int Close(int defer=0);
00100
00101 void DoIt();
00102
00103 void Enable();
00104
00105 int FDnum() {return FD;}
00106
00107 static XrdLink *fd2link(int fd)
00108 {if (fd < 0) fd = -fd;
00109 return (fd <= LTLast && LinkBat[fd] ? LinkTab[fd] : 0);
00110 }
00111
00112 static XrdLink *fd2link(int fd, unsigned int inst)
00113 {if (fd < 0) fd = -fd;
00114 if (fd <= LTLast && LinkBat[fd] && LinkTab[fd]
00115 && LinkTab[fd]->Instance == inst) return LinkTab[fd];
00116 return (XrdLink *)0;
00117 }
00118
00119 static XrdLink *Find(int &curr, XrdLinkMatch *who=0);
00120
00121 int getIOStats(long long &inbytes, long long &outbytes,
00122 int &numstall, int &numtardy)
00123 { inbytes = BytesIn + BytesInTot;
00124 outbytes = BytesOut+BytesOutTot;
00125 numstall = stallCnt + stallCntTot;
00126 numtardy = tardyCnt + tardyCntTot;
00127 return InUse;
00128 }
00129
00130 static int getName(int &curr, char *bname, int blen, XrdLinkMatch *who=0);
00131
00132 XrdProtocol *getProtocol() {return Protocol;}
00133
00134 void Hold(int lk) {(lk ? opMutex.Lock() : opMutex.UnLock());}
00135
00136
00141
00142
00143 const char *Host() {return (const char *)HostName;}
00144
00145 char *ID;
00146
00147 static void Init(XrdSysError *eP, XrdOucTrace *tP, XrdScheduler *sP)
00148 {XrdLog = eP; XrdTrace = tP; XrdSched = sP;}
00149
00150 static void Init(XrdInet *iP) {XrdNetTCP = iP;}
00151
00152
00156
00157 inline
00158 unsigned int Inst() {return Instance;}
00159
00160
00165
00166 inline
00167 bool isFlawed() {return Etext != 0;}
00168
00169
00177
00178 inline
00179 bool isInstance(unsigned int inst)
00180 {return FD >= 0 && Instance == inst;}
00181
00182
00187
00188 inline
00189 const char *Name() {return (const char *)Lname;}
00190
00191
00196
00197 inline const
00198 XrdNetAddr *NetAddr() {return &Addr;}
00199
00200 int Peek(char *buff, int blen, int timeout=-1);
00201
00202 int Recv(char *buff, int blen);
00203 int Recv(char *buff, int blen, int timeout);
00204
00205 int RecvAll(char *buff, int blen, int timeout=-1);
00206
00207 int Send(const char *buff, int blen);
00208 int Send(const struct iovec *iov, int iocnt, int bytes=0);
00209
00210 static int sfOK;
00211
00212 typedef XrdOucSFVec sfVec;
00213
00214 int Send(const sfVec *sdP, int sdn);
00215
00216 void Serialize();
00217
00218 int setEtext(const char *text);
00219
00220 void setID(const char *userid, int procid);
00221
00222 static void setKWT(int wkSec, int kwSec);
00223
00224 void setLocation(XrdNetAddrInfo::LocInfo &loc) {Addr.SetLocation(loc);}
00225
00226 XrdProtocol *setProtocol(XrdProtocol *pp);
00227
00228 void setRef(int cnt);
00229
00230 static int Setup(int maxfd, int idlewait);
00231
00232 static int Stats(char *buff, int blen, int do_sync=0);
00233
00234 void syncStats(int *ctime=0);
00235
00236 int Terminate(const XrdLink *owner, int fdnum, unsigned int inst);
00237
00238 time_t timeCon() {return conTime;}
00239
00240 int UseCnt() {return InUse;}
00241
00242 void armBridge() {isBridged = 1;}
00243 int hasBridge() {return isBridged;}
00244
00245 XrdLink();
00246 ~XrdLink() {}
00247
00248 private:
00249
00250 void Reset();
00251 int sendData(const char *Buff, int Blen);
00252
00253 static XrdSysError *XrdLog;
00254 static XrdOucTrace *XrdTrace;
00255 static XrdScheduler *XrdSched;
00256 static XrdInet *XrdNetTCP;
00257
00258 static XrdSysMutex LTMutex;
00259 static XrdLink **LinkTab;
00260 static char *LinkBat;
00261 static unsigned int LinkAlloc;
00262 static int LTLast;
00263 static const char *TraceID;
00264 static int devNull;
00265 static short killWait;
00266 static short waitKill;
00267
00268
00269
00270 static long long LinkBytesIn;
00271 static long long LinkBytesOut;
00272 static long long LinkConTime;
00273 static long long LinkCountTot;
00274 static int LinkCount;
00275 static int LinkCountMax;
00276 static int LinkTimeOuts;
00277 static int LinkStalls;
00278 static int LinkSfIntr;
00279 long long BytesIn;
00280 long long BytesInTot;
00281 long long BytesOut;
00282 long long BytesOutTot;
00283 int stallCnt;
00284 int stallCntTot;
00285 int tardyCnt;
00286 int tardyCntTot;
00287 int SfIntr;
00288 static XrdSysMutex statsMutex;
00289
00290
00291
00292 XrdNetAddr Addr;
00293 char Uname[24];
00294 char Lname[232];
00295 char *HostName;
00296 int HNlen;
00297 pthread_t TID;
00298
00299 XrdSysMutex opMutex;
00300 XrdSysMutex rdMutex;
00301 XrdSysMutex wrMutex;
00302 XrdSysSemaphore IOSemaphore;
00303 XrdSysCondVar *KillcvP;
00304 XrdLink *Next;
00305 XrdProtocol *Protocol;
00306 XrdProtocol *ProtoAlt;
00307 XrdPoll *Poller;
00308 struct pollfd *PollEnt;
00309 char *Etext;
00310 int FD;
00311 unsigned int Instance;
00312 time_t conTime;
00313 int InUse;
00314 int doPost;
00315 char LockReads;
00316 char KeepFD;
00317 char isEnabled;
00318 char isIdle;
00319 char inQ;
00320 char isBridged;
00321 char KillCnt;
00322 static const char KillMax = 60;
00323 static const char KillMsk = 0x7f;
00324 static const char KillXwt = 0x80;
00325 };
00326 #endif