Sfoglia il codice sorgente

bin/ircd: use AtomicBool instead of bool

Dastan-glitch 4 anni fa
parent
commit
8f472cdad8
3 ha cambiato i file con 16 aggiunte e 10 eliminazioni
  1. 2 2
      bin/ircd/src/main.rs
  2. 6 4
      bin/ircd/src/server.rs
  3. 8 4
      bin/ircd/src/settings.rs

+ 2 - 2
bin/ircd/src/main.rs

@@ -1,4 +1,4 @@
-use std::net::SocketAddr;
+use std::{net::SocketAddr, sync::atomic::Ordering};
 
 
 use async_channel::{Receiver, Sender};
 use async_channel::{Receiver, Sender};
 use async_executor::Executor;
 use async_executor::Executor;
@@ -118,7 +118,7 @@ async fn process(
                 // Try to potentially decrypt the incoming message.
                 // Try to potentially decrypt the incoming message.
                 if conn.configured_chans.contains_key(&msg.channel) {
                 if conn.configured_chans.contains_key(&msg.channel) {
                     let chan_info = conn.configured_chans.get(&msg.channel).unwrap();
                     let chan_info = conn.configured_chans.get(&msg.channel).unwrap();
-                    if !chan_info.joined {
+                    if !chan_info.joined.load(Ordering::Relaxed) {
                         continue
                         continue
                     }
                     }
                     if let Some(salt_box) = &chan_info.salt_box {
                     if let Some(salt_box) = &chan_info.salt_box {

+ 6 - 4
bin/ircd/src/server.rs

@@ -1,3 +1,5 @@
+use std::sync::atomic::Ordering;
+
 use async_std::net::TcpStream;
 use async_std::net::TcpStream;
 use futures::{io::WriteHalf, AsyncWriteExt};
 use futures::{io::WriteHalf, AsyncWriteExt};
 use fxhash::FxHashMap;
 use fxhash::FxHashMap;
@@ -79,7 +81,7 @@ impl IrcServerConnection {
                         self.configured_chans.insert(chan.to_string(), ChannelInfo::new()?);
                         self.configured_chans.insert(chan.to_string(), ChannelInfo::new()?);
                     } else {
                     } else {
                         let chan_info = self.configured_chans.get_mut(chan).unwrap();
                         let chan_info = self.configured_chans.get_mut(chan).unwrap();
-                        chan_info.joined = true;
+                        chan_info.joined.store(true, Ordering::Relaxed);
                     }
                     }
                 }
                 }
             }
             }
@@ -90,7 +92,7 @@ impl IrcServerConnection {
                     self.reply(&part_reply).await?;
                     self.reply(&part_reply).await?;
                     if self.configured_chans.contains_key(chan) {
                     if self.configured_chans.contains_key(chan) {
                         let chan_info = self.configured_chans.get_mut(chan).unwrap();
                         let chan_info = self.configured_chans.get_mut(chan).unwrap();
-                        chan_info.joined = false;
+                        chan_info.joined.store(false, Ordering::Relaxed);
                     }
                     }
                 }
                 }
             }
             }
@@ -124,7 +126,7 @@ impl IrcServerConnection {
             "PING" => {
             "PING" => {
                 let line_clone = line.clone();
                 let line_clone = line.clone();
                 let split_line: Vec<&str> = line_clone.split_whitespace().collect();
                 let split_line: Vec<&str> = line_clone.split_whitespace().collect();
-                if split_line.len() > 1 && split_line[0] == "PING" {
+                if split_line.len() > 1 {
                     let pong = format!("PONG {}\r\n", split_line[1]);
                     let pong = format!("PONG {}\r\n", split_line[1]);
                     self.reply(&pong).await?;
                     self.reply(&pong).await?;
                 }
                 }
@@ -142,7 +144,7 @@ impl IrcServerConnection {
 
 
                 if self.configured_chans.contains_key(channel) {
                 if self.configured_chans.contains_key(channel) {
                     let channel_info = self.configured_chans.get(channel).unwrap();
                     let channel_info = self.configured_chans.get(channel).unwrap();
-                    if channel_info.joined {
+                    if channel_info.joined.load(Ordering::Relaxed) {
                         let message = if let Some(salt_box) = &channel_info.salt_box {
                         let message = if let Some(salt_box) = &channel_info.salt_box {
                             let encrypted = encrypt_message(salt_box, message);
                             let encrypted = encrypt_message(salt_box, message);
                             info!("(Encrypted) PRIVMSG {} :{}", channel, encrypted);
                             info!("(Encrypted) PRIVMSG {} :{}", channel, encrypted);

+ 8 - 4
bin/ircd/src/settings.rs

@@ -1,4 +1,8 @@
-use std::{net::SocketAddr, path::PathBuf};
+use std::{
+    net::SocketAddr,
+    path::PathBuf,
+    sync::{atomic::AtomicBool, Arc},
+};
 
 
 use fxhash::FxHashMap;
 use fxhash::FxHashMap;
 use log::info;
 use log::info;
@@ -68,13 +72,13 @@ pub struct ChannelInfo {
     pub topic: Option<String>,
     pub topic: Option<String>,
     /// Optional NaCl box for the channel, used for {en,de}cryption.
     /// Optional NaCl box for the channel, used for {en,de}cryption.
     pub salt_box: Option<crypto_box::Box>,
     pub salt_box: Option<crypto_box::Box>,
-    ///
-    pub joined: bool,
+    /// Flag indicates whether the user has joined the channel or not
+    pub joined: Arc<AtomicBool>,
 }
 }
 
 
 impl ChannelInfo {
 impl ChannelInfo {
     pub fn new() -> Result<Self> {
     pub fn new() -> Result<Self> {
-        Ok(Self { topic: None, salt_box: None, joined: true })
+        Ok(Self { topic: None, salt_box: None, joined: Arc::new(AtomicBool::new(true)) })
     }
     }
 }
 }