Skip to content

Commit

Permalink
Support both ipv4 and ipv6
Browse files Browse the repository at this point in the history
  • Loading branch information
Jamyw7g committed Oct 21, 2022
1 parent 825cafa commit 3cd94b0
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions src/shared_socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use pyo3::prelude::*;

use log::debug;
use socket2::{Domain, Protocol, Socket, Type};
use std::net::SocketAddr;
use std::net::{IpAddr, SocketAddr};

#[pyclass]
#[derive(Debug)]
Expand All @@ -13,9 +13,14 @@ pub struct SocketHeld {
#[pymethods]
impl SocketHeld {
#[new]
pub fn new(address: String, port: i32) -> PyResult<SocketHeld> {
let socket = Socket::new(Domain::IPV4, Type::STREAM, Some(Protocol::TCP))?;
let address: SocketAddr = format!("{}:{}", address, port).parse()?;
pub fn new(ip: String, port: u16) -> PyResult<SocketHeld> {
let ip: IpAddr = ip.parse()?;
let socket = if ip.is_ipv4() {
Socket::new(Domain::IPV4, Type::STREAM, Some(Protocol::TCP))?
} else {
Socket::new(Domain::IPV6, Type::STREAM, Some(Protocol::TCP))?
};
let address = SocketAddr::new(ip, port);
debug!("{}", address);
// reuse port is not available on windows
#[cfg(not(target_os = "windows"))]
Expand Down

0 comments on commit 3cd94b0

Please sign in to comment.