Struct Channel

Source
pub struct Channel<R: Read, W: Write> { /* private fields */ }
Expand description

The Channel struct facilitates communication between a client and a server. It handles the transmission of requests from the client to the server and the reception of responses from the server to the client. Communication is achieved through the use of pipes.

§Fields

  • reader - Responsible for reading data from the channel.
  • writer - Responsible for writing data to the channel.

Implementations§

Source§

impl<R: Read, W: Write> Channel<R, W>

Source

pub fn new(reader: R, writer: W) -> Self

Source§

impl<R: Read, W: Write> Channel<R, W>

Source

pub fn execute<Req, Resp, S>(self, serve: &mut S) -> Result<(), IpcError>
where Req: Serialize + for<'de> Deserialize<'de>, Resp: Serialize + for<'de> Deserialize<'de>, S: Serve<Req = Req, Resp = Resp>,

Executes the server loop, processing incoming requests and sending responses.

This function runs an infinite loop, continuously receiving requests from the client, processing them using the provided service implementation, and sending back the responses. If an error occurs during the processing of a request or the sending of a response, the error is logged (if logging is enabled) and the error code is sent back to the client.

§Arguments
  • serve - A mutable reference to the service implementation that handles the requests and generates the responses. The service must implement the Serve trait with the appropriate request and response types.
§Type Parameters
  • Req - The type of the request messages. It must implement Serialize and Deserialize.
  • Resp - The type of the response messages. It must implement Serialize and Deserialize.
  • S - The type of the service implementation. It must implement the Serve trait with Req as the request type and Resp as the response type.
§Returns

A Result indicating the success or failure of the server execution. If the server runs successfully, it never returns. If an error occurs, it returns an IpcError.

Source

pub fn call<Req, Resp>( &mut self, _method_name: &'static str, req: Req, ) -> Result<Resp, IpcError>
where Req: Serialize + for<'de> Deserialize<'de>, Resp: Serialize + for<'de> Deserialize<'de>,

Sends a request to the server and waits for a response.

This function serializes the request, sends it to the server, and then waits for the server’s response. It returns the deserialized response or an IpcError if an error occurs during the process.

§Arguments
  • _method_name - A static string slice representing the name of the method being called.
  • req - The request message to be sent to the server. It must implement Serialize and Deserialize.
§Type Parameters
  • Req - The type of the request message. It must implement Serialize and Deserialize.
  • Resp - The type of the response message. It must implement Serialize and Deserialize.
§Returns

A Result containing the deserialized response message if the call is successful, or an IpcError if an error occurs during the process.

§Example
use ckb_script_ipc_common::channel::Channel;
use serde::{Serialize, Deserialize};

#[derive(Serialize, Deserialize)]
struct MyRequest {
    // request fields
}

#[derive(Serialize, Deserialize)]
struct MyResponse {
    // response fields
}

let mut channel = Channel::new(reader, writer);
let request = MyRequest { /* fields */ };
let response: MyResponse = channel.call("my_method", request).expect("Failed to call method");
Source

pub fn send_json_request(&mut self, json: &str) -> Result<(), IpcError>

Sends a raw JSON string request to the server.

This function takes a JSON string and sends it directly as a request packet to the server, without performing any serialization. This is useful when working with raw JSON data that doesn’t need to be converted from Rust types.

§Arguments
  • json - A string slice containing the JSON request to be sent.
§Returns

A Result indicating whether the request was successfully sent, or an IpcError if writing to the channel fails.

Source

pub fn receive_json_response(&mut self) -> Result<String, IpcError>

Receives a JSON string response from the server.

This function reads a response packet from the server and returns its payload as a String, without attempting to deserialize it into a specific type. This is useful when working with raw JSON responses that don’t need to be converted into specific Rust types.

§Returns
  • Ok(String) - A String containing the JSON response if successful
  • Err(IpcError) - An error if:
    • Reading from the channel fails
    • The server returns an error code
    • The response payload contains invalid UTF-8

Auto Trait Implementations§

§

impl<R, W> Freeze for Channel<R, W>
where R: Freeze, W: Freeze,

§

impl<R, W> RefUnwindSafe for Channel<R, W>

§

impl<R, W> Send for Channel<R, W>
where R: Send, W: Send,

§

impl<R, W> Sync for Channel<R, W>
where R: Sync, W: Sync,

§

impl<R, W> Unpin for Channel<R, W>
where R: Unpin, W: Unpin,

§

impl<R, W> UnwindSafe for Channel<R, W>
where R: UnwindSafe, W: UnwindSafe,

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.