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,
46  Connecting = 3
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  //------------------------------------------------------------------------
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 
XrdCl::Socket::SetSockOpt
Status SetSockOpt(int level, int optname, const void *optval, socklen_t optlen)
Set socket options.
XrdCl::Socket
A network socket.
Definition: XrdClSocket.hh:37
XrdCl::Socket::WriteRaw
Status WriteRaw(void *buffer, uint32_t size, int32_t timeout, uint32_t &bytesWritten)
XrdCl::Socket::SetStatus
void SetStatus(SocketStatus status)
Set socket status - do not use unless you know what you're doing.
Definition: XrdClSocket.hh:135
XrdCl::Socket::GetSockName
std::string GetSockName() const
Get the name of the socket.
XrdCl::Socket::Disconnected
@ Disconnected
The socket is disconnected.
Definition: XrdClSocket.hh:44
XrdCl::Socket::Initialize
Status Initialize(int family=AF_INET)
Initialize the socket.
XrdCl::Socket::GetFD
int GetFD()
Get the file descriptor.
Definition: XrdClSocket.hh:183
XrdCl::Socket::pChannelID
AnyObject * pChannelID
Definition: XrdClSocket.hh:252
XrdCl::Socket::GetChannelID
const AnyObject * GetChannelID() const
Definition: XrdClSocket.hh:224
XrdCl::Socket::GetPeerName
std::string GetPeerName() const
Get the name of the remote peer.
XrdCl::Socket::GetSockOpt
Status GetSockOpt(int level, int optname, void *optval, socklen_t *optlen)
Get socket options.
XrdCl::Socket::Connecting
@ Connecting
The connection process is in progress.
Definition: XrdClSocket.hh:46
XrdNetAddr.hh
XrdCl::Socket::WriteV
ssize_t WriteV(iovec *iov, int iovcnt)
XrdCl::Socket::pStatus
SocketStatus pStatus
Definition: XrdClSocket.hh:246
XrdCl::Socket::Poll
Status Poll(bool readyForReading, bool readyForWriting, int32_t timeout)
XrdCl::Socket::pPeerName
std::string pPeerName
Definition: XrdClSocket.hh:249
XrdCl::Socket::pSockName
std::string pSockName
Definition: XrdClSocket.hh:248
XrdCl::Socket::pSocket
int pSocket
Definition: XrdClSocket.hh:245
XrdCl::Socket::GetName
std::string GetName() const
Get the string representation of the socket.
XrdCl::Socket::Connected
@ Connected
The socket is connected.
Definition: XrdClSocket.hh:45
XrdCl::Socket::Socket
Socket(int socket=-1, SocketStatus status=Disconnected)
Definition: XrdClSocket.hh:55
XrdCl::Socket::pProtocolFamily
int pProtocolFamily
Definition: XrdClSocket.hh:251
XrdCl::Socket::ReadRaw
Status ReadRaw(void *buffer, uint32_t size, int32_t timeout, uint32_t &bytesRead)
XrdCl::Socket::GetServerAddress
const XrdNetAddr & GetServerAddress() const
Get the server address.
Definition: XrdClSocket.hh:206
XrdCl::Socket::SetChannelID
void SetChannelID(AnyObject *channelID)
Definition: XrdClSocket.hh:215
XrdCl
Definition: XrdClAnyObject.hh:26
XrdCl::Socket::Close
void Close()
Disconnect.
XrdCl::Socket::Send
ssize_t Send(void *buffer, uint32_t size)
XrdCl::Socket::Connect
Status Connect(const std::string &host, uint16_t port, uint16_t timout=10)
XrdCl::Socket::~Socket
virtual ~Socket()
Desctuctor.
Definition: XrdClSocket.hh:65
XrdCl::Socket::ConnectToAddress
Status ConnectToAddress(const XrdNetAddr &addr, uint16_t timout=10)
XrdClStatus.hh
XrdCl::Socket::SocketStatus
SocketStatus
Status of the socket.
Definition: XrdClSocket.hh:43
XrdCl::Status
Procedure execution status.
Definition: XrdClStatus.hh:110
XrdCl::Socket::SetFlags
Status SetFlags(int flags)
Set the socket flags (man fcntl)
XrdCl::Socket::pName
std::string pName
Definition: XrdClSocket.hh:250
XrdCl::Socket::GetFlags
Status GetFlags(int &flags)
Get the socket flags (man fcntl)
XrdNetAddr
Definition: XrdNetAddr.hh:42
XrdCl::AnyObject
Definition: XrdClAnyObject.hh:33
XrdCl::Socket::GetStatus
SocketStatus GetStatus() const
Get the socket status.
Definition: XrdClSocket.hh:127
XrdCl::Socket::pServerAddr
XrdNetAddr pServerAddr
Definition: XrdClSocket.hh:247