conetty is a general CS framework for rust based on coroutines with a focus on ease of use.
the general communication procedure is as below
- client send request to server
- server recv request from client
- server parsing and process the request
- server send out response to client
- client recv response from server
this lib provide a general request/response struct called Frame
, it's just a wrapper for the raw
data Vec<u8>
. you need to prepare and parsing it in the actual process functions that passed into
the framework
Add to your Cargo.toml
dependencies:
conetty = { git = "https://github.com/Xudong-Huang/conetty" }
extern crate conetty;
use std::str;
use std::io::Write;
use conetty::{Server, Client, WireError, TcpServer, TcpClient, ReqBuf, RspBuf};
struct Echo;
impl Server for Echo {
fn service(&self, req: &[u8], rsp: &mut RspBuf) -> Result<(), WireError> {
println!("req = {:?}", req);
rsp.write_all(req).map_err(|e| WireError::ServerSerialize(e.to_string()))
}
}
fn main() {
let addr = ("127.0.0.1", 4000);
let _server = Echo.start(&addr).unwrap();
let client = TcpClient::connect(addr).unwrap();
for i in 0..10 {
let mut buf = ReqBuf::new();
buf.write_fmt(format_args!("Hello World! id={}", i)).unwrap();
let data = client.call_service(buf).unwrap();
let rsp = data.decode_rsp().unwrap();
println!("recv = {:?}", str::from_utf8(&rsp).unwrap());
}
}
- Multiplex for a single connection
- support TCP/UDP
- Run any number of clients and services
conetty is distributed under the terms of the MIT license.
See LICENSE for details.