Message Routing

Message routing controls how an incoming request is dispatched to a specific service method. Routing rules are altered by the route argument of the #[export] attribute, applied to public methods on structs labelled with #[program] or #[service]. By default, every service exposed by a program is exposed under the service-constructor name converted to PascalCase; every service method is exposed under its method name converted to PascalCase. For example:

#[program]
impl MyProgram {
    // The `MyPing` service is exposed under the default route `PingSvc`
    // (the service-constructor name in PascalCase).
    pub fn ping_svc(&self) -> MyPing {
        MyPing::default()
    }
}

The default name can be overridden via #[export(route = "...")]. The supplied string is normalised to PascalCase before it is used as the route name:

#[program]
impl MyProgram {
    // The `MyPing` service is exposed as `Ping`.
    // The given route is converted into PascalCase.
    #[export(route = "ping")]
    pub fn ping_svc(&self) -> MyPing {
        MyPing::default()
    }
}

The same route argument works on service methods:

#[service]
impl MyPing {
    // `do_ping` is exposed as `Ping`.
    #[export(route = "ping")]
    pub fn do_ping(&mut self) {
        // ...
    }

    // `ping_count` is exposed using its default name `PingCount`.
    #[export]
    pub fn ping_count(&self) -> u64 {
        0
    }
}