Apache Mesos
sendfile.hpp
Go to the documentation of this file.
1 // Licensed under the Apache License, Version 2.0 (the "License");
2 // you may not use this file except in compliance with the License.
3 // You may obtain a copy of the License at
4 //
5 // http://www.apache.org/licenses/LICENSE-2.0
6 //
7 // Unless required by applicable law or agreed to in writing, software
8 // distributed under the License is distributed on an "AS IS" BASIS,
9 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10 // See the License for the specific language governing permissions and
11 // limitations under the License.
12 
13 #ifndef __STOUT_OS_WINDOWS_SENDFILE_HPP__
14 #define __STOUT_OS_WINDOWS_SENDFILE_HPP__
15 
16 #include <errno.h>
17 
18 #include <stout/error.hpp>
19 #include <stout/try.hpp>
20 #include <stout/windows.hpp> // For `winioctl.h`.
21 
22 #include <stout/os/windows/fd.hpp>
23 
24 namespace os {
25 
26 // Returns the amount of bytes written from the input file
27 // descriptor to the output socket.
28 // On error, `Try<ssize_t, SocketError>` contains the error.
30  const WindowsFD& s, const WindowsFD& fd, off_t offset, size_t length)
31 {
32  // NOTE: We convert the `offset` here to avoid potential data loss
33  // in the type casting and bitshifting below.
34  uint64_t offset_ = offset;
35 
36  OVERLAPPED from = {
37  0,
38  0,
39  {static_cast<DWORD>(offset_), static_cast<DWORD>(offset_ >> 32)},
40  nullptr};
41 
42  CHECK_LE(length, MAXDWORD);
43  if (TransmitFile(
44  s,
45  fd,
46  static_cast<DWORD>(length),
47  0,
48  &from,
49  nullptr,
50  0) == FALSE &&
51  (WSAGetLastError() == WSA_IO_PENDING ||
52  WSAGetLastError() == ERROR_IO_PENDING)) {
53  DWORD sent = 0;
54  DWORD flags = 0;
55 
56  if (WSAGetOverlappedResult(s, &from, &sent, TRUE, &flags) == TRUE) {
57  return sent;
58  }
59  }
60 
61  return SocketError();
62 }
63 
64 } // namespace os {
65 
66 #endif // __STOUT_OS_WINDOWS_SENDFILE_HPP__
Definition: fd.hpp:47
Definition: try.hpp:34
ErrnoError SocketError
Definition: error.hpp:33
#define flags
Definition: decoder.hpp:18
Try< ssize_t, SocketError > sendfile(int s, int fd, off_t offset, size_t length)
Definition: sendfile.hpp:42