IRSOL
C++ code implementing socket server for interacting with Baumer camera.
base.hpp
Go to the documentation of this file.
1
10#pragma once
11
12#include "irsol/protocol.hpp"
14#include "irsol/traits.hpp"
15
16#include <vector>
17
18namespace irsol {
19namespace server {
20
21namespace handlers {
24
25namespace internal {
26
34template<
35 typename T,
36 std::enable_if_t<irsol::traits::is_type_in_variant<T, irsol::protocol::InMessage>::value, int> =
37 0>
39{
40public:
50 virtual std::vector<out_message_t> operator()(
52 T&& message)
53 {
54 // Retrieve the current session using the client ID
55 auto session = ctx->getSession(clientId);
56 if(!session) {
57 IRSOL_LOG_ERROR("No session found for client {}", clientId);
58 return {};
59 }
60 return process(session, std::move(message));
61 }
62
63protected:
68 HandlerBase(std::shared_ptr<Context> ctx): ctx(ctx){};
69
76 virtual std::vector<out_message_t> process(
77 std::shared_ptr<irsol::server::ClientSession> session,
78 T&& message) = 0;
79
81 std::shared_ptr<Context> ctx;
82};
83}
84
97
98}
99}
100}
Generic handler base class for protocol messages.
Definition base.hpp:39
virtual std::vector< out_message_t > operator()(const irsol::types::client_id_t &clientId, T &&message)
Invokes the handler for a given client and message.
Definition base.hpp:50
virtual std::vector< out_message_t > process(std::shared_ptr< irsol::server::ClientSession > session, T &&message)=0
Processes the protocol message for the given session.
std::shared_ptr< Context > ctx
Handler context (provides access to app and utilities).
Definition base.hpp:81
HandlerBase(std::shared_ptr< Context > ctx)
Constructs the handler base.
Definition base.hpp:68
Declaration of the handler context struct.
#define IRSOL_LOG_ERROR(...)
Logs an error-level message using the default logger.
Definition logging.hpp:94
std::variant< Assignment, Inquiry, Command > InMessage
Variant type representing any incoming message.
Definition variants.hpp:86
std::variant< Success, BinaryDataBuffer, ImageBinaryData, ColorImageBinaryData, Error > OutMessage
Variant type representing any outgoing message.
Definition variants.hpp:149
protocol::OutMessage out_message_t
Definition base.hpp:23
constexpr auto makeHandler(std::shared_ptr< Context > ctx, Args &&... args)
Constructs a handler instance of the given type.
Definition factory.hpp:28
protocol::InMessage in_message_t
Definition base.hpp:22
std::string client_id_t
Represents a unique client identifier. Typically used to identify connected clients by string IDs.
Definition types.hpp:55
Template metaprogramming traits for type introspection in the irsol library.