18#include <dfx-fdwatch/BorrowedFd.hpp>
19#include <dfx-fdwatch/PollerFd.hpp>
80class TcpSession :
public std::enable_shared_from_this<TcpSession>
114 std::string
const &
host() const noexcept {
return _host; }
116 std::string
const &
serv() const noexcept {
return _serv; }
141 std::chrono::milliseconds
initialTimeout() const noexcept {
return _initialTimeout; }
199 void _onSocketReady(FdWatch::EventTriggers events);
200 void _onSocketReadyRead();
201 void _onSocketReadyWrite();
216 std::chrono::milliseconds _initialTimeout;
217 std::chrono::milliseconds _timeBeforeTimeout{0};
220 bool _canSendMessage =
true;
221 std::vector<uint8_t> _nextDataToWrite;
222 std::queue<std::vector<uint8_t>> _pendingDataQueue;
223 std::size_t _maxPendingDataQueueSize = 1024;
Convenience macros to explicitly control copy and move semantics.
Event interest and trigger flags for file-descriptor watching (Linux/epoll).
Non-owning wrapper around a file descriptor.
Definition BorrowedFd.hpp:37
Owning RAII wrapper around a file descriptor.
Definition OwnedFd.hpp:36
RAII wrapper for the registration of a FD in a Poller.
Definition PollerFd.hpp:42
Abstract interface for FD-based event polling.
Definition Poller.hpp:37
Asynchronous TCP server integrated with a FdWatch::Poller.
Definition TcpServer.hpp:66
TimeoutPolicy _timeoutPolicy
Timeout reset policy.
Definition TcpSession.hpp:210
virtual ~TcpSession()=default
Virtual destructor for polymorphic session types.
void resetTimeout()
Reset the remaining timeout countdown to the configured initial value.
bool subtractTimeAndCheckTimeout(std::chrono::milliseconds time)
Decrease the remaining time before timeout and report expiration.
std::string const & host() const noexcept
Remote host string.
Definition TcpSession.hpp:114
void init(FdWatch::OwnedFd socket, FdWatch::Poller &poller)
Bind the accepted socket to this session and register it to the poller.
std::string _host
Remote host string.
Definition TcpSession.hpp:205
TcpSession(std::string host, std::string serv, TcpServer &server, std::chrono::milliseconds initialTimeout)
Construct a session descriptor (not yet bound to a socket).
void setInitialTimeout(std::chrono::milliseconds timeout)
Update the initial timeout value.
void setMaxDataQueueSize(std::size_t size)
Set the maximum number of queued outgoing messages.
void sendMessage(std::vector< uint8_t > const &message)
Queue an outgoing message to be sent asynchronously.
FdWatch::BorrowedFd socket() const noexcept
Borrowed socket FD (valid after init()).
Definition TcpSession.hpp:112
std::size_t maxDataQueueSize() const noexcept
Maximum number of queued outgoing messages.
Definition TcpSession.hpp:187
virtual void handleMessage(std::vector< uint8_t > message)=0
Handle one received message payload.
DISABLE_COPY_AND_MOVE(TcpSession)
TcpSession are not copyable and not movable.
std::string const & serv() const noexcept
Remote service/port string.
Definition TcpSession.hpp:116
void terminate()
Ask the owning server to terminate this session.
std::string _serv
Remote service/port string.
Definition TcpSession.hpp:207
std::chrono::milliseconds initialTimeout() const noexcept
Current configured initial timeout value.
Definition TcpSession.hpp:141
TimeoutPolicy
Policy describing when I/O activity resets the inactivity timeout.
Definition TcpSession.hpp:85
@ ResetOnWrite
Writing data resets the timeout.
Definition TcpSession.hpp:89
@ ResetOnRead
Reading data resets the timeout.
Definition TcpSession.hpp:88
@ ResetOnAny
Any I/O activity resets the timeout (default).
Definition TcpSession.hpp:90
@ Disabled
Timeouts are disabled.
Definition TcpSession.hpp:86
@ Manual
Timeout is only reset explicitly (via resetTimeout()).
Definition TcpSession.hpp:87
Definition SocketClient.hpp:23
Definition BaseCommandHandler.hpp:16
std::shared_ptr< TcpSession > TcpSessionPtr
Shared ownership pointer type for sessions.
Definition TcpSession.hpp:227
std::weak_ptr< TcpSession > TcpSessionWPtr
Weak pointer type for sessions.
Definition TcpSession.hpp:229