use bytes::{Buf, ByteBuf}; use eligos::{Service, Codec, Framed, Receive, ClientInfo}; struct CountCodec; impl Codec for CountCodec { fn decode(&mut self, buf: &mut ByteBuf) -> Vec { println!("got some bytes in codec!"); vec![buf.bytes().len()] } fn encode(&self, us: usize) -> ByteBuf { let byte_str = format!("{}", us); let bytes = byte_str.as_bytes(); ByteBuf::from_slice(bytes) } } fn build_codec() -> Box> { Box::new(CountCodec) } struct ByteAddReceiver { counter: usize, } // take requests of usize, return responses of usize impl Receive for ByteAddReceiver { fn receive(&mut self, client_info: ClientInfo, req_bytes: &usize) -> Option { self.counter += *req_bytes; println!("got req from {:?}! bytes so far: {}", client_info, self.counter); Some(self.counter) } } #[test] fn it_works() { let receiver = Box::new(ByteAddReceiver { counter: 0, }); let mut service = Service::new( 6666, build_codec, // request codec build_codec, // response codec ).unwrap(); let n_workers = 4; service.run(vec![receiver]); }