work
This commit is contained in:
@@ -9,53 +9,37 @@ use quinn::{
|
||||
use std::{
|
||||
net::{Ipv6Addr, SocketAddr, SocketAddrV6, ToSocketAddrs},
|
||||
sync::Arc,
|
||||
thread::JoinHandle,
|
||||
time::Duration,
|
||||
};
|
||||
use tokio::sync::mpsc::UnboundedSender;
|
||||
use winit::{event_loop::EventLoopProxy, window::Window};
|
||||
use tokio::sync::{mpsc::UnboundedSender, oneshot::Receiver};
|
||||
use winit::event_loop::EventLoopProxy;
|
||||
|
||||
pub const CLIENT_SOCKET: SocketAddr =
|
||||
SocketAddr::V6(SocketAddrV6::new(Ipv6Addr::UNSPECIFIED, 0, 0, 0));
|
||||
|
||||
pub struct ConnectInfo {
|
||||
pub ip: String,
|
||||
pub url: String,
|
||||
}
|
||||
|
||||
pub struct NetHandle {
|
||||
pub send: NetSender,
|
||||
pub thread: JoinHandle<()>,
|
||||
send: UnboundedSender<NetCtrlMsg>,
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct AppHandle {
|
||||
pub proxy: EventLoopProxy<ClientEvent>,
|
||||
pub window: Arc<Window>,
|
||||
}
|
||||
|
||||
impl AppHandle {
|
||||
pub fn send(&self, event: ClientEvent) {
|
||||
self.proxy.send_event(event).unwrap_or_else(|_| panic!());
|
||||
self.window.request_redraw();
|
||||
}
|
||||
}
|
||||
|
||||
pub fn connect(handle: AppHandle, info: ConnectInfo) -> JoinHandle<()> {
|
||||
std::thread::spawn(move || {
|
||||
if let Err(msg) = connect_the(handle.clone(), info) {
|
||||
handle.send(ClientEvent::Err(msg));
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
type NetResult<T> = Result<T, String>;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct NetSender {
|
||||
send: UnboundedSender<NetCtrlMsg>,
|
||||
}
|
||||
|
||||
pub enum NetCtrlMsg {
|
||||
Exchange(ClientMsg, Receiver<>),
|
||||
Send(ClientMsg),
|
||||
Exit,
|
||||
}
|
||||
@@ -66,12 +50,6 @@ impl From<ClientMsg> for NetCtrlMsg {
|
||||
}
|
||||
}
|
||||
|
||||
impl NetSender {
|
||||
pub fn send(&self, msg: impl Into<NetCtrlMsg>) {
|
||||
let _ = self.send.send(msg.into());
|
||||
}
|
||||
}
|
||||
|
||||
impl NetHandle {
|
||||
pub fn send(&self, msg: impl Into<NetCtrlMsg>) {
|
||||
self.send.send(msg.into());
|
||||
@@ -79,7 +57,6 @@ impl NetHandle {
|
||||
|
||||
pub fn exit(self) {
|
||||
self.send(NetCtrlMsg::Exit);
|
||||
let _ = self.thread.join();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -140,12 +117,11 @@ async fn connection_no_cert(addr: SocketAddr) -> NetResult<(Endpoint, Connection
|
||||
Ok((endpoint, con))
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
async fn connect_the(handle: AppHandle, info: ConnectInfo) -> NetResult<()> {
|
||||
pub async fn connect(msg: impl MsgHandler, info: ConnectInfo) -> Result<NetHandle, String> {
|
||||
let (send, mut ui_recv) = tokio::sync::mpsc::unbounded_channel::<NetCtrlMsg>();
|
||||
|
||||
let addr = info
|
||||
.ip
|
||||
.url
|
||||
.to_socket_addrs()
|
||||
.map_err(|e| e.to_string())?
|
||||
.next()
|
||||
@@ -153,38 +129,44 @@ async fn connect_the(handle: AppHandle, info: ConnectInfo) -> NetResult<()> {
|
||||
let (endpoint, conn) = connection_no_cert(addr).await?;
|
||||
let conn_ = conn.clone();
|
||||
|
||||
handle.send(ClientEvent::Connect {
|
||||
send: NetSender { send },
|
||||
});
|
||||
|
||||
let recv = ServerRecv { handle };
|
||||
let recv = ServerRecv { msg };
|
||||
tokio::spawn(recv_uni(conn_, recv.into()));
|
||||
|
||||
while let Some(msg) = ui_recv.recv().await {
|
||||
match msg {
|
||||
NetCtrlMsg::Send(msg) => {
|
||||
if send_uni(&conn, msg).await.is_err() {
|
||||
println!("disconnected from server");
|
||||
tokio::spawn(async move {
|
||||
while let Some(msg) = ui_recv.recv().await {
|
||||
match msg {
|
||||
NetCtrlMsg::Send(msg) => {
|
||||
if send_uni(&conn, msg).await.is_err() {
|
||||
println!("disconnected from server");
|
||||
break;
|
||||
}
|
||||
}
|
||||
NetCtrlMsg::Exit => {
|
||||
conn.close(0u32.into(), &[]);
|
||||
endpoint.wait_idle().await;
|
||||
break;
|
||||
}
|
||||
}
|
||||
NetCtrlMsg::Exit => {
|
||||
conn.close(0u32.into(), &[]);
|
||||
endpoint.wait_idle().await;
|
||||
break;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
Ok(NetHandle { send })
|
||||
}
|
||||
|
||||
pub trait MsgHandler: Sync + Send + 'static {
|
||||
fn run(&self, msg: ServerMsg) -> impl Future<Output = ()> + Send;
|
||||
}
|
||||
impl<F: AsyncFn(ServerMsg) + Sync + Send + 'static> MsgHandler for F {
|
||||
async fn run(&self, msg: ServerMsg) {
|
||||
self(msg);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
struct ServerRecv {
|
||||
handle: AppHandle,
|
||||
struct ServerRecv<F: MsgHandler> {
|
||||
msg: F,
|
||||
}
|
||||
|
||||
impl RecvHandler<ServerMsg> for ServerRecv {
|
||||
impl<F: MsgHandler> RecvHandler<ServerMsg> for ServerRecv<F> {
|
||||
async fn msg(&self, msg: ServerMsg) {
|
||||
self.handle.send(ClientEvent::ServerMsg(msg));
|
||||
self.msg.run(msg).await;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user