#transport-layer #fibers #udp #abstraction #peer #built #top

fibers_transport

基于[fibers]crate构建的传输层抽象

4个版本

0.1.3 2020年9月11日
0.1.2 2018年9月17日
0.1.1 2018年9月17日
0.1.0 2018年9月16日

8#fibers 中排名

Download history 50/week @ 2024-01-11 105/week @ 2024-01-18 96/week @ 2024-01-25 98/week @ 2024-02-01 151/week @ 2024-02-08 153/week @ 2024-02-15 304/week @ 2024-02-22 102/week @ 2024-02-29 32/week @ 2024-03-07 62/week @ 2024-03-14 48/week @ 2024-03-21 38/week @ 2024-03-28 31/week @ 2024-04-04 23/week @ 2024-04-11 22/week @ 2024-04-18 38/week @ 2024-04-25

每月下载量:121
用于 2 crate

MIT 许可证

39KB
772 代码行

fibers_transport

Crates.io: fibers_transport Documentation Actions Status Coverage Status License: MIT

基于fiberscrate构建的传输层抽象。

文档

示例

UDP对等体。

use bytecodec::bytes::{Utf8Decoder, Utf8Encoder};
use fibers_transport::{Transport, UdpTransport, UdpTransporter, wait_send, wait_recv};

type UdpPeer = UdpTransporter<Utf8Encoder, Utf8Decoder>;

// Binds peers.
let mut peer0 = fibers_global::execute(UdpPeer::bind("127.0.0.1:0".parse().unwrap()))?;
let peer1 = fibers_global::execute(UdpPeer::bind("127.0.0.1:0".parse().unwrap()))?;

// `peer0` sends a message to `peer1`.
peer0.start_send(peer1.local_addr(), "foo".to_owned())?;
let peer0 = fibers_global::execute(wait_send(peer0))?;

// `peer1` receives a message from `peer0`.
let (_, addr, item) = fibers_global::execute(wait_recv(peer1))?;
assert_eq!(addr, peer0.local_addr());
assert_eq!(item, "foo");

TCP服务器和客户端。

use bytecodec::fixnum::{U8Decoder, U8Encoder};
use factory::DefaultFactory;
use fibers_transport::{Transport, TcpListener, TcpTransport, TcpTransporter, wait_send, wait_recv};
use futures::Stream;

type TcpServer = TcpListener<DefaultFactory<U8Encoder>, DefaultFactory<U8Decoder>>;
type TcpClient = TcpTransporter<U8Encoder, U8Decoder>;

let server = fibers_global::execute(TcpServer::listen("127.0.0.1:0".parse().unwrap()))?;
let mut client = fibers_global::execute(TcpClient::connect(server.local_addr()))?;

// Sends a message to the server.
client.start_send((), 123)?;
let client = fibers_global::execute(wait_send(client))?;

// Receives the message from the client.
let (server, _) = fibers_global::execute(server.into_future()).map_err(|(e, _)| e)?;
let server = server.unwrap();
assert_eq!(server.peer_addr(), client.local_addr());

let (mut server, _, item) = fibers_global::execute(wait_recv(server))?;
assert_eq!(item, 123);

// Replies to the client.
server.start_send((), 9)?;
let _ = fibers_global::execute(wait_send(server))?;

// Receives the reply from the server.
let (_, _, item) = fibers_global::execute(wait_recv(client))?;
assert_eq!(item, 9);

依赖关系

~3MB
~61K SLoC