Better Messenger recv-related docs and names
This commit is contained in:
parent
763c55d3e4
commit
c244834004
|
@ -172,7 +172,11 @@ impl<T: Write, I, O: Serialize> Messenger<T, I, O> {
|
|||
}
|
||||
|
||||
impl<T: Read, I: DeserializeOwned, O> Messenger<T, I, O> {
|
||||
/// Receives all pending messages and queues them for [recv].
|
||||
/// Synchronously receives all pending messages and queues them for [recv].
|
||||
///
|
||||
/// This function only works if the transport is in non-blocking mode.
|
||||
/// Otherwise, this may block while waiting for more data, even if the
|
||||
/// data it receives does not add up to a full message.
|
||||
pub fn flush_recv(&mut self) -> std::io::Result<()> {
|
||||
let mut buf = [0u8; 1024];
|
||||
|
||||
|
@ -199,7 +203,10 @@ impl<T: Read, I: DeserializeOwned, O> Messenger<T, I, O> {
|
|||
}
|
||||
|
||||
/// Tries to receive a single input packet.
|
||||
pub fn recv(&mut self) -> Option<I> {
|
||||
///
|
||||
/// For messages to be received here, [flush_recv] must be called to
|
||||
/// continuously read pending data from the transport.
|
||||
pub fn try_recv(&mut self) -> Option<I> {
|
||||
self.queue.recv()
|
||||
}
|
||||
}
|
||||
|
@ -211,16 +218,16 @@ mod async_messages {
|
|||
use std::marker::Unpin;
|
||||
|
||||
impl<T: AsyncReadExt + Unpin, I: DeserializeOwned, O: Serialize> Messenger<T, I, O> {
|
||||
pub async fn recv_async(&mut self) -> std::io::Result<I> {
|
||||
pub async fn recv(&mut self) -> std::io::Result<I> {
|
||||
let mut buf = [0u8; 1024];
|
||||
|
||||
loop {
|
||||
let num = self.transport.read(&mut buf).await?;
|
||||
self.queue.on_data(&buf[..num]);
|
||||
|
||||
if let Some(msg) = self.queue.recv() {
|
||||
return Ok(msg);
|
||||
}
|
||||
|
||||
let num = self.transport.read(&mut buf).await?;
|
||||
self.queue.on_data(&buf[..num])?;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -94,7 +94,7 @@ impl Client {
|
|||
eprintln!("flush_recv() error: {:?}", err);
|
||||
}
|
||||
|
||||
while let Some(msg) = self.messenger.recv() {
|
||||
while let Some(msg) = self.messenger.try_recv() {
|
||||
println!("Client #{}: {:?}", self.token.0, msg);
|
||||
match msg {
|
||||
MagpieServerMsg::CreatePanel(CreatePanel {
|
||||
|
|
Loading…
Reference in New Issue