Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
73 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
#![warn(clippy::pedantic)] | ||
|
||
mod errors; | ||
mod runner; | ||
mod shard; |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
use futures::channel::mpsc::UnboundedReceiver; | ||
use tokio::sync::mpsc::UnboundedSender; | ||
|
||
use crate::shard::socket::SenderExt; | ||
|
||
use crate::{ | ||
errors::Result, | ||
shard::{types::InterMessage, Shard}, | ||
}; | ||
|
||
pub struct ShardRunner { | ||
manager_tx: UnboundedSender<String>, | ||
runner_rx: UnboundedReceiver<InterMessage>, | ||
runner_tx: UnboundedSender<InterMessage>, | ||
pub shard: Shard, | ||
} | ||
|
||
impl ShardRunner { | ||
pub fn new( | ||
manager_tx: UnboundedSender<String>, | ||
runner_rx: UnboundedReceiver<InterMessage>, | ||
runner_tx: UnboundedSender<InterMessage>, | ||
shard: Shard, | ||
) -> Self { | ||
Self { | ||
manager_tx, | ||
runner_rx, | ||
runner_tx, | ||
shard, | ||
} | ||
} | ||
|
||
pub async fn run(&mut self) -> Result<()> { | ||
loop { | ||
if !self.recieve().await? { | ||
return Ok(()); | ||
} | ||
} | ||
} | ||
|
||
async fn recieve(&mut self) -> Result<bool> { | ||
loop { | ||
match self.runner_rx.try_next() { | ||
Ok(Some(message)) => { | ||
if !self.handle_rx_message(message).await { | ||
return Ok(false); | ||
} | ||
} | ||
|
||
Ok(None) => return Ok(false), | ||
|
||
Err(_) => break, | ||
} | ||
} | ||
|
||
Ok(true) | ||
} | ||
|
||
async fn handle_rx_message(&mut self, message: InterMessage) -> bool { | ||
match message { | ||
InterMessage::Json(json) => self.shard.client.send_json(&json).await.is_ok(), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
pub mod error; | ||
mod socket; | ||
pub mod socket; | ||
pub mod types; | ||
|
||
use std::sync::Arc; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters