User IO messages

This commit is contained in:
arkpar 2015-12-28 11:41:51 +01:00
parent 634b6be1e6
commit 267495c264
2 changed files with 31 additions and 0 deletions

View File

@ -162,6 +162,15 @@ pub enum HostMessage {
protocol: ProtocolId,
data: Vec<u8>,
},
UserMessage(UserMessage),
}
pub type UserMessageId = u32;
pub struct UserMessage {
protocol: ProtocolId,
id: UserMessageId,
data: Option<Vec<u8>>,
}
pub type PeerId = usize;
@ -237,9 +246,21 @@ impl<'s> HostIo<'s> {
}
}
pub fn message(&mut self, id: UserMessageId, data: Option<Vec<u8>>) {
match self.event_loop.channel().send(HostMessage::UserMessage(UserMessage {
protocol: self.protocol,
id: id,
data: data
})) {
Ok(_) => {}
Err(e) => { panic!("Error sending io message {:?}", e); }
}
}
pub fn disable_peer(&mut self, _peer: PeerId) {
//TODO: remove capability, disconnect if no capabilities left
}
}
struct UserTimer {
@ -652,6 +673,13 @@ impl Handler for Host {
}
}
},
HostMessage::UserMessage(message) => {
for (p, h) in self.handlers.iter_mut() {
if p != &message.protocol {
h.message(&mut HostIo::new(message.protocol, None, event_loop, &mut self.connections, &mut self.timers), &message);
}
}
}
}
}
}

View File

@ -74,6 +74,8 @@ pub type PeerId = host::PeerId;
pub type PacketId = host::PacketId;
pub type TimerToken = host::TimerToken;
pub type HandlerIo<'s> = host::HostIo<'s>;
pub type Message = host::UserMessage;
pub type MessageId = host::UserMessageId;
pub trait ProtocolHandler: Send {
fn initialize(&mut self, io: &mut HandlerIo);
@ -81,6 +83,7 @@ pub trait ProtocolHandler: Send {
fn connected(&mut self, io: &mut HandlerIo, peer: &PeerId);
fn disconnected(&mut self, io: &mut HandlerIo, peer: &PeerId);
fn timeout(&mut self, io: &mut HandlerIo, timer: TimerToken);
fn message(&mut self, io: &mut HandlerIo, message: &Message);
}
pub struct NetworkClient;