#offline #netcode #mocking #networking #transport-layer #demo #crystalorb

nightly crystalorb-mock-network

为 CrystalOrb 提供离线、内存中的传输层模拟

3 个不稳定版本

0.3.0 2021 年 12 月 7 日
0.2.1 2021 年 6 月 11 日
0.2.0 2021 年 6 月 8 日

139模拟 中排名

每月 40 次下载

Apache-2.0

8MB
3K SLoC

crystalorb-mock-network

提供离线工作的 crystalorb::network_recource::NetworkResource 实现,使用 VecDeque 支持,允许 CrystalOrb 用于离线演示和测试。

用法

use crystalorb_mock_network::MockNetwork;
use crystalorb_demo::DemoWorld;
use crystalorb::{client::Client, server::Server, Config};

let (mut server_net, (mut client_1_net, mut client_2_net)) =
    MockNetwork::new_mock_network::<DemoWorld>();

let client_1 = Client::<DemoWorld>::new(Config::default());
let client_2 = Client::<DemoWorld>::new(Config::default());
let server = Server::<DemoWorld>::new(Config::default(), 0.0);

// You need to manually call connect.
client_1_net.connect();
client_2_net.connect();

// (You can also simulate a disconnection later on)
// client_1_net.disconnect();
// client_2_net.disconnect();

// (You can also set the latency of each connection)
// client_1_net.set_delay(number_of_seconds_as_f64);

// Later, in your update loop:
fn update_loop(
  delta_seconds: f64,
  seconds_since_startup: f64,
  client_1: &mut Client<DemoWorld>,
  client_2: &mut Client<DemoWorld>,
  server: &mut Server<DemoWorld>,
  client_1_net: &mut MockNetwork,
  client_2_net: &mut MockNetwork,
  server_net: &mut MockNetwork,
) {
    // You can then use these network resource instances to update
    // the `crystalorb` clients and server.
    client_1.update(delta_seconds, seconds_since_startup, client_1_net);
    client_2.update(delta_seconds, seconds_since_startup, client_2_net);
    server.update(delta_seconds, seconds_since_startup, server_net);

    // Call the `tick` method to simulate the flow of messages.
    // If you don't call `tick`, then no messages will be able to pass through the network.
    client_1_net.tick(delta_seconds);
    client_2_net.tick(delta_seconds);
    server_net.tick(delta_seconds);
}

依赖