Program Listing for File Server.hpp

Return to documentation for file (BackendAsyncmsg/Server.hpp)

#ifndef NETIO3BACKEND_BACKENDASYNCMSG_SERVER_HPP
#define NETIO3BACKEND_BACKENDASYNCMSG_SERVER_HPP

#include <functional>
#include <memory>
#include <set>

#include <asyncmsg/Server.h>
#include <asyncmsg/Session.h>

#include "Buffer.hpp"
#include "Types.hpp"
#include "netio3-backend/Netio3Backend.hpp"

namespace netio3::asyncmsg {
  class Server : public felix::asyncmsg::Server
  {
  public:
    explicit Server(boost::asio::io_context& ioContext,
                    CallbacksConfig callbacksConfig,
                    std::function<void(const EndPointAddress&)> onShutdownCb,
                    std::function<void(std::shared_ptr<felix::asyncmsg::Session>)> onOpenedCb,
                    std::function<void(std::shared_ptr<felix::asyncmsg::Session>)> onClosedCb,
                    std::function<void(const ConnectionEvent&)> addEventCb,
                    BaseEventLoop* eventloop,
                    bool useAsioEventLoop,
                    Mode mode,
                    BufferManager* sharedBufferManager,
                    ConnectionParameters connectionParams);

    void startAccept();

    void onAccept(std::shared_ptr<felix::asyncmsg::Session> session) override;

    void onAcceptError(const boost::system::error_code& error,
                       std::shared_ptr<felix::asyncmsg::Session> session) override;

    void shutdown();

  private:
    void onSessionClosed(std::shared_ptr<felix::asyncmsg::Session> session);

    std::reference_wrapper<boost::asio::io_context> m_ioContext;
    CallbacksConfig m_callbacks{};
    std::function<void(const EndPointAddress&)> m_onShutdownCb{};
    std::function<void(std::shared_ptr<felix::asyncmsg::Session>)> m_onOpenedCb{};
    std::function<void(std::shared_ptr<felix::asyncmsg::Session>)> m_onClosedCb{};
    std::function<void(const ConnectionEvent&)> m_addEventCb{};
    BaseEventLoop* m_eventloop{nullptr};
    BufferManager* m_bufferManagerPtr{nullptr};
    std::set<std::shared_ptr<felix::asyncmsg::Session>> m_sessions{};
    bool m_useAsioEventLoop{};
    Mode m_mode{};
    ConnectionParameters m_connectionParams{};
    bool m_shuttingDown{false};
    mutable std::mutex m_mutex{};
  };
}  // namespace netio3::asyncmsg

#endif  // NETIO3BACKEND_BACKENDASYNCMSG_SERVER_HPP