[][src]Struct udp_server::UdpServer

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

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

Examples

#![feature(async_closure)]
use udp_server::UdpServer;
use tokio::net::UdpSocket;
use std::sync::Arc;
use tokio::sync::Mutex;

#[tokio::main]
async fn main() {
   let mut a = UdpServer::new("127.0.0.1:5555").await.unwrap();
   a.set_input(async move |_,peer,data|{
        let mut token = peer.token.lock().await;
        match token.get() {
            Some(x)=>{
                *x+=1;
                }
            None=>{
                token.set(Some(1));
            }
        }
        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(Weak<()>, Arc<Peer<T>>, Vec<u8>) -> 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]

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

pub async fn new_inner<A: ToSocketAddrs>(
    addr: A,
    inner: Arc<S>
) -> 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<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, S> !RefUnwindSafe for UdpServer<I, R, T, S>

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

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

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

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

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.