CCF
Loading...
Searching...
No Matches
before_io.h
Go to the documentation of this file.
1// Copyright (c) Microsoft Corporation. All rights reserved.
2// Licensed under the Apache 2.0 License.
3#pragma once
4
5#include "ccf/ds/logger.h"
6#include "proxy.h"
7
8namespace asynchost
9{
10 template <typename Behaviour>
11 class BeforeIO : public with_uv_handle<uv_prepare_t>
12 {
13 private:
14 friend class close_ptr<BeforeIO<Behaviour>>;
15 Behaviour behaviour;
16
17 template <typename... Args>
18 BeforeIO(Args&&... args) : behaviour(std::forward<Args>(args)...)
19 {
20 int rc;
21
22 if ((rc = uv_prepare_init(uv_default_loop(), &uv_handle)) < 0)
23 {
24 LOG_FAIL_FMT("uv_prepare_init failed: {}", uv_strerror(rc));
25 throw std::logic_error("uv_prepare_init failed");
26 }
27
28 uv_handle.data = this;
29
30 if ((rc = uv_prepare_start(&uv_handle, on_prepare)) < 0)
31 {
32 LOG_FAIL_FMT("uv_prepare_start failed: {}", uv_strerror(rc));
33 throw std::logic_error("uv_prepare_start failed");
34 }
35 }
36
37 static void on_prepare(uv_prepare_t* handle)
38 {
39 static_cast<BeforeIO*>(handle->data)->on_prepare();
40 }
41
42 void on_prepare()
43 {
44 behaviour.before_io();
45 }
46 };
47}
Definition before_io.h:12
Definition proxy.h:15
Definition proxy.h:82
uv_prepare_t uv_handle
Definition proxy.h:84
#define LOG_FAIL_FMT
Definition logger.h:363
Definition after_io.h:8