Jump to content

Connect Leeroopedia MCP: Equip your AI agents to search best practices, build plans, verify code, diagnose failures, and look up hyperparameter defaults.

Implementation:CARLA simulator Carla StreamingServer

From Leeroopedia
Knowledge Sources
Domains Streaming, Networking
Last Updated 2026-02-15 05:00 GMT

Overview

Server is the streaming server that creates and manages sensor data streams, each identified by a unique token that clients use to subscribe.

Description

The Server class in the carla::streaming namespace encapsulates a low_level::Server<detail::tcp::Server> and a ThreadPool to provide the server-side streaming infrastructure. It supports three constructor overloads for different deployment configurations:

  • Port-only: Binds to all interfaces on the specified port
  • Address + Port: Binds to a specific local address and port
  • Address + Port + External Address + Port: Binds locally but advertises a different external address and port (useful for NAT or Docker scenarios)

The MakeStream() method creates a new Stream object, which generates a unique token that clients use to subscribe. Streams can be closed with CloseStream(stream_id_type id). The GetToken(stream_id) method retrieves the token for an existing stream.

The server supports synchronous mode control via SetSynchronousMode(bool) and configurable timeouts via SetTimeout(time_duration). Execution is managed through Run() (blocking) or AsyncRun(size_t) (asynchronous with worker threads).

ROS2 integration is supported through EnableForROS, DisableForROS, and IsEnabledForROS methods, which control whether individual streams forward data to the ROS2 bridge.

Usage

This class is instantiated by the CARLA server to broadcast sensor data. Each sensor creates a stream via MakeStream(), obtains its token, and writes data to the stream. Clients receive the token and use streaming::Client to subscribe to the data feed.

Code Reference

Source Location

  • Repository: CARLA
  • File: LibCarla/source/carla/streaming/Server.h

Signature

class Server {
public:
    explicit Server(uint16_t port);
    explicit Server(const std::string &address, uint16_t port);
    explicit Server(const std::string &address, uint16_t port,
                    const std::string &external_address, uint16_t external_port);
    ~Server();

    auto GetLocalEndpoint() const;
    void SetTimeout(time_duration timeout);

    Stream MakeStream();
    void CloseStream(stream_id_type id);

    void Run();
    void AsyncRun(size_t worker_threads);

    void SetSynchronousMode(bool is_synchro);
    token_type GetToken(stream_id sensor_id);

    void EnableForROS(stream_id sensor_id);
    void DisableForROS(stream_id sensor_id);
    bool IsEnabledForROS(stream_id sensor_id);
};

Import

#include "carla/streaming/Server.h"

I/O Contract

Inputs

Name Type Required Description
port uint16_t Yes TCP port to listen on
address const std::string& No Local bind address (defaults to all interfaces)
external_address const std::string& No Externally-advertised address for NAT traversal
external_port uint16_t No Externally-advertised port for NAT traversal
timeout time_duration No Connection timeout for stream sessions
is_synchro bool No Whether to operate in synchronous mode
sensor_id stream_id Yes (per-stream ops) Stream/sensor identifier for token retrieval and ROS operations

Outputs

Name Type Description
MakeStream() Stream A new stream object that sensors can write data into
GetToken() token_type The token for a stream, used by clients to subscribe
GetLocalEndpoint() endpoint The local endpoint the server is bound to
IsEnabledForROS() bool Whether ROS2 forwarding is active for a stream

Usage Examples

// Create a streaming server on port 2000
carla::streaming::Server server("0.0.0.0", 2000);
server.AsyncRun(4);

// Create a new stream for a sensor
auto stream = server.MakeStream();
auto token = stream.token();

// Write sensor data to the stream
stream.Write(sensorBuffer);

// Enable ROS2 forwarding for this stream
server.EnableForROS(stream.id());

// Close the stream when the sensor is destroyed
server.CloseStream(stream.id());

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment