OPENWORM GAMAING (can send messages to server)
This commit is contained in:
75
:w
Normal file
75
:w
Normal file
@@ -0,0 +1,75 @@
|
|||||||
|
use crate::client::{AppHandle, ClientEvent};
|
||||||
|
use quinn::{crypto::rustls::QuicClientConfig, rustls::pki_types::CertificateDer};
|
||||||
|
use std::{
|
||||||
|
fs,
|
||||||
|
io::ErrorKind,
|
||||||
|
net::{IpAddr, Ipv6Addr, SocketAddr},
|
||||||
|
str::FromStr,
|
||||||
|
sync::Arc,
|
||||||
|
};
|
||||||
|
use tokio::sync::mpsc::UnboundedSender;
|
||||||
|
|
||||||
|
pub fn connect(handle: AppHandle) {
|
||||||
|
std::thread::spawn(|| {
|
||||||
|
connect_the(handle).unwrap();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
pub enum NetCmd {
|
||||||
|
SendMsg(String),
|
||||||
|
}
|
||||||
|
|
||||||
|
pub type NetSender = UnboundedSender<NetCmd>;
|
||||||
|
|
||||||
|
#[tokio::main]
|
||||||
|
async fn connect_the(handle: AppHandle) -> 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 (client_send, mut client_recv) = tokio::sync::mpsc::unbounded_channel::<NetCmd>();
|
||||||
|
|
||||||
|
handle.send(ClientEvent::Connect(client_send));
|
||||||
|
|
||||||
|
while let Some(msg) = client_recv.recv().await {
|
||||||
|
match msg {
|
||||||
|
NetCmd::SendMsg(content) => {
|
||||||
|
let (mut send, recv) = conn
|
||||||
|
.open_bi()
|
||||||
|
.await
|
||||||
|
.map_err(|e| anyhow::anyhow!("failed to open stream: {}", e))?;
|
||||||
|
|
||||||
|
drop(recv);
|
||||||
|
|
||||||
|
send.write_all(content.as_bytes()).await.expect("failed to send");
|
||||||
|
send.finish().unwrap();
|
||||||
|
send.stopped().await.unwrap();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
@@ -5,7 +5,10 @@ use iris::prelude::*;
|
|||||||
use render::Renderer;
|
use render::Renderer;
|
||||||
use winit::{event::WindowEvent, event_loop::ActiveEventLoop};
|
use winit::{event::WindowEvent, event_loop::ActiveEventLoop};
|
||||||
|
|
||||||
use crate::client::ui::{Submit, UiData};
|
use crate::{
|
||||||
|
client::ui::{Submit, main_view},
|
||||||
|
net::client::NetSender,
|
||||||
|
};
|
||||||
|
|
||||||
mod app;
|
mod app;
|
||||||
mod input;
|
mod input;
|
||||||
@@ -16,7 +19,7 @@ pub use app::AppHandle;
|
|||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub enum ClientEvent {
|
pub enum ClientEvent {
|
||||||
Connect,
|
Connect(NetSender),
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct Client {
|
pub struct Client {
|
||||||
@@ -24,7 +27,6 @@ pub struct Client {
|
|||||||
input: Input,
|
input: Input,
|
||||||
ui: Ui,
|
ui: Ui,
|
||||||
focus: Option<WidgetId<TextEdit>>,
|
focus: Option<WidgetId<TextEdit>>,
|
||||||
ui_data: UiData,
|
|
||||||
clipboard: Clipboard,
|
clipboard: Clipboard,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -32,13 +34,12 @@ impl Client {
|
|||||||
pub fn new(handle: AppHandle) -> Self {
|
pub fn new(handle: AppHandle) -> Self {
|
||||||
let renderer = Renderer::new(handle.window.clone());
|
let renderer = Renderer::new(handle.window.clone());
|
||||||
|
|
||||||
let (ui, ui_data) = ui::ui(handle);
|
let ui = ui::ui(handle);
|
||||||
|
|
||||||
Self {
|
Self {
|
||||||
renderer,
|
renderer,
|
||||||
input: Input::default(),
|
input: Input::default(),
|
||||||
ui,
|
ui,
|
||||||
ui_data,
|
|
||||||
focus: None,
|
focus: None,
|
||||||
clipboard: Clipboard::new().unwrap(),
|
clipboard: Clipboard::new().unwrap(),
|
||||||
}
|
}
|
||||||
@@ -46,8 +47,8 @@ impl Client {
|
|||||||
|
|
||||||
pub fn event(&mut self, event: ClientEvent, _: &ActiveEventLoop) {
|
pub fn event(&mut self, event: ClientEvent, _: &ActiveEventLoop) {
|
||||||
match event {
|
match event {
|
||||||
ClientEvent::Connect => {
|
ClientEvent::Connect(send) => {
|
||||||
self.ui.set_root(self.ui_data.main_view.clone());
|
main_view(&mut self.ui, send).set_root(&mut self.ui);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ use len_fns::*;
|
|||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
client::{Client, app::AppHandle},
|
client::{Client, app::AppHandle},
|
||||||
net::client::connect,
|
net::client::{NetCmd, NetSender, connect},
|
||||||
};
|
};
|
||||||
|
|
||||||
#[derive(Eq, PartialEq, Hash, Clone)]
|
#[derive(Eq, PartialEq, Hash, Clone)]
|
||||||
@@ -13,21 +13,17 @@ impl DefaultEvent for Submit {
|
|||||||
type Data = ();
|
type Data = ();
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct UiData {
|
pub fn ui(handle: AppHandle) -> Ui {
|
||||||
pub main_view: WidgetId<AnyWidget>,
|
let mut ui = Ui::new();
|
||||||
|
login_screen(&mut ui, handle).set_root(&mut ui);
|
||||||
|
ui
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn ui(handle: AppHandle) -> (Ui, UiData) {
|
pub fn main_view(ui: &mut Ui, network: NetSender) -> WidgetId<AnyWidget> {
|
||||||
let mut ui = Ui::new();
|
let msg_panel = msg_panel(ui, network);
|
||||||
|
|
||||||
let msg_panel = msg_panel(&mut ui);
|
|
||||||
let side_bar = rect(Color::BLACK.brighter(0.05)).width(80);
|
let side_bar = rect(Color::BLACK.brighter(0.05)).width(80);
|
||||||
let main_view = (side_bar, msg_panel).span(Dir::RIGHT).add(&mut ui).any();
|
|
||||||
|
|
||||||
login_screen(&mut ui, handle).set_root(&mut ui);
|
(side_bar, msg_panel).span(Dir::RIGHT).add(ui).any()
|
||||||
|
|
||||||
let data = UiData { main_view };
|
|
||||||
(ui, data)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn login_screen(ui: &mut Ui, handle: AppHandle) -> WidgetId<AnyWidget> {
|
fn login_screen(ui: &mut Ui, handle: AppHandle) -> WidgetId<AnyWidget> {
|
||||||
@@ -100,7 +96,7 @@ pub fn focus(id: WidgetId<TextEdit>) -> impl Fn(&mut Client, CursorData) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn msg_panel(ui: &mut Ui) -> impl WidgetFn<Stack> + use<> {
|
pub fn msg_panel(ui: &mut Ui, network: NetSender) -> impl WidgetFn<Stack> + use<> {
|
||||||
let msg_area = Span::empty(Dir::DOWN).gap(15).add(ui);
|
let msg_area = Span::empty(Dir::DOWN).gap(15).add(ui);
|
||||||
|
|
||||||
let send_text = text("some stuff idk")
|
let send_text = text("some stuff idk")
|
||||||
@@ -120,6 +116,7 @@ pub fn msg_panel(ui: &mut Ui) -> impl WidgetFn<Stack> + use<> {
|
|||||||
.clone()
|
.clone()
|
||||||
.id_on(Submit, move |id, client: &mut Client, _| {
|
.id_on(Submit, move |id, client: &mut Client, _| {
|
||||||
let content = client.ui.text(id).take();
|
let content = client.ui.text(id).take();
|
||||||
|
network.send(NetCmd::SendMsg(content.clone())).unwrap();
|
||||||
let msg = msg_widget(content).add(&mut client.ui);
|
let msg = msg_widget(content).add(&mut client.ui);
|
||||||
client.ui[&msg_area].children.push(msg.any());
|
client.ui[&msg_area].children.push(msg.any());
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ use std::{
|
|||||||
str::FromStr,
|
str::FromStr,
|
||||||
sync::Arc,
|
sync::Arc,
|
||||||
};
|
};
|
||||||
|
use tokio::sync::mpsc::UnboundedSender;
|
||||||
|
|
||||||
pub fn connect(handle: AppHandle) {
|
pub fn connect(handle: AppHandle) {
|
||||||
std::thread::spawn(|| {
|
std::thread::spawn(|| {
|
||||||
@@ -14,6 +15,12 @@ pub fn connect(handle: AppHandle) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub enum NetCmd {
|
||||||
|
SendMsg(String),
|
||||||
|
}
|
||||||
|
|
||||||
|
pub type NetSender = UnboundedSender<NetCmd>;
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn connect_the(handle: AppHandle) -> anyhow::Result<()> {
|
async fn connect_the(handle: AppHandle) -> anyhow::Result<()> {
|
||||||
let dirs = directories_next::ProjectDirs::from("", "", "openworm").unwrap();
|
let dirs = directories_next::ProjectDirs::from("", "", "openworm").unwrap();
|
||||||
@@ -43,18 +50,26 @@ async fn connect_the(handle: AppHandle) -> anyhow::Result<()> {
|
|||||||
.await
|
.await
|
||||||
.map_err(|e| anyhow::anyhow!("failed to connect: {}", e))?;
|
.map_err(|e| anyhow::anyhow!("failed to connect: {}", e))?;
|
||||||
|
|
||||||
let (mut send, recv) = conn
|
let (client_send, mut client_recv) = tokio::sync::mpsc::unbounded_channel::<NetCmd>();
|
||||||
.open_bi()
|
|
||||||
.await
|
|
||||||
.map_err(|e| anyhow::anyhow!("failed to open stream: {}", e))?;
|
|
||||||
|
|
||||||
drop(recv);
|
handle.send(ClientEvent::Connect(client_send));
|
||||||
|
|
||||||
handle.send(ClientEvent::Connect);
|
while let Some(msg) = client_recv.recv().await {
|
||||||
|
match msg {
|
||||||
|
NetCmd::SendMsg(content) => {
|
||||||
|
let (mut send, recv) = conn
|
||||||
|
.open_bi()
|
||||||
|
.await
|
||||||
|
.map_err(|e| anyhow::anyhow!("failed to open stream: {}", e))?;
|
||||||
|
|
||||||
send.write_all(&[39]).await.expect("failed to send");
|
drop(recv);
|
||||||
send.finish().unwrap();
|
|
||||||
send.stopped().await.unwrap();
|
send.write_all(content.as_bytes()).await.expect("failed to send");
|
||||||
|
send.finish().unwrap();
|
||||||
|
send.stopped().await.unwrap();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -112,11 +112,11 @@ async fn handle_stream(
|
|||||||
(send, mut recv): (quinn::SendStream, quinn::RecvStream),
|
(send, mut recv): (quinn::SendStream, quinn::RecvStream),
|
||||||
) -> Result<(), String> {
|
) -> Result<(), String> {
|
||||||
drop(send);
|
drop(send);
|
||||||
let msg = recv
|
let bytes = recv
|
||||||
.read_to_end(std::mem::size_of::<ClientMsg>())
|
.read_to_end(std::mem::size_of::<ClientMsg>())
|
||||||
.await
|
.await
|
||||||
.map_err(|e| format!("failed reading request: {}", e))?;
|
.map_err(|e| format!("failed reading request: {}", e))?;
|
||||||
println!("received message");
|
let msg = String::from_utf8(bytes).unwrap();
|
||||||
println!("{:?}", msg);
|
println!("received message: {:?}", msg);
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user