|
@@ -9,7 +9,7 @@ use util::SeqGenerator;
|
|
|
component! {
|
|
|
ChannelManager : ChannelManagerInner {
|
|
|
sequence: SeqGenerator<u16> = SeqGenerator::new(0),
|
|
|
- channels: HashMap<u16, mpsc::UnboundedSender<(u8, Vec<u8>)>> = HashMap::new(),
|
|
|
+ channels: HashMap<u16, mpsc::UnboundedSender<(u8, EasyBuf)>> = HashMap::new(),
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -17,7 +17,7 @@ component! {
|
|
|
pub struct ChannelError;
|
|
|
|
|
|
pub struct Channel {
|
|
|
- receiver: mpsc::UnboundedReceiver<(u8, Vec<u8>)>,
|
|
|
+ receiver: mpsc::UnboundedReceiver<(u8, EasyBuf)>,
|
|
|
state: ChannelState,
|
|
|
}
|
|
|
|
|
@@ -26,7 +26,7 @@ pub struct ChannelData(BiLock<Channel>);
|
|
|
|
|
|
pub enum ChannelEvent {
|
|
|
Header(u8, Vec<u8>),
|
|
|
- Data(Vec<u8>),
|
|
|
+ Data(EasyBuf),
|
|
|
}
|
|
|
|
|
|
#[derive(Clone)]
|
|
@@ -54,21 +54,21 @@ impl ChannelManager {
|
|
|
(seq, channel)
|
|
|
}
|
|
|
|
|
|
- pub fn dispatch(&self, cmd: u8, data: Vec<u8>) {
|
|
|
+ pub fn dispatch(&self, cmd: u8, mut data: EasyBuf) {
|
|
|
use std::collections::hash_map::Entry;
|
|
|
|
|
|
- let id: u16 = BigEndian::read_u16(&data[..2]);
|
|
|
+ let id: u16 = BigEndian::read_u16(data.drain_to(2).as_ref());
|
|
|
|
|
|
self.lock(|inner| {
|
|
|
if let Entry::Occupied(entry) = inner.channels.entry(id) {
|
|
|
- let _ = entry.get().send((cmd, data[2..].to_owned()));
|
|
|
+ let _ = entry.get().send((cmd, data));
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
}
|
|
|
|
|
|
impl Channel {
|
|
|
- fn recv_packet(&mut self) -> Poll<Vec<u8>, ChannelError> {
|
|
|
+ fn recv_packet(&mut self) -> Poll<EasyBuf, ChannelError> {
|
|
|
let (cmd, packet) = match self.receiver.poll() {
|
|
|
Ok(Async::Ready(t)) => t.expect("channel closed"),
|
|
|
Ok(Async::NotReady) => return Ok(Async::NotReady),
|
|
@@ -76,7 +76,7 @@ impl Channel {
|
|
|
};
|
|
|
|
|
|
if cmd == 0xa {
|
|
|
- let code = BigEndian::read_u16(&packet[..2]);
|
|
|
+ let code = BigEndian::read_u16(&packet.as_ref()[..2]);
|
|
|
error!("channel error: {} {}", packet.len(), code);
|
|
|
|
|
|
self.state = ChannelState::Closed;
|
|
@@ -104,7 +104,7 @@ impl Stream for Channel {
|
|
|
ChannelState::Closed => panic!("Polling already terminated channel"),
|
|
|
ChannelState::Header(mut data) => {
|
|
|
if data.len() == 0 {
|
|
|
- data = EasyBuf::from(try_ready!(self.recv_packet()));
|
|
|
+ data = try_ready!(self.recv_packet());
|
|
|
}
|
|
|
|
|
|
let length = BigEndian::read_u16(data.drain_to(2).as_ref()) as usize;
|
|
@@ -117,18 +117,20 @@ impl Stream for Channel {
|
|
|
|
|
|
self.state = ChannelState::Header(data);
|
|
|
|
|
|
- return Ok(Async::Ready(Some(ChannelEvent::Header(header_id, header_data))));
|
|
|
+ let event = ChannelEvent::Header(header_id, header_data);
|
|
|
+ return Ok(Async::Ready(Some(event)));
|
|
|
}
|
|
|
}
|
|
|
|
|
|
ChannelState::Data => {
|
|
|
let data = try_ready!(self.recv_packet());
|
|
|
- if data.is_empty() {
|
|
|
+ if data.len() == 0 {
|
|
|
self.receiver.close();
|
|
|
self.state = ChannelState::Closed;
|
|
|
return Ok(Async::Ready(None));
|
|
|
} else {
|
|
|
- return Ok(Async::Ready(Some(ChannelEvent::Data(data))));
|
|
|
+ let event = ChannelEvent::Data(data);
|
|
|
+ return Ok(Async::Ready(Some(event)));
|
|
|
}
|
|
|
}
|
|
|
}
|
|
@@ -137,7 +139,7 @@ impl Stream for Channel {
|
|
|
}
|
|
|
|
|
|
impl Stream for ChannelData {
|
|
|
- type Item = Vec<u8>;
|
|
|
+ type Item = EasyBuf;
|
|
|
type Error = ChannelError;
|
|
|
|
|
|
fn poll(&mut self) -> Poll<Option<Self::Item>, Self::Error> {
|