Apache Mesos
libwinio.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 __LIBWINIO_HPP__
14 #define __LIBWINIO_HPP__
15 
16 #include <stout/lambda.hpp>
17 #include <stout/try.hpp>
18 #include <stout/windows.hpp>
19 
20 #include <stout/os/int_fd.hpp>
21 
22 #include <process/address.hpp>
23 #include <process/future.hpp>
24 
25 namespace process {
26 namespace windows {
27 
28 class EventLoop
29 {
30 public:
31  static Try<EventLoop*> create();
32 
33  // Run the event loop forever.
34  Try<Nothing> run();
35 
36  // Signal the event loop to stop.
38 
39  // Timer event that calls the callback after the timer has fired.
41  const Duration& duration, const lambda::function<void()>& callback);
42 
43  // Register a handle for async IO.
45 
46 private:
47  EventLoop(HANDLE iocp_handle);
48 
49  // Custom Deleter for a RAII handle.
50  struct HandleDeleter
51  {
52  // `HANDLE` satisfies the C++ `NullablePointer` interface, since it's
53  // a literal pointer. So we can use the `HANDLE` directly instead of
54  // using a pointer to the `HANDLE`.
55  // See http://en.cppreference.com/w/cpp/memory/unique_ptr for more info.
56  typedef HANDLE pointer;
57  void operator()(HANDLE h) { ::CloseHandle(h); }
58  };
59 
60  std::unique_ptr<HANDLE, HandleDeleter> iocp_handle_;
61 };
62 
63 
64 // All of these functions do an asynchronous IO operation. The returned future
65 // can be discarded to cancel the operation.
66 Future<size_t> read(const int_fd& fd, void* buf, size_t size);
67 
68 Future<size_t> write(const int_fd& fd, const void* buf, size_t size);
69 
70 // Socket only functions.
71 Future<size_t> recv(const int_fd& fd, void* buf, size_t size);
72 
73 Future<size_t> send(const int_fd& fd, const void* buf, size_t size);
74 
75 Future<Nothing> accept(const int_fd& fd, const int_fd& accepted_socket);
76 
78 
80  const int_fd& fd, const int_fd& file_fd, off_t offset, size_t size);
81 
82 } // namespace windows {
83 } // namespace process {
84 
85 #endif // __LIBWINIO_HPP__
Try< Bytes > size(const std::string &path, const FollowSymlink follow=FollowSymlink::FOLLOW_SYMLINK)
Definition: stat.hpp:130
Definition: check.hpp:33
Future< Nothing > accept(const int_fd &fd, const int_fd &accepted_socket)
Try< Nothing > registerHandle(const int_fd &fd)
Definition: address.hpp:324
Future< size_t > sendfile(const int_fd &fd, const int_fd &file_fd, off_t offset, size_t size)
Definition: sendfile.hpp:58
Future< size_t > recv(const int_fd &fd, void *buf, size_t size)
static Try< EventLoop * > create()
Definition: duration.hpp:32
Future< size_t > write(const int_fd &fd, const void *buf, size_t size)
Definition: write.hpp:72
Try< Nothing > launchTimer(const Duration &duration, const lambda::function< void()> &callback)
Future< size_t > read(const int_fd &fd, void *buf, size_t size)
Definition: read.hpp:83
constexpr Handle HANDLE
Definition: ingress.hpp:37
Definition: executor.hpp:48
network::inet::Address address()
Returns the socket address associated with this instance of the library.
Definition: libwinio.hpp:28
int int_fd
Definition: int_fd.hpp:35
Future< Nothing > connect(const int_fd &fd, const network::Address &address)
Future< size_t > send(const int_fd &fd, const void *buf, size_t size)