4 个版本 (2 个破坏性更新)
0.3.0 | 2023年7月21日 |
---|---|
0.2.0 | 2023年4月16日 |
0.1.1 | 2023年2月20日 |
0.1.0 | 2022年12月15日 |
#677 in 并发
每月30次下载
31KB
473 行
geese_pool
该crate提供在多个Geese实例之间传递消息的能力,这些实例可能存在于不同的进程中,甚至在不同的计算机上。该crate通过维护到其他实例的通道连接池(ConnectionPool)来工作,通过这些通道可以发送或接收消息。系统可以通过触发适当的geese_pool事件来发送或接收消息。
该crate完全与协议无关;它不提供使用任何特定标准(如UDP或TCP)来联网Geese实例的实现。消费者需要提供消息序列化和网络传输的方法。
以下是一个示例,说明如何在不同的事件系统之间发送事件。示例首先创建了两个Geese上下文,并将ConnectionPool系统添加到其中。然后,它创建一个事件通道,通过该通道可以转发事件,并将一个通道放置在每个连接池中。最后,通知b的连接池向a发送一个整数。当a随后更新时,a的事件系统中会触发包含相同整数的消息事件。
struct Receiver(i32);
impl Receiver {
fn respond(&mut self, message: &geese_pool::on::Message<i32>) {
self.0 = **message;
}
}
impl GeeseSystem for Receiver {
fn new(_: GeeseContextHandle) -> Self {
Self(0)
}
fn register(with: &mut GeeseSystemData<Self>) {
with.event(Self::respond);
}
}
let mut a = GeeseContext::default();
a.raise_event(geese::notify::AddSystem::new::<ConnectionPool>());
a.raise_event(geese::notify::AddSystem::new::<Receiver>());
let mut b = GeeseContext::default();
b.raise_event(geese::notify::AddSystem::new::<ConnectionPool>());
let (chan_a, chan_b) = LocalChannel::new_pair();
a.system::<ConnectionPool>().add_peer(Box::new(chan_a));
let handle_a = b.system::<ConnectionPool>().add_peer(Box::new(chan_b));
b.raise_event(geese_pool::notify::message(1, handle_a));
b.flush_events();
a.raise_event(geese_pool::notify::Update);
a.flush_events();
assert_eq!(1, a.system::<Receiver>().0);
可选功能
serde - 提供一个PeerChannel实现,它序列化和反序列化其数据,因为这对于大多数进程间通信形式都是必需的。
依赖项
~1.4–3MB
~64K SLoC