2017-05-26 04:48:44 +08:00
|
|
|
/*
|
|
|
|
* AsioReactor.h
|
|
|
|
*
|
|
|
|
* This source file is part of the FoundationDB open source project
|
|
|
|
*
|
|
|
|
* Copyright 2013-2018 Apple Inc. and the FoundationDB project authors
|
2018-02-22 02:25:11 +08:00
|
|
|
*
|
2017-05-26 04:48:44 +08:00
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
2018-02-22 02:25:11 +08:00
|
|
|
*
|
2017-05-26 04:48:44 +08:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2018-02-22 02:25:11 +08:00
|
|
|
*
|
2017-05-26 04:48:44 +08:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef FLOW_ASIOREACTOR_H
|
|
|
|
#define FLOW_ASIOREACTOR_H
|
|
|
|
#pragma once
|
|
|
|
|
2019-05-14 09:39:31 +08:00
|
|
|
#include <boost/asio.hpp>
|
2021-11-19 04:29:16 +08:00
|
|
|
#include <boost/bind/bind.hpp>
|
2019-05-14 09:39:31 +08:00
|
|
|
|
|
|
|
#include "flow/flow.h"
|
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
namespace N2 { // No indent, it's the whole file
|
2017-05-26 04:48:44 +08:00
|
|
|
|
|
|
|
class Net2;
|
|
|
|
class Peer;
|
|
|
|
class Connection;
|
|
|
|
|
|
|
|
class ASIOReactor {
|
|
|
|
public:
|
|
|
|
explicit ASIOReactor(Net2*);
|
|
|
|
|
2019-08-29 05:35:48 +08:00
|
|
|
void sleep(double timeout);
|
|
|
|
void react();
|
2017-05-26 04:48:44 +08:00
|
|
|
|
|
|
|
void wake();
|
2021-03-11 02:06:03 +08:00
|
|
|
|
2017-05-26 04:48:44 +08:00
|
|
|
boost::asio::io_service ios;
|
2021-03-11 02:06:03 +08:00
|
|
|
boost::asio::io_service::work
|
|
|
|
do_not_stop; // Reactor needs to keep running when there is nothing to do until stopped explicitly
|
2017-05-26 04:48:44 +08:00
|
|
|
|
|
|
|
private:
|
|
|
|
Net2* network;
|
|
|
|
boost::asio::deadline_timer firstTimer;
|
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
static void nullWaitHandler(const boost::system::error_code&) {}
|
2017-05-26 04:48:44 +08:00
|
|
|
static void nullCompletionHandler() {}
|
|
|
|
|
|
|
|
#ifdef __linux__
|
|
|
|
class EventFD : public IEventFD {
|
|
|
|
int fd;
|
|
|
|
boost::asio::posix::stream_descriptor sd;
|
|
|
|
int64_t fdVal;
|
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
static void handle_read(Promise<int64_t> p,
|
|
|
|
int64_t* pVal,
|
|
|
|
const boost::system::error_code& ec,
|
|
|
|
std::size_t bytes_transferred) {
|
|
|
|
if (ec)
|
|
|
|
return; // Presumably, the EventFD was destroyed?
|
|
|
|
ASSERT(bytes_transferred == sizeof(*pVal));
|
2017-05-26 04:48:44 +08:00
|
|
|
p.send(*pVal);
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
2020-07-10 06:02:43 +08:00
|
|
|
EventFD(ASIOReactor* reactor) : sd(reactor->ios, open()) {}
|
2021-01-26 09:55:43 +08:00
|
|
|
~EventFD() override {
|
2021-03-11 02:06:03 +08:00
|
|
|
sd.close(); // Also closes the fd, I assume...
|
2017-05-26 04:48:44 +08:00
|
|
|
}
|
2021-01-26 09:55:43 +08:00
|
|
|
int getFD() override { return fd; }
|
|
|
|
Future<int64_t> read() override {
|
2017-05-26 04:48:44 +08:00
|
|
|
Promise<int64_t> p;
|
2021-03-11 02:06:03 +08:00
|
|
|
sd.async_read_some(boost::asio::mutable_buffers_1(&fdVal, sizeof(fdVal)),
|
|
|
|
boost::bind(&EventFD::handle_read,
|
|
|
|
p,
|
|
|
|
&fdVal,
|
|
|
|
boost::asio::placeholders::error,
|
|
|
|
boost::asio::placeholders::bytes_transferred));
|
2017-05-26 04:48:44 +08:00
|
|
|
return p.getFuture();
|
|
|
|
}
|
|
|
|
|
|
|
|
int open() {
|
2019-05-14 09:39:31 +08:00
|
|
|
fd = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC);
|
2021-03-11 02:06:03 +08:00
|
|
|
if (fd < 0) {
|
2018-06-09 04:57:00 +08:00
|
|
|
TraceEvent(SevError, "EventfdError").GetLastError();
|
2017-05-26 04:48:44 +08:00
|
|
|
throw platform_error();
|
|
|
|
}
|
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
public:
|
2021-03-11 02:06:03 +08:00
|
|
|
static IEventFD* getEventFD() { return static_cast<IEventFD*>((void*)g_network->global(INetwork::enEventFD)); }
|
2017-05-26 04:48:44 +08:00
|
|
|
static EventFD* newEventFD(ASIOReactor& reactor) { return new EventFD(&reactor); }
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
} // namespace N2
|
2017-05-26 04:48:44 +08:00
|
|
|
|
|
|
|
#endif
|