ENetMultiplayerPeer
A MultiplayerPeer implementation using the ENet library.
ENetMultiplayerPeer.swift:20class ENetMultiplayerPeer
A MultiplayerPeer implementation that should be passed to multiplayerPeer
after being initialized as either a client, server, or mesh. Events can then be handled by connecting to MultiplayerAPI
signals. See ENetConnection
for more information on the ENet library wrapper.
Superclasses
class MultiplayerPeer
Abstract class for specialized
PacketPeer
s used by theMultiplayerAPI
.
Citizens in SwiftGodot
Conformances
protocol CustomStringConvertible
A type with a customized textual representation.
protocol Equatable
A type that can be compared for value equality.
protocol Hashable
A type that can be hashed into a
Hasher
to produce an integer hash value.protocol Identifiable<ID>
A class of types whose instances hold the value of an entity with stable identity.
protocol VariantRepresentable
Types that conform to VariantRepresentable can be stored directly in
Variant
with no conversion. These include all of the Variant types from Godot (for exampleGString
,Rect
,Plane
), Godot objects (those that subclass SwiftGodot.Object) as well as the built-in Swift types UInt8, Int64 and Double.protocol VariantStorable
Types that conform to VariantStorable can be stored in a Variant and can be extracted back out of a Variant.
Type members
Instance members
var host: ENetConnection?
The underlying
ENetConnection
created aftercreateClient(address:port:channelCount:inBandwidth:outBandwidth:localPort:)
andcreateServer(port:maxClients:maxChannels:inBandwidth:outBandwidth:)
.func addMeshPeer(peerId: Int32, host: ENetConnection?
) -> GodotError Add a new remote peer with the given
peerId
connected to the givenhost
.func createClient(address: String, port: Int32, channelCount: Int32, inBandwidth: Int32, outBandwidth: Int32, localPort: Int32
) -> GodotError Create client that connects to a server at
address
using specifiedport
. The given address needs to be either a fully qualified domain name (e.g."www.example.com"
) or an IP address in IPv4 or IPv6 format (e.g."192.168.1.1"
). Theport
is the port the server is listening on. ThechannelCount
parameter can be used to specify the number of ENet channels allocated for the connection. TheinBandwidth
andoutBandwidth
parameters can be used to limit the incoming and outgoing bandwidth to the given number of bytes per second. The default of 0 means unlimited bandwidth. Note that ENet will strategically drop packets on specific sides of a connection between peers to ensure the peer’s bandwidth is not overwhelmed. The bandwidth parameters also determine the window size of a connection which limits the amount of reliable packets that may be in transit at any given time. Returnsok
if a client was created,errAlreadyInUse
if this ENetMultiplayerPeer instance already has an open connection (in which case you need to callclose
first) orerrCantCreate
if the client could not be created. IflocalPort
is specified, the client will also listen to the given port; this is useful for some NAT traversal techniques.func createMesh(uniqueId: Int32
) -> GodotError Initialize this
MultiplayerPeer
in mesh mode. The provideduniqueId
will be used as the local peer network unique ID once assigned as themultiplayerPeer
. In the mesh configuration you will need to set up each new peer manually usingENetConnection
before callingaddMeshPeer(peerId:host:)
. While this technique is more advanced, it allows for better control over the connection process (e.g. when dealing with NAT punch-through) and for better distribution of the network load (which would otherwise be more taxing on the server).func createServer(port: Int32, maxClients: Int32, maxChannels: Int32, inBandwidth: Int32, outBandwidth: Int32
) -> GodotError Create server that listens to connections via
port
. The port needs to be an available, unused port between 0 and 65535. Note that ports below 1024 are privileged and may require elevated permissions depending on the platform. To change the interface the server listens on, usesetBindIp(_:)
. The default IP is the wildcard"*"
, which listens on all available interfaces.maxClients
is the maximum number of clients that are allowed at once, any number up to 4095 may be used, although the achievable number of simultaneous clients may be far lower and depends on the application. For additional details on the bandwidth parameters, seecreateClient(address:port:channelCount:inBandwidth:outBandwidth:localPort:)
. Returnsok
if a server was created,errAlreadyInUse
if this ENetMultiplayerPeer instance already has an open connection (in which case you need to callclose
first) orerrCantCreate
if the server could not be created.func getPeer(id: Int32
) -> ENetPacketPeer? Returns the
ENetPacketPeer
associated to the givenid
.func setBindIp(String
) The IP used when creating a server. This is set to the wildcard
"*"
by default, which binds to all available interfaces. The given IP needs to be in IPv4 or IPv6 address format, for example:"192.168.1.1"
.