xrootd
XrdNetLink.hh
Go to the documentation of this file.
1 #ifndef __NET_LINK_H__
2 #define __NET_LINK_H__
3 /******************************************************************************/
4 /* */
5 /* X r d N e t L i n k . h h */
6 /* */
7 /* (c) 2004 by the Board of Trustees of the Leland Stanford, Jr., University */
8 /* All Rights Reserved */
9 /* Produced by Andrew Hanushevsky for Stanford University under contract */
10 /* DE-AC02-76-SFO0515 with the Department of Energy */
11 /******************************************************************************/
12 
13 #ifndef WIN32
14 #include <netinet/in.h>
15 #include <sys/socket.h>
16 #include <sys/types.h>
17 #include <sys/uio.h>
18 #include <fcntl.h>
19 #else
20 #include <Winsock2.h>
21 #endif
22 
23 #include "XrdNet/XrdNetBuffer.hh"
24 #include "XrdOuc/XrdOucChain.hh"
25 #include "XrdSys/XrdSysDNS.hh"
26 #include "XrdSys/XrdSysPthread.hh"
27 
28 // Options for SetOpts and Alloc()
29 //
30 #define XRDNETLINK_NOBLOCK 0x0001
31 #define XRDNETLINK_NOCLOSE 0x0002
32 #define XRDNETLINK_NOSTREAM 0x0004
33 
34 // The XrdNetLink class defines the i/o operations on a network link.
35 //
36 class XrdNet;
37 class XrdNetPeer;
38 class XrdSysError;
39 class XrdOucStream;
40 class XrdOucTokenizer;
41 
43 {
44 public:
45 
47 
48 static XrdNetLink *Alloc(XrdSysError *erp, XrdNet *Net, XrdNetPeer &Peer,
49  XrdNetBufferQ *bq, int opts=0);
50 
51 // Closes() closes the link. Specify defer=1 to postpone deallocating
52 // attached objects until the this object is destroyed. You should
53 // use defered close for cross-thread unsynchronized closes.
54 //
55 int Close(int defer=0);
56 
57 // FDnum() returns the associated file descriptor
58 //
59 int FDnum() {return FD;}
60 
61 // The following implement text-oriented read routines. The correspond to
62 // those implemented by the XrdOucStream object which is the one used.
63 //
64 char *GetLine();
65 
66 char *GetToken(char **rest);
67 char *GetToken(void);
68 void RetToken(void);
69 
70 // isConnected() returns true if this object is connected to an XrdOucStream
71 // object and false otherwise.
72 //
73 int isConnected(void) {return (Stream != 0) && (FD >= 0);}
74 
75 // LastError() returns the error number associated with the last error
76 //
77 int LastError();
78 
79 // Addr() returns the IPV4 address of the endpoint
80 //
81 unsigned int Addr() {return XrdSysDNS::IPAddr(&InetAddr);}
82 
83 // Moniker() returns the short form of the host name at the endpoint
84 //
85 const char *Moniker() {return Sname;}
86 
87 // Name() returns the full host name of the endpoint
88 //
89 const char *Name() {return Lname;}
90 
91 // Moniker() returns the short form of the host name at the endpoint
92 //
93 const char *Nick() {return Sname;}
94 
95 // OK2Recv() returns true if data can be received within tmo, false otherwise.
96 //
97 int OK2Recv(int mills);
98 
99 // Recycle() makes this object available for reuse.
100 //
101 void Recycle();
102 
103 // Send() set of methods that accept a char buffer are text oriented send
104 // routines. They all add a new-line (\n) character to end the buffer
105 // if it does not exist already.
106 //
107 int Send(const char *buff, // -> Data to send
108  int blen=0, // Length. If 0, it's compued via strlen()
109  int tmo=-1); // Millisecond timeout (default is none)
110 
111 int Send(const char *dest, // -> Hostname to send UDP datagram
112  const char *buff, // Remaining parms as above
113  int blen=0,
114  int tmo=-1);
115 
116 int Send(const struct iovec iov[], // writev() style plist
117  int iovcnt, // Number of elements om iov[]
118  int tmo = -1); // Optional timeout
119 
120 int Send(const char *dest, // Hostname to send UDP datagram
121  const struct iovec iov[], // Remaining parms as above
122  int iovcnt,
123  int tmo=-1);
124 
125 // Send() set of methods that accept a void buffer are byte oriented send
126 // routines. These do not inspect the data at all.
127 //
128 int Send(const void *buff, // -> Data to send
129  int blen=0, // Length. If 0, it's compued via strlen()
130  int tmo=-1); // Millisecond timeout (default is none)
131 
132 // Recv() receives up to blen bytes. It may receive less than that if
133 // additional bytes are not immediately available to receive.
134 // it returns the number of bytes read or -1 if an error occurs.
135 //
136 int Recv(char *buff, int blen);
137 
138 // Set() sets the maximum number of XrdNetLink objects that may be kept
139 // for future re-use.
140 //
141 void Set(int maxl);
142 
143 // SetOpts() is used to set socket options, as defined above.
144 //
145 void SetOpts(int opts);
146 
147 // Instantiate this object with the pointer to an error object for message
148 // routing. Additionally, a pointer to a UDP buffer may be provided. This
149 // buffer must contain a text datagram suitable for tokenization.
150 //
152  {FD = -1; Lname = Sname = 0; recvbuff = sendbuff = 0;
153  BuffQ = bq; Stream = 0; Bucket = 0; eDest = erp;
154  }
156 
157 private:
158 
159 int OK2Send(int timeout=0, const char *dest=0);
160 int retErr(int ecode, const char *dest=0);
161 
165 int FD;
168 struct sockaddr InetAddr;
169 char *Lname; // Long hostname
170 char *Sname; // Short hostname (may be the same as Lname)
171 XrdNetBuffer *recvbuff; // udp receive buffer
172 XrdNetBuffer *sendbuff; // udp send buffer
173 XrdOucStream *Stream; // tcp tokenizer
174 XrdOucTokenizer *Bucket; // udp tokenizer
176 
179 static int size;
180 static int maxlink;
181 static int numlink;
182 static int devNull;
183 };
184 #endif