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 RPC Server

From Leeroopedia
Knowledge Sources
Domains RPC Communication, Server Architecture
Last Updated 2026-02-15 05:00 GMT

Overview

Server is the RPC server class that provides synchronous and asynchronous function binding for the CARLA simulator's client-server communication, built on top of the rpclib library.

Description

Defined in the carla::rpc namespace (~168 lines), this class wraps a ::rpc::server instance and a boost::asio::io_context for synchronous task execution. It provides two binding modes:

  • BindSync: functions bound with this method execute within the SyncRunFor call on the game thread, using std::packaged_task and boost::asio::post to marshal calls to the io_context
  • BindAsync: functions execute directly in the RPC worker threads

The detail::FunctionWrapper template metaprogramming infrastructure unwraps callable signatures to properly wrap functions with metadata handling. When a client calls a function with the "response ignored" metadata flag, sync calls are posted but their results are discarded, and async calls skip returning results.

Key methods include AsyncRun(worker_threads) to start worker threads, SyncRunFor(duration) to process synchronous tasks for a time slice, and Stop() to halt the server.

Usage

This is the core RPC server used by the CARLA simulator. The game thread calls SyncRunFor each tick to process synchronous requests, while asynchronous requests run in background worker threads.

Code Reference

Source Location

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

Signature

namespace carla {
namespace rpc {

  class Server {
  public:
    template <typename... Args>
    explicit Server(Args &&... args);

    template <typename FunctorT>
    void BindSync(const std::string &name, FunctorT &&functor);

    template <typename FunctorT>
    void BindAsync(const std::string &name, FunctorT &&functor);

    void AsyncRun(size_t worker_threads);
    void SyncRunFor(time_duration duration);
    void Stop();

  private:
    boost::asio::io_context _sync_io_context;
    ::rpc::server _server;
  };

} // namespace rpc
} // namespace carla

Import

#include "carla/rpc/Server.h"

I/O Contract

Method Parameters Description
BindSync(name, functor) Function name, callable Binds a function to run on the game thread via SyncRunFor
BindAsync(name, functor) Function name, callable Binds a function to run on worker threads
AsyncRun(n) Worker thread count Starts n worker threads for async calls
SyncRunFor(duration) Time duration Processes sync tasks for the given duration
Stop() -- Stops the RPC server (does not stop game thread)

Usage Examples

#include "carla/rpc/Server.h"

carla::rpc::Server server(2000u); // Listen on port 2000

// Bind a sync function (runs on game thread)
server.BindSync("get_weather", [&]() {
    return world.GetWeather();
});

// Bind an async function (runs on worker thread)
server.BindAsync("get_map_name", [&]() {
    return map_name;
});

server.AsyncRun(4); // Start 4 worker threads

// In game loop:
server.SyncRunFor(carla::time_duration::milliseconds(10));

Related Pages

Page Connections

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