XRootD
XrdClAsyncHSReader.hh
Go to the documentation of this file.
1 //------------------------------------------------------------------------------
2 // Copyright (c) 2011-2012 by European Organization for Nuclear Research (CERN)
3 // Author: Michal Simon <michal.simon@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 SRC_XRDCL_XRDCLASYNCHSREADER_HH_
20 #define SRC_XRDCL_XRDCLASYNCHSREADER_HH_
21 
22 #include "XrdCl/XrdClMessage.hh"
25 #include "XrdCl/XrdClSocket.hh"
26 #include "XrdCl/XrdClConstants.hh"
27 #include "XrdCl/XrdClStream.hh"
28 
29 #include <memory>
30 
31 namespace XrdCl
32 {
33  //----------------------------------------------------------------------------
35  //----------------------------------------------------------------------------
37  {
38  public:
39  //------------------------------------------------------------------------
47  //------------------------------------------------------------------------
49  Socket &socket,
50  const std::string &strmname,
51  Stream &strm,
52  uint16_t substrmnb) : readstage( ReadStart ),
53  xrdTransport( xrdTransport ),
54  socket( socket ),
55  strmname( strmname ),
56  strm( strm ),
57  substrmnb( substrmnb )
58  {
59  }
60 
61  //------------------------------------------------------------------------
63  //------------------------------------------------------------------------
65  {
66  Log *log = DefaultEnv::GetLog();
67 
68  while( true )
69  {
70  switch( readstage )
71  {
72  //------------------------------------------------------------------
73  // There is no incoming message currently being processed so we
74  // create a new one
75  //------------------------------------------------------------------
76  case ReadStart:
77  {
78  inmsg.reset( new Message() );
79  //----------------------------------------------------------------
80  // The next step is to read the header
81  //----------------------------------------------------------------
82  readstage = ReadHeader;
83  continue;
84  }
85  //------------------------------------------------------------------
86  // We need to read the header
87  //------------------------------------------------------------------
88  case ReadHeader:
89  {
90  XRootDStatus st = xrdTransport.GetHeader( *inmsg, &socket );
91  if( !st.IsOK() || st.code == suRetry ) return st;
92  log->Dump( AsyncSockMsg,
93  "[%s] Received message header, size: %d",
94  strmname.c_str(), inmsg->GetCursor() );
95  //----------------------------------------------------------------
96  // The next step is to read the message body
97  //----------------------------------------------------------------
98  readstage = ReadMsgBody;
99  continue;
100  }
101  //------------------------------------------------------------------
102  // We read the message to the buffer
103  //------------------------------------------------------------------
104  case ReadMsgBody:
105  {
106  XRootDStatus st = xrdTransport.GetBody( *inmsg, &socket );
107  if( !st.IsOK() || st.code == suRetry ) return st;
108  log->Dump( AsyncSockMsg, "[%s] Received a message of %d bytes",
109  strmname.c_str(), inmsg->GetSize() );
110  readstage = ReadDone;
111  return st;
112  }
113 
114  case ReadDone: return XRootDStatus();
115  }
116  // just in case ...
117  break;
118  }
119  //----------------------------------------------------------------------
120  // We are done
121  //----------------------------------------------------------------------
122  return XRootDStatus();
123  }
124 
125  //------------------------------------------------------------------------
127  //------------------------------------------------------------------------
128  std::unique_ptr<Message> ReleaseMsg()
129  {
130  readstage = ReadStart;
131  return std::move( inmsg );
132  }
133 
134  //------------------------------------------------------------------------
136  //------------------------------------------------------------------------
137  inline void Reset()
138  {
139  readstage = ReadStart;
140  inmsg.reset();
141  }
142 
143  private:
144 
145  //------------------------------------------------------------------------
147  //------------------------------------------------------------------------
148  enum Stage
149  {
150  ReadStart, //< the next step is to initialize the read
151  ReadHeader, //< the next step is to read the header
152  ReadMsgBody, //< the next step is to read the body
153  ReadDone //< we are done
154  };
155 
156  //------------------------------------------------------------------------
157  // Current read stage
158  //------------------------------------------------------------------------
159  Stage readstage;
160 
161  //------------------------------------------------------------------------
162  // The context of the read operation
163  //------------------------------------------------------------------------
164  TransportHandler &xrdTransport;
165  Socket &socket;
166  const std::string &strmname;
167  Stream &strm;
168  uint16_t substrmnb;
169 
170  //------------------------------------------------------------------------
171  // The internal state of the the reader
172  //------------------------------------------------------------------------
173  std::unique_ptr<Message> inmsg;
174  };
175 }
176 
177 #endif /* SRC_XRDCL_XRDCLASYNCHSREADER_HH_ */
Utility class encapsulating reading hand-shake response logic.
void Reset()
Reset the state of the object (makes it ready to read out next msg)
std::unique_ptr< Message > ReleaseMsg()
Transfer the received message ownership.
AsyncHSReader(TransportHandler &xrdTransport, Socket &socket, const std::string &strmname, Stream &strm, uint16_t substrmnb)
XRootDStatus Read()
Read out the response from the socket.
static Log * GetLog()
Get default log.
Handle diagnostics.
Definition: XrdClLog.hh:101
void Dump(uint64_t topic, const char *format,...)
Print a dump message.
Definition: XrdClLog.cc:299
The message representation used throughout the system.
Definition: XrdClMessage.hh:32
A network socket.
Definition: XrdClSocket.hh:43
Perform the handshake and the authentication for each physical stream.
virtual XRootDStatus GetBody(Message &message, Socket *socket)=0
virtual XRootDStatus GetHeader(Message &message, Socket *socket)=0
const uint16_t suRetry
Definition: XrdClStatus.hh:40
const uint64_t AsyncSockMsg
uint16_t code
Error type, or additional hints on what to do.
Definition: XrdClStatus.hh:147
bool IsOK() const
We're fine.
Definition: XrdClStatus.hh:124