client module

Client package.

client.client

Module name: client

This module handles all client-related things such as the camera, the player and the world. It also initialises the connection handler to permit connections to servers.

Some of the attributes are linked with those in the managers, for instance the player attribute is linked with the local player attribute of the player manager, in order for the connection to have access to its properties. This is because the communication between connection and connection handler modules is not bidirectional, so the connection cannot directly recuperate the player attribute of the client module. This could be implemented in a different way but for now this is how it works.

(See client/managers/ for the different client managers.)

class game.client.client.Client

Bases: object

Class for all client-related tasks like client-side rendering and handling the connection.

events(game, e: pygame.event.Event)

Handle the client events.

initialise(game) None

Initialise the client.

stop()

Stop the client connection.

update(game) None

Update the client.

update_ui()

Update the client UI.

client.connection

Module name: connection

This particular module handles the connection to a server, its state and all of its tasks. It also calls the client tasks that send data packets to the server.

(See data/states/connection_states for the different connection states.) (See client/tasks for the different client tasks.)

class game.client.connection.Connection(host: str, port: int, client)

Bases: object

Class for creating a server connection.

connect(player_name: str) None

Attempt to connect to a server with the provided host and port. Any errors or failures will raise specific exceptions.

disconnect() None

Attempt to disconnect from the server. Any errors or failures will raise specific exceptions.

events(e: pygame.event.Event) None

Handle the connection events (unused).

start(task: Tasks, player_name: str = '') None

Start a client connection task.

update() None

Update and verify the connection every so often.

class game.client.connection.Tasks

Bases: object

Class for defining the connection tasks.

CONNECT = 0
UPDATE = 1
class game.client.connection.XSocket(family=AddressFamily.AF_INET, type_=SocketKind.SOCK_STREAM, proto=0, fileno=None)

Bases: socket

Class for socket with extra byte counter.

accept() -> (socket object, address info)

Wait for an incoming connection. Return a new socket representing the connection, and the address of the client. For IP sockets, the address info is a pair (hostaddr, port).

bind(address)

Bind the socket to a local address. For IP sockets, the address is a pair (host, port); the host must refer to the local host. For raw packet sockets the address is a tuple (ifname, proto [,pkttype [,hatype [,addr]]])

close()

Close the socket. It cannot be used after this call.

connect(address)

Connect the socket to a remote address. For IP sockets, the address is a pair (host, port).

connect_ex(address) errno

This is like connect(address), but returns an error code (the errno value) instead of raising an exception when an error occurs.

detach() file descriptor

Close the socket object without closing the underlying file descriptor. The object cannot be used after this call, but the file descriptor can be reused for other purposes. The file descriptor is returned.

dup() socket object

Duplicate the socket. Return a new socket object connected to the same system resource. The new socket is non-inheritable.

property family

Read-only access to the address family for this socket.

fileno() integer

Return the integer file descriptor of the socket.

get_inheritable()

Get the inheritable flag of the socket

get_r_tmp() int

Return temporary total bytes received from the server.

get_recv() int

Return total bytes received from the server since the existence of the socket.

get_s_tmp() int

Return temporary total bytes sent from the server.

get_sent() int

Return total bytes sent to the server since the existence of the socket.

getblocking()

Returns True if socket is in blocking mode, or False if it is in non-blocking mode.

getpeername() address info

Return the address of the remote endpoint. For IP sockets, the address info is a pair (hostaddr, port).

getsockname() address info

Return the address of the local endpoint. The format depends on the address family. For IPv4 sockets, the address info is a pair (hostaddr, port). For IPv6 sockets, the address info is a 4-tuple (hostaddr, port, flowinfo, scope_id).

getsockopt(level, option[, buffersize]) value

Get a socket option. See the Unix manual for level and option. If a nonzero buffersize argument is given, the return value is a string of that length; otherwise it is an integer.

gettimeout() timeout

