-
-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
rust: Add some default channels to library
And move the ad-hoc implementation in examples/cp.rs and examples.pd.rs Signed-off-by: Siddharth Chandrasekaran <[email protected]>
- Loading branch information
Showing
6 changed files
with
89 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
use std::{ | ||
io::{Read,Write}, | ||
os::unix::net::{UnixStream, UnixListener}, | ||
path::PathBuf, | ||
str::FromStr, | ||
}; | ||
use super::Channel; | ||
|
||
type Result<T> = anyhow::Result<T, anyhow::Error>; | ||
|
||
pub struct UnixChannel { | ||
id: i32, | ||
stream: UnixStream, | ||
} | ||
|
||
impl UnixChannel { | ||
pub fn connect(name: &str) -> Result<Self> { | ||
let path = format!("/tmp/osdp-{name}"); | ||
let id = super::str_to_channel_id(&path); | ||
let stream = UnixStream::connect(&path)?; | ||
Ok(Self { | ||
id, | ||
stream, | ||
}) | ||
} | ||
|
||
pub fn new(name: &str) -> Result<Self> { | ||
let path = format!("/tmp/osdp-{name}"); | ||
let id = super::str_to_channel_id(&path); | ||
let path = PathBuf::from_str(&path)?; | ||
if path.exists() { | ||
std::fs::remove_file(&path)?; | ||
} | ||
let listener = UnixListener::bind(&path)?; | ||
println!("Waiting for connection to unix::{}", path.display()); | ||
let (stream, _) = listener.accept()?; | ||
Ok(Self { | ||
id, | ||
stream, | ||
}) | ||
} | ||
} | ||
|
||
impl Read for UnixChannel { | ||
fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> { | ||
self.stream.read(buf) | ||
} | ||
} | ||
|
||
impl Write for UnixChannel { | ||
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> { | ||
self.stream.write(buf) | ||
} | ||
|
||
fn flush(&mut self) -> std::io::Result<()> { | ||
self.stream.flush() | ||
} | ||
} | ||
|
||
impl Channel for UnixChannel { | ||
fn get_id(&self) -> i32 { | ||
self.id | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.