Server Stage (TCP)

This stage allows you to establish a TCP server that some number of clients can connect to. All data that is read from this server will be published into TcpPacketGroup messages – you will need to check the addresses on the packets to understand what client they have come from.

While TCP connections are stream-oriented, and not packet-oriented, this stage treats each return from recv() as if it was a packet. These “packets” are grouped together.

Grouping is intended as a mechanism to batch messages on high-rate sensors, reducing performance overhead of publishing and logging individual TCP “packets”.

Configuration

Basic configuration is in the TcpServerStageConfig:

  • address - The address and port you will bind to.
  • maximum_connections - Number of simultaneous clients to allow.
  • group_size - The number of packets to bundle before publishing a group.
  • thread_priority - Priority to set the for the server thread.
  • thread_processor_affinity - Which processors the server thread can operate on.
  • enable_nodelay - If TCP_NODELAY should be on.
  • data_receive_timeout_ms - Disconnects client if data hasn’t been received in this interval.
  • egress_buffer_size_b - The size of the egress buffer (per-client).

Interacting

This stage will start a background thread which services a socket. The thread will retry binding the socket until it is successful. If any error occurs, the socket will be torn down and rebuilt.

All packet groups are published under packet_group in the stage’s namespace. They will contain packets mixed from multiple clients; check the address on the packet to make sense of them.

The stage listens for data to publish on the egress_packets channel. Anything received will be written to every connected client (or dropped otherwise). You should set the configuration egress_buffer_size_b to at least the size of the largest message you expect to send; that controls the egress buffer and how much data the stage will queue if the other side isn’t reading fast enough.

Metrics

The stage periodically (at 1Hz) emits a SocketStats structure containing rate and size information. This is published under the socket_stats channel within the stage’s namespace.