lemunozm / message-io

Fast and easy-to-use event-driven network library.
Apache License 2.0
1.11k stars 74 forks source link

Game server #124

Closed markusmoenig closed 2 years ago

markusmoenig commented 2 years ago

Hi,

I am looking into creating a game server for my classical RPG project at Eldiron.com.

Question: From your perspective what would be a good design implementation to have one thread on the server communicate with the clients and another thread performing the game ticks on the characters ? Is there some kind of best practice for this ?

Thanks!

lemunozm commented 2 years ago

Hi Markus, the project looks really awesome! I'm happy to see things like this in Rust :)

Good question. From my knowledge:

The network from a server perspective should be viewed as another kind of event that should be processed by the server. The input network messages are asynchronous events that the server processes and (maybe) generate a response/s to the client.

So, I would make some kind of network module that processes the messages at the network logic level. It means deserializing them, sending some ACK to the clients, etc... This module should generate and send (by a channel usually) game events to your server, for example, a MovePlayer(i32, i32) along with the client id (which refers to an endpoint in your network module) from who has generated the event. In this way, your server is completely agnostic about the network, it only understands game events.

In the opposite direction, the logic is more or less the same, you should have an output channel where you send events from your game server that your network module will be processed, serialized and sent to the user: For example, you could send an Update(data) event with the last frame data along with a list of client ids. The network module will map those clients' ids into the endpoints, serialize the update event in something sendable, and send the messages to the clients.

In the network module, you could make use of the NodeEvent::Signal to read the server events that should be transformed into messages to clients along with the network events in the same thread.

I hope this pattern can be useful!

markusmoenig commented 2 years ago

Thanks for the kind words and the help! I will try to implement it this way, will let you know if any problems ;)

From what I can see I really like the architecture of message-io (and your support!).

lemunozm commented 2 years ago

Thanks, Markus! Fell free to share any problem you find in the process :)

lemunozm commented 2 years ago

I close the issue, feel free to reopen it if needed something more

markusmoenig commented 2 years ago

Yes sorry I did not come back to this. I am working on the overall design of the client / server mechanism before actually getting to the transport layer. Will come back to you if I have questions once I am there. Thanks for the help so far!

lemunozm commented 2 years ago

Don't worry! And good luck with your project 😃