Commit 0e8f6c8b authored by knowledge4igor's avatar knowledge4igor

Several code improvements

parent 8cbb750f
...@@ -170,7 +170,7 @@ public: ...@@ -170,7 +170,7 @@ public:
typedef std::function<void()> OnResponseParsed; typedef std::function<void()> OnResponseParsed;
void onReady(const Aio::FdSet& fds); void onReady(const Aio::FdSet& fds) override;
void registerPoller(Polling::Epoll& poller); void registerPoller(Polling::Epoll& poller);
Async::Promise<void> Async::Promise<void>
......
...@@ -10,20 +10,17 @@ ...@@ -10,20 +10,17 @@
#pragma once #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/flags.h>
#include <pistache/os.h> #include <pistache/os.h>
#include <pistache/net.h> #include <pistache/net.h>
#include <pistache/prototype.h> #include <pistache/prototype.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <thread>
#include <memory>
#include <vector>
namespace Pistache { namespace Pistache {
namespace Aio { namespace Aio {
...@@ -58,8 +55,8 @@ public: ...@@ -58,8 +55,8 @@ public:
Polling::Tag getTag() const { return this->tag; } Polling::Tag getTag() const { return this->tag; }
}; };
typedef std::vector<Entry>::iterator iterator; using iterator = std::vector<Entry>::iterator;
typedef std::vector<Entry>::const_iterator const_iterator; using const_iterator = std::vector<Entry>::const_iterator;
size_t size() const { size_t size() const {
return events_.size(); return events_.size();
...@@ -171,7 +168,7 @@ public: ...@@ -171,7 +168,7 @@ public:
class SyncContext : public ExecutionContext { class SyncContext : public ExecutionContext {
public: public:
Reactor::Impl* makeImpl(Reactor* reactor) const; Reactor::Impl* makeImpl(Reactor* reactor) const override;
}; };
class AsyncContext : public ExecutionContext { class AsyncContext : public ExecutionContext {
...@@ -180,7 +177,7 @@ public: ...@@ -180,7 +177,7 @@ public:
: threads_(threads) : threads_(threads)
{ } { }
Reactor::Impl* makeImpl(Reactor* reactor) const; Reactor::Impl* makeImpl(Reactor* reactor) const override;
static AsyncContext singleThreaded(); static AsyncContext singleThreaded();
...@@ -194,6 +191,12 @@ public: ...@@ -194,6 +191,12 @@ public:
friend class SyncImpl; friend class SyncImpl;
friend class AsyncImpl; friend class AsyncImpl;
Handler()
: reactor_(nullptr)
, context_()
, key_()
{ }
struct Context { struct Context {
friend class SyncImpl; friend class SyncImpl;
...@@ -204,7 +207,7 @@ public: ...@@ -204,7 +207,7 @@ public:
}; };
virtual void onReady(const FdSet& fds) = 0; virtual void onReady(const FdSet& fds) = 0;
virtual void registerPoller(Polling::Epoll& poller) { UNUSED(poller) } virtual void registerPoller(Polling::Epoll& /*poller*/) { }
Reactor* reactor() const { Reactor* reactor() const {
return reactor_; return reactor_;
......
...@@ -29,7 +29,7 @@ public: ...@@ -29,7 +29,7 @@ public:
void registerPoller(Polling::Epoll& poller); void registerPoller(Polling::Epoll& poller);
void handleNewPeer(const std::shared_ptr<Peer>& peer); void handleNewPeer(const std::shared_ptr<Peer>& peer);
void onReady(const Aio::FdSet& fds); void onReady(const Aio::FdSet& fds) override;
template<typename Buf> template<typename Buf>
Async::Promise<ssize_t> asyncWrite(Fd fd, const Buf& buffer, int flags = 0) { Async::Promise<ssize_t> asyncWrite(Fd fd, const Buf& buffer, int flags = 0) {
......
...@@ -5,7 +5,13 @@ ...@@ -5,7 +5,13 @@
*/ */
#include <pistache/reactor.h> #include <pistache/reactor.h>
#include <array> #include <array>
#include <atomic>
#include <memory>
#include <mutex>
#include <unordered_map>
#include <vector>
namespace Pistache { namespace Pistache {
namespace Aio { namespace Aio {
...@@ -69,7 +75,7 @@ public: ...@@ -69,7 +75,7 @@ public:
} }
Reactor::Key addHandler( 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); handler->registerPoller(poller);
...@@ -85,7 +91,7 @@ public: ...@@ -85,7 +91,7 @@ public:
return handlers_[key.data()]; 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; std::vector<std::shared_ptr<Handler>> res;
res.push_back(handler(key)); res.push_back(handler(key));
...@@ -97,7 +103,7 @@ public: ...@@ -97,7 +103,7 @@ public:
Fd fd, Fd fd,
Polling::NotifyOn interest, Polling::NotifyOn interest,
Polling::Tag tag, Polling::Tag tag,
Polling::Mode mode = Polling::Mode::Level) { Polling::Mode mode = Polling::Mode::Level) override {
auto pollTag = encodeTag(key, tag); auto pollTag = encodeTag(key, tag);
poller.addFd(fd, interest, pollTag, mode); poller.addFd(fd, interest, pollTag, mode);
...@@ -108,7 +114,7 @@ public: ...@@ -108,7 +114,7 @@ public:
Fd fd, Fd fd,
Polling::NotifyOn interest, Polling::NotifyOn interest,
Polling::Tag tag, Polling::Tag tag,
Polling::Mode mode = Polling::Mode::Level) { Polling::Mode mode = Polling::Mode::Level) override {
auto pollTag = encodeTag(key, tag); auto pollTag = encodeTag(key, tag);
poller.addFdOneShot(fd, interest, pollTag, mode); poller.addFdOneShot(fd, interest, pollTag, mode);
...@@ -119,7 +125,7 @@ public: ...@@ -119,7 +125,7 @@ public:
Fd fd, Fd fd,
Polling::NotifyOn interest, Polling::NotifyOn interest,
Polling::Tag tag, Polling::Tag tag,
Polling::Mode mode = Polling::Mode::Level) { Polling::Mode mode = Polling::Mode::Level) override {
auto pollTag = encodeTag(key, tag); auto pollTag = encodeTag(key, tag);
poller.rearmFd(fd, interest, pollTag, mode); poller.rearmFd(fd, interest, pollTag, mode);
...@@ -341,7 +347,7 @@ public: ...@@ -341,7 +347,7 @@ public:
} }
Reactor::Key addHandler( Reactor::Key addHandler(
const std::shared_ptr<Handler>& handler, bool) { const std::shared_ptr<Handler>& handler, bool) override {
Reactor::Key keys[SyncImpl::MaxHandlers()]; Reactor::Key keys[SyncImpl::MaxHandlers()];
...@@ -361,7 +367,7 @@ public: ...@@ -361,7 +367,7 @@ public:
return Reactor::Key(data); 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 idx;
uint32_t marker; uint32_t marker;
...@@ -385,7 +391,7 @@ public: ...@@ -385,7 +391,7 @@ public:
Fd fd, Fd fd,
Polling::NotifyOn interest, Polling::NotifyOn interest,
Polling::Tag tag, Polling::Tag tag,
Polling::Mode mode = Polling::Mode::Level) { Polling::Mode mode = Polling::Mode::Level) override {
dispatchCall(key, &SyncImpl::registerFd, fd, interest, tag, mode); dispatchCall(key, &SyncImpl::registerFd, fd, interest, tag, mode);
} }
...@@ -394,7 +400,7 @@ public: ...@@ -394,7 +400,7 @@ public:
Fd fd, Fd fd,
Polling::NotifyOn interest, Polling::NotifyOn interest,
Polling::Tag tag, Polling::Tag tag,
Polling::Mode mode = Polling::Mode::Level) { Polling::Mode mode = Polling::Mode::Level) override {
dispatchCall(key, &SyncImpl::registerFdOneShot, fd, interest, tag, mode); dispatchCall(key, &SyncImpl::registerFdOneShot, fd, interest, tag, mode);
} }
...@@ -403,7 +409,7 @@ public: ...@@ -403,7 +409,7 @@ public:
Fd fd, Fd fd,
Polling::NotifyOn interest, Polling::NotifyOn interest,
Polling::Tag tag, Polling::Tag tag,
Polling::Mode mode = Polling::Mode::Level) { Polling::Mode mode = Polling::Mode::Level) override {
dispatchCall(key, &SyncImpl::modifyFd, fd, interest, tag, mode); dispatchCall(key, &SyncImpl::modifyFd, fd, interest, tag, mode);
} }
...@@ -455,21 +461,21 @@ private: ...@@ -455,21 +461,21 @@ private:
} }
~Worker() { ~Worker() {
if (thread) if (thread.joinable())
thread->join(); thread.join();
} }
void run() { void run() {
thread.reset(new std::thread([=]() { thread = std::thread([=]() {
sync->run(); sync->run();
})); });
} }
void shutdown() { void shutdown() {
sync->shutdown(); sync->shutdown();
} }
std::unique_ptr<std::thread> thread; std::thread thread;
std::unique_ptr<SyncImpl> sync; 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