Skip to content

Commit

Permalink
Merge pull request #186 from alexander-jackson/feat/return-raw-socket…
Browse files Browse the repository at this point in the history
…-address

feat: return raw socket address
  • Loading branch information
lipanski authored Nov 20, 2023
2 parents 496f26d + 2ed230b commit 3cce903
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ impl State {
///
#[derive(Debug)]
pub struct Server {
address: String,
address: SocketAddr,
state: Arc<RwLock<State>>,
}

Expand Down Expand Up @@ -199,7 +199,7 @@ impl Server {
pub(crate) fn try_new_with_port(port: u16) -> Result<Server, Error> {
let state = Arc::new(RwLock::new(State::new()));
let address = SocketAddr::from(([127, 0, 0, 1], port));
let (address_sender, address_receiver) = mpsc::channel::<String>();
let (address_sender, address_receiver) = mpsc::channel::<SocketAddr>();
let runtime = runtime::Builder::new_current_thread()
.enable_all()
.build()
Expand All @@ -226,7 +226,7 @@ impl Server {
pub(crate) async fn try_new_with_port_async(port: u16) -> Result<Server, Error> {
let state = Arc::new(RwLock::new(State::new()));
let address = SocketAddr::from(([127, 0, 0, 1], port));
let (address_sender, address_receiver) = mpsc::channel::<String>();
let (address_sender, address_receiver) = mpsc::channel::<SocketAddr>();
let runtime = runtime::Builder::new_current_thread()
.enable_all()
.build()
Expand All @@ -249,7 +249,7 @@ impl Server {

async fn bind_server(
address: SocketAddr,
address_sender: mpsc::Sender<String>,
address_sender: mpsc::Sender<SocketAddr>,
state: Arc<RwLock<State>>,
) -> Result<(), Error> {
let listener = TcpListener::bind(address)
Expand All @@ -260,7 +260,7 @@ impl Server {
.local_addr()
.map_err(|err| Error::new_with_context(ErrorKind::ServerFailure, err))?;

address_sender.send(address.to_string()).unwrap();
address_sender.send(address).unwrap();

while let Ok((stream, _)) = listener.accept().await {
let mutex = state.clone();
Expand Down Expand Up @@ -311,7 +311,14 @@ impl Server {
/// Can be used with `std::net::TcpStream`.
///
pub fn host_with_port(&self) -> String {
self.address.clone()
self.address.to_string()
}

///
/// The raw address of the mock server.
///
pub fn socket_address(&self) -> SocketAddr {
self.address
}

///
Expand Down

0 comments on commit 3cce903

Please sign in to comment.