libfilezilla
async_pipe.hpp
1 #ifndef LIBFILEZILLA_GLUE_ASYNC_PIPE_HEADER
2 #define LIBFILEZILLA_GLUE_ASYNC_PIPE_HEADER
3 
4 #include "../libfilezilla.hpp"
5 
6 #ifdef FZ_WINDOWS
7 
8 #include "../buffer.hpp"
9 #include "../event.hpp"
10 #include "../fsresult.hpp"
11 #include "../thread_pool.hpp"
12 
13 #include "windows.hpp"
14 
15 namespace fz {
16 
17 class event_handler;
18 
19 
24 enum class pipe_event_flag
25 {
27  read = 0x1,
28 
30  write = 0x2,
31 };
32 
34 struct pipe_event_type;
35 
36 class async_pipe;
37 
52 
53 
54 class FZ_PUBLIC_SYMBOL async_pipe final
55 {
56 public:
57  // Connect a named pipe to a pipe server
59  async_pipe(thread_pool & pool, event_handler& h, HANDLE read_pipe, HANDLE write_pipe);
60  async_pipe(thread_pool & pool, event_handler& h, std::wstring_view name);
61  ~async_pipe();
62 
63  async_pipe(async_pipe const&) = delete;
64  async_pipe& operator=(async_pipe const&) = delete;
65 
66  bool connect_named_pipe(std::wstring_view name);
67 
68  bool valid() const;
69 
70  void reset();
71 
78  rwresult read(void* buffer, size_t len);
79 
85  rwresult write(void const* buffer, size_t len);
86 
87  inline rwresult write(std::string_view const& s) {
88  return write(s.data(), s.size());
89  }
90 
91 private:
92  bool init();
93  void thread_entry();
94  thread_pool & pool_;
95  event_handler & handler_;
96 
97  mutex mutex_{false};
98  async_task task_;
99  buffer read_buffer_;
100  buffer write_buffer_;
101  HANDLE sync_{INVALID_HANDLE_VALUE};
102  OVERLAPPED ol_read_{};
103  OVERLAPPED ol_write_{};
104  rwresult write_error_{0};
105  bool waiting_read_{true};
106  bool waiting_write_{};
107  bool quit_{};
108 
109  HANDLE read_{INVALID_HANDLE_VALUE};
110  HANDLE write_{INVALID_HANDLE_VALUE};
111 };
112 
113 }
114 
115 #else
116 #error This file is for Windows only
117 #endif
118 
119 #endif
Data has become available.
pipe_event_flag
The type of a pipe event.
Definition: async_pipe.hpp:24
data can be written.
Definition: event_handler.hpp:60
Handle for asynchronous tasks.
Definition: thread_pool.hpp:24
This is the recommended event class.
Definition: event.hpp:67
Holds the result of read/write operations.
Definition: fsresult.hpp:79
Definition: async_pipe.hpp:54
simple_event< pipe_event_type, async_pipe *, pipe_event_flag > pipe_event
Definition: async_pipe.hpp:36
The namespace used by libfilezilla.
Definition: apply.hpp:17
data can be written.
Data has become available.
The buffer class is a simple buffer where data can be appended at the end and consumed at the front...
Definition: buffer.hpp:26
Lean replacement for std::(recursive_)mutex.
Definition: mutex.hpp:74
A dumb thread-pool for asynchronous tasks.
Definition: thread_pool.hpp:63