我正在开发一个echo服务器,它从TCP获取数据,并将一些逻辑应用于该数据。例如,如果客户机数据以hello的形式出现,我想以hello from server的形式响应它。
我能够使用copy函数转发输入数据,但这在我的情况下并不有用。
下面是我正在编写的起始代码:
extern crate futures;
extern crate tokio_core;
extern crate tokio_io;
use futures::stream::Stream;
use futures::Future;
use std::net::SocketAddr;
use tokio_core::net::TcpListener;
use tokio_core::reactor::Core;
use tokio_io::io::copy;
use tokio_io::AsyncRead;
fn main() {
let addr = "127.0.0.1:15000".parse::<SocketAddr>().unwrap();
let mut core = Core::new().unwrap();
let handle = core.handle();
let socket = TcpListener::bind(&addr, &handle).unwrap();
println!("Listening on: {}", addr);
let done = socket.incoming().for_each(move |(socket, addr)| {
let (reader, writer) = socket.split();
let amt = copy(reader, writer);
let msg = amt.then(move |result| {
match result {
Ok((amt, _, _)) => println!("wrote {} bytes to {}", amt, addr),
Err(e) => println!("error on {}: {}", addr, e),
}
Ok(())
});
handle.spawn(msg);
Ok(())
});
core.run(done).unwrap();
}我知道我需要添加一些逻辑,而不是这个复制函数,但是怎么做?
let amt = copy(reader, writer);发布于 2018-10-05 17:17:18
从某种意义上说,回送服务器是一种特殊的,即来自客户端的一个“请求”紧跟在服务器的一个响应之后。对于这样一个用例来说,一个非常好的例子是tokio的TinyDB实例。
但是,需要考虑的一件事是,虽然UDP是基于数据包的,但它以您发送它们的确切形式击中了对方,而TCP则不是。TCP是一种流协议--它有很强的保证,即数据包被对方接收,并且发送的数据与发送的顺序完全一致。然而,不能保证的是,一方面的一个“发送”调用会导致另一端的一个“接收”调用,返回与发送的数据完全相同的数据块。当发送非常长的数据块时,这尤其令人感兴趣,其中一个发送映射到多个接收端。因此,您应该选择一个定界符,服务器在尝试向客户端发送响应之前可以等待这个分隔符。在Telnet中,该分隔符为"\r\n“。这就是东京的解码器/编码器基础设施发挥作用的地方。这种编解码器的一个示例实现是LinesCodec。如果您想拥有Telnet,这正是您想要的。它将一次只给您一条消息,并允许您一次只发送一条这样的消息作为响应:
extern crate tokio;
use tokio::codec::Decoder;
use tokio::net::TcpListener;
use tokio::prelude::*;
use tokio::codec::LinesCodec;
use std::net::SocketAddr;
fn main() {
let addr = "127.0.0.1:15000".parse::<SocketAddr>().unwrap();
let socket = TcpListener::bind(&addr).unwrap();
println!("Listening on: {}", addr);
let done = socket.incoming()
.map_err(|e| println!("failed to accept socket; error = {:?}", e))
.for_each(move |socket| {
// Fit the line-based codec on top of the socket. This will take on the task of
// parsing incomming messages, as well as formatting outgoing ones (appending \r\n).
let (lines_tx, lines_rx) = LinesCodec::new().framed(socket).split();
// This takes every incomming message and allows to create one outgoing message for it,
// essentially generating a stream of responses.
let responses = lines_rx.map(|incomming_message| {
// Implement whatever transform rules here
if incomming_message == "hello" {
return String::from("hello from server");
}
return incomming_message;
});
// At this point `responses` is a stream of `Response` types which we
// now want to write back out to the client. To do that we use
// `Stream::fold` to perform a loop here, serializing each response and
// then writing it out to the client.
let writes = responses.fold(lines_tx, |writer, response| {
//Return the future that handles to send the response to the socket
writer.send(response)
});
// Run this request/response loop until the client closes the connection
// Then return Ok(()), ignoring all eventual errors.
tokio::spawn(
writes.then(move |_| Ok(()))
);
return Ok(());
});
tokio::run(done);
}https://stackoverflow.com/questions/52622206
复制相似问题