ui work
This commit is contained in:
754
Cargo.lock
generated
754
Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
@@ -5,8 +5,14 @@ edition = "2024"
|
||||
default-run = "client"
|
||||
|
||||
[dependencies]
|
||||
anyhow = "1.0.100"
|
||||
arboard = { version = "3.6.1", features = ["wayland-data-control"] }
|
||||
directories-next = "2.0.0"
|
||||
pollster = "0.4.0"
|
||||
quinn = { version = "0.11.9", features = ["rustls-aws-lc-rs"] }
|
||||
rcgen = "0.14.5"
|
||||
tokio = { version = "1.48.0", features = ["full"] }
|
||||
tracing = "0.1.41"
|
||||
ui = { path = "../ui" }
|
||||
wgpu = "27.0.1"
|
||||
winit = "0.30.12"
|
||||
|
||||
@@ -11,6 +11,8 @@ mod app;
|
||||
mod input;
|
||||
mod render;
|
||||
|
||||
use len_fns::*;
|
||||
|
||||
pub struct Client {
|
||||
renderer: Renderer,
|
||||
input: Input,
|
||||
@@ -38,59 +40,63 @@ impl Client {
|
||||
.text_align(Align::Left)
|
||||
.add(&mut ui);
|
||||
|
||||
let msg_area = Span::empty(Dir::DOWN).spacing(15).add(&mut ui);
|
||||
let msg_area = Span::empty(Dir::DOWN).gap(15).add(&mut ui);
|
||||
|
||||
quinn::rustls::crypto::aws_lc_rs::default_provider()
|
||||
.install_default()
|
||||
.unwrap();
|
||||
// connect().unwrap();
|
||||
|
||||
let msg_panel = (
|
||||
rect(Color::BLACK.brighter(0.1)),
|
||||
(
|
||||
msg_area
|
||||
.clone()
|
||||
.align(Align::BotLeft)
|
||||
.scroll()
|
||||
.pad(Padding::x(15)),
|
||||
(
|
||||
rect(Color::BLACK.brighter(0.05)).radius(15),
|
||||
send_text
|
||||
.clone()
|
||||
.id_on(Submit, move |id, client: &mut Client, _| {
|
||||
let content = client.ui.text(id).take();
|
||||
let content = text(content)
|
||||
.editable()
|
||||
.size(20)
|
||||
.text_align(Align::Left)
|
||||
.wrap(true)
|
||||
.id_on(CursorSense::click(), |id, client: &mut Client, ctx| {
|
||||
client.ui.text(id).select(ctx.cursor, ctx.size);
|
||||
client.focus = Some(id.clone());
|
||||
});
|
||||
let header = text("some user").size(20);
|
||||
let msg = (
|
||||
image(include_bytes!("./assets/sungals.png"))
|
||||
.sized(70)
|
||||
.align(Align::TopLeft),
|
||||
().sized(10),
|
||||
(header.align(Align::TopLeft), content.align(Align::TopLeft))
|
||||
.span(Dir::DOWN, [fixed(30), sized()]),
|
||||
)
|
||||
.span(Dir::RIGHT, [sized(), sized(), ratio(1)])
|
||||
.add(&mut client.ui);
|
||||
client.ui[&msg_area].children.push((msg.any(), sized()));
|
||||
msg_area
|
||||
.clone()
|
||||
.background(rect(Color::SKY))
|
||||
.align(Align::BotLeft)
|
||||
.scroll()
|
||||
.pad(Padding::x(15))
|
||||
.height(rest(1)),
|
||||
send_text
|
||||
.clone()
|
||||
.id_on(Submit, move |id, client: &mut Client, _| {
|
||||
let content = client.ui.text(id).take();
|
||||
let content = text(content)
|
||||
.editable()
|
||||
.size(20)
|
||||
.text_align(Align::Left)
|
||||
.wrap(true)
|
||||
.id_on(CursorSense::click(), |id, client: &mut Client, ctx| {
|
||||
client.ui.text(id).select(ctx.cursor, ctx.size);
|
||||
client.focus = Some(id.clone());
|
||||
})
|
||||
.pad(15)
|
||||
.on(CursorSense::click(), move |client: &mut Client, data| {
|
||||
client.ui.text(&send_text).select(data.cursor, data.size);
|
||||
client.focus = Some(send_text.clone());
|
||||
}),
|
||||
)
|
||||
.stack()
|
||||
.pad(15),
|
||||
)
|
||||
.span(Dir::DOWN, [ratio(1), fixed(80)]),
|
||||
.label("debug");
|
||||
let header = text("some user").size(20);
|
||||
let msg = (
|
||||
image(include_bytes!("./assets/sungals.png"))
|
||||
.sized((70, 70))
|
||||
.align(Align::TopLeft),
|
||||
(header.align(Align::TopLeft), content.align(Align::TopLeft))
|
||||
.span(Dir::DOWN)
|
||||
.gap(10),
|
||||
)
|
||||
.span(Dir::RIGHT)
|
||||
.add(&mut client.ui);
|
||||
client.ui[&msg_area].children.push(msg.any());
|
||||
})
|
||||
.pad(15)
|
||||
.on(CursorSense::click(), move |client: &mut Client, data| {
|
||||
client.ui.text(&send_text).select(data.cursor, data.size);
|
||||
client.focus = Some(send_text.clone());
|
||||
})
|
||||
.background(rect(Color::BLACK.brighter(0.05)).radius(15))
|
||||
.pad(15)
|
||||
.height(80),
|
||||
)
|
||||
.stack();
|
||||
.span(Dir::DOWN)
|
||||
.width(rest(1))
|
||||
.background(rect(Color::BLACK.brighter(0.1)));
|
||||
|
||||
(rect(Color::BLACK.brighter(0.05)), msg_panel)
|
||||
.span(Dir::RIGHT, [fixed(80), ratio(1)])
|
||||
(rect(Color::BLACK.brighter(0.05)).width(80), msg_panel)
|
||||
.span(Dir::RIGHT)
|
||||
.set_root(&mut ui);
|
||||
|
||||
Self {
|
||||
|
||||
@@ -1,2 +1,3 @@
|
||||
pub mod client;
|
||||
pub mod net;
|
||||
pub mod server;
|
||||
|
||||
51
src/net/client.rs
Normal file
51
src/net/client.rs
Normal file
@@ -0,0 +1,51 @@
|
||||
use quinn::{crypto::rustls::QuicClientConfig, rustls::pki_types::CertificateDer};
|
||||
use std::{
|
||||
fs,
|
||||
io::ErrorKind,
|
||||
net::{IpAddr, Ipv6Addr, SocketAddr},
|
||||
str::FromStr,
|
||||
sync::Arc,
|
||||
};
|
||||
|
||||
#[tokio::main]
|
||||
pub async fn connect() -> anyhow::Result<()> {
|
||||
let dirs = directories_next::ProjectDirs::from("", "", "openworm").unwrap();
|
||||
let mut roots = quinn::rustls::RootCertStore::empty();
|
||||
match fs::read(dirs.data_local_dir().join("cert.der")) {
|
||||
Ok(cert) => {
|
||||
roots.add(CertificateDer::from(cert))?;
|
||||
}
|
||||
Err(ref e) if e.kind() == ErrorKind::NotFound => {
|
||||
eprintln!("local server certificate not found");
|
||||
}
|
||||
Err(e) => {
|
||||
eprintln!("failed to open local server certificate: {}", e);
|
||||
}
|
||||
}
|
||||
let client_crypto = quinn::rustls::ClientConfig::builder()
|
||||
.with_root_certificates(roots)
|
||||
.with_no_client_auth();
|
||||
let client_config =
|
||||
quinn::ClientConfig::new(Arc::new(QuicClientConfig::try_from(client_crypto)?));
|
||||
let mut endpoint = quinn::Endpoint::client(SocketAddr::from_str("[::]:0").unwrap())?;
|
||||
endpoint.set_default_client_config(client_config);
|
||||
let remote = SocketAddr::new(IpAddr::V6(Ipv6Addr::LOCALHOST), 4433);
|
||||
let host = "localhost";
|
||||
let conn = endpoint
|
||||
.connect(remote, host)?
|
||||
.await
|
||||
.map_err(|e| anyhow::anyhow!("failed to connect: {}", e))?;
|
||||
|
||||
let (mut send, mut recv) = conn
|
||||
.open_bi()
|
||||
.await
|
||||
.map_err(|e| anyhow::anyhow!("failed to open stream: {}", e))?;
|
||||
|
||||
drop(recv);
|
||||
|
||||
send.write_all(&[39]).await.expect("failed to send");
|
||||
send.finish().unwrap();
|
||||
send.stopped().await.unwrap();
|
||||
|
||||
Ok(())
|
||||
}
|
||||
9
src/net/mod.rs
Normal file
9
src/net/mod.rs
Normal file
@@ -0,0 +1,9 @@
|
||||
pub mod client;
|
||||
|
||||
pub enum ClientMsg {
|
||||
SendMsg { content: String },
|
||||
}
|
||||
|
||||
pub enum ServerMsg {
|
||||
RecvMsg { content: String },
|
||||
}
|
||||
@@ -1,3 +1,122 @@
|
||||
pub fn run_server() {
|
||||
println!("hello world!")
|
||||
use quinn::{
|
||||
Endpoint,
|
||||
crypto::rustls::QuicServerConfig,
|
||||
rustls::pki_types::{CertificateDer, PrivateKeyDer, PrivatePkcs8KeyDer},
|
||||
};
|
||||
use std::{fs, net::SocketAddr, path::Path, str::FromStr, sync::Arc};
|
||||
use tracing::Instrument;
|
||||
|
||||
use crate::net::ClientMsg;
|
||||
|
||||
#[tokio::main]
|
||||
pub async fn run_server() {
|
||||
quinn::rustls::crypto::aws_lc_rs::default_provider()
|
||||
.install_default()
|
||||
.unwrap();
|
||||
|
||||
let dirs = directories_next::ProjectDirs::from("", "", "openworm").unwrap();
|
||||
let path = dirs.data_local_dir();
|
||||
let endpoint = listen(path);
|
||||
println!("listening on {}", endpoint.local_addr().unwrap());
|
||||
|
||||
while let Some(conn) = endpoint.accept().await {
|
||||
let fut = handle_connection(conn);
|
||||
tokio::spawn(async move {
|
||||
if let Err(e) = fut.await {
|
||||
eprintln!("connection failed: {reason}", reason = e)
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
println!("hello world!");
|
||||
}
|
||||
|
||||
pub fn listen(data_path: &Path) -> Endpoint {
|
||||
let cert_path = data_path.join("cert.der");
|
||||
let key_path = data_path.join("key.der");
|
||||
let (cert, key) = match fs::read(&cert_path).and_then(|x| Ok((x, fs::read(&key_path)?))) {
|
||||
Ok((cert, key)) => (
|
||||
CertificateDer::from(cert),
|
||||
PrivateKeyDer::try_from(key).unwrap(),
|
||||
),
|
||||
Err(ref e) if e.kind() == std::io::ErrorKind::NotFound => {
|
||||
let cert = rcgen::generate_simple_self_signed(vec!["localhost".into()]).unwrap();
|
||||
let key = PrivatePkcs8KeyDer::from(cert.signing_key.serialize_der());
|
||||
let cert = cert.cert.into();
|
||||
fs::create_dir_all(data_path).expect("failed to create certificate directory");
|
||||
fs::write(&cert_path, &cert).expect("failed to write certificate");
|
||||
fs::write(&key_path, key.secret_pkcs8_der()).expect("failed to write private key");
|
||||
(cert, key.into())
|
||||
}
|
||||
Err(e) => {
|
||||
panic!("failed to read certificate: {}", e);
|
||||
}
|
||||
};
|
||||
let server_crypto = quinn::rustls::ServerConfig::builder()
|
||||
.with_no_client_auth()
|
||||
.with_single_cert(vec![cert], key)
|
||||
.unwrap();
|
||||
|
||||
let mut server_config = quinn::ServerConfig::with_crypto(Arc::new(
|
||||
QuicServerConfig::try_from(server_crypto).unwrap(),
|
||||
));
|
||||
let transport_config = Arc::get_mut(&mut server_config.transport).unwrap();
|
||||
transport_config.max_concurrent_uni_streams(0_u8.into());
|
||||
|
||||
quinn::Endpoint::server(server_config, SocketAddr::from_str("[::1]:4433").unwrap()).unwrap()
|
||||
}
|
||||
|
||||
async fn handle_connection(conn: quinn::Incoming) -> std::io::Result<()> {
|
||||
let connection = conn.await?;
|
||||
let span = tracing::info_span!(
|
||||
"connection",
|
||||
remote = %connection.remote_address(),
|
||||
protocol = %connection
|
||||
.handshake_data()
|
||||
.unwrap()
|
||||
.downcast::<quinn::crypto::rustls::HandshakeData>().unwrap()
|
||||
.protocol
|
||||
.map_or_else(|| "<none>".into(), |x| String::from_utf8_lossy(&x).into_owned())
|
||||
);
|
||||
async {
|
||||
// Each stream initiated by the client constitutes a new request.
|
||||
loop {
|
||||
let stream = connection.accept_bi().await;
|
||||
// let time = Instant::now();
|
||||
let stream = match stream {
|
||||
Err(quinn::ConnectionError::ApplicationClosed { .. }) => {
|
||||
println!("connection closed");
|
||||
return Ok(());
|
||||
}
|
||||
Err(e) => {
|
||||
return Err(e);
|
||||
}
|
||||
Ok(s) => s,
|
||||
};
|
||||
tokio::spawn(
|
||||
async move {
|
||||
if let Err(e) = handle_stream(stream).await {
|
||||
eprintln!("failed: {reason}", reason = e);
|
||||
}
|
||||
}
|
||||
.instrument(tracing::info_span!("request")),
|
||||
);
|
||||
}
|
||||
}
|
||||
.instrument(span)
|
||||
.await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn handle_stream(
|
||||
(send, mut recv): (quinn::SendStream, quinn::RecvStream),
|
||||
) -> Result<(), String> {
|
||||
drop(send);
|
||||
let msg = recv
|
||||
.read_to_end(std::mem::size_of::<ClientMsg>())
|
||||
.await
|
||||
.map_err(|e| format!("failed reading request: {}", e))?;
|
||||
println!("received message");
|
||||
println!("{:?}", msg);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user