1 个不稳定版本
使用旧的 Rust 2015
0.1.0 | 2015年10月9日 |
---|
#65 in #mio
22KB
517 代码行(不含注释)
eligos
Rust 服务框架!
概念
Codec
反序列化请求并序列化响应Service
从监听套接字接收,并将连接交给提供的Receiver
之一Receiver
使用用户提供的逻辑处理请求并可选择响应。每个 Receiver 都有自己的线程和 MIO 事件循环。
extern crate bytes;
extern crate eligos;
use self::bytes::{Buf, ByteBuf};
use self::eligos::{Service, Codec, Receive};
struct CountCodec;
impl Codec<ByteBuf, usize> for CountCodec {
fn decode(&mut self, buf: &mut ByteBuf) -> Vec<usize> {
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<Codec<ByteBuf, usize>> {
Box::new(CountCodec)
}
struct ByteAddReceiver {
counter: usize,
}
// take requests of usize, return responses of usize
impl Receive<usize, usize> for ByteAddReceiver {
fn receive(&mut self, client_info: ClientInfo, req_bytes: &usize) -> Option<usize> {
self.counter += *req_bytes;
println!("got req from {:?}! bytes so far: {}", client_info, self.counter);
Some(self.counter)
}
}
fn main() {
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]);
}
依赖
~4MB
~72K SLoC