Connection to a remote server

Introduced properties

private var _config: RemoteServerConfig

gamnit :: RemoteServer :: _config

RemoteServerConfig used to initiate connection to the server
private var _reader: MsgPackDeserializer

gamnit :: RemoteServer :: _reader

MsgPackDeserializer used to receive data from this client through socket
private var _socket: nullable TCPStream

gamnit :: RemoteServer :: _socket

Communication socket with the server
private var _writer: MsgPackSerializer

gamnit :: RemoteServer :: _writer

MsgPackSerializer used to send data to this client through socket
fun config: RemoteServerConfig

gamnit :: RemoteServer :: config

RemoteServerConfig used to initiate connection to the server
protected fun config=(config: RemoteServerConfig)

gamnit :: RemoteServer :: config=

RemoteServerConfig used to initiate connection to the server
fun connect: Bool

gamnit :: RemoteServer :: connect

Attempt connection with the remote server
fun connected: Bool

gamnit :: RemoteServer :: connected

Is this connection connected?
fun handshake: Bool

gamnit :: RemoteServer :: handshake

Attempt handshake with server
fun reader: MsgPackDeserializer

gamnit :: RemoteServer :: reader

MsgPackDeserializer used to receive data from this client through socket
protected fun reader=(reader: MsgPackDeserializer)

gamnit :: RemoteServer :: reader=

MsgPackDeserializer used to receive data from this client through socket
fun socket: nullable TCPStream

gamnit :: RemoteServer :: socket

Communication socket with the server
protected fun socket=(socket: nullable TCPStream)

gamnit :: RemoteServer :: socket=

Communication socket with the server
fun writer: MsgPackSerializer

gamnit :: RemoteServer :: writer

MsgPackSerializer used to send data to this client through socket
protected fun writer=(writer: MsgPackSerializer)

gamnit :: RemoteServer :: writer=

MsgPackSerializer used to send data to this client through socket

Redefined properties

redef type SELF: RemoteServer

gamnit $ RemoteServer :: SELF

Type of this instance, automatically specialized in every class

All properties

fun !=(other: nullable Object): Bool

core :: Object :: !=

Have self and other different values?
fun ==(other: nullable Object): Bool

core :: Object :: ==

Have self and other the same value?
type CLASS: Class[SELF]

core :: Object :: CLASS

The type of the class of self.
type SELF: Object

core :: Object :: SELF

Type of this instance, automatically specialized in every class
private var _config: RemoteServerConfig

gamnit :: RemoteServer :: _config

RemoteServerConfig used to initiate connection to the server
private var _reader: MsgPackDeserializer

gamnit :: RemoteServer :: _reader

MsgPackDeserializer used to receive data from this client through socket
private var _socket: nullable TCPStream

gamnit :: RemoteServer :: _socket

Communication socket with the server
private var _writer: MsgPackSerializer

gamnit :: RemoteServer :: _writer

MsgPackSerializer used to send data to this client through socket
protected fun class_factory(name: String): CLASS

core :: Object :: class_factory

Implementation used by get_class to create the specific class.
fun class_name: String

core :: Object :: class_name

The class name of the object.
fun config: RemoteServerConfig

gamnit :: RemoteServer :: config

RemoteServerConfig used to initiate connection to the server
protected fun config=(config: RemoteServerConfig)

gamnit :: RemoteServer :: config=

RemoteServerConfig used to initiate connection to the server
fun connect: Bool

gamnit :: RemoteServer :: connect

Attempt connection with the remote server
fun connected: Bool

gamnit :: RemoteServer :: connected

Is this connection connected?
fun get_class: CLASS

core :: Object :: get_class

The meta-object representing the dynamic type of self.
fun handshake: Bool

gamnit :: RemoteServer :: handshake

Attempt handshake with server
fun hash: Int

core :: Object :: hash

The hash code of the object.
init init

core :: Object :: init

fun inspect: String

core :: Object :: inspect

Developer readable representation of self.
protected fun inspect_head: String

core :: Object :: inspect_head

Return "CLASSNAME:#OBJECTID".
intern fun is_same_instance(other: nullable Object): Bool

