Skip to content

Commit 2396cf3

Browse files
authored
Merge pull request #182 from jsturtevant/fix-get-client-connection
[Windows] Check for errors when connecting with client
2 parents 22cd9ca + d96bea3 commit 2396cf3

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
lines changed

src/error.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use std::result;
1919
use thiserror::Error;
2020

2121
/// The error type for ttrpc.
22-
#[derive(Error, Debug, Clone)]
22+
#[derive(Error, Debug, Clone, PartialEq)]
2323
pub enum Error {
2424
#[error("socket err: {0}")]
2525
Socket(String),

src/sync/sys/windows/net.rs

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -321,9 +321,14 @@ impl ClientConnection {
321321
opts.read(true)
322322
.write(true)
323323
.custom_flags(FILE_FLAG_OVERLAPPED);
324-
let file = opts.open(self.address.as_str());
325-
326-
return PipeConnection::new(file.unwrap().into_raw_handle() as isize)
324+
match opts.open(self.address.as_str()) {
325+
Ok(file) => {
326+
return PipeConnection::new(file.into_raw_handle() as isize)
327+
}
328+
Err(e) => {
329+
return Err(Error::Windows(e.raw_os_error().unwrap()))
330+
}
331+
}
327332
}
328333

329334
pub fn close_receiver(&self) -> Result<()> {
@@ -336,3 +341,22 @@ impl ClientConnection {
336341
Ok(())
337342
}
338343
}
344+
345+
#[cfg(test)]
346+
mod test {
347+
use super::*;
348+
use windows_sys::Win32::Foundation::ERROR_FILE_NOT_FOUND;
349+
350+
#[test]
351+
fn test_pipe_connection() {
352+
let client = ClientConnection::new("non_existent_pipe");
353+
match client.get_pipe_connection() {
354+
Ok(_) => {
355+
assert!(false, "should not be able to get a connection to a non existent pipe");
356+
}
357+
Err(e) => {
358+
assert_eq!(e, Error::Windows(ERROR_FILE_NOT_FOUND as i32));
359+
}
360+
}
361+
}
362+
}

0 commit comments

Comments
 (0)