17 : m_id(id), m_socket(std::move(sock)), m_app(app)
23 constexpr size_t INITIAL_BUFFER_SIZE = 1024;
24 std::vector<char> buffer(INITIAL_BUFFER_SIZE);
25 std::string messageBuffer;
31 auto readResult =
m_socket.read(buffer.data(), buffer.size());
39 size_t bytesRead = readResult.value();
46 messageBuffer.append(buffer.data(), bytesRead);
49 "Read {} bytes: '{}'",
51 messageBuffer.substr(messageBuffer.size() - bytesRead));
57 if(bytesRead == buffer.size()) {
59 buffer.resize(buffer.size() * 2);
71 for(
auto& message : messages) {
105 while((newlinePos = messageBuffer.find(
'\n')) != std::string::npos) {
107 std::string rawMessage = messageBuffer.substr(0, newlinePos);
110 messageBuffer.erase(0, newlinePos + 1);
126 std::optional<irsol::protocol::InMessage> optionalParsedMessage =
128 if(!optionalParsedMessage) {
141 for(
auto& message :
result) {
150 std::string preparedMessage =
msg;
155 m_id,
"Socket seems to be closed, this is unexpected. Ignoring send request.");
162 }
else if(
result.value() != preparedMessage.size()) {
164 m_id,
"Incomplete message sent: {} of {} bytes",
result.value(), preparedMessage.size());
174 m_id,
"Socket seems to be closed, this is unexpected. Ignoring send request.");
181 }
else if(
result.value() != size) {
Main server application managing client connections and camera streaming.
static std::optional< InMessage > parse(const std::string &line)
Attempts to parse a single protocol input line into a structured InMessage.
static internal::SerializedMessage serialize(OutMessage &&msg)
Serialize an irsol::protocol::OutMessage variant into a serialized protocol message.
Main server application that manages client connections and camera streaming.
const handlers::MessageHandler & messageHandler() const
Accessor for the message handler.
void run()
Starts the client's session logic.
ClientSession(const irsol::types::client_id_t &id, irsol::types::socket_t &&sock, App &app)
Constructs a new ClientSession.
irsol::types::socket_t m_socket
Socket used for communication with the client.
void handleOutMessages(std::vector< protocol::OutMessage > &&messages)
Handles multiple outbound messages to the client.
void processInMessageBuffer(std::string &messageBuffer)
Processes accumulated raw data into complete protocol messages.
irsol::types::client_id_t m_id
Unique ID identifying this session (maps to client_id_t).
void handleOutMessage(protocol::OutMessage &&message)
Handles a single outbound message to the client.
std::mutex m_socketMutex
Mutex to synchronize socket access from multiple threads.
const App & app() const
Returns a const reference to the owning App instance.
void processInRawMessage(const std::string &rawMessage)
Parses and processes a complete incoming raw message.
void handleSerializedMessage(const protocol::internal::SerializedMessage &serializedMessage)
Sends an already-serialized message to the client.
void send(const std::string &message)
Sends a text message over the socket to the client.
handling_function_response_t handle(const irsol::types::client_id_t &clientId, protocol::InMessage &&message) const
Dispatches an incoming message to the correct user-defined handler.
#define IRSOL_NAMED_LOG_INFO(name,...)
Logs an info-level message using a named logger.
#define IRSOL_NAMED_LOG_DEBUG(name,...)
Logs a debug-level message using a named logger.
#define IRSOL_NAMED_LOG_ERROR(name,...)
Logs an error-level message using a named logger.
#define IRSOL_NAMED_LOG_WARN(name,...)
Logs a warning-level message using a named logger.
#define IRSOL_NAMED_LOG_TRACE(name,...)
Logs a trace-level message using a named logger.
std::variant< Success, BinaryDataBuffer, ImageBinaryData, ColorImageBinaryData, Error > OutMessage
Variant type representing any outgoing message.
std::string toString(const InMessage &msg)
Converts an incoming message variant to a human-readable string.
Logging utilities and configuration for the irsol library.
Message routing layer between protocol and application logic.
sockpp::tcp_socket socket_t
Alias for a TCP socket.
std::byte byte_t
Alias for a single byte used in serialization or binary data handling.
std::string stripString(const std::string &s, const std::string &strippedString)
Removes all occurrences of a specific substring from the start and end of a string.
Parses raw protocol input strings into structured messages.
Declaration of the ClientSession class.
Represents a serialized protocol message with header and payload.
std::vector< irsol::types::byte_t > payload
The binary payload of the serialized message.
std::string header
The textual header of the serialized message.
bool hasHeader() const
Checks whether the serialized message contains a non-empty header.
size_t payloadSize() const
Returns the size of the payload in bytes.
bool hasPayload() const
Checks whether the serialized message contains a non-empty payload.
General utility functions used throughout the irsol library.