Class Session

Inheritance Relationships

Base Type

  • public felix::asyncmsg::Session

Class Documentation

class Session : public felix::asyncmsg::Session

Represents a TCP connection between two endpoints.

This class provides callbacks that are invoked when conections are opened, closed, or when messages are received or sent. It also provide callbacks that are invoked on errors. This class can be used for sending and receiving data, no specific classes are derived for sending and receiving. However, one conection is only used to either send or received. For bi-rectional traffic two sessions will be opened. In principle, this class would support re-use of the same connection for sending and receiving.

The session contains a link back to the server (if spawned by one) to notify it when the session was closed so the server can remove it from its list of sessions.

Public Functions

Session(boost::asio::io_service &ioService, NetworkConfig config, BaseEventLoop *eventloop, std::function<void(std::shared_ptr<felix::asyncmsg::Session>)> connectionClosedCb, std::function<void(const ConnectionEvent&)> addEventCb, bool useAsioEventLoop, Mode mode, const ConnectionParameters &connectionParams = {})

Create a Session object.

Create a session to open a connection. The ioService is shared by all sessions. A signal is added to the event loop in case events are processed by the netio3 event loop (DELEGATE mode). In case it is not used, this fd will never become readable. The FD will be removed from the event loop on destruction of the session.

The connectionParams parameter can specify the number of buffers to be used for sending.

The connectionClosedCb is used to notify the holder of the session that it was closed so it can be deleted.

Parameters:
  • ioService – The boost::asio::io_service object used for asynchronous I/O operations

  • config – The network configuration settings

  • eventloop – A shared pointer to the event loop

  • connectionClosedCb – The callback to be invoked when a connection is closed

  • addEventCb – The callback to be invoked when a connection event is added (for DELEGATE)

  • mode – The Mode specifying how to integrate boost asio events into the event loop

  • connectionParams – The connection parameters for the TCP session (optional)

void sendData(std::unique_ptr<SendMessageUnbuffered> message)

Send a message and keep track of it.

Parameters:

message – The message to send

void onOpen() override

Callback called when connection is opened.

Start receiving packets asynchronously and call on_connection_established_cb callback.

void onOpenError(const boost::system::error_code &error) override

Callback called when connection open fails.

Call on_connection_refused_cb callback.

Parameters:

error – The error code

std::unique_ptr<felix::asyncmsg::InputMessage> createMessage(std::uint32_t typeId, std::uint32_t transactionId, std::uint32_t size) override

Callback called when a message is received to create the message object.

This function is called by the asyncmsg library when a message is received. It creates the ReceiveMessage object and binds the network buffers to the message.

Parameters:
  • typeId – The type ID of the message

  • transactionId – The transaction ID of the message

  • size – The size of the message

Returns:

The received message

void onReceive(std::unique_ptr<felix::asyncmsg::InputMessage> message) override

Callback called when a message is received.

If the message is a ReceiveMessage, put it on the receive queue and fire a signal to notify the event loop that it can be handled if DELEGATE mode is used. Otherwise, handle it directly (see handleOnReceive). If the message is of the wrong type do nothing. In all cases, keep receiving messages.

Parameters:

message – The received message

void onReceiveError(const boost::system::error_code &error, std::unique_ptr<felix::asyncmsg::InputMessage> message) override

Callback called when receiving a message fails.

In case that the connection is not open, ignore the error. This can happen if the connection was closed from this side while messages were still sent.

In case the error is eof, assume that the connection was closed by the other side and close it on this side as well.

Otherwise log the issue unless the connection was already closed.

Parameters:
  • error – The error code

  • message – The message that failed to be received

void onSend(std::unique_ptr<const felix::asyncmsg::OutputMessage> message) override

Callback called when a message is sent.

If the send message is of correct type call on_send_completed_cb callback, return otherwise. Check if a buffer was used and return it to the buffer manager (see checkAndReturnBuffer).

Parameters:

message – The message that was sent

void onSendError(const boost::system::error_code &error, std::unique_ptr<const felix::asyncmsg::OutputMessage> message) override

Callback called when sending a message fails.

Log and check if a buffer was used and return it to the buffer manager (see checkAndReturnBuffer).

Parameters:
  • error – The error code

  • message – The message that failed to get sent

void onClose() override

Callback called when the connection is closed.

Call on_connection_closed_cb callback. Also notify the server that the connection was closed if the session was spawned by a server.

void onCloseError(const boost::system::error_code &error) override

Callback called when closing the connection fails.

Try to close the connection again. If it fails again, log the error and stop trying after MAX_CLOSE_ATTEMPTS attempts.

Parameters:

error – The error code

NetworkBuffer *getBuffer()

Returns the network buffer for sending data.

This function returns a network buffer for sending data. If no buffer is available, nullptr is returned.

Returns:

The network buffer (or nullptr if none available)

std::size_t getNumAvailableBuffers()

Retrieves the number of available buffers for the specified endpoint address.

Returns the mininum number of available buffers since the last call to this function.

Returns:

The number of available buffers

std::vector<std::uint64_t> getPendingSends() const

Retrieves the keys of pending zero-copy send operations.

Returns:

The keys of pending send operations