Io channel

This commit is contained in:
arkpar 2016-01-13 23:13:57 +01:00
parent 76dc2d6074
commit 4d2437906e
5 changed files with 28 additions and 11 deletions

View File

@ -70,6 +70,7 @@ pub type TimerToken = service::TimerToken;
pub type StreamToken = service::StreamToken; pub type StreamToken = service::StreamToken;
pub type IoContext<'s, M> = service::IoContext<'s, M>; pub type IoContext<'s, M> = service::IoContext<'s, M>;
pub type IoService<M> = service::IoService<M>; pub type IoService<M> = service::IoService<M>;
pub type IoChannel<M> = service::IoChannel<M>;
//pub const USER_TOKEN_START: usize = service::USER_TOKEN; // TODO: ICE in rustc 1.7.0-nightly (49c382779 2016-01-12) //pub const USER_TOKEN_START: usize = service::USER_TOKEN; // TODO: ICE in rustc 1.7.0-nightly (49c382779 2016-01-12)
#[cfg(test)] #[cfg(test)]

View File

@ -146,6 +146,19 @@ impl<Message> Handler for IoManager<Message> where Message: Send + 'static {
} }
} }
/// Allows sending messages into the event loop. All the IO handlers will get the message
/// in the `message` callback.
pub struct IoChannel<Message> where Message: Send {
channel: Sender<IoMessage<Message>>
}
impl<Message> IoChannel<Message> where Message: Send {
pub fn send(&mut self, message: Message) -> Result<(), IoError> {
try!(self.channel.send(IoMessage::UserMessage(message)));
Ok(())
}
}
/// General IO Service. Starts an event loop and dispatches IO requests. /// General IO Service. Starts an event loop and dispatches IO requests.
/// 'Message' is a notification message type /// 'Message' is a notification message type
pub struct IoService<Message> where Message: Send + 'static { pub struct IoService<Message> where Message: Send + 'static {
@ -180,6 +193,11 @@ impl<Message> IoService<Message> where Message: Send + 'static {
try!(self.host_channel.send(IoMessage::UserMessage(message))); try!(self.host_channel.send(IoMessage::UserMessage(message)));
Ok(()) Ok(())
} }
/// Create a new message channel
pub fn channel(&mut self) -> IoChannel<Message> {
IoChannel { channel: self.host_channel.clone() }
}
} }
impl<Message> Drop for IoService<Message> where Message: Send { impl<Message> Drop for IoService<Message> where Message: Send {

View File

@ -76,10 +76,7 @@ pub enum NetworkIoMessage<Message> where Message: Send {
data: Vec<u8>, data: Vec<u8>,
}, },
/// User message /// User message
User { User(Message),
protocol: ProtocolId,
message: Message,
},
} }
/// Local (temporary) peer session ID. /// Local (temporary) peer session ID.
@ -609,16 +606,11 @@ impl<Message> IoHandler<NetworkIoMessage<Message>> for Host<Message> where Messa
} }
} }
}, },
&mut NetworkIoMessage::User { &mut NetworkIoMessage::User(ref message) => {
ref protocol,
ref message
} => {
for (p, h) in self.handlers.iter_mut() { for (p, h) in self.handlers.iter_mut() {
if p != protocol {
h.message(&mut NetworkContext::new(io, p, None, &mut self.connections, &mut self.timers), &message); h.message(&mut NetworkContext::new(io, p, None, &mut self.connections, &mut self.timers), &message);
} }
} }
} }
} }
} }
}

View File

@ -61,6 +61,7 @@ pub type PacketId = host::PacketId;
pub type NetworkContext<'s,'io, Message> = host::NetworkContext<'s, 'io, Message>; pub type NetworkContext<'s,'io, Message> = host::NetworkContext<'s, 'io, Message>;
pub type NetworkService<Message> = service::NetworkService<Message>; pub type NetworkService<Message> = service::NetworkService<Message>;
pub type NetworkIoMessage<Message> = host::NetworkIoMessage<Message>; pub type NetworkIoMessage<Message> = host::NetworkIoMessage<Message>;
pub use network::host::NetworkIoMessage::User as UserMessage;
pub type NetworkError = error::NetworkError; pub type NetworkError = error::NetworkError;
use io::*; use io::*;

View File

@ -40,5 +40,10 @@ impl<Message> NetworkService<Message> where Message: Send + 'static {
})); }));
Ok(()) Ok(())
} }
pub fn io(&mut self) -> &mut IoService<NetworkIoMessage<Message>> {
&mut self.io_service
}
} }