Skip to content

Commit

Permalink
Fix up windows files
Browse files Browse the repository at this point in the history
Signed-off-by: James Sturtevant <[email protected]>
  • Loading branch information
jsturtevant committed Feb 23, 2023
1 parent d0ced61 commit 263aea0
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 17 deletions.
8 changes: 3 additions & 5 deletions example/utils.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#![allow(dead_code)]
use std::fs;
use std::io::Result;
use std::path::Path;

#[cfg(unix)]
pub const SOCK_ADDR: &str = r"unix:///tmp/ttrpc-test";
Expand All @@ -15,15 +13,15 @@ pub fn remove_if_sock_exist(sock_addr: &str) -> Result<()> {
.strip_prefix("unix://")
.expect("socket address is not expected");

if Path::new(path).exists() {
fs::remove_file(path)?;
if std::path::Path::new(path).exists() {
std::fs::remove_file(path)?;
}

Ok(())
}

#[cfg(windows)]
pub fn remove_if_sock_exist(sock_addr: &str) -> Result<()> {
pub fn remove_if_sock_exist(_sock_addr: &str) -> Result<()> {
//todo force close file handle?

Ok(())
Expand Down
22 changes: 10 additions & 12 deletions src/sync/sys/windows/net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/

use crate::error::Result;
use crate::error::Error;

use mio::windows::NamedPipe;

Expand All @@ -29,7 +30,7 @@ use std::sync::{Arc, Mutex};
use std::{io};


use windows_sys::Win32::Foundation::{ERROR_NO_DATA, INVALID_HANDLE_VALUE, CloseHandle, GetLastError};
use windows_sys::Win32::Foundation::{ERROR_NO_DATA, INVALID_HANDLE_VALUE, CloseHandle};
use windows_sys::Win32::Storage::FileSystem::{
FILE_FLAG_FIRST_PIPE_INSTANCE, FILE_FLAG_OVERLAPPED, PIPE_ACCESS_DUPLEX,
};
Expand Down Expand Up @@ -130,13 +131,13 @@ impl PipeListener {
self.first_instance.swap(false, Ordering::SeqCst);
}

let h = match unsafe { CreateNamedPipeW(name.as_ptr(), open_mode, PIPE_TYPE_BYTE, PIPE_UNLIMITED_INSTANCES, 65536, 65536, 0, std::ptr::null_mut())} {
match unsafe { CreateNamedPipeW(name.as_ptr(), open_mode, PIPE_TYPE_BYTE, PIPE_UNLIMITED_INSTANCES, 65536, 65536, 0, std::ptr::null_mut())} {
INVALID_HANDLE_VALUE => {
return Err(io::Error::last_os_error());
return Err(io::Error::last_os_error())
}
h => {
let pipe = unsafe { NamedPipe::from_raw_handle(h as RawHandle) };
Ok(pipe)
return Ok(pipe)
},
};
}
Expand All @@ -152,9 +153,6 @@ pub struct PipeConnection {
poller: Mutex<Poll>,
}

unsafe impl Send for PipeConnection {}
unsafe impl Sync for PipeConnection {}

impl PipeConnection {
pub(crate) fn new(h: RawHandle) -> PipeConnection {
let mut pipe = unsafe { NamedPipe::from_raw_handle(h as RawHandle) };
Expand Down Expand Up @@ -219,14 +217,14 @@ impl PipeConnection {
continue;
}
Err(e) if e.raw_os_error() == Some(ERROR_NO_DATA as i32) => {
return Err(crate::Error::Windows(e.raw_os_error().unwrap()))
return Err(Error::Windows(e.raw_os_error().unwrap()))
}
Err(e) if e.raw_os_error().is_some() => {
return Err(crate::Error::Windows(e.raw_os_error().unwrap()))
return Err(Error::Windows(e.raw_os_error().unwrap()))
}
Err(e) => {
trace!("Error writing to pipe: {}", e);
return Err(crate::Error::Others(e.to_string()));
return Err(Error::Others(e.to_string()));
}
}
}
Expand All @@ -236,15 +234,15 @@ impl PipeConnection {
let h = self.named_pipe.lock().unwrap().as_raw_handle();
let result = unsafe { CloseHandle(h as isize) };
match result {
0 => Err(crate::Error::Windows(io::Error::last_os_error())),
0 => Err(Error::Windows(io::Error::last_os_error().raw_os_error().unwrap())),
_ => Ok(())
}
}

pub fn shutdown(&self) -> Result<()> {
match self.named_pipe.lock().unwrap().disconnect() {
Ok(_) => Ok(()),
Err(e) => Err(crate::Error::Others(e.to_string()))
Err(e) => Err(Error::Others(e.to_string()))
}
}
}
Expand Down

0 comments on commit 263aea0

Please sign in to comment.