#clients #pipe #server #version #octopipes #octopipes-protocol

rustypipes

RustyPipes 是一个用于实现 Octopipes 客户端和服务器功能的 Rust 库。

2 个版本

0.1.1 2020年1月12日
0.1.0 2020年1月11日

#67 in #clients

MIT 许可证

160KB
3K SLoC

RustyPipes

Crates.io

当前版本:0.1.1 (2020年12月1日) 由 Christian Visintin 开发

RustyPipes 是一个用于实现 Octopipes 客户端和服务器功能的 Rust 库。

cargo add rustypipes 0.1.1

[dependencies]
rustypipes = "0.1.1"

库文档可以在 https://docs.rs/unix-named-pipe/0.1.1/rustypipes/ 找到


客户端实现

实现 Octopipes 客户端相对简单,只需以下步骤

初始化客户端

let cap_pipe: String = String::from("/tmp/cap.fifo");
let mut client: rustypipes::OctopipesClient = rustypipes::OctopipesClient::new(
    String::from("myclient-123somerandomchars456"),
    cap_pipe,
    rustypipes::OctopipesProtocolVersion::Version1,
);

订阅服务器

let my_groups: Vec<String> = vec![String::from("myclient"), String::from("BROADCAST")];
match client.subscribe(&my_groups) {
  Ok(cap_error) => {
      match cap_error {
          rustypipes::OctopipesCapError::NoError => {
              println!("client subscribed (no CAP error)");
          },
          _ => panic!("client couldn't subscribe, CAP error: {}\n", cap_error)
      }
  },
  Err(error) => panic!("Error while client was trying to subscribe to server: {}\n", error)
}

等待接收消息

//Start client loop
if let Err(error) = client.loop_start() {
    panic!("Couldn't start client loop: {}\n", error);
}
//...
//In your main loop
loop {
    //...
    //Get first available message
    match client.get_next_message() {
        Ok(recv) => {
            match recv {
                Some(message) => {
                    println!("Received message from {}", message.origin.as_ref().unwrap());
                    //Handle message...
                },
                None => println!("No message in client's inbox")
            }
        },
        Err(error) => {
            panic!("Error while trying to get messages on client: {}\n", error)
        }
    }
    //...
}

发送消息

let mydata: Vec<u8> = vec![0x01, 0x02, 0x03, 0x04];
if let Err(error) = client.send(&String::from("SomeRemote"), mydata) {
    panic!("Error while trying to send data: {}\n", error);
}

完成后,从服务器取消订阅并终止客户端

if let Err(error) = client.unsubscribe() {
    panic!("Error while client was trying to unsubscribe: {}\n", error);
}

服务器实现

RustyPipes 被设计成以最简单和最直接的方式实现 Octopipes 服务器,从而节省开发者的时间和代码行数。

初始化服务器

let cap_pipe: String = String::from("/tmp/cap.fifo");
let client_folder: String = String::from("/tmp/clients/");
let mut server: rustypipes::OctopipesServer = rustypipes::OctopipesServer::new(
    rustypipes::OctopipesProtocolVersion::Version1,
    cap_pipe,
    client_folder,
);

启动 CAP 监听器

CAP 监听器将启动一个线程,该线程将监听 CAP 上的新消息。一旦接收到新消息,就可以使用后续将要介绍的 cap process 函数来检索。

if let Err(error) = server.start_cap_listener() {
    panic!("Could not start CAP listener: {}", error);
}

服务器主循环

这两个简单的函数将为您处理服务器需要完成的所有任务。尽量频繁地调用这些函数(大约每100毫秒或更短时间)。

loop {
    //This function will check if there's any message available on the CAP
    //If there's a message available it will handle the request for you automatically
    //If a new client subscribed, a new worker will start
    //If a client unsubscribed, its worker will be stopped
    //NOTE: process_cap_all can be used too, but cap_once should be preferred
    if let Err(error) = server.process_cap_once() {
        println!("Error while processing CAP: {}", error);
    }
    //This function will check for each worker if there is any message on their inbox
    //If a message is found, it'll be dispatched to the clients subscribed to that remote
    //NOTE: process_first and process_all can be used too, but process_once should be preferred
    if let Err((fault_client, error)) = server.process_once() {
        println!("Error while processing client '{}': {}", fault_client, error);
    }
}

终止服务器

if let Err(error) = server.stop_server() {
    panic!("Could not stop Server: {}\n", error);
}
//You could also just call, since drop trait is implemented and stop the server gracefully
//drop(server);

变更日志

0.1.1 (12/01/2020)

  • 为 "写入失败" 添加了缺少的错误描述

许可证

MIT License

Copyright (c) 2019-2020 Christian Visintin

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

依赖项

~99–340KB