xrootd
XrdClSocket.hh
Go to the documentation of this file.
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 #include <netinet/in.h>
00026 
00027 #include "XrdCl/XrdClStatus.hh"
00028 
00029 namespace XrdCl
00030 {
00031   //----------------------------------------------------------------------------
00033   //----------------------------------------------------------------------------
00034   class Socket
00035   {
00036     public:
00037       //------------------------------------------------------------------------
00039       //------------------------------------------------------------------------
00040       enum SocketStatus
00041       {
00042         Disconnected  = 1,      
00043         Connected     = 2,      
00044         Connecting    = 3       
00045       };
00046 
00047       //------------------------------------------------------------------------
00052       //------------------------------------------------------------------------
00053       Socket( int socket = -1, SocketStatus status = Disconnected ):
00054         pSocket(socket), pStatus( status ), pServerAddr( 0 )
00055       {
00056       };
00057 
00058       //------------------------------------------------------------------------
00060       //------------------------------------------------------------------------
00061       virtual ~Socket()
00062       {
00063         Close();
00064       };
00065 
00066       //------------------------------------------------------------------------
00068       //------------------------------------------------------------------------
00069       Status Initialize();
00070 
00071       //------------------------------------------------------------------------
00073       //------------------------------------------------------------------------
00074       Status SetFlags( int flags );
00075 
00076       //------------------------------------------------------------------------
00078       //------------------------------------------------------------------------
00079       Status GetFlags( int &flags );
00080 
00081       //------------------------------------------------------------------------
00083       //------------------------------------------------------------------------
00084       Status GetSockOpt( int level, int optname, void *optval,
00085                          socklen_t *optlen );
00086 
00087       //------------------------------------------------------------------------
00089       //------------------------------------------------------------------------
00090       Status SetSockOpt( int level, int optname, const void *optval,
00091                          socklen_t optlen );
00092 
00093       //------------------------------------------------------------------------
00100       //------------------------------------------------------------------------
00101       Status Connect( const std::string &host,
00102                       uint16_t           port,
00103                       uint16_t           timout = 10 );
00104 
00105       //------------------------------------------------------------------------
00111       //------------------------------------------------------------------------
00112       Status ConnectToAddress( const sockaddr_in &addr,
00113                                uint16_t           timout = 10 );
00114 
00115       //------------------------------------------------------------------------
00117       //------------------------------------------------------------------------
00118       void Close();
00119 
00120       //------------------------------------------------------------------------
00122       //------------------------------------------------------------------------
00123       SocketStatus GetStatus() const
00124       {
00125         return pStatus;
00126       }
00127 
00128       //------------------------------------------------------------------------
00130       //------------------------------------------------------------------------
00131       void SetStatus( SocketStatus status )
00132       {
00133         pStatus = status;
00134       }
00135 
00136       //------------------------------------------------------------------------
00143       //------------------------------------------------------------------------
00144       Status ReadRaw( void *buffer, uint32_t size, int32_t timeout,
00145                       uint32_t &bytesRead );
00146 
00147       //------------------------------------------------------------------------
00154       //------------------------------------------------------------------------
00155       Status WriteRaw( void *buffer, uint32_t size, int32_t timeout,
00156                        uint32_t &bytesWritten );
00157 
00158       //------------------------------------------------------------------------
00164       //------------------------------------------------------------------------
00165       ssize_t Send( void *buffer, uint32_t size );
00166 
00167       //------------------------------------------------------------------------
00169       //------------------------------------------------------------------------
00170       int GetFD()
00171       {
00172         return pSocket;
00173       }
00174 
00175       //------------------------------------------------------------------------
00177       //------------------------------------------------------------------------
00178       std::string GetSockName() const;
00179 
00180       //------------------------------------------------------------------------
00182       //------------------------------------------------------------------------
00183       std::string GetPeerName() const;
00184 
00185       //------------------------------------------------------------------------
00187       //------------------------------------------------------------------------
00188       std::string GetName() const;
00189 
00190       //------------------------------------------------------------------------
00192       //------------------------------------------------------------------------
00193       const sockaddr_in *GetServerAddress() const
00194       {
00195         return pServerAddr;
00196       }
00197 
00198     private:
00199       //------------------------------------------------------------------------
00210       //------------------------------------------------------------------------
00211       Status Poll( bool readyForReading, bool readyForWriting,
00212                    int32_t timeout );
00213 
00214       int                  pSocket;
00215       SocketStatus         pStatus;
00216       sockaddr_in         *pServerAddr;
00217       mutable std::string  pSockName;     // mutable because it's for caching
00218       mutable std::string  pPeerName;
00219       mutable std::string  pName;
00220   };
00221 }
00222 
00223 #endif // __XRD_CL_SOCKET_HH__
00224