Skip to content

Commit

Permalink
Fix typo & fix an [issue 19](b23r0#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
mikhaillav committed Mar 29, 2024
1 parent 69d4e09 commit e2118d0
Showing 1 changed file with 54 additions and 15 deletions.
69 changes: 54 additions & 15 deletions src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ pub struct RaknetListener {
all_session_closed_notifier: Arc<Notify>,
drop_notifier: Arc<Notify>,
version_map: Arc<Mutex<HashMap<String, u8>>>,
motd_receiver: Arc<Mutex<Receiver<String>>>,
motd_sender: Sender<String>,
}

impl RaknetListener {
Expand All @@ -43,11 +45,12 @@ impl RaknetListener {
let s = match UdpSocket::bind(sockaddr).await {
Ok(p) => p,
Err(_) => {
return Err(RaknetError::BindAdressError);
return Err(RaknetError::BindAddressError);
}
};

let (connection_sender, connection_receiver) = channel::<RaknetSocket>(10);
let (motd_sender, motd_receiver) = channel::<String>(10);

let ret = Self {
motd: String::new(),
Expand All @@ -61,6 +64,8 @@ impl RaknetListener {
all_session_closed_notifier: Arc::new(Notify::new()),
drop_notifier: Arc::new(Notify::new()),
version_map: Arc::new(Mutex::new(HashMap::new())),
motd_receiver: Arc::new(Mutex::new(motd_receiver)),
motd_sender,
};

ret.drop_watcher().await;
Expand All @@ -86,6 +91,7 @@ impl RaknetListener {
};

let (connection_sender, connection_receiver) = channel::<RaknetSocket>(10);
let (motd_sender, motd_receiver) = channel::<String>(10);

let ret = Self {
motd: String::new(),
Expand All @@ -99,6 +105,8 @@ impl RaknetListener {
all_session_closed_notifier: Arc::new(Notify::new()),
drop_notifier: Arc::new(Notify::new()),
version_map: Arc::new(Mutex::new(HashMap::new())),
motd_receiver: Arc::new(Mutex::new(motd_receiver)),
motd_sender,
};

ret.drop_watcher().await;
Expand Down Expand Up @@ -209,7 +217,7 @@ impl RaknetListener {
}

if self.motd.is_empty() {
self.set_motd(
let _ = self.set_motd(
SERVER_NAME,
MAX_CONNECTION,
"486",
Expand All @@ -236,13 +244,22 @@ impl RaknetListener {
let local_addr = socket.local_addr().unwrap();
let close_notify = self.close_notifier.clone();
let version_map = self.version_map.clone();
let motd_receiver = self.motd_receiver.clone();

tokio::spawn(async move {
let mut buf = [0u8; 2048];

raknet_log_debug!("start listen worker : {}", local_addr);

let mut motd = motd.clone();
loop {
let motd = motd.clone();
let new_motd = match motd_receiver.lock().await.try_recv() {
Ok(m) => {
motd = m.clone();
m
}
Err(_) => motd.clone()
};
let size: usize;
let addr: SocketAddr;

Expand Down Expand Up @@ -284,7 +301,7 @@ impl RaknetListener {
time: cur_timestamp_millis(),
guid,
magic: true,
motd,
motd: new_motd,
};

let pong = match write_packet_pong(&packet) {
Expand All @@ -310,7 +327,7 @@ impl RaknetListener {
time: cur_timestamp_millis(),
guid,
magic: true,
motd,
motd: new_motd,
};

let pong = match write_packet_pong(&packet) {
Expand Down Expand Up @@ -508,10 +525,17 @@ impl RaknetListener {
///
/// Call this method must be after calling RaknetListener::listen()
///
/// Returns a result with RaknetError::SetMotdError if failed to send new motd to motd_receiver (so it doesn't update) or with () if everything went smooth
///
/// # Example
/// ```ignore
/// let mut listener = RaknetListener::bind("127.0.0.1:19132".parse().unwrap()).await.unwrap();
/// listener.set_motd("Another Minecraft Server" , 999999 , "486" , "1.18.11", "Survival" , 19132).await;
/// match listener.set_full_motd(String::from("motd")).await {
/// Ok(_) => (),
/// Err(_) => {
/// println!("Failed to update full motd!");
/// },
/// };
/// ```
pub async fn set_motd(
&mut self,
Expand All @@ -521,8 +545,8 @@ impl RaknetListener {
mc_version: &str,
game_type: &str,
port: u16,
) {
self.motd = format!(
) -> Result<()> {
let motd = format!(
"MCPE;{};{};{};0;{};{};Bedrock level;{};1;{};",
server_name,
mc_protocol_version,
Expand All @@ -532,6 +556,14 @@ impl RaknetListener {
game_type,
port
);

match self.motd_sender.send(motd.clone()).await {
Ok(_) => {
self.motd = motd;
Ok(())
}
Err(_) => Err(RaknetError::SetMotdError)
}
}

/// Get the current motd, this motd will be provided to the client in the unconnected pong.
Expand Down Expand Up @@ -583,15 +615,22 @@ impl RaknetListener {
}

/// Set full motd string.
///
///
/// Returns a result with RaknetError::SetMotdError if failed to send new motd to motd_receiver (so it doesn't update) or with () if everything went smooth
///
/// # Example
/// ```ignore
/// let mut socket = RaknetListener::bind("127.0.0.1:19132".parse().unwrap()).await.unwrap();
/// socket.set_full_motd("motd").await;
/// let mut listener = RaknetListener::bind("127.0.0.1:19132".parse().unwrap()).await.unwrap();
/// listener.set_motd("Another Minecraft Server" , 999999 , "486" , "1.18.11", "Survival" , 19132).await.unwrap();
/// ```
pub fn set_full_motd(&mut self, motd: String) -> Result<()> {
self.motd = motd;
Ok(())
pub async fn set_full_motd(&mut self, motd: String) -> Result<()>{
match self.motd_sender.send(motd.clone()).await {
Ok(_) => {
self.motd = motd;
Ok(())
},
Err(_) => Err(RaknetError::SetMotdError),
}
}

pub async fn get_peer_raknet_version(&self, peer: &SocketAddr) -> Result<u8> {
Expand Down Expand Up @@ -627,4 +666,4 @@ impl Drop for RaknetListener {
fn drop(&mut self) {
self.drop_notifier.notify_one();
}
}
}

0 comments on commit e2118d0

Please sign in to comment.