toml
[dependencies]
rollo = { version = "0.6.0", features = ["full"] }
```rust,norun use rollo::{ error::Error, game::GameTime, packet::tobytes, packet::Packet, server::{ListenerSecurity, SocketTools, World, WorldSession, WorldSocketMgr}, tokio, AtomicCell, }; use std::sync::Arc; use std::time::Duration;
async fn main() { 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 with an interval (15ms)
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
impl World for MyWorld { type WorldSessionimplementer = MyWorldSession; fn update(&'static self, diff: i64, gametime: GameTime) { // it's cheaper than an Atomic. println!("Update at : {}", gametime.timestamp); println!("Elapsed is {}", self.gametime.load().elapsed.as_millis()); }
// Your GameTime will be update automatically.
fn game_time(&'static self) -> Option<&'static AtomicCell<GameTime>> {
Some(&self.game_time)
}
}
struct MyWorldSession { socket_tools: SocketTools, }
impl WorldSession
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);
if let Ok(new_packet) = new_packet {
// Send it to the player
world_session.socket_tools.send_data(new_packet.freeze());
}
}
}
async fn on_close(_world_session: &Arc<Self>, _world: &'static MyWorld) {
println!("Session closed");
}
} ```
[Payload size(u32); Command(u16); Payload]
MIT license (LICENSE-MIT)