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