bincode gaming

This commit is contained in:
2025-11-16 23:06:51 -05:00
parent 3ecd7a5565
commit 8a9e766633
7 changed files with 194 additions and 95 deletions

View File

@@ -1,14 +1,13 @@
use crate::{
client::{AppHandle, ClientEvent},
net::{SERVER_NAME, no_cert::SkipServerVerification},
net::{BINCODE_CONFIG, ClientMsg, SERVER_NAME, no_cert::SkipServerVerification},
};
use quinn::{ClientConfig, Connection, Endpoint, crypto::rustls::QuicClientConfig};
use std::{
net::{Ipv6Addr, SocketAddr, SocketAddrV6, ToSocketAddrs},
str::FromStr,
sync::Arc,
};
use tokio::sync::mpsc::UnboundedSender;
use tokio::{io::AsyncWriteExt, sync::mpsc::UnboundedSender};
pub const CLIENT_SOCKET: SocketAddr =
SocketAddr::V6(SocketAddrV6::new(Ipv6Addr::UNSPECIFIED, 0, 0, 0));
@@ -19,11 +18,7 @@ pub fn connect(handle: AppHandle, ip: String) {
});
}
pub enum NetCmd {
SendMsg(String),
}
pub type NetSender = UnboundedSender<NetCmd>;
pub type NetSender = UnboundedSender<ClientMsg>;
// async fn connection_cert(addr: SocketAddr) -> anyhow::Result<Connection> {
// let dirs = directories_next::ProjectDirs::from("", "", "openworm").unwrap();
@@ -71,30 +66,27 @@ async fn connection_no_cert(addr: SocketAddr) -> anyhow::Result<Connection> {
#[tokio::main]
async fn connect_the(handle: AppHandle, ip: String) -> anyhow::Result<()> {
let (client_send, mut client_recv) = tokio::sync::mpsc::unbounded_channel::<NetCmd>();
let (client_send, mut client_recv) = tokio::sync::mpsc::unbounded_channel::<ClientMsg>();
handle.send(ClientEvent::Connect(client_send));
let addr = ip.to_socket_addrs().unwrap().next().unwrap();
// let addr = SocketAddr::from_str(&ip).unwrap();
let conn = connection_no_cert(addr).await?;
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}"))?;
let bytes = bincode::encode_to_vec(msg, BINCODE_CONFIG).unwrap();
let (mut send, recv) = conn
.open_bi()
.await
.map_err(|e| anyhow::anyhow!("failed to open stream: {e}"))?;
drop(recv);
drop(recv);
send.write_all(content.as_bytes())
.await
.expect("failed to send");
send.finish().unwrap();
send.stopped().await.unwrap();
}
}
send.write_u64(bytes.len() as u64)
.await
.expect("failed to send");
send.write_all(&bytes).await.expect("failed to send");
send.finish().unwrap();
send.stopped().await.unwrap();
}
Ok(())