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