Cargo
[package]
name = "thdemo1"
version = "0.1.0"
authors = ["BBDXF <zhang206zyx@163.com>"]
edition = "2018"
[dependencies]
thrift=""
try_from=""
ordered-float=""
clap=""
Tree
├── Cargo.toml
├── note.md
├── src
│ ├── comms
│ ├── main.rs
│ └── MathService.thrift
├── target
│ └── debug
MathService.thrift
service Demo1 {
i32 Add(1:i32 A, 2:i32 B)
}
Server
extern crate thrift;
use thrift::protocol::{TCompactInputProtocolFactory, TCompactOutputProtocolFactory};
use thrift::server::TServer;
use thrift::transport::{TFramedReadTransportFactory, TFramedWriteTransportFactory};
mod comms;
use comms::math_service::Demo1SyncHandler;
struct Demo1Server {}
impl Demo1SyncHandler for Demo1Server {
fn handle_add(&self, a: i32, b: i32) -> thrift::Result<i32> {
Ok(a + b)
}
}
fn main() {
println!("Hello, thrift!");
let s_addr = format!("{}:{}", "0.0.0.0", 9090);
let i_tran_fact = TFramedReadTransportFactory::new();
let i_prot_fact = TCompactInputProtocolFactory::new();
let o_tran_fact = TFramedWriteTransportFactory::new();
let o_prot_fact = TCompactOutputProtocolFactory::new();
let processer = comms::math_service::Demo1SyncProcessor::new(
Demo1Server {}
);
let mut server = TServer::new(
i_tran_fact, i_prot_fact,
o_tran_fact, o_prot_fact,
processer,
10,
);
println!("Server Run: {}", s_addr);
match server.listen(&s_addr) {
Ok(()) => println!("server ran successfully"),
Err(e) => {
println!("server failed with error {:?}", e);
std::process::exit(1);
}
}
}
Client
extern crate thrift;
use thrift::protocol::{TCompactInputProtocol, TCompactOutputProtocol};
use thrift::transport::{TFramedReadTransport, TTcpChannel, TIoChannel, TFramedWriteTransport};
mod comms;
use crate::comms::math_service::TDemo1SyncClient;
fn main() {
println!("Hello, thrift client!");
let s_addr = format!("{}:{}", "127.0.0.1", 9090);
let mut c = TTcpChannel::new();
c.open(&s_addr);
let (i_chan, o_chan) = c.split().unwrap();
let i_tran = TFramedReadTransport::new(i_chan);
let o_tran = TFramedWriteTransport::new(o_chan);
let i_prot = TCompactInputProtocol::new(i_tran);
let o_prot = TCompactOutputProtocol::new(o_tran);
let mut client = comms::math_service::Demo1SyncClient::new(
i_prot, o_prot,
);
let rs = client.add(11, 22);
println!("{:?}", rs);
}