Skip to content

Commit

Permalink
feat:update multi
Browse files Browse the repository at this point in the history
  • Loading branch information
limuy2022 committed Jun 20, 2024
1 parent 73844a2 commit 472f550
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 10 deletions.
1 change: 1 addition & 0 deletions gdrust/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion gdrust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ godot = { git = "https://github.com/godot-rust/gdext", branch = "master" }
rand = "0.8"
derive = { path = "./derive" }
proto = { path = "proto" }
tokio = "1"
tokio = { version = "1", features = ["full"] }
anyhow = "1"
base = { path = "base" }
prost = "0"
prost-types = { version = "0", optional = true }
bytes = "1"


[lib]
Expand Down
6 changes: 6 additions & 0 deletions gdrust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use std::{
panic::{set_hook, PanicInfo},
sync::{Arc, Mutex, OnceLock},
};
use tokio::runtime::{Builder, Runtime};

struct GdExtension;

Expand All @@ -25,6 +26,11 @@ fn get_multi_single() -> &'static MultiSingle {
TMP.get_or_init(|| Arc::new(Mutex::new(MultiManagerImpl::new())))
}

fn get_tokio_runtime() -> &'static Runtime {
static TMP: OnceLock<Runtime> = OnceLock::new();
TMP.get_or_init(|| Builder::new_multi_thread().enable_all().build().unwrap())
}

fn panic_handler(info: &PanicInfo) {
if let Some(p) = info.location() {
godot_error!(
Expand Down
50 changes: 41 additions & 9 deletions gdrust/src/multi.rs
Original file line number Diff line number Diff line change
@@ -1,48 +1,80 @@
use anyhow::Ok;
use anyhow::{anyhow, Ok};
use bytes::{Bytes, BytesMut};
use godot::engine::{INode, Node};
use godot::prelude::*;
use proto::connect::Join;
use prost::Message;
use proto::connect::{self, Join};
use std::collections::HashMap;
use std::sync::{Arc, Mutex};
use tokio::io::AsyncWriteExt;
use tokio::net::TcpStream;
use tokio::runtime::{Builder, Runtime};
use tokio::sync::mpsc;

use crate::get_multi_single;
use crate::{get_multi_single, get_tokio_runtime};

pub struct MultiPlayerConnection {}

impl MultiPlayerConnection {}

pub struct MultiManagerImpl {
clients: HashMap<usize, MultiPlayerConnection>,
socket: Option<TcpStream>,
runtime: Runtime,
socket: Option<mpsc::Sender<bytes::Bytes>>,
}

async fn send(sender: mpsc::Sender<Bytes>, data: BytesMut) -> anyhow::Result<()> {
sender.send(data.into()).await?;
Ok(())
}

impl MultiManagerImpl {
pub fn connect_to_server(&mut self, ip: String) -> anyhow::Result<()> {
if self.socket.is_some() {
godot_warn!("Socket has value,but reset")
}
self.socket = Some(TcpStream::from_std(std::net::TcpStream::connect(&ip)?)?);
// self.socket = Some(Arc::new(Mutex::new(TcpStream::from_std(
// std::net::TcpStream::connect(&ip)?,
// )?)));
let (sender, mut receiver) = mpsc::channel(32);
self.socket = Some(sender);
get_tokio_runtime().spawn(async move {
let mut socket = TcpStream::connect(&ip).await?;
while let Some(data) = receiver.recv().await {
socket.write_all(&data).await?;
}
Ok(())
});
Ok(())
}

pub fn join_to_server(&mut self, player_name: String) -> anyhow::Result<()> {
let mut data = Join {
let socket = match &mut self.socket {
None => {
godot_error!("Socket doesn't exist");
return Err(anyhow!("Socket doesn't exist"));
}
Some(s) => s,
};
let data = Join {
player_name,
version: base::build::COMMIT_HASH.to_string(),
};
let mut buf = bytes::BytesMut::new();
data.encode(&mut buf)?;
let sender = socket.clone();
get_tokio_runtime().spawn(send(sender, buf));
Ok(())
}

pub fn close(&mut self) {
self.socket = None;
}
}

impl MultiManagerImpl {
pub fn new() -> Self {
Self {
clients: HashMap::new(),
socket: None,
runtime: Builder::new_multi_thread().enable_all().build().unwrap(),
}
}
}
Expand Down

0 comments on commit 472f550

Please sign in to comment.