pub trait Server {
// Required methods
fn get_identifier(&self) -> &'static str;
fn check_version(&self, version: &str) -> bool;
fn get_function<R, W>(
&self,
func: &str,
) -> Option<Box<dyn FuncHandler<R, W>>>
where R: AsyncReadExt + Unpin + Send,
W: AsyncWriteExt + Unpin + Send;
// Provided methods
fn handle_error<'life0, 'life1, 'life2, 'async_trait, R, W>(
&'life0 self,
func: &'life1 str,
error: Error,
_stream: &'life2 mut IOStream<R, W>,
) -> Pin<Box<dyn Future<Output = Result<(), NetworkError>> + Send + 'async_trait>>
where R: AsyncReadExt + Unpin + Send + 'async_trait,
W: AsyncWriteExt + Unpin + Send + 'async_trait,
Self: Sync + 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait { ... }
fn start<'async_trait>(
&'static self,
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
where Self: Sync + 'async_trait { ... }
}Expand description
The basic trait for a server.
§Example
use tcp_server::{func_handler, Server};
use tcp_server::anyhow::anyhow;
use tcp_server::handler_base::FuncHandler;
use tcp_server::tcp_handler::bytes::{Buf, BufMut, BytesMut};
use tcp_server::tcp_handler::variable_len_reader::{VariableReader, VariableWriter};
use tcp_server::tokio::io::{AsyncReadExt, AsyncWriteExt};
struct MyServer;
impl Server for MyServer {
fn get_identifier(&self) -> &'static str {
"MyTcpApplication"
}
fn check_version(&self, version: &str) -> bool {
version == env!("CARGO_PKG_VERSION")
}
fn get_function<R, W>(&self, func: &str) -> Option<Box<dyn FuncHandler<R, W>>>
where R: AsyncReadExt + Unpin + Send, W: AsyncWriteExt + Unpin + Send {
match func {
"hello" => Some(Box::new(HelloHandler)),
_ => None,
}
}
}
func_handler!(HelloHandler, |stream| {
let mut reader = stream.recv().await?.reader();
if "hello server" != reader.read_string()? {
return Err(anyhow!("Invalid message."));
}
let mut writer = BytesMut::new().writer();
writer.write_string("hello client")?;
stream.send(&mut writer.into_inner()).await?;
Ok(())
});
#[tokio::main]
async fn main() {
MyServer.start().await.unwrap();
}Required Methods§
Sourcefn get_identifier(&self) -> &'static str
fn get_identifier(&self) -> &'static str
Sourcefn check_version(&self, version: &str) -> bool
fn check_version(&self, version: &str) -> bool
Sourcefn get_function<R, W>(&self, func: &str) -> Option<Box<dyn FuncHandler<R, W>>>
fn get_function<R, W>(&self, func: &str) -> Option<Box<dyn FuncHandler<R, W>>>
Provided Methods§
Sourcefn handle_error<'life0, 'life1, 'life2, 'async_trait, R, W>(
&'life0 self,
func: &'life1 str,
error: Error,
_stream: &'life2 mut IOStream<R, W>,
) -> Pin<Box<dyn Future<Output = Result<(), NetworkError>> + Send + 'async_trait>>where
R: AsyncReadExt + Unpin + Send + 'async_trait,
W: AsyncWriteExt + Unpin + Send + 'async_trait,
Self: Sync + 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
fn handle_error<'life0, 'life1, 'life2, 'async_trait, R, W>(
&'life0 self,
func: &'life1 str,
error: Error,
_stream: &'life2 mut IOStream<R, W>,
) -> Pin<Box<dyn Future<Output = Result<(), NetworkError>> + Send + 'async_trait>>where
R: AsyncReadExt + Unpin + Send + 'async_trait,
W: AsyncWriteExt + Unpin + Send + 'async_trait,
Self: Sync + 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
Handle the error which returned by the FuncHandler.
Default only print the error message.
Sourcefn start<'async_trait>(
&'static self,
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>where
Self: Sync + 'async_trait,
fn start<'async_trait>(
&'static self,
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>where
Self: Sync + 'async_trait,
Start the server. This method will block the caller thread.
It only will return an error if the server cannot start.
If you want to handle errors returned by FuncHandler, you should override Server::handle_error.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.