Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix "Socket is already connected" #9 #11

Merged
merged 1 commit into from
Jun 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ version = '0.0.11'
keywords = ["stream", "udp", "dtls", "tokio"]

[dependencies]
bytes = "1.5"
bytes = "1.6"
log = "0.4"
tokio = { version = "1", features = ["rt", "sync", "net", "macros", "io-util"] }

Expand Down
9 changes: 5 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,17 +173,18 @@ impl UdpStream {
SocketAddr::new(IpAddr::V6(Ipv6Addr::UNSPECIFIED), 0)
};
let socket = UdpSocket::bind(local_addr).await?;
socket.connect(&addr).await?;
Self::from_tokio(socket).await
Self::from_tokio(socket, addr).await
}
/// Creates a new UdpStream from a tokio::net::UdpSocket.
/// This function is intended to be used to wrap a UDP socket from the tokio library.
/// Note: The UdpSocket must have the UdpSocket::connect method called before invoking this function.
pub async fn from_tokio(socket: UdpSocket) -> Result<Self, tokio::io::Error> {
pub async fn from_tokio(
socket: UdpSocket,
peer_addr: SocketAddr,
) -> Result<Self, tokio::io::Error> {
let socket = Arc::new(socket);

let local_addr = socket.local_addr()?;
let peer_addr = socket.peer_addr()?;

let (child_tx, child_rx) = mpsc::channel(CHANNEL_LEN);

Expand Down
Loading