Returns the timeout in seconds (float) associated with socket operations. A timeout of None indicates that timeouts on socket operations are disabled.

listen([backlog])

Enable a server to accept connections. If backlog is specified, it must be at least 0 (if it is lower, it is set to 0); it specifies the number of unaccepted connections that the system will allow before refusing new connections. If not specified, a default reasonable value is chosen.

makefile(...) an I/O stream connected to the socket

The arguments are as for io.open() after the filename, except the only supported mode values are ‘r’ (default), ‘w’, ‘b’, or a combination of those.

proto

the socket protocol

recv(bufsize: int, flags: int = 0) bytes

Override socket recv with counter.

recv_into(buffer[, nbytes[, flags]]) nbytes_read

A version of recv() that stores its data into a buffer rather than creating a new string. Receive up to buffersize bytes from the socket. If buffersize is not specified (or 0), receive up to the size available in the given buffer.

See recv() for documentation about the flags.

recvfrom(buffersize[, flags]) -> (data, address info)

Like recv(buffersize, flags) but also return the sender’s address info.

recvfrom_into(buffer[, nbytes[, flags]]) -> (nbytes, address info)

Like recv_into(buffer[, nbytes[, flags]]) but also return the sender’s address info.

recvmsg(bufsize[, ancbufsize[, flags]]) -> (data, ancdata, msg_flags, address)

Receive normal data (up to bufsize bytes) and ancillary data from the socket. The ancbufsize argument sets the size in bytes of the internal buffer used to receive the ancillary data; it defaults to 0, meaning that no ancillary data will be received. Appropriate buffer sizes for ancillary data can be calculated using CMSG_SPACE() or CMSG_LEN(), and items which do not fit into the buffer might be truncated or discarded. The flags argument defaults to 0 and has the same meaning as for recv().

The return value is a 4-tuple: (data, ancdata, msg_flags, address). The data item is a bytes object holding the non-ancillary data received. The ancdata item is a list of zero or more tuples (cmsg_level, cmsg_type, cmsg_data) representing the ancillary data (control messages) received: cmsg_level and cmsg_type are integers specifying the protocol level and protocol-specific type respectively, and cmsg_data is a bytes object holding the associated data. The msg_flags item is the bitwise OR of various flags indicating conditions on the received message; see your system documentation for details. If the receiving socket is unconnected, address is the address of the sending socket, if available; otherwise, its value is unspecified.

If recvmsg() raises an exception after the system call returns, it will first attempt to close any file descriptors received via the SCM_RIGHTS mechanism.

recvmsg_into(buffers[, ancbufsize[, flags]]) -> (nbytes, ancdata, msg_flags, address)

Receive normal data and ancillary data from the socket, scattering the non-ancillary data into a series of buffers. The buffers argument must be an iterable of objects that export writable buffers (e.g. bytearray objects); these will be filled with successive chunks of the non-ancillary data until it has all been written or there are no more buffers. The ancbufsize argument sets the size in bytes of the internal buffer used to receive the ancillary data; it defaults to 0, meaning that no ancillary data will be received. Appropriate buffer sizes for ancillary data can be calculated using CMSG_SPACE() or CMSG_LEN(), and items which do not fit into the buffer might be truncated or discarded. The flags argument defaults to 0 and has the same meaning as for recv().

The return value is a 4-tuple: (nbytes, ancdata, msg_flags, address). The nbytes item is the total number of bytes of non-ancillary data written into the buffers. The ancdata item is a list of zero or more tuples (cmsg_level, cmsg_type, cmsg_data) representing the ancillary data (control messages) received: cmsg_level and cmsg_type are integers specifying the protocol level and protocol-specific type respectively, and cmsg_data is a bytes object holding the associated data. The msg_flags item is the bitwise OR of various flags indicating conditions on the received message; see your system documentation for details. If the receiving socket is unconnected, address is the address of the sending socket, if available; otherwise, its value is unspecified.

