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
Serializable::inspect
to show more useful information
msgpack :: serialization_common
Serialization services forserialization_write
and serialization_read
serialization :: serialization_core
Abstract services to serialize Nit objects to different formatsdeserialize_json
and JsonDeserializer
msgpack :: serialization_write
Serialize full Nit objects to MessagePack formatserialize_to_json
and JsonSerializer
core :: union_find
union–find algorithm using an efficient disjoint-set data structure
# Easy client/server logic for games and simple distributed applications
#
# Both `gamnit::client` and `gamnit::server` can be used separately or
# together by importing `gamnit::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
# ~~~
module network
import server
import client
lib/gamnit/network/network.nit:15,1--70,13