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