XRootD
XrdClAsyncRawReaderIntfc.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_XRDCLASYNCRAWREADERINTFC_HH_
20 #define SRC_XRDCL_XRDCLASYNCRAWREADERINTFC_HH_
21 
23 #include "XrdCl/XrdClSocket.hh"
24 #include "XrdCl/XrdClDefaultEnv.hh"
25 #include "XrdCl/XrdClLog.hh"
26 #include "XrdCl/XrdClConstants.hh"
27 
28 namespace XrdCl
29 {
30 
31  //----------------------------------------------------------------------------
33  //----------------------------------------------------------------------------
35  {
36  public:
37 
40  url( url ),
41  request( request ),
42  chunks( nullptr ),
43  dlen( 0 ),
44  msgbtsrd( 0 ),
45  rawbtsrd( 0 ),
46  chidx( 0 ),
47  choff( 0 ),
48  chlen( 0 ),
49  dataerr( false )
50  {
51  }
52 
53  //------------------------------------------------------------------------
55  //------------------------------------------------------------------------
57  {
58  }
59 
60  //------------------------------------------------------------------------
62  //------------------------------------------------------------------------
63  void SetDataLength( int dlen )
64  {
65  this->dlen = dlen;
66  this->readstage = ReadStart;
67  }
68 
69  //------------------------------------------------------------------------
71  //------------------------------------------------------------------------
73  {
74  this->chunks = chunks;
75  if( chunks )
76  this->chstatus.resize( chunks->size() );
77  }
78 
79  //------------------------------------------------------------------------
85  //------------------------------------------------------------------------
86  virtual XRootDStatus Read( Socket &socket, uint32_t &btsret ) = 0;
87 
88  //------------------------------------------------------------------------
90  //------------------------------------------------------------------------
91  virtual XRootDStatus GetResponse( AnyObject *&response ) = 0;
92 
93  protected:
94 
95  //--------------------------------------------------------------------------
96  // Read a buffer asynchronously
97  //--------------------------------------------------------------------------
99  char *buffer,
100  uint32_t toBeRead,
101  uint32_t &bytesRead )
102  {
103  size_t shift = 0;
104  while( toBeRead > 0 )
105  {
106  int btsRead = 0;
107  Status status = socket.Read( buffer + shift, toBeRead, btsRead );
108 
109  if( !status.IsOK() || status.code == suRetry )
110  return status;
111 
112  bytesRead += btsRead;
113  toBeRead -= btsRead;
114  shift += btsRead;
115  }
116  return XRootDStatus( stOK, suDone );
117  }
118 
119  //------------------------------------------------------------------------
120  // Helper struct for async reading of chunks
121  //------------------------------------------------------------------------
122  struct ChunkStatus
123  {
124  ChunkStatus(): sizeerr( false ), done( false ) {}
125  bool sizeerr;
126  bool done;
127  };
128 
129  //------------------------------------------------------------------------
130  // internal buffer type
131  //------------------------------------------------------------------------
132  using buffer_t = std::vector<char>;
133 
134  //------------------------------------------------------------------------
136  //------------------------------------------------------------------------
137  enum Stage
138  {
139  ReadStart, //< the next step is to initialize the read
140  ReadRdLst, //< the next step is to read the read_list
141  ReadRaw, //< the next step is to read the raw data
142  ReadDiscard, //< there was an error, we are in discard mode
143  ReadDone //< the next step is to finalize the read
144  };
145 
146  //------------------------------------------------------------------------
147  // Current read stage
148  //------------------------------------------------------------------------
150 
151  //------------------------------------------------------------------------
152  // The context of the read operation
153  //------------------------------------------------------------------------
154  const URL &url; //< for logging purposes
155  const Message &request; //< client request
156 
157  ChunkList *chunks; //< list of data chunks to be filled with user data
158  std::vector<ChunkStatus> chstatus; //< status per chunk
159  uint32_t dlen; //< size of the data in the message
160  uint32_t msgbtsrd; //< number of bytes read out from the socket for the current message
161  uint32_t rawbtsrd; //< total number of bytes read out from the socket (raw data only)
162 
163  size_t chidx; //< index of the current data buffer
164  size_t choff; //< offset within the current buffer
165  size_t chlen; //< bytes left to be readout into the current chunk
166 
167  buffer_t discardbuff; //< buffer for discarding data in case of an error
168  bool dataerr; //< true if the server send us too much data, false otherwise
169  };
170 
171 }
172 
173 #endif /* SRC_XRDCL_XRDCLASYNCRAWREADERINTFC_HH_ */
Base class for any message's body reader.
virtual XRootDStatus GetResponse(AnyObject *&response)=0
Get the response.
std::vector< ChunkStatus > chstatus
XRootDStatus ReadBytesAsync(Socket &socket, char *buffer, uint32_t toBeRead, uint32_t &bytesRead)
void SetDataLength(int dlen)
Sets response data length.
virtual XRootDStatus Read(Socket &socket, uint32_t &btsret)=0
AsyncRawReaderIntfc(const URL &url, const Message &request)
Stage
Stages of reading out a response from the socket.
virtual ~AsyncRawReaderIntfc()
Destructor.
void SetChunkList(ChunkList *chunks)
Sets the chunk list with user buffers.
The message representation used throughout the system.
Definition: XrdClMessage.hh:32
A network socket.
Definition: XrdClSocket.hh:43
virtual XRootDStatus Read(char *buffer, size_t size, int &bytesRead)
Definition: XrdClSocket.cc:740
URL representation.
Definition: XrdClURL.hh:31
const uint16_t suRetry
Definition: XrdClStatus.hh:40
const uint16_t stOK
Everything went OK.
Definition: XrdClStatus.hh:31
std::vector< ChunkInfo > ChunkList
List of chunks.
const uint16_t suDone
Definition: XrdClStatus.hh:38
Procedure execution status.
Definition: XrdClStatus.hh:115
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