153 lines
5.8 KiB
Rust
153 lines
5.8 KiB
Rust
use std::net::SocketAddr;
|
|
|
|
use godot::{prelude::*, tools::get_autoload_by_name};
|
|
use teanet::Peer;
|
|
|
|
use crate::ui::cli::{CLI_GLOBAL_NAME, CliColor, CommandLinePanel};
|
|
|
|
#[derive(GodotClass)]
|
|
#[class(base=Node)]
|
|
pub struct NetworkManager {
|
|
peer: Option<PeerKind>,
|
|
|
|
base: Base<Node>,
|
|
}
|
|
|
|
pub const NETWORK_SINGLETON_NAME: &str = "NetworkManagerGlob";
|
|
|
|
#[godot_api]
|
|
impl INode for NetworkManager {
|
|
fn init(base: Base<Node>) -> Self {
|
|
NetworkManager { peer: None, base }
|
|
}
|
|
|
|
fn ready(&mut self) {
|
|
let mut cli = get_autoload_by_name::<CommandLinePanel>(CLI_GLOBAL_NAME);
|
|
|
|
cli.bind_mut()
|
|
.signals()
|
|
.on_host()
|
|
.connect_other(self, |s, port| s.host(port));
|
|
cli.bind_mut()
|
|
.signals()
|
|
.on_join()
|
|
.connect_other(self, |s, addr| s.join(addr.parse().unwrap()));
|
|
}
|
|
|
|
fn process(&mut self, delta: f64) {
|
|
if let Some(peer) = &mut self.peer {
|
|
let mut cli = get_autoload_by_name::<CommandLinePanel>(CLI_GLOBAL_NAME);
|
|
|
|
match peer {
|
|
PeerKind::Client(peer, _) => match peer.poll() {
|
|
Ok(message) => {
|
|
if let Some(message) = message {
|
|
match message {
|
|
teanet::PeerMessage::NewConnection(connection) => {
|
|
cli.bind_mut().publish_message(
|
|
format!("Connected to: {}", connection.address),
|
|
CliColor::Info,
|
|
);
|
|
}
|
|
teanet::PeerMessage::Disconnected(connection, connection_error) => {
|
|
cli.bind_mut().publish_message(
|
|
format!("Disconnected from: {}", connection.address),
|
|
CliColor::Info,
|
|
);
|
|
if let Some(err) = &connection_error {
|
|
cli.bind_mut().publish_message(
|
|
format!("Error with connection: {}", err),
|
|
CliColor::Error,
|
|
);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
Err(err) => {
|
|
cli.bind_mut()
|
|
.publish_message(format!("Error: {}", err), CliColor::Info);
|
|
}
|
|
},
|
|
PeerKind::Server(peer, _) => match peer.poll() {
|
|
Ok(message) => {
|
|
if let Some(message) = message {
|
|
match message {
|
|
teanet::PeerMessage::NewConnection(connection) => {
|
|
cli.bind_mut().publish_message(
|
|
format!("Client connected from: {}", connection.address),
|
|
CliColor::Info,
|
|
);
|
|
}
|
|
teanet::PeerMessage::Disconnected(connection, connection_error) => {
|
|
cli.bind_mut().publish_message(
|
|
format!("Client disconnected: {}", connection.address),
|
|
CliColor::Info,
|
|
);
|
|
if let Some(err) = &connection_error {
|
|
cli.bind_mut().publish_message(
|
|
format!("Error with connection: {}", err),
|
|
CliColor::Error,
|
|
);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
Err(err) => {
|
|
cli.bind_mut()
|
|
.publish_message(format!("Error: {}", err), CliColor::Info);
|
|
}
|
|
},
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
impl NetworkManager {
|
|
pub fn host(&mut self, port: u16) {
|
|
if self.peer.is_none() {
|
|
let mut cli = get_autoload_by_name::<CommandLinePanel>(CLI_GLOBAL_NAME);
|
|
cli.bind_mut()
|
|
.publish_message(format!("Hosting server at {}", port), CliColor::Info);
|
|
let peer = Peer::listen(Some(port));
|
|
match peer {
|
|
Ok(peer) => {
|
|
self.peer = Some(PeerKind::Server(peer, port));
|
|
|
|
cli.bind_mut()
|
|
.publish_message(format!("Server hosted"), CliColor::Info);
|
|
}
|
|
Err(err) => {
|
|
cli.bind_mut()
|
|
.publish_message(format!("Error hosting server: {}", err), CliColor::Error);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
pub fn join(&mut self, addr: SocketAddr) {
|
|
if self.peer.is_none() {
|
|
let mut cli = get_autoload_by_name::<CommandLinePanel>(CLI_GLOBAL_NAME);
|
|
cli.bind_mut()
|
|
.publish_message(format!("Joining server at {}", addr), CliColor::Info);
|
|
let peer = Peer::listen(None);
|
|
match peer {
|
|
Ok(mut peer) => {
|
|
peer.connect_to(addr);
|
|
self.peer = Some(PeerKind::Client(peer, addr));
|
|
}
|
|
Err(err) => {
|
|
cli.bind_mut()
|
|
.publish_message(format!("Error joining server: {}", err), CliColor::Error);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
pub enum PeerKind {
|
|
Client(Peer, SocketAddr),
|
|
Server(Peer, u16),
|
|
}
|