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