00001 //------------------------------------------------------------------------------ 00002 // Copyright (c) 2011-2012 by European Organization for Nuclear Research (CERN) 00003 // Author: Lukasz Janyst <ljanyst@cern.ch> 00004 //------------------------------------------------------------------------------ 00005 // XRootD is free software: you can redistribute it and/or modify 00006 // it under the terms of the GNU Lesser General Public License as published by 00007 // the Free Software Foundation, either version 3 of the License, or 00008 // (at your option) any later version. 00009 // 00010 // XRootD is distributed in the hope that it will be useful, 00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 // GNU General Public License for more details. 00014 // 00015 // You should have received a copy of the GNU Lesser General Public License 00016 // along with XRootD. If not, see <http://www.gnu.org/licenses/>. 00017 //------------------------------------------------------------------------------ 00018 00019 #ifndef __XRD_CL_SOCKET_HH__ 00020 #define __XRD_CL_SOCKET_HH__ 00021 00022 #include <stdint.h> 00023 #include <string> 00024 #include <sys/socket.h> 00025 00026 #include "XrdCl/XrdClStatus.hh" 00027 #include "XrdNet/XrdNetAddr.hh" 00028 00029 namespace XrdCl 00030 { 00031 class AnyObject; 00032 00033 //---------------------------------------------------------------------------- 00035 //---------------------------------------------------------------------------- 00036 class Socket 00037 { 00038 public: 00039 //------------------------------------------------------------------------ 00041 //------------------------------------------------------------------------ 00042 enum SocketStatus 00043 { 00044 Disconnected = 1, 00045 Connected = 2, 00046 Connecting = 3 00047 }; 00048 00049 //------------------------------------------------------------------------ 00054 //------------------------------------------------------------------------ 00055 Socket( int socket = -1, SocketStatus status = Disconnected ): 00056 pSocket(socket), pStatus( status ), pServerAddr( 0 ), 00057 pProtocolFamily( AF_INET ), 00058 pChannelID( 0 ) 00059 { 00060 }; 00061 00062 //------------------------------------------------------------------------ 00064 //------------------------------------------------------------------------ 00065 virtual ~Socket() 00066 { 00067 Close(); 00068 }; 00069 00070 //------------------------------------------------------------------------ 00072 //------------------------------------------------------------------------ 00073 Status Initialize( int family = AF_INET ); 00074 00075 //------------------------------------------------------------------------ 00077 //------------------------------------------------------------------------ 00078 Status SetFlags( int flags ); 00079 00080 //------------------------------------------------------------------------ 00082 //------------------------------------------------------------------------ 00083 Status GetFlags( int &flags ); 00084 00085 //------------------------------------------------------------------------ 00087 //------------------------------------------------------------------------ 00088 Status GetSockOpt( int level, int optname, void *optval, 00089 socklen_t *optlen ); 00090 00091 //------------------------------------------------------------------------ 00093 //------------------------------------------------------------------------ 00094 Status SetSockOpt( int level, int optname, const void *optval, 00095 socklen_t optlen ); 00096 00097 //------------------------------------------------------------------------ 00104 //------------------------------------------------------------------------ 00105 Status Connect( const std::string &host, 00106 uint16_t port, 00107 uint16_t timout = 10 ); 00108 00109 //------------------------------------------------------------------------ 00115 //------------------------------------------------------------------------ 00116 Status ConnectToAddress( const XrdNetAddr &addr, 00117 uint16_t timout = 10 ); 00118 00119 //------------------------------------------------------------------------ 00121 //------------------------------------------------------------------------ 00122 void Close(); 00123 00124 //------------------------------------------------------------------------ 00126 //------------------------------------------------------------------------ 00127 SocketStatus GetStatus() const 00128 { 00129 return pStatus; 00130 } 00131 00132 //------------------------------------------------------------------------ 00134 //------------------------------------------------------------------------ 00135 void SetStatus( SocketStatus status ) 00136 { 00137 pStatus = status; 00138 } 00139 00140 //------------------------------------------------------------------------ 00147 //------------------------------------------------------------------------ 00148 Status ReadRaw( void *buffer, uint32_t size, int32_t timeout, 00149 uint32_t &bytesRead ); 00150 00151 //------------------------------------------------------------------------ 00158 //------------------------------------------------------------------------ 00159 Status WriteRaw( void *buffer, uint32_t size, int32_t timeout, 00160 uint32_t &bytesWritten ); 00161 00162 //------------------------------------------------------------------------ 00168 //------------------------------------------------------------------------ 00169 ssize_t Send( void *buffer, uint32_t size ); 00170 00171 //------------------------------------------------------------------------ 00177 //------------------------------------------------------------------------ 00178 ssize_t WriteV( iovec *iov, int iovcnt ); 00179 00180 //------------------------------------------------------------------------ 00182 //------------------------------------------------------------------------ 00183 int GetFD() 00184 { 00185 return pSocket; 00186 } 00187 00188 //------------------------------------------------------------------------ 00190 //------------------------------------------------------------------------ 00191 std::string GetSockName() const; 00192 00193 //------------------------------------------------------------------------ 00195 //------------------------------------------------------------------------ 00196 std::string GetPeerName() const; 00197 00198 //------------------------------------------------------------------------ 00200 //------------------------------------------------------------------------ 00201 std::string GetName() const; 00202 00203 //------------------------------------------------------------------------ 00205 //------------------------------------------------------------------------ 00206 const XrdNetAddr &GetServerAddress() const 00207 { 00208 return pServerAddr; 00209 } 00210 00211 //------------------------------------------------------------------------ 00214 //------------------------------------------------------------------------ 00215 void SetChannelID( AnyObject *channelID ) 00216 { 00217 pChannelID = channelID; 00218 } 00219 00220 //------------------------------------------------------------------------ 00223 //------------------------------------------------------------------------ 00224 const AnyObject* GetChannelID() const 00225 { 00226 return pChannelID; 00227 } 00228 00229 private: 00230 //------------------------------------------------------------------------ 00241 //------------------------------------------------------------------------ 00242 Status Poll( bool readyForReading, bool readyForWriting, 00243 int32_t timeout ); 00244 00245 int pSocket; 00246 SocketStatus pStatus; 00247 XrdNetAddr pServerAddr; 00248 mutable std::string pSockName; // mutable because it's for caching 00249 mutable std::string pPeerName; 00250 mutable std::string pName; 00251 int pProtocolFamily; 00252 AnyObject *pChannelID; 00253 }; 00254 } 00255 00256 #endif // __XRD_CL_SOCKET_HH__ 00257