62个版本

0.13.6 2024年3月22日
0.13.4 2023年6月3日
0.13.0 2023年2月25日
0.12.2 2022年4月19日
0.7.1 2021年11月25日

#177游戏开发

Download history 4/week @ 2024-06-30

2,220 每月下载量
game_chess_server 中使用

MIT 许可证

77KB
2K SLoC

logo

rollo

一个基于Rust的多玩家框架。



特性

  • Unity客户端
  • TCP(带TLS支持)
  • 数据包(命令/负载)
  • 游戏循环(更新)
  • 事件管理器(示例:event.rs
  • 间隔管理器(示例:interval.rs
  • DoS保护(示例:dos.rs

安装

将以下内容添加到您的 Cargo.toml 文件中

[dependencies]
rollo = { version = "0.13.0", features = ["full"] }

示例

use rollo::{
    error::Error,
    game::GameTime,
    packet::to_bytes,
    packet::Packet,
    server::{ListenerSecurity, SocketTools, World, WorldSession, WorldSocketMgr},
    AtomicCell,
};
use std::{sync::Arc, time::Duration};

#[tokio::main]
async fn main() {
    // lazy_static works as well.
    let world = Box::leak(Box::new(MyWorld {
        game_time: AtomicCell::new(GameTime::new()),
    }));

    let mut socket_manager = WorldSocketMgr::new(world);
    // Run the server and the game loop at a 15ms interval
    socket_manager
        .start_game_loop(Duration::from_millis(15))
        .start_network("127.0.0.1:6666", ListenerSecurity::Tcp)
        .await
        .unwrap();
}

struct MyWorld {
    game_time: AtomicCell<GameTime>,
}

impl World for MyWorld {
    type WorldSessionimplementer = MyWorldSession;
    fn update(&'static self, _diff: i64, game_time: GameTime) {
        println!("Update at : {}", game_time.timestamp);
    }

    // Your GameTime will be updated automatically. (Optional)
    fn game_time(&'static self) -> Option<&'static AtomicCell<GameTime>> {
        Some(&self.game_time)
    }
}

struct MyWorldSession {
    socket_tools: SocketTools,
}

#[rollo::async_trait]
impl WorldSession<MyWorld> for MyWorldSession {
    async fn on_open(
        tools: SocketTools,
        _world: &'static MyWorld,
    ) -> Result<std::sync::Arc<Self>, Error> {
        Ok(Arc::new(Self {
            socket_tools: tools,
        }))
    }

    fn socket_tools(&self) -> &SocketTools {
        &self.socket_tools
    }

    async fn on_message(world_session: &Arc<Self>, _world: &'static MyWorld, packet: Packet) {
        // If the message received is Login(1), send a response to the player.
        if packet.cmd == 1 {
            // Create a packet without payload
            let new_packet = to_bytes(10, None);
            let new_packet = new_packet;
            // Send it to the player
            world_session.socket_tools.send_data(new_packet.into());
        }
    }

    async fn on_close(_world_session: &Arc<Self>, _world: &'static MyWorld) {
        println!("Session closed");
    }
}

数据包结构

[负载大小(u32); 命令(u16); 负载]

许可证

MIT许可证(LICENSE-MIT

依赖项

~0–12MB
~127K SLoC