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>
impl<R: Read, W: Write> Channel<R, W>
Sourcepub 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>,
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 theServe
trait with the appropriate request and response types.
§Type Parameters
Req
- The type of the request messages. It must implementSerialize
andDeserialize
.Resp
- The type of the response messages. It must implementSerialize
andDeserialize
.S
- The type of the service implementation. It must implement theServe
trait withReq
as the request type andResp
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
.
Sourcepub fn call<Req, Resp>(
&mut self,
_method_name: &'static str,
req: Req,
) -> Result<Resp, IpcError>
pub fn call<Req, Resp>( &mut self, _method_name: &'static str, req: Req, ) -> Result<Resp, IpcError>
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 implementSerialize
andDeserialize
.
§Type Parameters
Req
- The type of the request message. It must implementSerialize
andDeserialize
.Resp
- The type of the response message. It must implementSerialize
andDeserialize
.
§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");
Sourcepub fn send_json_request(&mut self, json: &str) -> Result<(), IpcError>
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.
Sourcepub fn receive_json_response(&mut self) -> Result<String, IpcError>
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 successfulErr(IpcError)
- An error if:- Reading from the channel fails
- The server returns an error code
- The response payload contains invalid UTF-8