this->server = std::make_unique<Server>(path, dispatcher);
}
- inline void start(unsigned int timeout, Timer::Predicate condition)
+ inline void start()
{
- this->server->run(timeout, condition);
+ this->server->run();
}
inline void stop()
Gateway::~Gateway() = default;
-void Gateway::start(unsigned int timeout, Timer::Predicate condition)
+void Gateway::start()
{
- this->pImpl->start(timeout, condition);
+ this->pImpl->start();
}
void Gateway::stop(void)
#define EXPOSE(gateway, object, function) gateway.expose(object, #function, function)
#include <vist/klass/functor.hpp>
-#include <vist/timer.hpp>
#include <memory>
#include <string>
Gateway(Gateway&&) = default;
Gateway& operator=(Gateway&&) = default;
- void start(unsigned int timeout = 0, Timer::Predicate condition = nullptr);
- void stop(void);
+ void start();
+ void stop();
template<typename O, typename F>
void expose(O& object, const std::string& name, F&& func);
return Protocol::Recv(socket);
}
-void Protocol::Async::dispatch(const Task& task, std::atomic<bool>& polling)
+void Protocol::Async::dispatch(const Task& task)
{
- polling = true;
auto self = shared_from_this();
const auto& header = boost::asio::buffer(&this->message.header,
sizeof(Message::Header));
- auto handler = [self, task, &polling](const auto& error, std::size_t size) {
+ auto handler = [self, task](const auto& error, std::size_t size) {
if (error) {
if (error == boost::asio::error::eof) {
DEBUG(VIST) << "Socket EoF event occured.";
<< readen << ", " << self->message.size();
self->message.disclose(self->message.signature);
- self->process(task, polling);
+ self->process(task);
};
boost::asio::async_read(self->socket, header, handler);
}
-void Protocol::Async::process(const Task& task, std::atomic<bool>& polling)
+void Protocol::Async::process(const Task& task)
{
bool raised = false;
std::string errMsg;
auto self = shared_from_this();
const auto& headerBuffer = boost::asio::buffer(&this->message.header,
sizeof(Message::Header));
- auto handler = [self, task, &polling](const auto& error, std::size_t size) {
+ auto handler = [self, task](const auto& error, std::size_t size) {
if (error || size != sizeof(Message::Header))
THROW(ErrCode::ProtocolBroken) << "Failed to send message header: "
<< error.message();
THROW(ErrCode::ProtocolBroken) << "Failed to send message content.";
/// Re-dispatch for next request.
- self->dispatch(task, polling);
+ self->dispatch(task);
};
boost::asio::async_write(self->socket, headerBuffer, handler);
#include <vist/rmi/message.hpp>
-#include <atomic>
#include <functional>
#include <boost/asio.hpp>
class Async : public std::enable_shared_from_this<Async> {
public:
explicit Async(Context& context) : socket(context) {}
- void dispatch(const Task& task, std::atomic<bool>& polling);
- void process(const Task& task, std::atomic<bool>& polling);
+ void dispatch(const Task& task);
+ void process(const Task& task);
inline Socket& getSocket()
{
#include "protocol.hpp"
-#include <vist/timer.hpp>
#include <vist/exception.hpp>
#include <vist/logger.hpp>
-#include <atomic>
#include <chrono>
#include <memory>
#include <thread>
class Server {
public:
- Server(const std::string& path, const Protocol::Task& task) : polling(false)
+ Server(const std::string& path, const Protocol::Task& task)
{
errno = 0;
if (::unlink(path.c_str()) == -1 && errno != ENOENT)
if (error)
THROW(ErrCode::RuntimeError) << error.message();
- asyncSession->dispatch(task, this->polling);
+ asyncSession->dispatch(task);
this->accept(task);
};
this->acceptor->async_accept(asyncSession->getSocket(), handler);
}
- inline void run(unsigned int timeout = 0, Timer::Predicate condition = nullptr)
+ inline void run()
{
- if (timeout > 0) {
- auto stopper = [this]() {
- INFO(VIST) << "There are no sessions. And timeout is occured.";
- this->context.stop();
- };
-
- auto wrapper = [this, condition]() -> bool {
- if (condition)
- return condition() && polling == false;
-
- return polling == false;
- };
-
- Timer::ExecOnce(stopper, wrapper, timeout);
- }
-
this->context.run();
}
private:
Protocol::Context context;
std::unique_ptr<Protocol::Acceptor> acceptor;
-
- /// check for session is maintained
- std::atomic<bool> polling;
};
} // namespace impl
serverThread.join();
}
-/* example without polling memeber variable
TEST(ProtocolTests, async_server_sync_client)
{
std::string sockPath = "vist-test.sock";
if (serverThread.joinable())
serverThread.join();
}
-*/