Add sample server + client unix socket
This commit is contained in:
parent
b94c621ab1
commit
6d1582792c
8 changed files with 127 additions and 27 deletions
72
Cargo.lock
generated
72
Cargo.lock
generated
|
@ -13,4 +13,76 @@ name = "command_gateway"
|
|||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"anyhow",
|
||||
"jsonrpc-derive",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "jsonrpc-derive"
|
||||
version = "18.0.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5b939a78fa820cdfcb7ee7484466746a7377760970f6f9c6fe19f9edcc8a38d2"
|
||||
dependencies = [
|
||||
"proc-macro-crate",
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "proc-macro-crate"
|
||||
version = "0.1.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1d6ea3c4595b96363c13943497db34af4460fb474a95c43f4446ad341b8c9785"
|
||||
dependencies = [
|
||||
"toml",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "proc-macro2"
|
||||
version = "1.0.50"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6ef7d57beacfaf2d8aee5937dab7b7f28de3cb8b1828479bb5de2a7106f2bae2"
|
||||
dependencies = [
|
||||
"unicode-ident",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "quote"
|
||||
version = "1.0.23"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8856d8364d252a14d474036ea1358d63c9e6965c8e5c1885c18f73d70bff9c7b"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "serde"
|
||||
version = "1.0.152"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "bb7d1f0d3021d347a83e556fc4683dea2ea09d87bccdf88ff5c12545d89d5efb"
|
||||
|
||||
[[package]]
|
||||
name = "syn"
|
||||
version = "1.0.107"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1f4064b5b16e03ae50984a5a8ed5d4f8803e6bc1fd170a3cda91a1be4b18e3f5"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"unicode-ident",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "toml"
|
||||
version = "0.5.11"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f4f7f0dd8d50a853a531c426359045b1998f04219d88799810762cd4ad314234"
|
||||
dependencies = [
|
||||
"serde",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "unicode-ident"
|
||||
version = "1.0.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "84a22b9f218b40614adcb3f4ff08b703773ad44fa9423e4e0d346d5db86e4ebc"
|
||||
|
|
10
Cargo.toml
10
Cargo.toml
|
@ -5,13 +5,17 @@ edition = "2021"
|
|||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[lib]
|
||||
crate-type = ["cdylib"]
|
||||
|
||||
[[bin]]
|
||||
name = "daemon"
|
||||
path = "src/daemon/bin/main.rs"
|
||||
path = "src/daemon/main.rs"
|
||||
|
||||
[[bin]]
|
||||
name = "client"
|
||||
path = "src/client/bin/main.rs"
|
||||
path = "src/client/main.rs"
|
||||
|
||||
[dependencies]
|
||||
anyhow = "^1.0.42"
|
||||
anyhow = "^1.0"
|
||||
jsonrpc-derive = "18.0"
|
|
@ -1,22 +0,0 @@
|
|||
use std::os::unix::net::{UnixListener, UnixStream};
|
||||
use std::io::{Read, Write};
|
||||
|
||||
use anyhow::Context;
|
||||
|
||||
fn main() -> anyhow::Result<()> {
|
||||
let socket_path = "mysocket";
|
||||
|
||||
let mut unix_stream =
|
||||
UnixStream::connect(socket_path).context("Could not create stream")?;
|
||||
|
||||
unix_stream
|
||||
.write(b"Hello?") // we write bytes, &[u8]
|
||||
.context("Failed at writing onto the unix stream")?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn handle_stream(mut stream: UnixStream) -> anyhow::Result<()> {
|
||||
// to be filled
|
||||
Ok(())
|
||||
}
|
41
src/client/main.rs
Normal file
41
src/client/main.rs
Normal file
|
@ -0,0 +1,41 @@
|
|||
use std::os::unix::net::{UnixListener, UnixStream};
|
||||
use std::io::{Read, Write};
|
||||
|
||||
use anyhow::Context;
|
||||
|
||||
fn main() -> anyhow::Result<()> {
|
||||
let socket_path = "mysocket";
|
||||
|
||||
let mut unix_stream =
|
||||
UnixStream::connect(socket_path).context("Could not create stream")?;
|
||||
|
||||
write_request_and_shutdown(&mut unix_stream)?;
|
||||
read_from_stream(&mut unix_stream)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn write_request_and_shutdown(unix_stream: &mut UnixStream) -> anyhow::Result<()> {
|
||||
unix_stream
|
||||
.write(b"Hello?")
|
||||
.context("Failed at writing onto the unix stream")?;
|
||||
|
||||
println!("We sent a request");
|
||||
println!("Shutting down writing on the stream, waiting for response...");
|
||||
|
||||
unix_stream
|
||||
.shutdown(std::net::Shutdown::Write)
|
||||
.context("Could not shutdown writing on the stream")?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn read_from_stream(unix_stream: &mut UnixStream) -> anyhow::Result<()> {
|
||||
let mut response = String::new();
|
||||
unix_stream
|
||||
.read_to_string(&mut response)
|
||||
.context("Failed at reading the unix stream")?;
|
||||
|
||||
println!("We received this response: {}", response);
|
||||
Ok(())
|
||||
}
|
0
src/common/internal_rpc.rs
Normal file
0
src/common/internal_rpc.rs
Normal file
1
src/common/mod.rs
Normal file
1
src/common/mod.rs
Normal file
|
@ -0,0 +1 @@
|
|||
pub mod internal_rpc;
|
|
@ -22,7 +22,6 @@ fn main() -> anyhow::Result<()> {
|
|||
.context("Failed at accepting a connection on the unix listener")?;
|
||||
handle_stream(unix_stream)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn handle_stream(mut unix_stream: UnixStream) -> anyhow::Result<()> {
|
||||
|
@ -31,6 +30,10 @@ fn handle_stream(mut unix_stream: UnixStream) -> anyhow::Result<()> {
|
|||
.read_to_string(&mut message)
|
||||
.context("Failed at reading the unix stream")?;
|
||||
|
||||
println!("{}", message);
|
||||
println!("We received this message: {}\nReplying...", message);
|
||||
|
||||
unix_stream
|
||||
.write(b"I hear you!")
|
||||
.context("Failed at writing onto the unix stream")?;
|
||||
Ok(())
|
||||
}
|
1
src/lib.rs
Normal file
1
src/lib.rs
Normal file
|
@ -0,0 +1 @@
|
|||
pub(self) mod common;
|
Reference in a new issue