mzehr.net: Types Modinfo Source  

Network module based on ENet (http://enet.bespin.org)

Types Summary

TNetClient Class used to establish a connection with a server.
TNetClientEvent An event raised by the client.
TNetPacket A data packet/message. Data can be written into or read from the packet.
TNetPeer Represents a peer/client/player on the server.
TNetServer Class used to run a server.
TNetServerEvent An event raised by the server.

Types

Type TNetClient
DescriptionClass used to establish a connection with a server.
Methods Summary
Connect Establishes a connection with a server.
Disconnect Closes the connection to the server.
Flush Sends all buffered messages as soon as possible or immediately.
Send Sends a packet / a message to the server.
Update Updates the client (receives and sends messages).
Functions Summary
Create Creates a new and unconnected client.
Method Connect:Int(host:String, port:Int, connectTimeout:Int = 5000, channelCount:Int = 2)
ReturnsTrue if the connection has been established, False if not.
DescriptionEstablishes a connection with a server.
Information
host:String			IP address or hostname on which to listen for incoming connections.
port:Int			Port on which to listen for incoming connections.
connectTimeout:Int		(Optional) Maximum time in ms to wait for the connection.
channelCount:Int		(Optional) Number of communication channels. Will be limited to 1-255. (Default: 2)
Method Disconnect(disconnectTime:Int = 2000)
DescriptionCloses the connection to the server.
Information
disconnectTime:Int		(Optional) Maximum time in ms to wait for remaining messages to be sent before the connection will be closed.
Method Flush()
DescriptionSends all buffered messages as soon as possible or immediately.
InformationIt's better to use Update, because Update does almost the same and also receives incoming messages.
Method Send(packet:TNetPacket, reliable:Int = False, channel:Int = 0, compressed:Int = False)
DescriptionSends a packet / a message to the server.
Information
packet:TNetPacket		The packet that will be sent
reliable:Int			(Optional) Whether the packet should be sent reliable or not (Default: False, this is suitable for unimportant anf fast data like player positions)
channel:Int			(Optional) The channel used for transmitting the packet (Default: 0, reliable/important messages should be sent on a different channel than unimportant messages, for example channel 1)
compressed:Int			(Optional) Whether the data will be compressed or not (Default: False, should only be used with large data, i.e. when sending 100x100 tiles etc.)
Method Update:TNetClientEvent(waitTime:Int = 0)
ReturnsAn object of type TNetClientEvent that represents the last event or Null if there was no event.
DescriptionUpdates the client (receives and sends messages).
Information
waitTime:Int		(Optional) Time in ms to wait for incoming messages. (Default = 0, this means the function will return immediatley if there are no messages to receive).
Function Create:TNetClient()
DescriptionCreates a new and unconnected client.

Type TNetClientEvent
DescriptionAn event raised by the client.
Constants Summary
CONNECT , DATA , DISCONNECT
Fields Summary
Channel , Event , Packet
Const CONNECT:Int
DescriptionServer: A new client has established a connection with the server. | Client: The connection with the server has been established.
Const DATA:Int
DescriptionData received (client: from the server | server: from a specific client).
Const DISCONNECT:Int
DescriptionThe connection was closed (client: connection with the server | server: connection with a specific client).
Field Channel:Int
DescriptionThe channel on which the packet was received.
Field Event:Int
DescriptionEvent that was raised (equals to TNetClientEvent.CONNECT or DATA).
Field Packet:TNetPacket
DescriptionThe packet that was received (only available in a DATA event).

Type TNetPacket
DescriptionA data packet/message. Data can be written into or read from the packet.
Methods Summary
Destroy Releases the memory used by the packet.
EnetPacket Converts the packet to an ENet packet struct.
Read Reads multiple Bytes from the packet and writes them to the specified memory location.
ReadByte Reads a byte from the packet.
ReadBytes Reads multiple Bytes from the packet and writes them to the specified memory location.
ReadDouble Reads a double from the packet.
ReadFloat Reads a float from the packet.
ReadInt Reads an int from the packet.
ReadLine Reads a line of text from the packet.
ReadLong Reads a long from the packet.
ReadObject Reads an object from the packet (untested).
ReadShort Reads a short from the packet.
ReadString Reads a string from the packet.
Write Writes multiple bytes into the packet.
WriteByte Writes a byte into the packet.
WriteBytes Writes multiple bytes into the packet.
WriteDouble Writes a double into the packet.
WriteFloat Writes a float into the packet.
WriteInt Writes an int into the packet.
WriteLine Writes a line of text into the packet.
WriteLong Writes a long into the packet.
WriteObject Writes an object to the packet (untested).
WriteShort Writes a short into the packet.
WriteString Writes a string into the packet.
Functions Summary
Create Creates a new packet (for sending).
FromEnet Reads a packet from an ENet packet struct.
Method Destroy()
DescriptionReleases the memory used by the packet.
InformationWill be called automatically on deconstruction of the object (garbage collector), but can also be called manually.
Method EnetPacket:Byte Ptr(enet_flags:Int = 0, compressed:Int = False)
DescriptionConverts the packet to an ENet packet struct.
InformationThis method is used internally and should not be called manually.
Method Read:Int(buf:Byte Ptr, count:Int)
ReturnsNumber of successfully read bytes.
DescriptionReads multiple Bytes from the packet and writes them to the specified memory location.
Information
buf:Byte Ptr		Memory location where the bytes will be written to.
count:Int		Number of bytes that should be read.
Method ReadByte:Byte()
DescriptionReads a byte from the packet.
Method ReadBytes:Int(buf:Byte Ptr, count:Int)
ReturnsNumber of successfully read bytes.
DescriptionReads multiple Bytes from the packet and writes them to the specified memory location.
Information
buf:Byte Ptr		Memory location where the bytes will be written to.
count:Int		Number of bytes that should be read.
Method ReadDouble:Double()
DescriptionReads a double from the packet.
Method ReadFloat:Float()
DescriptionReads a float from the packet.
Method ReadInt:Int()
DescriptionReads an int from the packet.
Method ReadLine:String()
DescriptionReads a line of text from the packet.
Method ReadLong:Long()
DescriptionReads a long from the packet.
Method ReadObject:Object()
DescriptionReads an object from the packet (untested).
Method ReadShort:Short()
DescriptionReads a short from the packet.
Method ReadString:String()
DescriptionReads a string from the packet.
InformationReads an int that specifies the length of the string and then the string itself.
Method Write(buf:Byte Ptr, count:Int)
DescriptionWrites multiple bytes into the packet.
Information
buf:Byte Ptr		Memory location from where the data is read and then written to the packet.
count:Int		Number of bytes that should be read/written.
Method WriteByte(value:Byte)
DescriptionWrites a byte into the packet.
Method WriteBytes(buf:Byte Ptr, count:Int)
DescriptionWrites multiple bytes into the packet.
Information
buf:Byte Ptr		Memory location from where the data is read and then written to the packet.
count:Int		Number of bytes that should be read/written.
Method WriteDouble(value:Double)
DescriptionWrites a double into the packet.
Method WriteFloat(value:Float)
DescriptionWrites a float into the packet.
Method WriteInt(value:Int)
DescriptionWrites an int into the packet.
Method WriteLine(value:String)
DescriptionWrites a line of text into the packet.
Method WriteLong(value:Long)
DescriptionWrites a long into the packet.
Method WriteObject(value:Object)
DescriptionWrites an object to the packet (untested).
Method WriteShort(value:Short)
DescriptionWrites a short into the packet.
Method WriteString(value:String)
DescriptionWrites a string into the packet.
InformationWrites an int containing the length of the string followed by the string itself.
Function Create:TNetPacket()
DescriptionCreates a new packet (for sending).
Function FromEnet:TNetPacket(p:Byte Ptr)
DescriptionReads a packet from an ENet packet struct.
InformationThis function is used internally and should not be called manually.

Type TNetPeer
DescriptionRepresents a peer/client/player on the server.
Methods Summary
GetData Gets the data assigned to this peer (see SetData).
GetPeer Returns the ENet peer struct.
SetData Assigns a custom data object to this peer.
Functions Summary
Create Creates a new peer from an ENet peer struct.
Method GetData:Object()
DescriptionGets the data assigned to this peer (see SetData).
Method GetPeer:Byte Ptr()
DescriptionReturns the ENet peer struct.
InformationThis methos is used internally by the server and should not be called manually.
Method SetData(value:Object)
DescriptionAssigns a custom data object to this peer.
InformationThis can be used to assign a player object or other data to the peer. On the server, you can retrieve a list of connected peers with TNetServer.GetClients() and those peers (TNetPeer) contain the assigned data object which can be fetched by calling TNetPeer.GetData().
Function Create:TNetPeer(peer:Byte Ptr)
DescriptionCreates a new peer from an ENet peer struct.
InformationThis method is used internally by the server and should not be called manually.

Type TNetServer
DescriptionClass used to run a server.
Methods Summary
Broadcast Sends a packet to all connected peers / players.
DisconnectClient Sends a disconnect message to the peer / player and closes the connection after the message has been acknowledged.
DropClient Closes the connection to a peer immediately. The peer won't be notified and will only notice the disconnection after the ping timeout.
Flush Sends all buffered messages as soon as possible or immediately.
GetClients Gets a list containing all the connected peers / players.
Send Sends a packet to the specified peer / player.
Shutdown Shuts down the server.
Update Updates the server (receives and sends messages).
Functions Summary
Create Creates a new server that listens for incoming connections on the specified ip address and port.
Method Broadcast(packet:TNetPacket, reliable:Int = False, channel:Int = 0, compressed:Int = False)
DescriptionSends a packet to all connected peers / players.
Information
packet:TNetPacket	The packet that will be sent.
reliable:Int		(Optional) Whether the packet should be sent reliable or not (Default: False, this is suitable for unimportant anf fast data like player positions)
channel:Int		(Optional) The channel used for transmitting the packet (Default: 0, reliable/important messages should be sent on a different channel than unimportant messages, for example channel 1).
compressed:Int		(Optional) Whether the data will be compressed or not (Default: False, should only be used with large data, i.e. when sending 100x100 tiles etc.)
Method DisconnectClient(peer:TNetPeer)
DescriptionSends a disconnect message to the peer / player and closes the connection after the message has been acknowledged.
Method DropClient(peer:TNetPeer)
DescriptionCloses the connection to a peer immediately. The peer won't be notified and will only notice the disconnection after the ping timeout.
Method Flush()
DescriptionSends all buffered messages as soon as possible or immediately.
InformationIt's better to use Update, because Update does almost the same and also receives incoming messages.
Method GetClients:TList()
DescriptionGets a list containing all the connected peers / players.
InformationYou should not modify the list in any way! Use the DisconnectClient or DropClient functions instead.
Method Send(peer:TNetPeer, packet:TNetPacket, reliable:Int = False, channel:Int = 0, compressed:Int = False)
DescriptionSends a packet to the specified peer / player.
Information
peer:TNetPeer		The peer / player that will receive the packet.
packet:TNetPacket	The packet that will be sent.
reliable:Int		(Optional) Whether the packet should be sent reliable or not (Default: False, this is suitable for unimportant anf fast data like player positions)
channel:Int		(Optional) The channel used for transmitting the packet (Default: 0, reliable/important messages should be sent on a different channel than unimportant messages, for example channel 1).
compressed:Int		(Optional) Whether the data will be compressed or not (Default: False, should only be used with large data, i.e. when sending 100x100 tiles etc.)
Method Shutdown(shutdownTime:Int = 5000)
DescriptionShuts down the server.
Information
shutdownTime:Int		(Optional) Maximum time in ms to wait for the remaining messages to be sent before shutting down. (Default: 5000)
Method Update:TNetServerEvent(waitTime:Int = 0)
ReturnsAn object of Type TNetServerEvent tha represents the last event or null if there was no event.
DescriptionUpdates the server (receives and sends messages).
Information
waitTime:Int			(Optional) Time in ms to wait for incoming messages. (Default = 0, this means the function will return immediatley if there are no messages to receive).
Function Create:TNetServer(ip:String, port:Int, maxConnections:Int, maxChannels:Int = 2)
ReturnsAn object of Type TNetServer or Null in case the server could not be started.
DescriptionCreates a new server that listens for incoming connections on the specified ip address and port.
Information
ip:String			IP address on which to listen for incoming connections (use Null, "127.0.0.1" or "localhost" to listen on all local addresses).
port:Int			Port number (should be greater than 1000 and as high as possible, i.e. 50000 or another number that is not used by other protocols or services).
maxConnections:Int		Maximum number of allowed connections / players.
maxChannels:Int			(Optional) Maximum number of communication channels. Will be limited to 1-255. (Default: 2)

Type TNetServerEvent Extends TNetClientEvent
DescriptionAn event raised by the server.
Fields Summary
Peer
Field Peer:TNetPeer
DescriptionThe client that received data/connected/disconnected.

Module Information

AuthorMichael Zehr (Jolinah)
LicenseMIT X11 License (License.txt)
CreditLee Salzman (http://enet.bespin.org)
History1.0 First release
History1.1 Instead of importing pub.enet, the newest source code of ENet has been compiled into the module (version 1.3.13 as of now). However, the protocol of ENet version 1.3 and above is not compatible with ENet versions 1.2 and below. You will need to redistribute the client to all existing players after updating the server or they won't be able to play on the new server.