![]() |
dfx 0.1.0
Linux-based dynamic dataflow executor
|
One connected client session of a UnixServer control socket. More...
#include <dfx-server/UnixSession.hpp>
Public Member Functions | |
| UnixSession (UnixServer &server, UnixRouter &router, std::chrono::milliseconds timeout) | |
| Create a session descriptor (not yet bound to a socket FD). | |
| DISABLE_COPY_AND_MOVE (UnixSession) | |
| UnixSessions are neither copyable nor movable. | |
| FdWatch::BorrowedFd | socket () const noexcept |
| Borrowed socket FD for this session. | |
| uint64_t | inMsgCount () const noexcept |
| Total number of received request messages. | |
| uint64_t | outMsgCount () const noexcept |
| Total number of sent response messages (success + error). | |
| uint64_t | outErrMsgCount () const noexcept |
| Total number of sent error responses. | |
| uint64_t | outConsecutiveErrMsgCount () const noexcept |
| Number of consecutive error responses sent. | |
| bool | subtractTimeAndCheckTimeout (std::chrono::milliseconds time) |
| Decrease the remaining time before timeout and report expiration. | |
| std::chrono::milliseconds | initialTimeout () const noexcept |
| Current configured initial timeout value. | |
| void | setInitialTimeout (std::chrono::milliseconds timeout) |
| Update the initial timeout value. | |
| void | init (FdWatch::OwnedFd socket, FdWatch::Poller &poller) |
| Bind an accepted socket to this session and register it into a poller. | |
| void | reply (nlohmann::json response, std::string_view id) |
| Send a JSON response associated with a request id. | |
| void | replyError (std::string message, std::string_view id, nlohmann::json baseJson={}) |
| Convenience overload to reply with a single error message. | |
| void | replyError (std::vector< std::string > const &messages, std::string_view id, nlohmann::json baseJson={}) |
| Reply with an error response containing one or more messages. | |
| void | terminate () |
| Terminate the session. | |
One connected client session of a UnixServer control socket.
UnixSession represents a single accepted Unix domain socket connection managed by UnixServer.
The session is:
Both reply helpers take an explicit request identifier (id), which is echoed back in the response so clients can match replies to requests (including when multiple requests are in flight).
The counters are:
| dfx::Server::UnixSession::UnixSession | ( | UnixServer & | server, |
| UnixRouter & | router, | ||
| std::chrono::milliseconds | timeout ) |
Create a session descriptor (not yet bound to a socket FD).
The accepted socket FD is provided later via init(). init() is called tight after the constructor and this is done this way because shared_from_this and weak_from_this function only returns a valid value after the instance is stored in a std::shared_ptr.
| server | Owning server responsible for lifecycle and termination. |
| router | Router used to dispatch decoded requests to command handlers. |
| timeout | Initial inactivity timeout value for this session. |
| dfx::Server::UnixSession::DISABLE_COPY_AND_MOVE | ( | UnixSession | ) |
UnixSessions are neither copyable nor movable.
| void dfx::Server::UnixSession::init | ( | FdWatch::OwnedFd | socket, |
| FdWatch::Poller & | poller ) |
Bind an accepted socket to this session and register it into a poller.
Stores the FD in a FdWatch::PollerFd and registers a readiness callback that reads incoming request messages.
| socket | Accepted socket FD (ownership transferred to the session). |
| poller | Poller that will drive readability notifications. |
|
inlinenoexcept |
Current configured initial timeout value.
|
inlinenoexcept |
Total number of received request messages.
|
inlinenoexcept |
Number of consecutive error responses sent.
This counter is intended for server-side policies such as "close a client after N consecutive errors".
It is reset back to 0 when a non-error response is sent (implementation detail in reply()).
|
inlinenoexcept |
Total number of sent error responses.
|
inlinenoexcept |
Total number of sent response messages (success + error).
| void dfx::Server::UnixSession::reply | ( | nlohmann::json | response, |
| std::string_view | id ) |
Send a JSON response associated with a request id.
Serializes response and sends it to the client. This increments the outgoing counters and resets the consecutive-error counter.
| response | JSON response body. |
| id | Request identifier to echo back in the response. |
|
inline |
Convenience overload to reply with a single error message.
| message | Error message. |
| id | Request identifier to echo back in the response. |
| baseJson | Optional JSON object to merge into the error response. |
| void dfx::Server::UnixSession::replyError | ( | std::vector< std::string > const & | messages, |
| std::string_view | id, | ||
| nlohmann::json | baseJson = {} ) |
Reply with an error response containing one or more messages.
Builds a standardized error JSON payload and sends it to the client. Updates outgoing counters:
| messages | List of error messages. |
| id | Request identifier to echo back in the response. |
| baseJson | Optional JSON object to merge into the error response. |
| void dfx::Server::UnixSession::setInitialTimeout | ( | std::chrono::milliseconds | timeout | ) |
Update the initial timeout value.
| timeout | New timeout value. |
|
inlinenoexcept |
Borrowed socket FD for this session.
| bool dfx::Server::UnixSession::subtractTimeAndCheckTimeout | ( | std::chrono::milliseconds | time | ) |
Decrease the remaining time before timeout and report expiration.
Typically called periodically by UnixServer. The countdown is reset on successful I/O activity.
| time | Amount of time to subtract. |
| void dfx::Server::UnixSession::terminate | ( | ) |
Terminate the session.
Requests shutdown of this session. Actual removal from the server registry is performed by UnixServer but this function takes care of this.