xrootd
XrdClSocket.hh
Go to the documentation of this file.
1 //------------------------------------------------------------------------------
2 // Copyright (c) 2011-2012 by European Organization for Nuclear Research (CERN)
3 // Author: Lukasz Janyst <ljanyst@cern.ch>
4 //------------------------------------------------------------------------------
5 // XRootD is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU Lesser General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // XRootD is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public License
16 // along with XRootD. If not, see <http://www.gnu.org/licenses/>.
17 //------------------------------------------------------------------------------
18 
19 #ifndef __XRD_CL_SOCKET_HH__
20 #define __XRD_CL_SOCKET_HH__
21 
22 #include <stdint.h>
23 #include <string>
24 #include <sys/socket.h>
25 
26 #include "XrdCl/XrdClStatus.hh"
27 #include "XrdNet/XrdNetAddr.hh"
28 
29 namespace XrdCl
30 {
31  class AnyObject;
32 
33  //----------------------------------------------------------------------------
35  //----------------------------------------------------------------------------
36  class Socket
37  {
38  public:
39  //------------------------------------------------------------------------
41  //------------------------------------------------------------------------
43  {
45  Connected = 2,
47  };
48 
49  //------------------------------------------------------------------------
54  //------------------------------------------------------------------------
55  Socket( int socket = -1, SocketStatus status = Disconnected ):
56  pSocket(socket), pStatus( status ), pServerAddr( 0 ),
57  pProtocolFamily( AF_INET ),
58  pChannelID( 0 )
59  {
60  };
61 
62  //------------------------------------------------------------------------
64  //------------------------------------------------------------------------
65  virtual ~Socket()
66  {
67  Close();
68  };
69 
70  //------------------------------------------------------------------------
72  //------------------------------------------------------------------------
73  Status Initialize( int family = AF_INET );
74 
75  //------------------------------------------------------------------------
77  //------------------------------------------------------------------------
78  Status SetFlags( int flags );
79 
80  //------------------------------------------------------------------------
82  //------------------------------------------------------------------------
83  Status GetFlags( int &flags );
84 
85  //------------------------------------------------------------------------
87  //------------------------------------------------------------------------
88  Status GetSockOpt( int level, int optname, void *optval,
89  socklen_t *optlen );
90 
91  //------------------------------------------------------------------------
93  //------------------------------------------------------------------------
94  Status SetSockOpt( int level, int optname, const void *optval,
95  socklen_t optlen );
96 
97  //------------------------------------------------------------------------
104  //------------------------------------------------------------------------
105  Status Connect( const std::string &host,
106  uint16_t port,
107  uint16_t timout = 10 );
108 
109  //------------------------------------------------------------------------
115  //------------------------------------------------------------------------
116  Status ConnectToAddress( const XrdNetAddr &addr,
117  uint16_t timout = 10 );
118 
119  //------------------------------------------------------------------------
121  //------------------------------------------------------------------------
122  void Close();
123 
124  //------------------------------------------------------------------------
126  //------------------------------------------------------------------------
128  {
129  return pStatus;
130  }
131 
132  //------------------------------------------------------------------------
134  //------------------------------------------------------------------------
135  void SetStatus( SocketStatus status )
136  {
137  pStatus = status;
138  }
139 
140  //------------------------------------------------------------------------
147  //------------------------------------------------------------------------
148  Status ReadRaw( void *buffer, uint32_t size, int32_t timeout,
149  uint32_t &bytesRead );
150 
151  //------------------------------------------------------------------------
158  //------------------------------------------------------------------------
159  Status WriteRaw( void *buffer, uint32_t size, int32_t timeout,
160  uint32_t &bytesWritten );
161 
162  //------------------------------------------------------------------------
168  //------------------------------------------------------------------------
169  ssize_t Send( void *buffer, uint32_t size );
170 
171  //------------------------------------------------------------------------
177  //------------------------------------------------------------------------
178  ssize_t WriteV( iovec *iov, int iovcnt );
179 
180  //------------------------------------------------------------------------
182  //------------------------------------------------------------------------
183  int GetFD()
184  {
185  return pSocket;
186  }
187 
188  //------------------------------------------------------------------------
190  //------------------------------------------------------------------------
191  std::string GetSockName() const;
192 
193  //------------------------------------------------------------------------
195  //------------------------------------------------------------------------
196  std::string GetPeerName() const;
197 
198  //------------------------------------------------------------------------
200  //------------------------------------------------------------------------
201  std::string GetName() const;
202 
203  //------------------------------------------------------------------------
205  //------------------------------------------------------------------------
207  {
208  return pServerAddr;
209  }
210 
211  //------------------------------------------------------------------------
214  //------------------------------------------------------------------------
215  void SetChannelID( AnyObject *channelID )
216  {
217  pChannelID = channelID;
218  }
219 
220  //------------------------------------------------------------------------
223  //------------------------------------------------------------------------
224  const AnyObject* GetChannelID() const
225  {
226  return pChannelID;
227  }
228 
229  private:
230  //------------------------------------------------------------------------
241  //------------------------------------------------------------------------
242  Status Poll( bool readyForReading, bool readyForWriting,
243  int32_t timeout );
244 
245  int pSocket;
248  mutable std::string pSockName; // mutable because it's for caching
249  mutable std::string pPeerName;
250  mutable std::string pName;
253  };
254 }
255 
256 #endif // __XRD_CL_SOCKET_HH__
257 
Definition: XrdClAnyObject.hh:32
Socket(int socket=-1, SocketStatus status=Disconnected)
Definition: XrdClSocket.hh:55
std::string pPeerName
Definition: XrdClSocket.hh:249
Status WriteRaw(void *buffer, uint32_t size, int32_t timeout, uint32_t &bytesWritten)
Status Poll(bool readyForReading, bool readyForWriting, int32_t timeout)
SocketStatus pStatus
Definition: XrdClSocket.hh:246
Status Connect(const std::string &host, uint16_t port, uint16_t timout=10)
std::string pSockName
Definition: XrdClSocket.hh:248
The socket is disconnected.
Definition: XrdClSocket.hh:44
std::string pName
Definition: XrdClSocket.hh:250
Definition: XrdNetAddr.hh:41
Procedure execution status.
Definition: XrdClStatus.hh:109
void SetChannelID(AnyObject *channelID)
Definition: XrdClSocket.hh:215
ssize_t WriteV(iovec *iov, int iovcnt)
Status GetSockOpt(int level, int optname, void *optval, socklen_t *optlen)
Get socket options.
void SetStatus(SocketStatus status)
Set socket status - do not use unless you know what you're doing.
Definition: XrdClSocket.hh:135
Definition: XrdClAnyObject.hh:25
ssize_t Send(void *buffer, uint32_t size)
virtual ~Socket()
Desctuctor.
Definition: XrdClSocket.hh:65
std::string GetPeerName() const
Get the name of the remote peer.
Status Initialize(int family=AF_INET)
Initialize the socket.
Status SetFlags(int flags)
Set the socket flags (man fcntl)
Status GetFlags(int &flags)
Get the socket flags (man fcntl)
const AnyObject * GetChannelID() const
Definition: XrdClSocket.hh:224
The socket is connected.
Definition: XrdClSocket.hh:45
AnyObject * pChannelID
Definition: XrdClSocket.hh:252
const XrdNetAddr & GetServerAddress() const
Get the server address.
Definition: XrdClSocket.hh:206
int pProtocolFamily
Definition: XrdClSocket.hh:251
XrdNetAddr pServerAddr
Definition: XrdClSocket.hh:247
Status ConnectToAddress(const XrdNetAddr &addr, uint16_t timout=10)
std::string GetName() const
Get the string representation of the socket.
int pSocket
Definition: XrdClSocket.hh:245
Status ReadRaw(void *buffer, uint32_t size, int32_t timeout, uint32_t &bytesRead)
void Close()
Disconnect.
Status SetSockOpt(int level, int optname, const void *optval, socklen_t optlen)
Set socket options.
SocketStatus
Status of the socket.
Definition: XrdClSocket.hh:42
std::string GetSockName() const
Get the name of the socket.
A network socket.
Definition: XrdClSocket.hh:36
int GetFD()
Get the file descriptor.
Definition: XrdClSocket.hh:183
SocketStatus GetStatus() const
Get the socket status.
Definition: XrdClSocket.hh:127
The connection process is in progress.
Definition: XrdClSocket.hh:46