Skip to content

Commit

Permalink
modified trait implement object
Browse files Browse the repository at this point in the history
  • Loading branch information
luo4lu committed Apr 15, 2020
1 parent 63be9cc commit ee69468
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 33 deletions.
16 changes: 9 additions & 7 deletions examples/echo.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,28 @@
use net_hal::udp::{UdpSocket, UdpServer};
use addr_hal::SocketAddr;

use net_hal::udp::{UdpServer, UdpSocket};

async fn echo_request<S: UdpSocket>(socket: &mut S) {
match socket.connect(SocketAddr::from(([127, 0, 0, 1], 3400))).await {
match socket
.connect(SocketAddr::from(([127, 0, 0, 1], 3400)))
.await
{
Ok(s) => s,
Err(_error) => panic!("couldn't connect to address"),
};

match socket.send(&[0, 1, 2]).await {
Ok(s) => println!("send buffer size = {}",s),
Ok(s) => println!("send buffer size = {}", s),
Err(_error) => panic!("couldn't send to address"),
};
}

#[tokio::main]
async fn main() {
use net_hal_tokio::udp::TokioUdpSocket;
let mut handle = match TokioUdpSocket::bind(SocketAddr::from(([127, 0, 0, 1], 3401))).await{

let mut handle = match TokioUdpSocket::bind(SocketAddr::from(([127, 0, 0, 1], 3401))).await {
Ok(s) => s,
Err(_error) => panic!("couldn't bind to address"),
};
echo_request(&mut handle).await;
}
}
19 changes: 9 additions & 10 deletions examples/echo_server.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
use net_hal::udp::{UdpSocket, UdpServer};
use addr_hal::SocketAddr;

use net_hal::udp::{UdpServer, UdpSocket};

async fn echo_recv<S: UdpSocket>(socket: &mut S) {
let mut buf = [0;10];
let _buf_size = match socket.recv(&mut buf).await {
Ok(received) => println!("received {} bytes {:?}", received, &buf[..received]),
Err(_e) => panic!("recv function failed:"),
};
let mut buf = [0; 10];
let _buf_size = match socket.recv(&mut buf).await {
Ok(received) => println!("received {} bytes {:?}", received, &buf[..received]),
Err(_e) => panic!("recv function failed:"),
};
}

#[tokio::main]
async fn main() {
use net_hal_tokio::udp::TokioUdpSocket;
let mut handle = match TokioUdpSocket::bind(SocketAddr::from(([127, 0, 0, 1], 3400))).await{

let mut handle = match TokioUdpSocket::bind(SocketAddr::from(([127, 0, 0, 1], 3400))).await {
Ok(s) => s,
Err(_error) => panic!("couldn't bind to address"),
};
echo_recv(&mut handle).await;
}
}
19 changes: 9 additions & 10 deletions examples/echo_server2.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
use net_hal::udp::{UdpSocket, UdpServer};
use addr_hal::SocketAddr;

use net_hal::udp::{UdpServer, UdpSocket};

async fn echo_recv<S: UdpSocket>(socket: &mut S) {
let mut buf = [0;10];
let _buf_size = match socket.recv(&mut buf).await {
Ok(received) => println!("received {} bytes {:?}", received, &buf[..received]),
Err(_e) => panic!("recv function failed:"),
};
let mut buf = [0; 10];
let _buf_size = match socket.recv(&mut buf).await {
Ok(received) => println!("received {} bytes {:?}", received, &buf[..received]),
Err(_e) => panic!("recv function failed:"),
};
}

#[tokio::main]
async fn main() {
use net_hal_tokio::udp::TokioUdpSocket;
let mut handle = match TokioUdpSocket::bind(SocketAddr::from(([127, 0, 0, 1], 3400))).await{

let mut handle = match TokioUdpSocket::bind(SocketAddr::from(([127, 0, 0, 1], 3400))).await {
Ok(s) => s,
Err(_error) => panic!("couldn't bind to address"),
};
echo_recv(&mut handle).await;
}
}
11 changes: 5 additions & 6 deletions src/udp.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use addr_hal::SocketAddr;
use addr_hal::SocketAddr;
use async_trait::async_trait;
use net_hal::udp::{UdpServer, UdpSocket};

Expand Down Expand Up @@ -37,7 +37,6 @@ impl UdpSocket for TokioUdpSocket {
}
}


#[async_trait]
impl UdpServer for TokioUdpSocket {
type SA4 = addr::SocketV4Inner;
Expand All @@ -61,12 +60,13 @@ mod tests {

#[tokio::test]
async fn test_server() {
let mut server = match TokioUdpSocket::bind(SocketAddr::from(([127, 0, 0, 1], 3401))).await {
let mut server = match TokioUdpSocket::bind(SocketAddr::from(([127, 0, 0, 1], 3401))).await
{
Ok(s) => s,
Err(error) => panic!("couldn't bind to address{:?}", error),
};

let mut buf = [0;10];
let mut buf = [0; 10];
let _buf_size = match server.recv(&mut buf).await {
Ok(received) => println!("received {} bytes {:?}", received, &buf[..received]),
Err(e) => panic!("recv function failed: {:?}", e),
Expand All @@ -86,9 +86,8 @@ mod tests {
};

let _buf_size = match sock.send(&[0, 1, 2]).await {
Ok(s) => println!("send buffer size = {}",s),
Ok(s) => println!("send buffer size = {}", s),
Err(error) => panic!("couldn't send to address{:?}", error),
};
//assert_eq!(buf_size,3);
}
}

0 comments on commit ee69468

Please sign in to comment.