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 #include <netinet/in.h>
26 
27 #include "XrdCl/XrdClStatus.hh"
28 
29 namespace XrdCl
30 {
31  //----------------------------------------------------------------------------
33  //----------------------------------------------------------------------------
34  class Socket
35  {
36  public:
37  //------------------------------------------------------------------------
39  //------------------------------------------------------------------------
41  {
43  Connected = 2,
45  };
46 
47  //------------------------------------------------------------------------
52  //------------------------------------------------------------------------
53  Socket( int socket = -1, SocketStatus status = Disconnected ):
54  pSocket(socket), pStatus( status ), pServerAddr( 0 )
55  {
56  };
57 
58  //------------------------------------------------------------------------
60  //------------------------------------------------------------------------
61  virtual ~Socket()
62  {
63  Close();
64  };
65 
66  //------------------------------------------------------------------------
68  //------------------------------------------------------------------------
70 
71  //------------------------------------------------------------------------
73  //------------------------------------------------------------------------
74  Status SetFlags( int flags );
75 
76  //------------------------------------------------------------------------
78  //------------------------------------------------------------------------
79  Status GetFlags( int &flags );
80 
81  //------------------------------------------------------------------------
83  //------------------------------------------------------------------------
84  Status GetSockOpt( int level, int optname, void *optval,
85  socklen_t *optlen );
86 
87  //------------------------------------------------------------------------
89  //------------------------------------------------------------------------
90  Status SetSockOpt( int level, int optname, const void *optval,
91  socklen_t optlen );
92 
93  //------------------------------------------------------------------------
100  //------------------------------------------------------------------------
101  Status Connect( const std::string &host,
102  uint16_t port,
103  uint16_t timout = 10 );
104 
105  //------------------------------------------------------------------------
111  //------------------------------------------------------------------------
112  Status ConnectToAddress( const sockaddr_in &addr,
113  uint16_t timout = 10 );
114 
115  //------------------------------------------------------------------------
117  //------------------------------------------------------------------------
118  void Close();
119 
120  //------------------------------------------------------------------------
122  //------------------------------------------------------------------------
124  {
125  return pStatus;
126  }
127 
128  //------------------------------------------------------------------------
130  //------------------------------------------------------------------------
131  void SetStatus( SocketStatus status )
132  {
133  pStatus = status;
134  }
135 
136  //------------------------------------------------------------------------
143  //------------------------------------------------------------------------
144  Status ReadRaw( void *buffer, uint32_t size, int32_t timeout,
145  uint32_t &bytesRead );
146 
147  //------------------------------------------------------------------------
154  //------------------------------------------------------------------------
155  Status WriteRaw( void *buffer, uint32_t size, int32_t timeout,
156  uint32_t &bytesWritten );
157 
158  //------------------------------------------------------------------------
164  //------------------------------------------------------------------------
165  ssize_t Send( void *buffer, uint32_t size );
166 
167  //------------------------------------------------------------------------
169  //------------------------------------------------------------------------
170  int GetFD()
171  {
172  return pSocket;
173  }
174 
175  //------------------------------------------------------------------------
177  //------------------------------------------------------------------------
178  std::string GetSockName() const;
179 
180  //------------------------------------------------------------------------
182  //------------------------------------------------------------------------
183  std::string GetPeerName() const;
184 
185  //------------------------------------------------------------------------
187  //------------------------------------------------------------------------
188  std::string GetName() const;
189 
190  //------------------------------------------------------------------------
192  //------------------------------------------------------------------------
193  const sockaddr_in *GetServerAddress() const
194  {
195  return pServerAddr;
196  }
197 
198  private:
199  //------------------------------------------------------------------------
210  //------------------------------------------------------------------------
211  Status Poll( bool readyForReading, bool readyForWriting,
212  int32_t timeout );
213 
214  int pSocket;
216  sockaddr_in *pServerAddr;
217  mutable std::string pSockName; // mutable because it's for caching
218  mutable std::string pPeerName;
219  mutable std::string pName;
220  };
221 }
222 
223 #endif // __XRD_CL_SOCKET_HH__
224