magpie-client and magpie-types

This commit is contained in:
mars 2022-10-28 23:22:51 -06:00
parent e1230c9710
commit 4e46832cd0
4 changed files with 74 additions and 1 deletions

View File

@ -3,6 +3,7 @@ members = [
"apps/magpie",
"apps/music-player",
"apps/sandbox",
"crates/magpie-client",
"crates/magpie-types",
"crates/script",
"crates/types",

View File

@ -0,0 +1,8 @@
[package]
name = "magpie-client"
version = "0.1.0"
edition = "2021"
[dependencies]
magpie-types = { path = "../magpie-types" }
serde_json = "1"

View File

@ -0,0 +1,29 @@
use std::io::Write;
use std::os::unix::net::UnixStream;
use std::path::Path;
use magpie_types::MagpieServerMsg;
/// The name of the Magpie server socket.
pub const MAGPIE_SOCK: &str = "magpie.sock";
/// A client to a Magpie server.
pub struct MagpieClient {
socket: UnixStream,
}
impl MagpieClient {
pub fn new() -> std::io::Result<Self> {
let sock_dir = std::env::var("XDG_RUNTIME_DIR").expect("XDG_RUNTIME_DIR not set");
let sock_dir = Path::new(&sock_dir);
let sock_path = sock_dir.join(MAGPIE_SOCK);
let socket = UnixStream::connect(sock_path)?;
Ok(Self { socket })
}
pub fn send_msg(&mut self, msg: &MagpieServerMsg) -> std::io::Result<()> {
let bytes = serde_json::to_vec(msg).unwrap();
self.socket.write_all(&bytes)?;
Ok(())
}
}

View File

@ -1 +1,36 @@
// protocol types go here
use serde::{Deserialize, Serialize};
/// An identifier for a Magpie panel.
///
/// Only valid on a connection between a single client and its server. Clients
/// are allowed to use arbitrary values for [PanelId].
pub type PanelId = u32;
/// Creates a new Magpie panel with a given ID.
///
/// If the given [PanelId] is already being used on this connection, the server
/// will delete the old panel using that [PanelId].
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct CreatePanel {
pub id: PanelId,
}
/// Sends a panel a message.
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct SendMessage {
pub id: PanelId,
pub msg: Vec<u8>,
}
/// A message sent from a Magpie client to the server.
#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(tag = "kind")]
pub enum MagpieServerMsg {
CreatePanel(CreatePanel),
SendMessage(SendMessage),
}
/// A message sent from the Magpie server to a client.
#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(tag = "kind")]
pub enum MagpieClientMsg {}