Skip to content

Commit

Permalink
chore: remove useless reference in actor handle
Browse files Browse the repository at this point in the history
  • Loading branch information
y5c4l3 committed Oct 22, 2023
1 parent 4637c76 commit 9e40f4c
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 43 deletions.
102 changes: 62 additions & 40 deletions src/bancho.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use std::collections::{HashMap, VecDeque};
use std::fmt;
use std::pin::Pin;
use std::str::FromStr;
use std::sync::Arc;

use self::cache::UserCache;
use self::irc::Transport;
Expand Down Expand Up @@ -45,8 +46,8 @@ pub trait IrcCommand {
}

#[derive(Debug)]
pub struct IrcContext<'a> {
handle: &'a Client,
pub struct IrcContext {
options: Arc<ClientOptions>,
history: Vec<irc::Message>,
}

Expand All @@ -61,15 +62,15 @@ impl<T: IrcCommand> IrcResponse<T> {
}
}

impl<'a> IrcContext<'a> {
fn new(handle: &'a Client) -> Self {
impl IrcContext {
fn new(options: Arc<ClientOptions>) -> Self {
Self {
handle,
options,
history: Vec::new(),
}
}
pub fn options(&self) -> &ClientOptions {
&self.handle.options
&self.options
}
pub fn history(&self) -> &[irc::Message] {
&self.history
Expand Down Expand Up @@ -712,6 +713,9 @@ pub struct ClientOptions {
}

impl ClientOptions {
pub fn builder() -> ClientOptionsBuilder {
ClientOptionsBuilder::new()
}
pub fn endpoint(&self) -> &str {
&self.endpoint
}
Expand Down Expand Up @@ -799,14 +803,14 @@ impl ClientOptionsBuilder {
}

#[derive(Debug)]
pub struct Operator<'a> {
handle: &'a Client,
pub struct Operator {
options: Arc<ClientOptions>,
tx: mpsc::Sender<Action>,
rx: broadcast::Receiver<Event>,
irc_rx: broadcast::Receiver<irc::Message>,
}

impl<'a> Operator<'a> {
impl Operator {
/// Sends a raw IRC command to the server.
///
/// This method is unreliable, because it does not track the response. To
Expand Down Expand Up @@ -834,7 +838,7 @@ impl<'a> Operator<'a> {
Some(command) => self.tx.send(Action::Raw(command)).await?,
None => self.tx.send(Action::RawGroup(command.commands())).await?,
};
let mut context = IrcContext::new(self.handle);
let mut context = IrcContext::new(self.options.clone());

let mut skip_count: usize = 0;

Expand All @@ -857,8 +861,8 @@ impl<'a> Operator<'a> {
async fn send_action(&self, action: Action) -> StdResult<(), OperatorError<Action>> {
Ok(self.tx.send(action).await?)
}
pub fn options(&'a self) -> &'a ClientOptions {
&self.handle.options
pub fn options(&self) -> &ClientOptions {
&self.options
}
/// Subscribes to the event stream.
pub fn subscribe(&self) -> broadcast::Receiver<Event> {
Expand Down Expand Up @@ -1144,7 +1148,7 @@ impl<'a> Operator<'a> {
Ok(response.body().ok().map(|info| multiplayer::Match {
id,
internal_id: info.match_internal_id(),
operator: self.handle.operator(),
operator: self.clone(),
}))
}
pub async fn has_joined(
Expand All @@ -1160,6 +1164,17 @@ impl<'a> Operator<'a> {
}
}

impl Clone for Operator {
fn clone(&self) -> Self {
Self {
options: self.options.clone(),
tx: self.tx.clone(),
rx: self.rx.resubscribe(),
irc_rx: self.irc_rx.resubscribe(),
}
}
}

#[derive(Debug)]
pub enum Action {
Raw(irc::Command),
Expand All @@ -1177,7 +1192,7 @@ struct ChannelState {
}

struct ClientActor {
options: ClientOptions,
options: Arc<ClientOptions>,

assembler: bot::MessageAssembler,
channels: HashMap<Channel, ChannelState>,
Expand Down Expand Up @@ -1430,18 +1445,25 @@ impl ClientActor {

#[derive(Debug)]
pub struct Client {
options: ClientOptions,
actor: JoinHandle<Result<()>>,
action_tx: mpsc::Sender<Action>,
event_rx: broadcast::Receiver<Event>,
irc_rx: broadcast::Receiver<irc::Message>,
options: Arc<ClientOptions>,
actor: Option<JoinHandle<Result<()>>>,
operator: Option<Operator>,
}

impl Client {
const CHANNEL_BUFFER: usize = 2048;
const CACHE_SIZE: usize = 2048;
pub async fn new(options: ClientOptions) -> Result<Self> {
let stream = TcpStream::connect(options.endpoint.clone()).await?;
pub fn new(options: ClientOptions) -> Self {
let options = Arc::new(options);

Self {
options,
actor: None,
operator: None,
}
}
pub async fn run(&mut self) -> Result<()> {
let stream = TcpStream::connect(self.options.endpoint.clone()).await?;
let transport = Transport::new(stream);

let (irc_tx, irc_rx) = broadcast::channel(Self::CHANNEL_BUFFER);
Expand All @@ -1453,24 +1475,28 @@ impl Client {

let mut actor = ClientActor {
assembler: bot::MessageAssembler::new(),
options: options.clone(),
options: self.options.clone(),
channels: HashMap::new(),
user_cache: UserCache::new(Self::CACHE_SIZE),
transport,
event_tx,
action_rx,
irc_tx,
};

let actor = tokio::spawn(async move { actor.run().await });

Ok(Self {
options,
actor,
action_tx: tx,
event_rx: rx,
self.actor = Some(tokio::spawn(async move { actor.run().await }));
self.operator = Some(Operator {
options: self.options.clone(),
tx,
rx,
irc_rx,
})
});
Ok(())
}
pub fn new_operator(&self) -> Operator {
self.operator.as_ref().unwrap().clone()
}
pub fn operator(&self) -> &Operator {
self.operator.as_ref().unwrap()
}
pub async fn auth(&self) -> StdResult<(), Error> {
let response = self
Expand All @@ -1489,16 +1515,12 @@ impl Client {
pub fn options(&self) -> &ClientOptions {
&self.options
}
pub fn operator<'a>(&'a self) -> Operator<'a> {
Operator {
handle: &self,
tx: self.action_tx.clone(),
rx: self.event_rx.resubscribe(),
irc_rx: self.irc_rx.resubscribe(),
}
}
pub async fn shutdown(self) -> Result<()> {
self.actor.await.unwrap()
if let Some(actor) = self.actor {
actor.await.unwrap()
} else {
Ok(())
}
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/bancho/multiplayer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,14 +147,14 @@ pub type MatchId = u64;
pub type MatchInternalId = u32;

#[derive(Debug)]
pub struct Match<'a> {
pub struct Match {
pub(super) id: MatchId,
/// internal match id, appeared in invite link e.g. `osump://123456`
pub(super) internal_id: MatchInternalId,
pub(super) operator: Operator<'a>,
pub(super) operator: Operator,
}

impl<'a> Match<'a> {
impl Match {
pub fn channel(&self) -> Channel {
Channel::Multiplayer(self.id)
}
Expand Down

0 comments on commit 9e40f4c

Please sign in to comment.