xrootd
XrdTpcState.hh
Go to the documentation of this file.
1 
6 #pragma once
7 
8 #include <memory>
9 #include <vector>
10 
11 // Forward dec'ls
12 class XrdSfsFile;
14 typedef void CURL;
15 struct curl_slist;
16 
17 namespace TPC {
18 class Stream;
19 
20 class State {
21 public:
22 
23  State() :
24  m_push(true),
25  m_recv_status_line(false),
26  m_recv_all_headers(false),
27  m_offset(0),
28  m_start_offset(0),
29  m_status_code(-1),
30  m_error_code(0),
31  m_content_length(-1),
32  m_stream(NULL),
33  m_curl(NULL),
34  m_headers(NULL)
35  {}
36 
37  // Note that we are "borrowing" a reference to the curl handle;
38  // it is not owned / freed by the State object. However, we use it
39  // as if there's only one handle per State.
40  State (off_t start_offset, Stream &stream, CURL *curl, bool push) :
41  m_push(push),
42  m_recv_status_line(false),
43  m_recv_all_headers(false),
44  m_offset(0),
45  m_start_offset(start_offset),
46  m_status_code(-1),
47  m_error_code(0),
48  m_content_length(-1),
49  m_stream(&stream),
50  m_curl(curl),
51  m_headers(NULL)
52  {
53  InstallHandlers(curl);
54  }
55 
56  ~State();
57 
58  void SetTransferParameters(off_t offset, size_t size);
59 
60  void CopyHeaders(XrdHttpExtReq &req);
61 
62  off_t BytesTransferred() const {return m_offset;}
63 
64  off_t GetContentLength() const {return m_content_length;}
65 
66  int GetErrorCode() const {return m_error_code;}
67 
68  void SetErrorCode(int error_code) {m_error_code = error_code;}
69 
70  int GetStatusCode() const {return m_status_code;}
71 
72  std::string GetErrorMessage() const {return m_error_buf;}
73 
74  void SetErrorMessage(const std::string &error_msg) {m_error_buf = error_msg;}
75 
76  void ResetAfterRequest();
77 
78  CURL *GetHandle() const {return m_curl;}
79 
80  int AvailableBuffers() const;
81 
82  void DumpBuffers() const;
83 
84  // Returns true if at least one byte of the response has been received,
85  // but not the entire contents of the response.
87 
88  // Duplicate the current state; all settings are copied over, but those
89  // related to the transient state are reset as if from a constructor.
90  State *Duplicate();
91 
92  // Move the contents of a State object. To be replaced by a move
93  // constructor once C++11 is allowed in XRootD.
94  void Move (State &other);
95 
96  // Flush and finalize a transfer state. Eventually calls close() on the underlying
97  // file handle, which should hopefully synchronize the file metadata across
98  // all readers (even other load-balanced servers on the same distributed file
99  // system).
100  //
101  // Returns true on success; false otherwise. Failures can happen, for example, if
102  // not all buffers have been reordered by the underlying stream.
103  bool Finalize();
104 
105  // Flush the data in memory to disk, even if it may cause unaligned or short
106  // writes. Typically, only done while shutting down the transfer (note some
107  // backends may be unable to handle unaligned writes unless it's the last write).
108  int Flush();
109 
110  // Retrieve the description of the remote connection; is of the form:
111  // tcp:129.93.3.4:1234
112  // tcp:[2600:900:6:1301:268a:7ff:fef6:a590]:2345
113  // This is meant to facilitate the monitoring via the performance markers.
114  std::string GetConnectionDescription();
115 
116 private:
117  bool InstallHandlers(CURL *curl);
118 
119  State(const State&);
120  // Add back once C++11 is available
121  //State(State &&) noexcept;
122 
123  // libcurl callback functions, along with the corresponding class methods.
124  static size_t HeaderCB(char *buffer, size_t size, size_t nitems,
125  void *userdata);
126  int Header(const std::string &header);
127  static size_t WriteCB(void *buffer, size_t size, size_t nitems, void *userdata);
128  ssize_t Write(char *buffer, size_t size);
129  static size_t ReadCB(void *buffer, size_t size, size_t nitems, void *userdata);
130  int Read(char *buffer, size_t size);
131 
132  bool m_push; // whether we are transferring in "push-mode"
133  bool m_recv_status_line; // whether we have received a status line in the response from the remote host.
134  bool m_recv_all_headers; // true if we have seen the end of headers.
135  off_t m_offset; // number of bytes we have received.
136  off_t m_start_offset; // offset where we started in the file.
137  int m_status_code; // status code from HTTP response.
138  int m_error_code; // error code from underlying stream operations.
139  off_t m_content_length; // value of Content-Length header, if we received one.
140  Stream *m_stream; // stream corresponding to this transfer.
141  CURL *m_curl; // libcurl handle
142  struct curl_slist *m_headers; // any headers we set as part of the libcurl request.
143  std::vector<std::string> m_headers_copy; // Copies of custom headers.
144  std::string m_resp_protocol; // Response protocol in the HTTP status line.
145  std::string m_error_buf; // Any error associated with a response.
146 };
147 
148 };
int GetErrorCode() const
Definition: XrdTpcState.hh:66
Definition: XrdTpcStream.hh:22
Definition: XrdTpcState.hh:20
bool InstallHandlers(CURL *curl)
bool m_push
Definition: XrdTpcState.hh:132
int AvailableBuffers() const
ssize_t Write(char *buffer, size_t size)
void CopyHeaders(XrdHttpExtReq &req)
static size_t HeaderCB(char *buffer, size_t size, size_t nitems, void *userdata)
static size_t ReadCB(void *buffer, size_t size, size_t nitems, void *userdata)
bool Finalize()
std::string GetConnectionDescription()
State(off_t start_offset, Stream &stream, CURL *curl, bool push)
Definition: XrdTpcState.hh:40
void ResetAfterRequest()
void SetErrorCode(int error_code)
Definition: XrdTpcState.hh:68
void SetErrorMessage(const std::string &error_msg)
Definition: XrdTpcState.hh:74
State()
Definition: XrdTpcState.hh:23
CURL * m_curl
Definition: XrdTpcState.hh:141
bool BodyTransferInProgress() const
Definition: XrdTpcState.hh:86
std::string GetErrorMessage() const
Definition: XrdTpcState.hh:72
off_t GetContentLength() const
Definition: XrdTpcState.hh:64
void SetTransferParameters(off_t offset, size_t size)
std::string m_error_buf
Definition: XrdTpcState.hh:145
static size_t WriteCB(void *buffer, size_t size, size_t nitems, void *userdata)
Definition: XrdTpcState.hh:17
Stream * m_stream
Definition: XrdTpcState.hh:140
void Move(State &other)
off_t m_offset
Definition: XrdTpcState.hh:135
off_t BytesTransferred() const
Definition: XrdTpcState.hh:62
int m_status_code
Definition: XrdTpcState.hh:137
std::vector< std::string > m_headers_copy
Definition: XrdTpcState.hh:143
bool m_recv_status_line
Definition: XrdTpcState.hh:133
void DumpBuffers() const
std::string m_resp_protocol
Definition: XrdTpcState.hh:144
bool m_recv_all_headers
Definition: XrdTpcState.hh:134
off_t m_content_length
Definition: XrdTpcState.hh:139
int Header(const std::string &header)
State * Duplicate()
Definition: XrdSfsInterface.hh:364
off_t m_start_offset
Definition: XrdTpcState.hh:136
Definition: XrdHttpExtHandler.hh:45
int m_error_code
Definition: XrdTpcState.hh:138
int Read(char *buffer, size_t size)
struct curl_slist * m_headers
Definition: XrdTpcState.hh:142
void CURL
Definition: XrdTpcState.hh:13
int GetStatusCode() const
Definition: XrdTpcState.hh:70
CURL * GetHandle() const
Definition: XrdTpcState.hh:78