xrootd
XrdClAsyncSocketHandler.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_ASYNC_SOCKET_HANDLER_HH__
00020 #define __XRD_CL_ASYNC_SOCKET_HANDLER_HH__
00021 
00022 #include "XrdCl/XrdClSocket.hh"
00023 #include "XrdCl/XrdClConstants.hh"
00024 #include "XrdCl/XrdClDefaultEnv.hh"
00025 #include "XrdCl/XrdClPoller.hh"
00026 #include "XrdCl/XrdClPostMasterInterfaces.hh"
00027 
00028 #include <sys/types.h>
00029 #include <sys/socket.h>
00030 
00031 namespace XrdCl
00032 {
00033   class Stream;
00034 
00035   //----------------------------------------------------------------------------
00038   //----------------------------------------------------------------------------
00039   class AsyncSocketHandler: public SocketHandler
00040   {
00041     public:
00042       //------------------------------------------------------------------------
00044       //------------------------------------------------------------------------
00045       AsyncSocketHandler( Poller           *poller,
00046                           TransportHandler *transport,
00047                           AnyObject        *channelData,
00048                           uint16_t          subStreamNum );
00049 
00050       //------------------------------------------------------------------------
00052       //------------------------------------------------------------------------
00053       ~AsyncSocketHandler();
00054 
00055       //------------------------------------------------------------------------
00057       //------------------------------------------------------------------------
00058       void SetAddress( const sockaddr_in &address )
00059       {
00060         memcpy( &pSockAddr, &address, sizeof( sockaddr_in ) );
00061       }
00062 
00063       //------------------------------------------------------------------------
00065       //------------------------------------------------------------------------
00066       const sockaddr_in &GetAddress() const
00067       {
00068         return pSockAddr;
00069       }
00070 
00071       //------------------------------------------------------------------------
00073       //------------------------------------------------------------------------
00074       Status Connect( time_t timeout );
00075 
00076       //------------------------------------------------------------------------
00078       //------------------------------------------------------------------------
00079       Status Close();
00080 
00081       //------------------------------------------------------------------------
00083       //------------------------------------------------------------------------
00084       void SetStream( Stream *stream );
00085 
00086       //------------------------------------------------------------------------
00088       //------------------------------------------------------------------------
00089       virtual void Event( uint8_t type, XrdCl::Socket */*socket*/ );
00090 
00091       //------------------------------------------------------------------------
00093       //------------------------------------------------------------------------
00094       Status EnableUplink()
00095       {
00096         if( !pPoller->EnableWriteNotification( pSocket, true, pTimeoutResolution ) )
00097           return Status( stFatal, errPollerError );
00098         return Status();
00099       }
00100 
00101       //------------------------------------------------------------------------
00103       //------------------------------------------------------------------------
00104       Status DisableUplink()
00105       {
00106         if( !pPoller->EnableWriteNotification( pSocket, false ) )
00107           return Status( stFatal, errPollerError );
00108         return Status();
00109       }
00110 
00111       //------------------------------------------------------------------------
00113       //------------------------------------------------------------------------
00114       const std::string &GetStreamName()
00115       {
00116         return pStreamName;
00117       }
00118 
00119     private:
00120 
00121       //------------------------------------------------------------------------
00122       // Connect returned
00123       //------------------------------------------------------------------------
00124       void OnConnectionReturn();
00125 
00126       //------------------------------------------------------------------------
00127       // Got a write readiness event
00128       //------------------------------------------------------------------------
00129       void OnWrite();
00130 
00131       //------------------------------------------------------------------------
00132       // Got a write readiness event while handshaking
00133       //------------------------------------------------------------------------
00134       void OnWriteWhileHandshaking();
00135 
00136       //------------------------------------------------------------------------
00137       // Write the current message
00138       //------------------------------------------------------------------------
00139       Status WriteCurrentMessage();
00140 
00141       //------------------------------------------------------------------------
00142       // Got a read readiness event
00143       //------------------------------------------------------------------------
00144       void OnRead();
00145 
00146       //------------------------------------------------------------------------
00147       // Got a read readiness event while handshaking
00148       //------------------------------------------------------------------------
00149       void OnReadWhileHandshaking();
00150 
00151       //------------------------------------------------------------------------
00152       // Read a message
00153       //------------------------------------------------------------------------
00154       Status ReadMessage();
00155 
00156       //------------------------------------------------------------------------
00157       // Handle fault
00158       //------------------------------------------------------------------------
00159       void OnFault( Status st );
00160 
00161       //------------------------------------------------------------------------
00162       // Handle fault while handshaking
00163       //------------------------------------------------------------------------
00164       void OnFaultWhileHandshaking( Status st );
00165 
00166       //------------------------------------------------------------------------
00167       // Handle write timeout event
00168       //------------------------------------------------------------------------
00169       void OnWriteTimeout();
00170 
00171       //------------------------------------------------------------------------
00172       // Handle read timeout event
00173       //------------------------------------------------------------------------
00174       void OnReadTimeout();
00175 
00176       //------------------------------------------------------------------------
00177       // Handle timeout event while handshaking
00178       //------------------------------------------------------------------------
00179       void OnTimeoutWhileHandshaking();
00180 
00181       //------------------------------------------------------------------------
00182       // Data members
00183       //------------------------------------------------------------------------
00184       Poller                        *pPoller;
00185       TransportHandler              *pTransport;
00186       AnyObject                     *pChannelData;
00187       uint16_t                       pSubStreamNum;
00188       Stream                        *pStream;
00189       std::string                    pStreamName;
00190       Socket                        *pSocket;
00191       Message                       *pIncoming;
00192       Message                       *pOutgoing;
00193       sockaddr_in                    pSockAddr;
00194       HandShakeData                 *pHandShakeData;
00195       bool                           pHandShakeDone;
00196       uint16_t                       pTimeoutResolution;
00197       time_t                         pConnectionStarted;
00198       time_t                         pConnectionTimeout;
00199       bool                           pHeaderDone;
00200       std::pair<IncomingMsgHandler*, bool> pIncHandler;
00201       bool                           pOutMsgDone;
00202       OutgoingMsgHandler            *pOutHandler;
00203       uint32_t                       pIncMsgSize;
00204       uint32_t                       pOutMsgSize;
00205   };
00206 }
00207 
00208 #endif // __XRD_CL_ASYNC_SOCKET_HANDLER_HH__