Skip to content
New issue

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

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

Already on GitHub? # to your account

[Windows] Check for errors when connecting with client #182

Merged
merged 1 commit into from
Apr 29, 2023
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 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));
}
}
}
}