If recvmsg_into() raises an exception after the system call returns, it will first attempt to close any file descriptors received via the SCM_RIGHTS mechanism.

send(buffer: bytes, flags: int = 0) int

Override socket send with counter.

sendall(data[, flags])

Send a data string to the socket. For the optional flags argument, see the Unix manual. This calls send() repeatedly until all data is sent. If an error occurs, it’s impossible to tell how much data has been sent.

sendfile(file[, offset[, count]]) sent

Send a file until EOF is reached by using high-performance os.sendfile() and return the total number of bytes which were sent. file must be a regular file object opened in binary mode. If os.sendfile() is not available (e.g. Windows) or file is not a regular file socket.send() will be used instead. offset tells from where to start reading the file. If specified, count is the total number of bytes to transmit as opposed to sending the file until EOF is reached. File position is updated on return or also in case of error in which case file.tell() can be used to figure out the number of bytes which were sent. The socket must be of SOCK_STREAM type. Non-blocking sockets are not supported.

sendmsg(buffers[, ancdata[, flags[, address]]]) count

Send normal and ancillary data to the socket, gathering the non-ancillary data from a series of buffers and concatenating it into a single message. The buffers argument specifies the non-ancillary data as an iterable of bytes-like objects (e.g. bytes objects). The ancdata argument specifies the ancillary data (control messages) as an iterable of zero or more tuples (cmsg_level, cmsg_type, cmsg_data), where cmsg_level and cmsg_type are integers specifying the protocol level and protocol-specific type respectively, and cmsg_data is a bytes-like object holding the associated data. The flags argument defaults to 0 and has the same meaning as for send(). If address is supplied and not None, it sets a destination address for the message. The return value is the number of bytes of non-ancillary data sent.

sendmsg_afalg([msg, ]*, op[, iv[, assoclen[, flags=MSG_MORE]]])

Set operation mode, IV and length of associated data for an AF_ALG operation socket.

sendto(data, [flags, ]address) count

Like send(data, flags) but allows specifying the destination address. For IP sockets, the address is a pair (hostaddr, port).

set_inheritable(inheritable)

Set the inheritable flag of the socket

set_r_tmp() None

Set temporary total bytes to current total bytes received from the server since the existence of the socket.

set_s_tmp() None

Set temporary total bytes to current total bytes sent from the server since the existence of the socket.

setblocking(flag)

Set the socket to blocking (flag is true) or non-blocking (false). setblocking(True) is equivalent to settimeout(None); setblocking(False) is equivalent to settimeout(0.0).

setsockopt(level, option, value: int)
setsockopt(level, option, value: buffer) None
setsockopt(level, option, None, optlen: int) None

Set a socket option. See the Unix manual for level and option. The value argument can either be an integer, a string buffer, or None, optlen.

settimeout(timeout)

Set a timeout on socket operations. ‘timeout’ can be a float, giving in seconds, or None. Setting a timeout of None disables the timeout feature and is equivalent to setblocking(1). Setting a timeout of zero is the same as setblocking(0).

shutdown(flag)

Shut down the reading side of the socket (flag == SHUT_RD), the writing side of the socket (flag == SHUT_WR), or both ends (flag == SHUT_RDWR).

timeout

the socket timeout

property type

Read-only access to the socket type.

client.connection_handler

Module name: connection_handler

This module acts as a pathway between the client and connection modules. This means that whatever the connection module doesn’t have access to, the connection handler will provide it with what it needs. This is because most of the connection handling done by the connection module is run on a different thread. And the way the modules are structure disallow for any direct communication between the client and connection modules. Shared attributes probably will (eventually) be implemented to make it thread-safe(r).

class game.client.connection_handler.ConnectionHandler

Bases: object

Class for creating the connection handler.

draw_other_players(game, delta) None

Display all other server players. We don’t draw the main player as it is redundant.

events(e: pygame.event.Event) None

