peng3dnet.net - Core Networking Classes¶
-
peng3dnet.net.STRUCT_HEADER= <Struct object>¶ struct.Structinstance used for rapid encoding and decoding of header data.See also
See
peng3dnet.constants.STRUCT_FORMAT_HEADERfor more information.
-
peng3dnet.net.STRUCT_LENGTH32= <Struct object>¶ struct.Structinstance used for rapid encoding and decoding of the length prefix.See also
See
peng3dnet.constants.STRUCT_FORMAT_LENGTH32for more information.
-
class
peng3dnet.net.Server(peng=None, addr=None, clientcls=None, cfg=None)[source]¶ Server class representing the server side of the client-server relationship.
Usually, a server will be able to serve many clients simultaneously without problems. This is achieved using the
selectorsstandard library module, which internally usesselector similiar techniques.If given,
pengshould be an instance ofpeng3d.peng.Pengand will be used for sending events and the configuration system. Note that without a validpengparameter, the event system will not work and a custom config stack will be created. If apengparameter is given, its config stack will be adapted and the event system enabled.See also
See
net.events.enableand Events used by peng3dnet for more information on the event system.addr, if given, should be a value parseable bypeng3dnet.util.normalize_addr_socketstyle(). Ifaddris not given, firstnet.server.addris tried, thennet.server.addr.hostandnet.server.addr.port. If any given address is missing an explicitly specified port,net.server.addr.portis supplemented.clientclsmay be used to override the class used for creating new client-on-server objects. Defaults toClientOnServer.cfgmay be used to override initial configuration values and should be a dictionary.-
addConnType(t, obj)[source]¶ Adds a connection type to the internal registry.
tshould be the string name of the connection type.objshould be an instance of a subclass ofpeng3dnet.conntypes.ConnectionType().Trying to register a name multiple times will cause an
AlreadyRegisteredError.
-
bind()[source]¶ Creates and binds the socket used for listening for new connections.
Repeated calls of this method will be ignored.
If SSL is enabled, an SSL context will be created and the socket wrapped according to the SSL settings.
See also
See
net.ssl.enabledfor more information about the SSL configuration.Currently, the socket will be configured to listen for up to 100 connection requests in parallel.
After binding of the socket, the event
peng3dnet:server.bindwill be sent.
-
close_connection(cid, reason=None)[source]¶ Closes the connection to the given peer due to the optional reason.
cidshould be the Client ID number.reasonmay be a string describing the reason.
-
genCID()[source]¶ Generates a client ID number.
These IDs are guaranteed to be unique to the instance that generated them.
Usually, these will be integers that simply count up and are not meant to be cryptographically secure.
-
initialize()[source]¶ Initializes internal registries used during runtime.
Calling this method repeatedly will be ignored.
Currently, this registers packets and connection types. Additionally, the
peng3dnet:server.initializeevent is sent.Subclasses and Mix-ins may hook into this method via definition of methods named
_reg_packets_*or_reg_conntypes_*with the star being an arbitrary string. The method may not take any arguments.
-
interrupt()[source]¶ Wakes up the main loop by sending a special message to an internal socket.
This forces the main loop to iterate once and check that the system is still running.
Also sends the
peng3dnet:server.interruptevent.
-
join(timeout=None)[source]¶ Waits for all spawned threads to finish.
If
timeoutis given, it indicates the total amount of time spent waiting.If a thread has not been started yet, it will be skipped and not waited for.
-
process(wait=False, timeout=None)[source]¶ Processes all packets awaiting processing.
If
waitis true, this function will wait up totimeoutseconds for new data to arrive.It will then process all packets in the queue, decoding them and then calling the appropriate event handlers. This method assumes all messages are packed with msgpack.
If the connection type allows it, event handlers will be called and the
peng3dnet:server.connection.recvevent is sent.This method returns the number of packets processed.
-
process_async()[source]¶ Processes packets asynchronously.
Internally calls
process_forever()in a separate daemon thread namedpeng3dnet process Thread.
-
process_forever()[source]¶ Processes packets in a blocking manner.
Note that this method is currently not interruptable and thus uses short timeouts of about 10ms, causing a slight delay in stopping this loop.
-
process_single_packet(client)[source]¶ Called when there should be enough data to process a single packet.
clientis an instance ofClientOnServerrepresenting the client.Currently parses a single packet including length prefix and calls
receive_packet()with the packet data.
-
receive_data(data, cid)[source]¶ Called when new raw data has been read from a socket.
Note that the given
datamay contain only parts of a packet or even multiple packets.cidis the integer ID number of the client the data was received from.By default, the received data is stored in a buffer until enough data is available to process a packet, then
process_single_packet()is called.
-
receive_packet(data, cid)[source]¶ Called when a full packet has been received.
datais the raw packet data without length prefix.cidis the integer ID number of the client.Currently, this puts the data in a queue to be processed further by
process().
-
register_packet(name, obj, n=None)[source]¶ Registers a new packet with the internal registry.
nameshould be a string of formatnamespace:category.subcategory.namewhere category may be repeated.objshould be an instance of a subclass ofpeng3dnet.packet.Packet().nmay be optionally used to force a packet to use a specific packet ID, otherwise one will be generated.
-
runAsync(selector=<class 'selectors.EpollSelector'>)[source]¶ Runs the server main loop in a seperate thread.
selectormay be changed to override the selector used for smart waiting.This method does not block and should return immediately.
The newly created thread will be named
peng3dnet Server Threadand is a daemon thread, i.e. it will not keep the program alive.
-
runBlocking(selector=<class 'selectors.EpollSelector'>)[source]¶ Runs the server main loop in a blocking manner.
selectormay be changed to override the selector used for smart waiting.This method blocks until
stop()is called.
-
sendEvent(event, data=None)[source]¶ Helper method used to send events.
Checks if the event system is enabled, adds the
pengandserverdata attributes and then sends it.
-
send_message(ptype, data, cid)[source]¶ Sends a message to the specified peer.
ptypeshould be a valid packet type, e.g. either an ID, name or object.datashould be the data to send to the peer.cidshould be the Client ID number to send the message to.Note that all data encoding will be done synchronously and may cause this method to not return immediately. The packet may also be encrypted and compressed, if applicable.
Additionally, the
peng3dnet:server.connection.sendevent is sent if the connection type allows it.
-
shutdown(join=True, timeout=0, reason='servershutdown')[source]¶ Shuts down the server, disconnecting all clients.
If
joinis true, this method will block until all clients are disconnected ortimeoutseconds have passed. Iftimeoutis0, it will be ignored.reasonwill be used as the closing reason and transmitted to all clients.After these messages have been sent,
stop()is called and thepeng3dnet:server.shutdownevent is sent.
-
stop()[source]¶ Stops the running server main loop.
Will set an internal flag, then sends the event
peng3dnet:server.stopand callsinterrupt()to force the close.Note that this will not close open connections properly, use
shutdown()instead.
-
-
class
peng3dnet.net.ClientOnServer(server, conn, addr, cid)[source]¶ Class representing a client on the server.
This serves mainly as a data structure for storing the state of a specific connection.
This class is not intended to be created manually.
serveris the instance ofServerthat created this object.connis the socket that should be used for communication with this client.addris the address of this client.cidis the unique Client ID assigned to this client.-
close(reason=None)[source]¶ Called to close the connection to this client.
This method is not an event handler, use
on_close()instead.
-
on_close(reason=None)[source]¶ Called when the connection has been closed.
reasonis the reason sent either by the peer or passed by the caller ofServer.close_connection().
-
on_connect()[source]¶ Sent once a connection has been established.
Note that at the time this method is called the handshake may not be finished, see
on_handshake_complete()instead.
-
on_handshake_complete()[source]¶ Called when the handshake has been completed.
The default implementation sends the
peng3dnet:server.connection.handshakecompleteevent and changes the connection state toSTATE_ACTIVE.May be overridden by subclasses.
-
-
class
peng3dnet.net.Client(peng=None, addr=None, cfg=None, conntype='classic')[source]¶ Client class representing the client side of the client-server relationship.
A client can only be connected to a single server during its lifetime, recycling of client instances is not supported.
If given,
pengshould be an instance ofpeng3d.peng.Pengand will be used for sending events and the configuration system. Note that without a validpengparameter, the event system will not work and a custom config stack will be created. If apengparameter is given, its config stack will be adapted and the event system enabled.See also
See
net.events.enableand Events used by peng3dnet for more information on the event system.addr, if given, should be a value parseable bypeng3dnet.util.normalize_addr_socketstyle(). Ifaddris not given, firstnet.client.addris tried, thennet.client.addr.hostandnet.client.addr.port. If any given address is missing an explicitly specified port,net.client.addr.portis supplemented.cfgmay be used to override initial configuration values and should be a dictionary.Optionally, the connection type may be specified via
conntype, which may be set to a string identifying the type of the connection. This should usually beCONNTYPE_CLASSICor one of the otherCONNTYPE_*constants. Note that the connection type specified must also be registered viaaddConnType(), except for the built-in connection types.-
addConnType(t, obj)[source]¶ Adds a connection type to the internal registry.
tshould be the string name of the connection type.objshould be an instance of a subclass ofpeng3dnet.conntypes.ConnectionType().Trying to register a name multiple times will cause an
AlreadyRegisteredError.
-
close(reason=None)[source]¶ Called to close the connection to the server.
This method is not an event handler, use
on_close()instead.Also sends the event
peng3dnet:client.close.
-
close_connection(cid=None, reason=None)[source]¶ Closes the connection to the server due to the optional reason.
cidis a dummy value used for compatibility with server applications.reasonmay be a string describing the reason.
-
connect()[source]¶ Connects the client with a server.
Note that the server must have been specified before calling this method via either the
addrargument to the initializer or any of thenet.client.addrconfig values.Repeated calls of this method will be ignored.
If SSL is enabled, this method will also initialize the SSL Context and load the certificates.
After the connection has been made, the
peng3dnet.client.connectevent is sent.
-
initialize()[source]¶ Initializes internal registries used during runtime.
Calling this method repeatedly will be ignored.
Currently, this registers packets and connection types. Additionally, the
peng3dnet:client.initializeevent is sent.Subclasses and Mix-ins may hook into this method via definition of methods named
_reg_packets_*or_reg_conntypes_*with the star being an arbitrary string. The methods may not take any arguments.
-
interrupt()[source]¶ Wakes up the main loop by sending a special message to an internal socket.
This forces the main loop to iterate once and check that the system is still running.
Also sends the
peng3dnet:client.interruptevent.
-
join(timeout=None)[source]¶ Waits for all spawned threads to finish.
If
timeoutis given, it indicates the total amount of time spent waiting.If a thread has not been started yet, it will be skipped and not waited for.
-
on_close(reason=None)[source]¶ Event handler called during connection shutdown.
It may or may not be possible to send data over the connection within this method, depending on various factors.
-
on_connect()[source]¶ Event handler called once a connection has been established.
Note that usually the handshake will still be in progress while this method is called.
See also
See the
on_handshake_complete()event handler for a better indicator when data can be sent.
-
on_handshake_complete()[source]¶ Callback called once the handshake has been completed.
The default implementation sends the
peng3dnet:client.handshakecompleteevent and sets the connection state toSTATE_ACTIVE.
-
process(wait=False, timeout=None)[source]¶ Processes all packets awaiting processing.
If
waitis true, this function will wait up totimeoutseconds for new data to arrive.It will then process all packets in the queue, decoding them and then calling the appropriate event handlers. This method assumes all messages are packed with msgpack.
If the connection type allows it, event handlers will be called and the
peng3dnet:client.recvevent is sent.This method returns the number of packets processed.
-
process_async()[source]¶ Processes packets asynchronously.
Internally calls
process_forever()in a separate daemon thread namedpeng3dnet process Thread.
-
process_forever()[source]¶ Processes packets in a blocking manner.
Note that this method is currently not interruptable and thus uses short timeouts of about 10ms, causing a slight delay in stopping this loop.
-
process_single_packet(client=None)[source]¶ Called when there should be enough data to process a single packet.
clientis a dummy value used for compatibilizy with server applications.Currently parses a single packet including length prefix and calls
receive_packet()with the packet data.
-
pump_write_buffer()[source]¶ Tries to send as much of the data in the internal buffer as possible.
Note that depending on various factors, not all data may be sent at once. It is possible that sent data will be fragmented at arbitrary points.
If an exception occurs while sending the data, it will be ignored and the error printed to the console.
-
receive_data(data, cid=None)[source]¶ Called when new raw data has been read from the socket.
Note that the given
datamay contain only parts of a packet or even multiple packets.cidis a dummy value used for compatibility with server applications.By default, the received data is stored in a buffer until enough data is available to process a packet, then
process_single_packet()is called.
-
receive_packet(data, cid=None)[source]¶ Called when a full packet has been received.
datais the raw packet data without length prefix.cidis a dummy value used for compatibility with server applications.Currently, this puts the data in a queue to be processed further by
process().
-
register_packet(name, obj, n=None)[source]¶ Registers a new packet with the internal registry.
nameshould be a string of formatnamespace:category.subcategory.namewhere category may be repeated.objshould be an instance of a subclass ofpeng3dnet.packet.Packet().nmay be optionally used to force a packet to use a specific packet ID, otherwise one will be generated.
-
runAsync(selector=<class 'selectors.EpollSelector'>)[source]¶ Runs the client main loop in a seperate thread.
selectormay be changed to override the selector used for smart waiting.This method does not block and should return immediately.
The newly created thread will be named
peng3dnet Client Threadand is a daemon thread, i.e. it will not keep the program alive.
-
runBlocking(selector=<class 'selectors.EpollSelector'>)[source]¶ Runs the client main loop in a blocking manner.
selectormay be changed to override the selector used for smart waiting.This method blocks until
stop()is called.
-
sendEvent(event, data)[source]¶ Helper method used to send events.
Checks if the event system is enabled, adds the
pengandclientdata attributes and then sends it.
-
send_message(ptype, data, cid=None)[source]¶ Sends a message to the server.
ptypeshould be a valid packet type, e.g. either an ID, name or object.datashould be the data to send to the server.cidwill be ignored, available for compatibility with server applications.Note that all data encoding will be done synchronously and may cause this method to not return immediately. The packet may also be encrypted and compressed, if applicable.
Additionally, the
peng3dnet:client.sendevent is sent if the connection type allows it.
-
stop()[source]¶ Stops the running client main loop.
Will set an internal flag, then sends the event
peng3dnet:client.stopand callsinterrupt()to force the close.Note that this will not close open connections properly, call
close_connection()before calling this method.
-