Struct Connection

Source
pub struct Connection { /* private fields */ }
Expand description

表示一个TCP连接

Connection 封装了一个TCP流和相关的缓冲区,提供了读取请求和发送响应的方法。 它负责处理底层的网络IO操作,并将原始字节数据转换为应用层的请求和响应对象。

Implementations§

Source§

impl Connection

Source

pub fn new(stream: TcpStream) -> Self

创建一个新的连接实例

§参数
  • stream - TCP流,用于与客户端进行网络通信
§返回值

返回一个新的 Connection 实例

Source

pub fn remote_addr(&self) -> Result<SocketAddr, ZerustError>

获取远程客户端的套接字地址

该函数通过底层的流连接获取对端的网络地址信息。

§返回值
  • Ok(SocketAddr) - 成功获取到的远程套接字地址
  • Err(ZerustError) - 获取地址失败时返回的错误信息
§错误处理

当底层IO操作出现错误时,会将IO错误转换为ZerustError::IoError返回

Source

pub async fn read_request(&mut self) -> Result<Request, ZerustError>

从连接中异步读取一个完整的请求消息

该函数首先读取固定大小的消息头,解析出消息ID和数据长度, 然后根据数据长度读取相应的消息体数据,最后构造成Request对象返回。

§Returns
  • Result<Request, ZerustError> - 成功时返回解析出的请求对象,失败时返回错误信息
Source

pub async fn send_response( &mut self, resp: &Response, ) -> Result<(), ZerustError>

发送响应消息

将响应消息打包并发送到网络流中

§参数
  • resp - 要发送的响应消息
§返回值
  • Result<(),ZerustError> - 发送结果,成功返回Ok(()),失败返回ZerustError错误
§异常
  • 当网络写入失败时会返回ZerustError错误

Auto Trait Implementations§

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.