[][src]Struct udp_server::UdpServer

pub struct UdpServer<I, R, T> where
    I: Fn(Arc<Mutex<Peer<T>>>, Bytes) -> R + Send + Sync + 'static,
    R: Future<Output = Result<(), Box<dyn Error>>> + Send,
    T: Send + 'static, 
{ /* fields omitted */ }

UDP 服务器对象 I 用来限制必须input的FN 原型, R 用来限制 必须input的是 异步函数 T 用来设置返回值

Examples

#![feature(async_closure)]
use udp_server::UdpServer;
use std::cell::RefCell;

#[tokio::main]
async fn main() {
   use tokio::net::UdpSocket;
   let mut a = UdpServer::new("0.0.0.0:5555").await.unwrap();
   a.set_input(async move |peer,data|{
        let mut un_peer = peer.lock().await;
        match &un_peer.token {
            Some(x)=>{
                *x.borrow_mut()+=1;
                }
            None=>{
                un_peer.token=Some(RefCell::new(1));
            }
        }
        un_peer.send(&data).await?;
        Err("stop it".into())
    });

 let mut sender = UdpSocket::bind("127.0.0.1:0").await.unwrap();
 sender.connect("127.0.0.1:5555").await.unwrap();
 let message = b"hello!";
 for _ in 0..100 {
    sender.send(message).await.unwrap();
 }

 a.start().await.unwrap();
}

Implementations

impl<I, R, T> UdpServer<I, R, T> where
    I: Fn(Arc<Mutex<Peer<T>>>, Bytes) -> R + Send + Sync + 'static,
    R: Future<Output = Result<(), Box<dyn Error>>> + Send,
    T: Send + 'static, 
[src]

pub async fn new<A: ToSocketAddrs>(addr: A) -> Result<Self, Box<dyn Error>>[src]

创建UdpServer 如果是linux 是系统,他会根据CPU核心数创建等比的UDP SOCKET 监听同一端口 已达到 M级的DPS 数量

pub fn set_input(&mut self, input: I)[src]

设置收包函数 此函数必须符合 async 模式

pub fn set_err_input<P: Fn(Option<Arc<Mutex<Peer<T>>>>, Box<dyn Error>) -> bool + Send + 'static>(
    &mut self,
    err_input: P
)
[src]

设置错误输出 返回bool 如果 true 表示停止服务

pub async fn start<'_>(&'_ self) -> Result<(), Box<dyn Error>>[src]

启动服务 如果input 发生异常,将会发生错误 这个时候回触发 err_input, 如果没有使用 set_err_input 设置错误回调 那么 就会输出默认的 err_input,如果输出默认的 err_input 那么整个服务将会停止 所以如果不想服务停止,那么必须自己实现 err_input 并且返回 false

Auto Trait Implementations

impl<I, R, T> !RefUnwindSafe for UdpServer<I, R, T>

impl<I, R, T> Send for UdpServer<I, R, T>

impl<I, R, T> Sync for UdpServer<I, R, T>

impl<I, R, T> Unpin for UdpServer<I, R, T>

impl<I, R, T> !UnwindSafe for UdpServer<I, R, T>

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.