Handle the connection handler events.

get_average_data_received() float

Return the average data received per minute, rounded to kilobytes (not kibibytes).

get_average_data_sent() float

Return the average data sent per minute, rounded to kilobytes (not kibibytes).

get_ping() int

Return the connection ping, only if a connection has been established.

get_player_index_by_name(player_name: str) int | None

Return the index of the player in the player list based on their name, otherwise return None if no connection has been established or if player was not found.

get_players() list

Return the current players in the server, otherwise return an empty list if no connection has been established.

get_total_data_received() float

Return the total data received from the server, rounded to megabytes (not mebibytes).

get_total_data_sent() float

Return the total data sent to the server, rounded to megabytes (not mebibytes).

queue_packet(packet: bytes) None

Queue a packet in the packet queue.

update(game) None

Keep track of the connection state.

client.managers

Client manager package.

Managers in this package are classes that manage different parts of the client, and are mostly used during a live connection to a server.

client.managers.player_manager

Module name: player_manager

This module manages the local player and the local player list received from the server’s global game state, as well as rendering the other players to the screen.

class game.client.managers.player_manager.PlayerManager(player)

Bases: object

Class for handling client player information.

draw_players(player_name: str, delta: float, game: Game) None

Draw to the screen each player present in the players list.

get_player(player_name: str) dict | None

Return the player by player name if they exist, None otherwise.

get_player_index(player_name: str) int | None

Return the index of a player by name if they exist, None otherwise.

set_players(players: list | bytes | None) Self

Set the players list, then return the player manager itself.

client.managers.world_manager

Module name: world_manager

This module manages the local world received from the server’s global game state.

class game.client.managers.world_manager.WorldManager(world)

Bases: object

Class for handling client world information.

build_dynatile_from_bytes(bytes_obj: bytes) None

Build the map’s dynatile data from a bytes object.

build_world_from_bytes(bytes_obj: bytes, update_map_size=False) None

Build the local world from a bytes object.

client.tasks

Module name: tasks

This module defines all the client-side tasks when attempting to establish a connection to a server, or during an established connection to a server. These tasks check the authenticity of the client and any potential errors, as well as send various required packets of game data, requests and responses. The packets are built using the different packet builders.

(See network/builders for the different packet builders.) (See network/protocol for the different requests and responses.)

class game.client.tasks.ClientTasks

Bases: object

Class for regrouping all client-side requests.

static check_packet_queue(sock, queue: list[bytes]) bool

Task for verifying the packet queue. If a packet is present, take the first one, send it to the server and remove it from the queue. [FIFO]

static confirm_local_player(data: bytes) bool

Task for confirming the reception of the local player sent to the server. Return True if the data is a valid packet reception response, otherwise False.

static confirm_map_data(data: bytes) bool

Task for confirming the reception of the map data request. Returns True if the data is a valid server response, otherwise False.

static confirm_queued_packet(data) bool

Task for confirming the reception of the queued packet that was sent to the server. Return True if the data is a valid packet reception response, otherwise False.

static get_global_game_state(sock, data: bytes) bytes | None

Task for receiving the global server-side game state. Return the game state list object if received successfully, otherwise None.

static get_map_data(sock) bytes

Task for receiving the map data object and decompressing it. Return the decompressed map data object once received.

static map_data(sock) None

Task for requesting the map data. Return True if the map data response from the server was received successfully, otherwise False.

static player_join(sock) bool

Task for joining a player to the server. Return True if the response from the server is received, otherwise False.

static recognition(sock) bool

Task for client authenticity. Return True if the client’s connection was successful, otherwise False.

static send_local_player(sock, data: bytes, player_manager: PlayerManager) bool

Task for sending the local player to the server. Return True if the local player packet was sent to and received by the server, otherwise False.

static send_player_name(sock, player_name) bool

Task for sending the client’s name to the server. Raise exception when player name is already taken.