[−][src]Struct udp_server::UdpServer
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]
I: Fn(Weak<()>, Arc<Peer<T>>, Vec<u8>) -> R + Send + Sync + 'static,
R: Future<Output = Result<(), Box<dyn Error>>> + Send,
T: Send + 'static,
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]
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,
pub async fn new_inner<A: ToSocketAddrs>(
addr: A,
inner: Arc<S>
) -> Result<Self, Box<dyn Error>>
[src]
addr: A,
inner: Arc<S>
) -> Result<Self, Box<dyn Error>>
创建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]
&mut self,
err_input: P
)
设置错误输出 返回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]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,