Skip to main content

Server

Struct Server 

Source
pub struct Server<I: Invoker> {
    pub invoker: Arc<I>,
    /* private fields */
}
Expand description

Server for handling incoming RPC connections.

The Server routes incoming RPC calls to the appropriate handler via the provided Invoker (typically a Mux).

§Example

use starpc::{Server, Mux};

let mux = Arc::new(Mux::new());
mux.register(Arc::new(MyServiceHandler))?;

let server = Server::new(mux);
server.handle_stream(tcp_stream).await?;

Fields§

§invoker: Arc<I>

The invoker for routing RPC calls.

Implementations§

Source§

impl<I: Invoker + 'static> Server<I>

Source

pub fn new(invoker: I) -> Self

Creates a new server with the given invoker.

Source

pub fn with_arc(invoker: Arc<I>) -> Self

Creates a new server with a shared invoker.

Source

pub fn with_config(self, config: ServerConfig) -> Self

Sets the server configuration.

Source

pub fn with_error_handler<F>(self, handler: F) -> Self
where F: Fn(Error) + Send + Sync + 'static,

Sets an error handler for connection errors.

The handler is called when an error occurs during stream handling. This is useful for logging or metrics.

Source

pub async fn handle_stream<T>(&self, transport: T) -> Result<()>
where T: AsyncRead + AsyncWrite + Send + Unpin + 'static,

Handles a single stream connection.

This reads packets from the stream, routes the RPC call to the appropriate handler, and writes responses back.

The method returns when the RPC completes or an error occurs.

Source

pub async fn serve<L, T>(&self, listener: L) -> Result<()>
where L: Stream<Item = Result<T>> + Unpin, T: AsyncRead + AsyncWrite + Send + Unpin + 'static,

Accepts and handles connections in a loop.

This is a convenience method that accepts connections from a listener and spawns a task to handle each one.

Errors from individual connections are reported via the error handler (if configured) but don’t stop the server.

Auto Trait Implementations§

§

impl<I> Freeze for Server<I>

§

impl<I> !RefUnwindSafe for Server<I>

§

impl<I> Send for Server<I>

§

impl<I> Sync for Server<I>

§

impl<I> Unpin for Server<I>

§

impl<I> UnsafeUnpin for Server<I>

§

impl<I> !UnwindSafe for Server<I>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.