Implementation:CARLA simulator Carla TrafficManagerServer
| Knowledge Sources | |
|---|---|
| Domains | Traffic_Management, Autonomous_Driving |
| Last Updated | 2026-02-15 05:00 GMT |
Overview
The TrafficManagerServer class wraps an RPC server that exposes the full Traffic Manager API over the network, allowing remote TrafficManagerClient instances to control a local TrafficManagerLocal instance.
Description
TrafficManagerServer encapsulates a ::rpc::server instance (from the rpclib library) and binds all Traffic Manager operations as named RPC endpoints. It is created by TrafficManagerLocal during construction and runs asynchronously to handle incoming remote requests. Key features include:
- Port Binding with Retry: The constructor attempts to bind the RPC server to the specified port up to MIN_TRY_COUNT times, sleeping 500ms between attempts. If all attempts fail, it throws a std::runtime_error. The port may be updated during retry if needed.
- Lambda-based RPC Bindings: Each Traffic Manager operation is bound as a named lambda that deserializes carla::rpc::Actor arguments (using carla::client::detail::ActorVariant) and delegates to the TrafficManagerBase pointer. The bindings include:
- register_vehicle / unregister_vehicle: Deserialize actor lists and register/unregister vehicles.
- set_percentage_speed_difference / set_desired_speed: Set per-vehicle speed configuration.
- set_lane_offset / set_global_lane_offset: Set lane offset parameters.
- set_collision_detection: Configure collision detection between two actors.
- set_force_lane_change / set_auto_lane_change: Control lane change behavior.
- set_distance_to_leading_vehicle / set_global_distance_to_leading_vehicle: Set following distances.
- set_percentage_running_light / set_percentage_running_sign: Set traffic rule violation probabilities.
- set_percentage_ignore_walkers / set_percentage_ignore_vehicles: Set collision ignore probabilities.
- set_hybrid_physics_mode / set_hybrid_physics_radius: Control hybrid physics.
- set_osm_mode: Toggle Open Street Map mode.
- set_path / remove_custom_path / update_custom_path: Manage custom vehicle paths.
- set_imported_route / remove_imported_route / update_imported_route: Manage custom routes.
- set_respawn_dormant_vehicles / set_boundaries_respawn_dormant_vehicles: Control dormant vehicle respawning.
- set_synchronous_mode / set_synchronous_mode_timeout_in_milisecond: Configure synchronous execution.
- synchronous_tick: Provide synchronous tick (returns bool).
- set_random_device_seed: Set randomization seed.
- get_next_action / get_all_actions: Query planned vehicle actions.
- health_check_remote_TM: No-op endpoint for connection health monitoring.
- shut_down: Trigger graceful release of the traffic manager.
- update_vehicle_lights: Set automatic vehicle light management.
- Asynchronous Execution: After binding all endpoints, the server is started via server->async_run() which runs the RPC event loop in a separate thread.
- Cleanup: The destructor stops the server and deletes the ::rpc::server pointer.
Usage
TrafficManagerServer is created and owned by TrafficManagerLocal. It is not instantiated directly by user code. Remote clients connect to it via TrafficManagerClient (wrapped by TrafficManagerRemote).
Code Reference
Source Location
- Repository: CARLA
- File:
LibCarla/source/carla/trafficmanager/TrafficManagerServer.h
Signature
class TrafficManagerServer {
public:
TrafficManagerServer(const TrafficManagerServer &) = default;
TrafficManagerServer(TrafficManagerServer &&) = default;
TrafficManagerServer &operator=(const TrafficManagerServer &) = default;
TrafficManagerServer &operator=(TrafficManagerServer &&) = default;
TrafficManagerServer(
uint16_t &RPCPort,
carla::traffic_manager::TrafficManagerBase* tm);
~TrafficManagerServer();
uint16_t port() const;
private:
uint16_t _RPCPort;
::rpc::server *server = nullptr;
};
Import
#include "carla/trafficmanager/TrafficManagerServer.h"
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| RPCPort | uint16_t& | Yes | Port number to bind the RPC server to (may be updated during retry) |
| tm | TrafficManagerBase* | Yes | Pointer to the local traffic manager instance that handles all operations |
Outputs
| Name | Type | Description |
|---|---|---|
| port() | uint16_t | The port number the server is bound to |
| RPC endpoints | Network | Named RPC endpoints accessible by TrafficManagerClient instances |
Usage Examples
// TrafficManagerServer is created inside TrafficManagerLocal constructor:
server(TrafficManagerServer(RPCportTM,
static_cast<carla::traffic_manager::TrafficManagerBase*>(this)));
// The server runs asynchronously, handling remote RPC calls automatically.
// Query the server port:
uint16_t bound_port = server.port();
// Destruction happens automatically when TrafficManagerLocal is destroyed:
// ~TrafficManagerServer() calls server->stop() and deletes the rpc::server.