Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correcting typo (TransportLater actually sounds nice!) #41

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions crates/proto/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ pub enum TransportLayerError {
IOError(#[from] Arc<IOError>),
#[error("Raknet UDP Error: {0}")]
RaknetUDPError(#[from] RaknetError),
#[error("UDP Error: {0}")]
UDPError(#[from] UdpError),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove udp stuff please

}

#[derive(Error, Debug, Clone)]
Expand All @@ -84,3 +86,11 @@ pub enum RaknetError {
#[error("Format Error: {0}")]
FormatError(String),
}

#[derive(Error, Debug, Clone)]
pub enum UdpError {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove udp stuff please

#[error("Error while Receive: {0}")]
RecvError(#[from] RecvError),
#[error("Format Error: {0}")]
FormatError(String),
}
6 changes: 3 additions & 3 deletions crates/proto/src/listener.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ use rand::RngCore;
use crate::connection::Connection;
use crate::error::{ListenerError, RaknetError, TransportLayerError};
use crate::info::{MINECRAFT_EDITION_MOTD, MINECRAFT_VERSION, PROTOCOL_VERSION};
use crate::transport_layer::TransportLaterListener;
use crate::transport_layer::TransportLayerListener;

pub struct Listener {
listener: TransportLaterListener,
listener: TransportLayerListener,
name: String,
sub_name: String,
player_count_max: u32,
Expand Down Expand Up @@ -61,7 +61,7 @@ impl Listener {
};

Ok(Self {
listener: TransportLaterListener::RaknetUDP(rak_listener),
listener: TransportLayerListener::RaknetUDP(rak_listener),
name,
sub_name,
player_count_max,
Expand Down
58 changes: 55 additions & 3 deletions crates/proto/src/transport_layer/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::sync::Arc;

use bedrockrs_core::int::LE;

use crate::error::{RaknetError, TransportLayerError};
use crate::error::{RaknetError, TransportLayerError, UdpError};
use crate::info::RAKNET_GAME_PACKET_ID;

///
Expand All @@ -12,8 +12,8 @@ pub enum TransportLayerConnection {
// TODO RaknetTCP(...),
NetherNet(/* TODO */),
// TODO Quic(s2n_quic::connection::Connection),
// TODO Tcp(net::TcpStream),
// TODO Udp(net::UdpSocket)
// Tcp(std::net::TcpStream),
Udp(tokio::net::UdpSocket) //MCBE over UDP :sobb:??
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove udp stuff please

}

impl TransportLayerConnection {
Expand All @@ -34,6 +34,22 @@ impl TransportLayerConnection {
.await
.map_err(|e| TransportLayerError::RaknetUDPError(RaknetError::SendError(e)))
}
TransportLayerConnection::Udp(conn) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove udp stuff please

//BIO
let mut final_stream = vec![];

LE::<u8>::write(&LE::new(RAKNET_GAME_PACKET_ID), &mut final_stream)
.map_err(|e| TransportLayerError::IOError(Arc::new(e)))?;

final_stream
.write_all(stream.get_ref())
.map_err(|e| TransportLayerError::IOError(Arc::new(e)))?;

conn.send(final_stream.as_slice())
.await
.map(|_| ())
.map_err(|e| TransportLayerError::IOError(Arc::new(e))) //udp send error is std::io::Error
}
_ => {
todo!()
}
Expand Down Expand Up @@ -70,6 +86,39 @@ impl TransportLayerConnection {
.map_err(|e| TransportLayerError::IOError(Arc::new(e)))?)
}

TransportLayerConnection::Udp(conn) => {
// TODO: Configure the socket for non-blocking (NIO) mode to improve efficiency and responsiveness.
// Non-blocking I/O allows the program to perform other tasks while waiting for I/O operations to complete,
// thereby avoiding potential bottlenecks and enhancing overall performance in high-concurrency scenarios.
let mut recv_buffer: Vec<u8> = vec![0; 4096];
let _amt = conn
.recv(&mut recv_buffer)
.await
.map(|_| ())
.map_err(|e| TransportLayerError::IOError(Arc::new(e)))?;

let mut recv_buffer = Cursor::new(recv_buffer.as_slice());

match LE::<u8>::read(&mut recv_buffer)
.map_err(|e| TransportLayerError::IOError(Arc::new(e)))?
.into_inner()
{
RAKNET_GAME_PACKET_ID => {}
other => {
return Err(TransportLayerError::UDPError(
UdpError::FormatError(format!(
"Expected RakNet Game Packet ID ({:?}), got: {:?}",
RAKNET_GAME_PACKET_ID, other
)),
));
}
};

Ok(stream
.write_all(recv_buffer.into_inner())
.map_err(|e| TransportLayerError::IOError(Arc::new(e)))?)
}

_ => {
todo!()
}
Expand All @@ -81,6 +130,9 @@ impl TransportLayerConnection {
TransportLayerConnection::RaknetUDP(conn) => {
conn.close().await;
}
TransportLayerConnection::Udp(socket) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove udp stuff please

std::mem::drop(socket); //for closing the socket explicitly(...do we need it?)
}
_ => {
todo!()
}
Expand Down
8 changes: 4 additions & 4 deletions crates/proto/src/transport_layer/listener.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
use crate::error::{RaknetError, TransportLayerError};
use crate::transport_layer::TransportLayerConnection;

pub enum TransportLaterListener {
pub enum TransportLayerListener {
RaknetUDP(rak_rs::Listener),
NetherNet(/* TODO */),
}

impl TransportLaterListener {
impl TransportLayerListener {
pub async fn start(&mut self) -> Result<(), TransportLayerError> {
match self {
TransportLaterListener::RaknetUDP(listener) => match listener.start().await {
TransportLayerListener::RaknetUDP(listener) => match listener.start().await {
Ok(_) => Ok(()),
Err(e) => Err(TransportLayerError::RaknetUDPError(
RaknetError::ServerError(e),
Expand All @@ -23,7 +23,7 @@ impl TransportLaterListener {

pub async fn accept(&mut self) -> Result<TransportLayerConnection, TransportLayerError> {
match self {
TransportLaterListener::RaknetUDP(listener) => match listener.accept().await {
TransportLayerListener::RaknetUDP(listener) => match listener.accept().await {
Ok(conn) => Ok(TransportLayerConnection::RaknetUDP(conn)),
Err(e) => Err(TransportLayerError::RaknetUDPError(
RaknetError::ServerError(e),
Expand Down
Loading