2018-09-11 16:43:43 +02:00
|
|
|
//
|
2022-01-01 01:35:39 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2022
|
2018-09-11 16:43:43 +02:00
|
|
|
//
|
|
|
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
//
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "td/utils/port/config.h"
|
|
|
|
|
|
|
|
#ifdef TD_PORT_WINDOWS
|
|
|
|
|
|
|
|
#include "td/utils/common.h"
|
|
|
|
#include "td/utils/Context.h"
|
|
|
|
#include "td/utils/port/detail/NativeFd.h"
|
|
|
|
#include "td/utils/Status.h"
|
|
|
|
|
2019-07-23 00:50:12 +02:00
|
|
|
#include <memory>
|
|
|
|
|
2018-09-11 16:43:43 +02:00
|
|
|
namespace td {
|
|
|
|
namespace detail {
|
|
|
|
|
2019-07-06 13:29:15 +02:00
|
|
|
class IocpRef;
|
2018-09-11 16:43:43 +02:00
|
|
|
class Iocp final : public Context<Iocp> {
|
|
|
|
public:
|
|
|
|
Iocp() = default;
|
|
|
|
Iocp(const Iocp &) = delete;
|
|
|
|
Iocp &operator=(const Iocp &) = delete;
|
|
|
|
Iocp(Iocp &&) = delete;
|
|
|
|
Iocp &operator=(Iocp &&) = delete;
|
|
|
|
~Iocp();
|
|
|
|
|
|
|
|
class Callback {
|
|
|
|
public:
|
|
|
|
virtual ~Callback() = default;
|
|
|
|
virtual void on_iocp(Result<size_t> r_size, WSAOVERLAPPED *overlapped) = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
void init();
|
|
|
|
void subscribe(const NativeFd &fd, Callback *callback);
|
|
|
|
void post(size_t size, Callback *callback, WSAOVERLAPPED *overlapped);
|
|
|
|
void loop();
|
|
|
|
void interrupt_loop();
|
|
|
|
void clear();
|
|
|
|
|
2019-07-06 13:29:15 +02:00
|
|
|
IocpRef get_ref() const;
|
|
|
|
|
2018-09-11 16:43:43 +02:00
|
|
|
private:
|
2019-07-06 13:29:15 +02:00
|
|
|
std::shared_ptr<NativeFd> iocp_handle_;
|
2018-09-11 16:43:43 +02:00
|
|
|
};
|
|
|
|
|
2019-07-06 13:29:15 +02:00
|
|
|
class IocpRef {
|
|
|
|
public:
|
|
|
|
IocpRef() = default;
|
|
|
|
IocpRef(const Iocp &) = delete;
|
|
|
|
IocpRef &operator=(const Iocp &) = delete;
|
|
|
|
IocpRef(IocpRef &&) = default;
|
|
|
|
IocpRef &operator=(IocpRef &&) = default;
|
|
|
|
|
2019-07-22 06:01:51 +02:00
|
|
|
explicit IocpRef(std::weak_ptr<NativeFd> iocp_handle);
|
2019-07-06 13:29:15 +02:00
|
|
|
|
|
|
|
bool post(size_t size, Iocp::Callback *callback, WSAOVERLAPPED *overlapped);
|
2019-07-21 20:07:07 +02:00
|
|
|
|
2019-07-06 13:29:15 +02:00
|
|
|
private:
|
|
|
|
std::weak_ptr<NativeFd> iocp_handle_;
|
|
|
|
};
|
2019-07-21 20:07:07 +02:00
|
|
|
|
|
|
|
} // namespace detail
|
2018-09-11 16:43:43 +02:00
|
|
|
} // namespace td
|
|
|
|
|
|
|
|
#endif
|