Easy client/server logic for games and simple distributed applications

Both client and server can be used separately or together by importing network. Use both modules to create an program that discover local servers or create one if none is found:

redef fun handshake_app_name do return "network_test"

# Discover local servers
var servers = discover_local_servers
if servers.not_empty then
    # Try to connect to the first local server
    var server_info = servers.first
    var server = new RemoteServer(server_info)

    if not server.connect then
        print_error "Failed to connect to {server_info.address}:{server_info.port}"
    else if not server.handshake then
        print_error "Failed handshake with {server_info.address}:{server_info.port}"
    else
        # Connected!
        print "Connected to {server_info.address}:{server_info.port}"

        # Write something and close connection
        server.writer.serialize "hello server"
        server.socket.as(not null).close
    end
else
    # Create a local server
    var connect_port = 33729
    print "Launching server: connect on {connect_port}, discovery on {discovery_port}"
    var server = new Server(connect_port)

    # Don't loop if testing
    if "NIT_TESTING".environ == "true" then exit 0

    loop
        # Respond to discovery requests
        server.answer_discovery_requests

        # Accept new clients
        var new_clients = server.accept_clients
        for client in new_clients do
            # Read something and close connection
            assert client.reader.deserialize == "hello server"
            client.socket.close
        end
    end
end

All subgroups and modules

module client

gamnit :: client

Client-side network services for games and such
module common

gamnit :: common

Services common to the client and server modules
module network

gamnit :: network

Easy client/server logic for games and simple distributed applications
module server

gamnit :: server

Server-side network services for games and such
package_diagram gamnit\>network\> network socket socket gamnit\>network\>->socket msgpack msgpack gamnit\>network\>->msgpack core core socket->core msgpack->core serialization serialization msgpack->serialization json json msgpack->json binary binary msgpack->binary ...core ... ...core->core ...serialization ... ...serialization->serialization ...json ... ...json->json ...binary ... ...binary->binary

Ancestors

group binary

binary

Read and write binary data with any Reader and Writer
group codecs

core > codecs

Group module for all codec-related manipulations
group collection

core > collection

This module define several collection classes.
group core

core

Nit common library of core classes and methods
group json

json

read and write JSON formatted text
group meta

meta

Simple user-defined meta-level to manipulate types of instances as object.
group parser_base

parser_base

Simple base for hand-made parsers of all kinds
group poset

poset

Pre order sets and partial order set (ie hierarchies)
group serialization

serialization

Abstract serialization services
group text

core > text

All the classes and methods related to the manipulation of text entities

Parents

group msgpack

msgpack

MessagePack, an efficient binary serialization format
group socket

socket

Socket services