Skip to content

Commit

Permalink
Fix connection issues
Browse files Browse the repository at this point in the history
Fix connection issues
  • Loading branch information
vyfor authored Apr 11, 2024
2 parents a0729c1 + 4b3bcb1 commit 402b5d2
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/ipc/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub struct RichClient {
#[cfg(not(target_os = "windows"))]
pub struct RichClient {
pub client_id: u64,
pub pipe: std::os::unix::net::UnixStream,
pub pipe: Option<std::os::unix::net::UnixStream>,
pub last_activity: Option<Activity>,
}

Expand Down
17 changes: 8 additions & 9 deletions src/ipc/platform/unix_connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,11 @@ impl Connection for RichClient {
fn write(&mut self, opcode: u32, data: Option<&[u8]>) -> io::Result<()> {
if let Some(pipe) = self.pipe.as_mut() {
if let Some(packet) = data {
self.stream.write_all(
utils::encode(opcode, packet.len() as u32).as_slice(),
)?;
self.stream.write_all(packet)?;
let mut payload = utils::encode(opcode, packet.len() as u32);
payload.append(&mut packet.to_vec());
pipe.write_all(payload.as_slice())?;
} else {
self.stream.write_all(utils::encode(opcode, 0).as_slice())?;
pipe.write_all(utils::encode(opcode, 0).as_slice())?;
}
}
Ok(())
Expand All @@ -48,9 +47,9 @@ impl Connection for RichClient {
fn read(&mut self) -> Result<Vec<u8>, Box<dyn std::error::Error>> {
if let Some(pipe) = self.pipe.as_mut() {
let mut header = [0; 8];
self.stream.read_exact(&mut header)?;
pipe.read_exact(&mut header)?;
let mut buffer = vec![0u8; utils::decode(&header) as usize];
self.stream.read_exact(&mut buffer)?;
pipe.read_exact(&mut buffer)?;
return Ok(buffer);
}

Expand All @@ -59,8 +58,8 @@ impl Connection for RichClient {

fn close(&mut self) -> io::Result<()> {
if let Some(mut pipe) = self.pipe.take() {
self.stream.write_all(utils::encode(2, 0).as_slice())?;
self.stream.flush()?;
pipe.write_all(utils::encode(2, 0).as_slice())?;
pipe.flush()?;
}

Ok(())
Expand Down
7 changes: 3 additions & 4 deletions src/ipc/platform/windows_connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,9 @@ impl Connection for RichClient {
fn write(&mut self, opcode: u32, data: Option<&[u8]>) -> io::Result<()> {
if let Some(pipe) = self.pipe.as_mut() {
if let Some(packet) = data {
pipe.write_all(
utils::encode(opcode, packet.len() as u32).as_slice(),
)?;
pipe.write_all(packet)?;
let mut payload = utils::encode(opcode, packet.len() as u32);
payload.append(&mut packet.to_vec());
pipe.write_all(payload.as_slice())?;
} else {
pipe.write_all(utils::encode(opcode, 0).as_slice())?;
}
Expand Down

0 comments on commit 402b5d2

Please sign in to comment.