aboutsummaryrefslogtreecommitdiff
path: root/crates/whirl_server/src/interaction/shared.rs
blob: eb712b495cb4f909714888d8309cc67465a0f679 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
// Copyright (C) 2021-2021 The Whirlsplash Collective
// SPDX-License-Identifier: GPL-3.0-only

use std::collections::HashMap;

use bytes::BytesMut;

pub struct Shared {
  pub peers: HashMap<String, tokio::sync::mpsc::UnboundedSender<BytesMut>>,
}
impl Shared {
  pub fn new() -> Self {
    Self {
      peers: HashMap::new(),
    }
  }

  pub async fn broadcast(&mut self, message: &[u8]) {
    for peer in &mut self.peers {
      peer.1.send(BytesMut::from(message)).unwrap();
    }
  }
}
impl Default for Shared {
  fn default() -> Self { Self::new() }
}