xrootd
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
XrdClAsyncSocketHandler.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_ASYNC_SOCKET_HANDLER_HH__
20 #define __XRD_CL_ASYNC_SOCKET_HANDLER_HH__
21 
22 #include "XrdCl/XrdClSocket.hh"
23 #include "XrdCl/XrdClDefaultEnv.hh"
24 #include "XrdCl/XrdClPoller.hh"
28 #include "XrdCl/XrdClURL.hh"
29 
30 namespace XrdCl
31 {
32  class Stream;
33 
34  //----------------------------------------------------------------------------
37  //----------------------------------------------------------------------------
39  {
40  //------------------------------------------------------------------------
41  // We need an extra task for rescheduling of HS request that received
42  // a wait response.
43  //------------------------------------------------------------------------
44  class WaitTask: public XrdCl::Task
45  {
46  public:
48  pHandler( handler )
49  {
50  std::ostringstream o;
51  o << "WaitTask for: 0x" << handler->pHandShakeData->out;
52  SetName( o.str() );
53  }
54 
55  virtual time_t Run( time_t now )
56  {
58  return 0;
59  }
60 
61  private:
63  };
64 
65  public:
66  //------------------------------------------------------------------------
68  //------------------------------------------------------------------------
69  AsyncSocketHandler( const URL &url,
70  Poller *poller,
71  TransportHandler *transport,
72  AnyObject *channelData,
73  uint16_t subStreamNum );
74 
75  //------------------------------------------------------------------------
77  //------------------------------------------------------------------------
79 
80  //------------------------------------------------------------------------
82  //------------------------------------------------------------------------
83  void SetAddress( const XrdNetAddr &address )
84  {
85  pSockAddr = address;
86  }
87 
88  //------------------------------------------------------------------------
90  //------------------------------------------------------------------------
91  const XrdNetAddr &GetAddress() const
92  {
93  return pSockAddr;
94  }
95 
96  //------------------------------------------------------------------------
98  //------------------------------------------------------------------------
99  XRootDStatus Connect( time_t timeout );
100 
101  //------------------------------------------------------------------------
103  //------------------------------------------------------------------------
105 
106  //------------------------------------------------------------------------
108  //------------------------------------------------------------------------
109  void SetStream( Stream *stream );
110 
111  //------------------------------------------------------------------------
113  //------------------------------------------------------------------------
114  virtual void Event( uint8_t type, XrdCl::Socket */*socket*/ );
115 
116  //------------------------------------------------------------------------
118  //------------------------------------------------------------------------
120  {
123  return XRootDStatus();
124  }
125 
126  //------------------------------------------------------------------------
128  //------------------------------------------------------------------------
130  {
131  if( !pPoller->EnableWriteNotification( pSocket, false ) )
133  return XRootDStatus();
134  }
135 
136  //------------------------------------------------------------------------
138  //------------------------------------------------------------------------
139  const std::string &GetStreamName()
140  {
141  return pStreamName;
142  }
143 
144  //------------------------------------------------------------------------
146  //------------------------------------------------------------------------
148  {
149  return pLastActivity;
150  }
151 
152  protected:
153 
154  //------------------------------------------------------------------------
155  // Connect returned
156  //------------------------------------------------------------------------
157  virtual void OnConnectionReturn();
158 
159  //------------------------------------------------------------------------
160  // Got a write readiness event
161  //------------------------------------------------------------------------
162  void OnWrite();
163 
164  //------------------------------------------------------------------------
165  // Got a write readiness event while handshaking
166  //------------------------------------------------------------------------
168 
169  //------------------------------------------------------------------------
170  // Write the message and it's signature in one go with writev
171  //------------------------------------------------------------------------
172  XRootDStatus WriteMessageAndRaw( Message *toWrite, Message *&sign );
173 
174  //------------------------------------------------------------------------
175  // Write the current message
176  //------------------------------------------------------------------------
178 
179  //------------------------------------------------------------------------
180  // Got a read readiness event
181  //------------------------------------------------------------------------
182  void OnRead();
183 
184  //------------------------------------------------------------------------
185  // Got a read readiness event while handshaking
186  //------------------------------------------------------------------------
187  void OnReadWhileHandshaking();
188 
189  //------------------------------------------------------------------------
190  // Handle the handshake message
191  //------------------------------------------------------------------------
192  void HandleHandShake();
193 
194  //------------------------------------------------------------------------
195  // Prepare the next step of the hand-shake procedure
196  //------------------------------------------------------------------------
197  void HandShakeNextStep( bool done );
198 
199  //------------------------------------------------------------------------
200  // Read a message
201  //------------------------------------------------------------------------
202  XRootDStatus ReadMessage( Message *&toRead );
203 
204  //------------------------------------------------------------------------
205  // Handle fault
206  //------------------------------------------------------------------------
207  void OnFault( XRootDStatus st );
208 
209  //------------------------------------------------------------------------
210  // Handle fault while handshaking
211  //------------------------------------------------------------------------
213 
214  //------------------------------------------------------------------------
215  // Handle write timeout event
216  //------------------------------------------------------------------------
217  void OnWriteTimeout();
218 
219  //------------------------------------------------------------------------
220  // Handle read timeout event
221  //------------------------------------------------------------------------
222  void OnReadTimeout();
223 
224  //------------------------------------------------------------------------
225  // Handle timeout event while handshaking
226  //------------------------------------------------------------------------
228 
229  //------------------------------------------------------------------------
230  // Handle header corruption in case of kXR_status response
231  //------------------------------------------------------------------------
232  void OnHeaderCorruption();
233 
234  //------------------------------------------------------------------------
235  // Carry out the TLS hand-shake
236  //
237  // The TLS hand-shake is being initiated in HandleHandShake() by calling
238  // Socket::TlsHandShake(), however it returns suRetry the TLS hand-shake
239  // needs to be followed up by OnTlsHandShake().
240  //
241  // However, once the TLS connection has been established the server may
242  // decide to redo the TLS hand-shake at any time, this operation is handled
243  // under the hood by read and write requests and facilitated by
244  // Socket::MapEvent()
245  //------------------------------------------------------------------------
247 
248  //------------------------------------------------------------------------
249  // Handle read/write event if we are in the middle of a TLS hand-shake
250  //------------------------------------------------------------------------
251  // Handle read/write event if we are in the middle of a TLS hand-shake
252  void OnTLSHandShake();
253 
254  //------------------------------------------------------------------------
255  // Retry hand shake message
256  //------------------------------------------------------------------------
257  void SendHSMsg();
258 
259  //------------------------------------------------------------------------
260  // Extract the value of a wait response
261  //
262  // @param rsp : the server response
263  // @return : if rsp is a wait response then its value
264  // otherwise -1
265  //------------------------------------------------------------------------
266  inline kXR_int32 HandleWaitRsp( Message *rsp );
267 
268  //------------------------------------------------------------------------
269  // Data members
270  //------------------------------------------------------------------------
274  uint16_t pSubStreamNum;
276  std::string pStreamName;
290  // true means the handler owns the server response
291  std::pair<IncomingMsgHandler*, bool> pIncHandler;
294  uint32_t pIncMsgSize;
295  uint32_t pOutMsgSize;
299  };
300 }
301 
302 #endif // __XRD_CL_ASYNC_SOCKET_HANDLER_HH__
Definition: XrdClAnyObject.hh:32
Interface.
Definition: XrdClPoller.hh:33
Interface for socket pollers.
Definition: XrdClPoller.hh:86
Definition: XrdClAsyncSocketHandler.hh:44
virtual void Event(uint8_t type, XrdCl::Socket *)
Handle a socket event.
Message * pOutgoing
Definition: XrdClAsyncSocketHandler.hh:280
bool pHandShakeDone
Definition: XrdClAsyncSocketHandler.hh:285
The message representation used throughout the system.
Definition: XrdClMessage.hh:29
time_t GetLastActivity()
Get timestamp of last registered socket activity.
Definition: XrdClAsyncSocketHandler.hh:147
bool pHeaderDone
Definition: XrdClAsyncSocketHandler.hh:289
std::pair< IncomingMsgHandler *, bool > pIncHandler
Definition: XrdClAsyncSocketHandler.hh:291
bool pTlsHandShakeOngoing
Definition: XrdClAsyncSocketHandler.hh:298
XRootDStatus Close()
Close the connection.
AnyObject * pChannelData
Definition: XrdClAsyncSocketHandler.hh:273
virtual void OnConnectionReturn()
uint16_t pSubStreamNum
Definition: XrdClAsyncSocketHandler.hh:274
const XrdNetAddr & GetAddress() const
Get the address that the socket is connected to.
Definition: XrdClAsyncSocketHandler.hh:91
time_t pConnectionStarted
Definition: XrdClAsyncSocketHandler.hh:287
URL pUrl
Definition: XrdClAsyncSocketHandler.hh:297
Definition: XrdNetAddr.hh:41
void SetName(const std::string &name)
Set name of the task.
Definition: XrdClTaskManager.hh:60
XRootDStatus DisableUplink()
Disable uplink.
Definition: XrdClAsyncSocketHandler.hh:129
bool pOutMsgDone
Definition: XrdClAsyncSocketHandler.hh:292
void OnFault(XRootDStatus st)
void OnFaultWhileHandshaking(XRootDStatus st)
Data structure that carries the handshake information.
Definition: XrdClPostMasterInterfaces.hh:271
Interface for a task to be run by the TaskManager.
Definition: XrdClTaskManager.hh:35
uint32_t pOutMsgSize
Definition: XrdClAsyncSocketHandler.hh:295
Perform the handshake and the authentication for each physical stream.
Definition: XrdClPostMasterInterfaces.hh:316
const uint16_t stFatal
Fatal error, it&#39;s still an error.
Definition: XrdClStatus.hh:33
XRootDStatus WriteCurrentMessage(Message *toWrite)
Request status.
Definition: XrdClXRootDResponses.hh:214
Socket * pSocket
Definition: XrdClAsyncSocketHandler.hh:277
Poller * pPoller
Definition: XrdClAsyncSocketHandler.hh:271
XRootDStatus ReadMessage(Message *&toRead)
const std::string & GetStreamName()
Get stream name.
Definition: XrdClAsyncSocketHandler.hh:139
TransportHandler * pTransport
Definition: XrdClAsyncSocketHandler.hh:272
XrdNetAddr pSockAddr
Definition: XrdClAsyncSocketHandler.hh:283
const uint16_t errPollerError
Definition: XrdClStatus.hh:74
void SetStream(Stream *stream)
Set a stream object to be notified about the status of the operations.
URL representation.
Definition: XrdClURL.hh:30
XRootDStatus DoTlsHandShake()
~AsyncSocketHandler()
Destructor.
WaitTask(XrdCl::AsyncSocketHandler *handler)
Definition: XrdClAsyncSocketHandler.hh:47
time_t pLastActivity
Definition: XrdClAsyncSocketHandler.hh:296
time_t pConnectionTimeout
Definition: XrdClAsyncSocketHandler.hh:288
int kXR_int32
Definition: XPtypes.hh:89
XRootDStatus Connect(time_t timeout)
Connect to the currently set address.
Message * pHSOutgoing
Definition: XrdClAsyncSocketHandler.hh:282
Message status handler.
Definition: XrdClPostMasterInterfaces.hh:184
AsyncSocketHandler(const URL &url, Poller *poller, TransportHandler *transport, AnyObject *channelData, uint16_t subStreamNum)
Constructor.
Stream.
Definition: XrdClStream.hh:48
Message * pIncoming
Definition: XrdClAsyncSocketHandler.hh:278
std::string pStreamName
Definition: XrdClAsyncSocketHandler.hh:276
XrdCl::AsyncSocketHandler * pHandler
Definition: XrdClAsyncSocketHandler.hh:62
kXR_int32 HandleWaitRsp(Message *rsp)
Message * pHSIncoming
Definition: XrdClAsyncSocketHandler.hh:279
uint32_t pIncMsgSize
Definition: XrdClAsyncSocketHandler.hh:294
void HandShakeNextStep(bool done)
virtual time_t Run(time_t now)
Definition: XrdClAsyncSocketHandler.hh:55
Message * out
Message to be sent out.
Definition: XrdClPostMasterInterfaces.hh:281
A network socket.
Definition: XrdClSocket.hh:41
XRootDStatus WriteMessageAndRaw(Message *toWrite, Message *&sign)
OutgoingMsgHandler * pOutHandler
Definition: XrdClAsyncSocketHandler.hh:293
Definition: XrdClAsyncSocketHandler.hh:38
virtual bool EnableWriteNotification(Socket *socket, bool notify, uint16_t timeout=60)=0
uint16_t pTimeoutResolution
Definition: XrdClAsyncSocketHandler.hh:286
Message * pSignature
Definition: XrdClAsyncSocketHandler.hh:281
void SetAddress(const XrdNetAddr &address)
Set address.
Definition: XrdClAsyncSocketHandler.hh:83
Stream * pStream
Definition: XrdClAsyncSocketHandler.hh:275
HandShakeData * pHandShakeData
Definition: XrdClAsyncSocketHandler.hh:284
XRootDStatus EnableUplink()
Enable uplink.
Definition: XrdClAsyncSocketHandler.hh:119