core :: Object :: is_same_instance

Return true if self and other are the same instance (i.e. same identity).
fun is_same_serialized(other: nullable Object): Bool

core :: Object :: is_same_serialized

Is self the same as other in a serialization context?
intern fun is_same_type(other: Object): Bool

core :: Object :: is_same_type

Return true if self and other have the same dynamic type.
private intern fun native_class_name: CString

core :: Object :: native_class_name

The class name of the object in CString format.
intern fun object_id: Int

core :: Object :: object_id

An internal hash code for the object based on its identity.
fun output

core :: Object :: output

Display self on stdout (debug only).
intern fun output_class_name

core :: Object :: output_class_name

Display class name on stdout (debug only).
fun reader: MsgPackDeserializer

gamnit :: RemoteServer :: reader

MsgPackDeserializer used to receive data from this client through socket
protected fun reader=(reader: MsgPackDeserializer)

gamnit :: RemoteServer :: reader=

MsgPackDeserializer used to receive data from this client through socket
fun serialization_hash: Int

core :: Object :: serialization_hash

Hash value use for serialization
fun socket: nullable TCPStream

gamnit :: RemoteServer :: socket

Communication socket with the server
protected fun socket=(socket: nullable TCPStream)

gamnit :: RemoteServer :: socket=

Communication socket with the server
intern fun sys: Sys

core :: Object :: sys

Return the global sys object, the only instance of the Sys class.
abstract fun to_jvalue(env: JniEnv): JValue

core :: Object :: to_jvalue

fun to_s: String

core :: Object :: to_s

User readable representation of self.
fun writer: MsgPackSerializer

gamnit :: RemoteServer :: writer

MsgPackSerializer used to send data to this client through socket
protected fun writer=(writer: MsgPackSerializer)

gamnit :: RemoteServer :: writer=

MsgPackSerializer used to send data to this client through socket
package_diagram gamnit::RemoteServer RemoteServer core::Object Object gamnit::RemoteServer->core::Object

Parents

interface Object

core :: Object

The root of the class hierarchy.

Class definitions

gamnit $ RemoteServer
# Connection to a remote server
class RemoteServer

	# `RemoteServerConfig` used to initiate connection to the server
	var config: RemoteServerConfig

	# Communication socket with the server
	var socket: nullable TCPStream = null

	# Is this connection connected?
	fun connected: Bool
	do
		var socket = socket
		return socket != null and socket.connected
	end

	# `MsgPackSerializer` used to send data to this client through `socket`
	var writer: MsgPackSerializer is noinit

	# `MsgPackDeserializer` used to receive data from this client through `socket`
	var reader: MsgPackDeserializer is noinit

	# Attempt connection with the remote server
	fun connect: Bool
	do
		print "Connecting to {config.address}:{config.port}..."
		var socket = new TCPStream.connect(config.address.to_s, config.port)
		self.socket = socket

		if not socket.connected then
			print "Connection failed: {socket.last_error or else "Internal error"}"
			return false
		end

		# Setup serialization
		writer = new MsgPackSerializer(socket)
		writer.cache = new AsyncCache(false)
		reader = new MsgPackDeserializer(socket)
		writer.link reader

		return true
	end

	# Attempt handshake with server
	#
	# Validates compatibility between `handshake_app_name` and `handshake_app_version`.
	#
	# On error, close `socket`.
	fun handshake: Bool
	do
		# The client goes first so that the server doesn't show its hand
		var socket = socket
		assert socket != null

		# App name
		var app_name = sys.handshake_app_name
		socket.serialize_msgpack app_name

		var server_app = socket.deserialize_msgpack("String")
		if server_app != app_name then
			print_error "Handshake Error: server app name is '{server_app or else "<invalid>"}'"
			socket.close
			return false
		end

		# App version
		socket.serialize_msgpack sys.handshake_app_version

		var server_version = socket.deserialize_msgpack("String")
		if server_version != sys.handshake_app_version then
			print_error "Handshake Error: server version is different '{server_version or else "<invalid>"}'"
			socket.close
			return false
		end

		return true
	end
end
lib/gamnit/network/client.nit:55,1--132,3