padeir0 / gna

game networking abstraction.
The Unlicense
7 stars 1 forks source link

GNA

GNA stands for Game Networking Abstraction, it packs a server and a client for games written in Go. It's built around encoding/gob and persistent TCP connections (i'm planning to support UDP Broadcasting as well, but the TCP will still be the foundation to comunicate UDP addresses). Although very simple, it is very powerfull, you can:

Note: Right now i do not recommend using this for servers over ~64 players, see issue #1. Although i can run 300 players in my machine with the Blobs example using 25% on all 12 threads (Ryzen 5 2600x), i woudn't recomend it.

Installation

go get github.com/kazhmir/gna

Usage

Components

This image helps visualize how the components play together, from bottom up:

Although the above image uses every component as an example, most simple servers will only be:

How it goes

You will need to create a struct to represent your instance, like the following:

type Instance struct {
    /*Embedding is necessary to import the GetData, Terminate, NetAbs, and Dispatch methods
    as well as all the functionality*/
    gna.Net
}

/*Here you will validate the Player,
 you can use Player.Send() and Player.Recv() to
authenticate the Player. If not valid,
 you can just close the connection: Player.Close()*/
func (e *Instance) Auth(p *gna.Player) {}

/*This is the Disconn handler, the connection 
is already closed at this stage, you can access 
the cause of disconnection with Player.Error()
 and keep track of the players with Player.ID*/
func (e *Instance) Disconn(p *gna.Player) {}

/*Update is called every tick, here you can
 update your game state. Use Instance.GetData to get
the data from the Players and Instance.Dispatch to send data.*/
func (e *Instance) Update() {}

and then run it with:

err := gna.RunServer(":8888", &Instance{})

To better understand how it all works, go through the examples.

Examples

All examples also contain their respective clients.