Unverified Commit c9ddf189 authored by Dennis Jenkins's avatar Dennis Jenkins Committed by GitHub

Merge pull request #321 from knowledge4igor/code_improvements_5

Several code improvements
parents 8e612353 0533ed57
......@@ -170,8 +170,8 @@ public:
typedef std::function<void()> OnResponseParsed;
void onReady(const Aio::FdSet& fds);
void registerPoller(Polling::Epoll& poller);
void onReady(const Aio::FdSet& fds) override;
void registerPoller(Polling::Epoll& poller) override;
Async::Promise<void>
asyncConnect(const std::shared_ptr<Connection>& connection, const struct sockaddr* address, socklen_t addr_len);
......
......@@ -10,20 +10,17 @@
#pragma once
#include <thread>
#include <mutex>
#include <atomic>
#include <memory>
#include <unordered_map>
#include <sys/time.h>
#include <sys/resource.h>
#include <pistache/flags.h>
#include <pistache/os.h>
#include <pistache/net.h>
#include <pistache/prototype.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <thread>
#include <memory>
#include <vector>
namespace Pistache {
namespace Aio {
......@@ -58,8 +55,8 @@ public:
Polling::Tag getTag() const { return this->tag; }
};
typedef std::vector<Entry>::iterator iterator;
typedef std::vector<Entry>::const_iterator const_iterator;
using iterator = std::vector<Entry>::iterator;
using const_iterator = std::vector<Entry>::const_iterator;
size_t size() const {
return events_.size();
......@@ -171,7 +168,7 @@ public:
class SyncContext : public ExecutionContext {
public:
Reactor::Impl* makeImpl(Reactor* reactor) const;
Reactor::Impl* makeImpl(Reactor* reactor) const override;
};
class AsyncContext : public ExecutionContext {
......@@ -180,7 +177,7 @@ public:
: threads_(threads)
{ }
Reactor::Impl* makeImpl(Reactor* reactor) const;
Reactor::Impl* makeImpl(Reactor* reactor) const override;
static AsyncContext singleThreaded();
......@@ -194,6 +191,12 @@ public:
friend class SyncImpl;
friend class AsyncImpl;
Handler()
: reactor_(nullptr)
, context_()
, key_()
{ }
struct Context {
friend class SyncImpl;
......@@ -204,7 +207,7 @@ public:
};
virtual void onReady(const FdSet& fds) = 0;
virtual void registerPoller(Polling::Epoll& poller) { UNUSED(poller) }
virtual void registerPoller(Polling::Epoll& /*poller*/) { }
Reactor* reactor() const {
return reactor_;
......
......@@ -12,7 +12,10 @@
#include <pistache/async.h>
#include <pistache/stream.h>
#include <chrono>
#include <deque>
#include <memory>
#include <unordered_map>
namespace Pistache {
namespace Tcp {
......@@ -26,10 +29,10 @@ public:
void init(const std::shared_ptr<Tcp::Handler>& handler);
void registerPoller(Polling::Epoll& poller);
void registerPoller(Polling::Epoll& poller) override;
void handleNewPeer(const std::shared_ptr<Peer>& peer);
void onReady(const Aio::FdSet& fds);
void onReady(const Aio::FdSet& fds) override;
template<typename Buf>
Async::Promise<ssize_t> asyncWrite(Fd fd, const Buf& buffer, int flags = 0) {
......
......@@ -5,7 +5,13 @@
*/
#include <pistache/reactor.h>
#include <array>
#include <atomic>
#include <memory>
#include <mutex>
#include <unordered_map>
#include <vector>
namespace Pistache {
namespace Aio {
......@@ -69,7 +75,7 @@ public:
}
Reactor::Key addHandler(
const std::shared_ptr<Handler>& handler, bool setKey = true) {
const std::shared_ptr<Handler>& handler, bool setKey = true) override {
handler->registerPoller(poller);
......@@ -85,7 +91,7 @@ public:
return handlers_[key.data()];
}
std::vector<std::shared_ptr<Handler>> handlers(const Reactor::Key& key) const {
std::vector<std::shared_ptr<Handler>> handlers(const Reactor::Key& key) const override {
std::vector<std::shared_ptr<Handler>> res;
res.push_back(handler(key));
......@@ -97,7 +103,7 @@ public:
Fd fd,
Polling::NotifyOn interest,
Polling::Tag tag,
Polling::Mode mode = Polling::Mode::Level) {
Polling::Mode mode = Polling::Mode::Level) override {
auto pollTag = encodeTag(key, tag);
poller.addFd(fd, interest, pollTag, mode);
......@@ -108,7 +114,7 @@ public:
Fd fd,
Polling::NotifyOn interest,
Polling::Tag tag,
Polling::Mode mode = Polling::Mode::Level) {
Polling::Mode mode = Polling::Mode::Level) override {
auto pollTag = encodeTag(key, tag);
poller.addFdOneShot(fd, interest, pollTag, mode);
......@@ -119,13 +125,13 @@ public:
Fd fd,
Polling::NotifyOn interest,
Polling::Tag tag,
Polling::Mode mode = Polling::Mode::Level) {
Polling::Mode mode = Polling::Mode::Level) override {
auto pollTag = encodeTag(key, tag);
poller.rearmFd(fd, interest, pollTag, mode);
}
void runOnce() {
void runOnce() override {
if (handlers_.empty())
throw std::runtime_error("You need to set at least one handler");
......@@ -147,7 +153,7 @@ public:
}
}
void run() {
void run() override {
handlers_.forEachHandler([](const std::shared_ptr<Handler> handler) {
handler->context_.tid = std::this_thread::get_id();
});
......@@ -156,7 +162,7 @@ public:
runOnce();
}
void shutdown() {
void shutdown() override {
shutdown_.store(true);
shutdownFd.notify();
}
......@@ -341,7 +347,7 @@ public:
}
Reactor::Key addHandler(
const std::shared_ptr<Handler>& handler, bool) {
const std::shared_ptr<Handler>& handler, bool) override {
Reactor::Key keys[SyncImpl::MaxHandlers()];
......@@ -361,7 +367,7 @@ public:
return Reactor::Key(data);
}
std::vector<std::shared_ptr<Handler>> handlers(const Reactor::Key& key) const {
std::vector<std::shared_ptr<Handler>> handlers(const Reactor::Key& key) const override {
uint32_t idx;
uint32_t marker;
......@@ -385,7 +391,7 @@ public:
Fd fd,
Polling::NotifyOn interest,
Polling::Tag tag,
Polling::Mode mode = Polling::Mode::Level) {
Polling::Mode mode = Polling::Mode::Level) override {
dispatchCall(key, &SyncImpl::registerFd, fd, interest, tag, mode);
}
......@@ -394,7 +400,7 @@ public:
Fd fd,
Polling::NotifyOn interest,
Polling::Tag tag,
Polling::Mode mode = Polling::Mode::Level) {
Polling::Mode mode = Polling::Mode::Level) override {
dispatchCall(key, &SyncImpl::registerFdOneShot, fd, interest, tag, mode);
}
......@@ -403,19 +409,19 @@ public:
Fd fd,
Polling::NotifyOn interest,
Polling::Tag tag,
Polling::Mode mode = Polling::Mode::Level) {
Polling::Mode mode = Polling::Mode::Level) override {
dispatchCall(key, &SyncImpl::modifyFd, fd, interest, tag, mode);
}
void runOnce() {
void runOnce() override {
}
void run() {
void run() override {
for (auto& wrk: workers_)
wrk->run();
}
void shutdown() {
void shutdown() override {
for (auto& wrk: workers_)
wrk->shutdown();
}
......@@ -455,21 +461,21 @@ private:
}
~Worker() {
if (thread)
thread->join();
if (thread.joinable())
thread.join();
}
void run() {
thread.reset(new std::thread([=]() {
thread = std::thread([=]() {
sync->run();
}));
});
}
void shutdown() {
sync->shutdown();
}
std::unique_ptr<std::thread> thread;
std::thread thread;
std::unique_ptr<SyncImpl> sync;
};
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment