Directly pass socket address strings to the connect/bind functions

pull/5/head
Sebastian Dröge 5 years ago
parent c2374308b0
commit 5613f9e47d
  1. 9
      examples/autobahn-server.rs
  2. 8
      examples/echo-server.rs
  3. 34
      tests/communication.rs
  4. 18
      tests/handshakes.rs

@ -1,4 +1,4 @@
use async_std::net::{SocketAddr, TcpListener, TcpStream, ToSocketAddrs}; use async_std::net::{SocketAddr, TcpListener, TcpStream};
use async_tungstenite::accept_async; use async_tungstenite::accept_async;
use futures::{SinkExt, StreamExt}; use futures::{SinkExt, StreamExt};
use log::*; use log::*;
@ -19,12 +19,7 @@ async fn accept_connection(peer: SocketAddr, stream: TcpStream) {
async fn run() { async fn run() {
env_logger::init(); env_logger::init();
let addr = "127.0.0.1:9002" let addr = "127.0.0.1:9002";
.to_socket_addrs()
.await
.expect("Not a valid address")
.next()
.expect("Not a socket address");
let listener = TcpListener::bind(&addr).await.unwrap(); let listener = TcpListener::bind(&addr).await.unwrap();
info!("Listening on: {}", addr); info!("Listening on: {}", addr);

@ -10,7 +10,7 @@
use std::{env, io::Error}; use std::{env, io::Error};
use async_std::net::{TcpListener, TcpStream, ToSocketAddrs}; use async_std::net::{TcpListener, TcpStream};
use async_std::task; use async_std::task;
use futures::StreamExt; use futures::StreamExt;
use log::info; use log::info;
@ -20,12 +20,6 @@ async fn run() -> Result<(), Error> {
let addr = env::args() let addr = env::args()
.nth(1) .nth(1)
.unwrap_or_else(|| "127.0.0.1:8080".to_string()); .unwrap_or_else(|| "127.0.0.1:8080".to_string());
let addr = addr
.to_socket_addrs()
.await
.expect("Not a valid address")
.next()
.expect("Not a socket address");
// Create the event loop and TCP listener we'll accept connections on. // Create the event loop and TCP listener we'll accept connections on.
let try_socket = TcpListener::bind(&addr).await; let try_socket = TcpListener::bind(&addr).await;

@ -1,4 +1,4 @@
use async_std::net::{TcpListener, TcpStream, ToSocketAddrs}; use async_std::net::{TcpListener, TcpStream};
use async_std::task; use async_std::task;
use async_tungstenite::{accept_async, client_async, WebSocketStream}; use async_tungstenite::{accept_async, client_async, WebSocketStream};
use futures::{AsyncRead, AsyncWrite, SinkExt, StreamExt}; use futures::{AsyncRead, AsyncWrite, SinkExt, StreamExt};
@ -30,13 +30,7 @@ async fn communication() {
let (msg_tx, msg_rx) = futures::channel::oneshot::channel(); let (msg_tx, msg_rx) = futures::channel::oneshot::channel();
let f = async move { let f = async move {
let address = "0.0.0.0:12345" let listener = TcpListener::bind("0.0.0.0:12345").await.unwrap();
.to_socket_addrs()
.await
.expect("Not a valid address")
.next()
.expect("No address resolved");
let listener = TcpListener::bind(&address).await.unwrap();
info!("Server ready"); info!("Server ready");
con_tx.send(()).unwrap(); con_tx.send(()).unwrap();
info!("Waiting on next connection"); info!("Waiting on next connection");
@ -51,13 +45,7 @@ async fn communication() {
info!("Waiting for server to be ready"); info!("Waiting for server to be ready");
con_rx.await.expect("Server not ready"); con_rx.await.expect("Server not ready");
let address = "0.0.0.0:12345" let tcp = TcpStream::connect("0.0.0.0:12345")
.to_socket_addrs()
.await
.expect("Not a valid address")
.next()
.expect("No address resolved");
let tcp = TcpStream::connect(&address)
.await .await
.expect("Failed to connect"); .expect("Failed to connect");
let url = url::Url::parse("ws://localhost:12345/").unwrap(); let url = url::Url::parse("ws://localhost:12345/").unwrap();
@ -88,13 +76,7 @@ async fn split_communication() {
let (msg_tx, msg_rx) = futures::channel::oneshot::channel(); let (msg_tx, msg_rx) = futures::channel::oneshot::channel();
let f = async move { let f = async move {
let address = "0.0.0.0:12346" let listener = TcpListener::bind("0.0.0.0:12346").await.unwrap();
.to_socket_addrs()
.await
.expect("Not a valid address")
.next()
.expect("No address resolved");
let listener = TcpListener::bind(&address).await.unwrap();
info!("Server ready"); info!("Server ready");
con_tx.send(()).unwrap(); con_tx.send(()).unwrap();
info!("Waiting on next connection"); info!("Waiting on next connection");
@ -109,13 +91,7 @@ async fn split_communication() {
info!("Waiting for server to be ready"); info!("Waiting for server to be ready");
con_rx.await.expect("Server not ready"); con_rx.await.expect("Server not ready");
let address = "0.0.0.0:12346" let tcp = TcpStream::connect("0.0.0.0:12346")
.to_socket_addrs()
.await
.expect("Not a valid address")
.next()
.expect("No address resolved");
let tcp = TcpStream::connect(&address)
.await .await
.expect("Failed to connect"); .expect("Failed to connect");
let url = url::Url::parse("ws://localhost:12345/").unwrap(); let url = url::Url::parse("ws://localhost:12345/").unwrap();

@ -1,4 +1,4 @@
use async_std::net::{TcpListener, TcpStream, ToSocketAddrs}; use async_std::net::{TcpListener, TcpStream};
use async_std::task; use async_std::task;
use async_tungstenite::{accept_async, client_async}; use async_tungstenite::{accept_async, client_async};
@ -7,13 +7,7 @@ async fn handshakes() {
let (tx, rx) = futures::channel::oneshot::channel(); let (tx, rx) = futures::channel::oneshot::channel();
let f = async move { let f = async move {
let address = "0.0.0.0:12345" let listener = TcpListener::bind("0.0.0.0:12345").await.unwrap();
.to_socket_addrs()
.await
.expect("Not a valid address")
.next()
.expect("No address resolved");
let listener = TcpListener::bind(&address).await.unwrap();
tx.send(()).unwrap(); tx.send(()).unwrap();
while let Ok((connection, _)) = listener.accept().await { while let Ok((connection, _)) = listener.accept().await {
let stream = accept_async(connection).await; let stream = accept_async(connection).await;
@ -24,13 +18,7 @@ async fn handshakes() {
task::spawn(f); task::spawn(f);
rx.await.expect("Failed to wait for server to be ready"); rx.await.expect("Failed to wait for server to be ready");
let address = "0.0.0.0:12345" let tcp = TcpStream::connect("0.0.0.0:12345")
.to_socket_addrs()
.await
.expect("Not a valid address")
.next()
.expect("No address resolved");
let tcp = TcpStream::connect(&address)
.await .await
.expect("Failed to connect"); .expect("Failed to connect");
let url = url::Url::parse("ws://localhost:12345/").unwrap(); let url = url::Url::parse("ws://localhost:12345/").unwrap();

Loading…
Cancel
Save