use std::convert::From;
use std::sync::mpsc;
use std::sync::mpsc::{Receiver, Sender};
use crate::{Bytes, Event};
use super::{Dispatcher, DispatcherHandle};
pub struct RawDispatcher {
dispatch: u8,
pub rx: Receiver<Event<Bytes>>,
pub tx: Sender<Event<Bytes>>,
handle: Option<DispatcherHandle>,
}
impl RawDispatcher {
pub fn new(dispatch: u8) -> RawDispatcher {
let (transport_tx, rx) = mpsc::channel();
let (tx, transport_rx) = mpsc::channel();
RawDispatcher {
dispatch,
rx,
tx,
handle: Some(DispatcherHandle::new(transport_tx, transport_rx)),
}
}
pub fn rx(&self) -> &Receiver<Event<Bytes>> {
&self.rx
}
pub fn tx(&self) -> &Sender<Event<Bytes>> {
&self.tx
}
}
impl Dispatcher for RawDispatcher {
fn dispatch_byte(&self) -> u8 {
self.dispatch
}
fn get_handle(&mut self) -> DispatcherHandle {
self.handle.take().unwrap()
}
}
pub struct Packet {
pub payload: Vec<u8>,
}
impl From<Vec<u8>> for Packet {
fn from(bytes: Vec<u8>) -> Self {
Packet { payload: bytes }
}
}