Skip to content

Commit

Permalink
Merge pull request #182 from jsturtevant/fix-get-client-connection
Browse files Browse the repository at this point in the history
[Windows] Check for errors when connecting with client
  • Loading branch information
wllenyj authored Apr 29, 2023
2 parents 22cd9ca + d96bea3 commit 2396cf3
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use std::result;
use thiserror::Error;

/// The error type for ttrpc.
#[derive(Error, Debug, Clone)]
#[derive(Error, Debug, Clone, PartialEq)]
pub enum Error {
#[error("socket err: {0}")]
Socket(String),
Expand Down
30 changes: 27 additions & 3 deletions src/sync/sys/windows/net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -321,9 +321,14 @@ impl ClientConnection {
opts.read(true)
.write(true)
.custom_flags(FILE_FLAG_OVERLAPPED);
let file = opts.open(self.address.as_str());

return PipeConnection::new(file.unwrap().into_raw_handle() as isize)
match opts.open(self.address.as_str()) {
Ok(file) => {
return PipeConnection::new(file.into_raw_handle() as isize)
}
Err(e) => {
return Err(Error::Windows(e.raw_os_error().unwrap()))
}
}
}

pub fn close_receiver(&self) -> Result<()> {
Expand All @@ -336,3 +341,22 @@ impl ClientConnection {
Ok(())
}
}

#[cfg(test)]
mod test {
use super::*;
use windows_sys::Win32::Foundation::ERROR_FILE_NOT_FOUND;

#[test]
fn test_pipe_connection() {
let client = ClientConnection::new("non_existent_pipe");
match client.get_pipe_connection() {
Ok(_) => {
assert!(false, "should not be able to get a connection to a non existent pipe");
}
Err(e) => {
assert_eq!(e, Error::Windows(ERROR_FILE_NOT_FOUND as i32));
}
}
}
}

0 comments on commit 2396cf3

Please # to comment.