Browse Source

Using a waker in rust's Degeon stream

master
Lev 3 years ago
parent
commit
f94c9849d6
  1. 17
      degeon_core/src/degeon_worker.rs
  2. 2
      degeon_py/degeon.py
  3. BIN
      degeon_py/degeon_core.so
  4. 2
      src/ironforce.rs

17
degeon_core/src/degeon_worker.rs

@ -343,18 +343,17 @@ impl Degeon {
impl Stream for Degeon { impl Stream for Degeon {
type Item = GuiEvent; type Item = GuiEvent;
fn poll_next(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Option<Self::Item>> { fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
if self.ironforce.try_lock().is_err() { if self.ironforce.lock().unwrap().messages.is_empty() {
return Poll::Ready(Some(GuiEvent::None)); let waker = cx.waker().clone();
std::thread::spawn(move || {
std::thread::sleep(std::time::Duration::from_millis(1000));
waker.wake();
});
return Poll::Pending;
} }
let timestamp_0 = std::time::Instant::now(); let timestamp_0 = std::time::Instant::now();
let msg = self.read_message_and_create_event(); let msg = self.read_message_and_create_event();
if timestamp_0.elapsed() < std::time::Duration::from_millis(5) {
std::thread::sleep(std::time::Duration::from_millis(5));
if rand::random::<bool>() {
return Poll::Pending;
}
}
if timestamp_0.elapsed() > std::time::Duration::from_millis(800) { if timestamp_0.elapsed() > std::time::Duration::from_millis(800) {
println!("Poll_next took {:?}", timestamp_0.elapsed()); println!("Poll_next took {:?}", timestamp_0.elapsed());
} }

2
degeon_py/degeon.py

@ -67,6 +67,8 @@ class Degeon:
self.chat_selector.chats = self.core.chats self.chat_selector.chats = self.core.chats
if 0 <= self.chat_selector.active_chat < len(self.chat_selector.chats) and self.active_chat is None: if 0 <= self.chat_selector.active_chat < len(self.chat_selector.chats) and self.active_chat is None:
self.active_chat = ActiveChat.new(self.chat_selector.chats[self.chat_selector.active_chat]) self.active_chat = ActiveChat.new(self.chat_selector.chats[self.chat_selector.active_chat])
if self.active_chat is not None:
self.active_chat.chat = self.core.chats[self.chat_selector.active_chat]
def process_event(self, event: pygame.event.Event): def process_event(self, event: pygame.event.Event):
""" """

BIN
degeon_py/degeon_core.so

Binary file not shown.

2
src/ironforce.rs

@ -119,6 +119,7 @@ impl IronForce {
/// Send a multicast or broadcast message /// Send a multicast or broadcast message
pub fn send_to_all(&mut self, message: Message) -> IFResult<()> { pub fn send_to_all(&mut self, message: Message) -> IFResult<()> {
self.processed_messages.push(message.message_id);
self.transport self.transport
.send_message(serde_cbor::to_vec(&message)?, None) .send_message(serde_cbor::to_vec(&message)?, None)
} }
@ -130,6 +131,7 @@ impl IronForce {
mut message: Message, mut message: Message,
direction: Option<bool>, direction: Option<bool>,
) -> IFResult<()> { ) -> IFResult<()> {
self.processed_messages.push(message.message_id);
let tunnel: Tunnel = if let Some(tun) = self let tunnel: Tunnel = if let Some(tun) = self
.tunnels .tunnels
.iter() .iter()

Loading…
Cancel
Save