Difference between revisions of "lua-enet"

m
m
(12 intermediate revisions by 7 users not shown)
Line 1: Line 1:
{{notice|Official documentation for lua-enet is available [http://leafo.net/lua-enet/ here]. ENet's features are listed on its [http://enet.bespin.org/Features.html homepage]. Be weary that there's misspellings and misleading examples in the official documentation.}}
+
{{newin|[[0.9.0]]|090|type=module}}
  
lua-enet is simply some lua bindings for ENet.
+
{{notice|Official documentation for lua-enet is available [http://leafo.net/lua-enet/ here]. ENet's features are listed on its [http://enet.bespin.org/Features.html homepage]. The official documentation may have typos. The documentation on this wiki reflects Löve's implementation, meaning it should be safe to follow what's written here.}}
  
ENet's purpose is to provide a relatively thin, simple and robust network communication layer on top of UDP (User Datagram Protocol).The primary feature it provides is optional reliable, in-order delivery of packets.
+
lua-enet is simply some Lua bindings for ENet.
 +
 
 +
ENet's purpose is to provide a relatively thin, simple and robust network communication layer for games on top of UDP (User Datagram Protocol).The primary feature it provides is optional reliable, in-order delivery of packets.
  
 
ENet omits certain higher level networking features such as authentication, lobbying, server discovery, encryption, or other similar tasks that are particularly application specific so that the library remains flexible, portable, and easily embeddable.
 
ENet omits certain higher level networking features such as authentication, lobbying, server discovery, encryption, or other similar tasks that are particularly application specific so that the library remains flexible, portable, and easily embeddable.
Line 27: Line 29:
 
|-
 
|-
 
|-
 
|-
|[[enet.host:service | host:service]]
+
|[[enet.host_create | host_create]]
|Wait for [[enet.event | events]], send and receive any ready packets.
+
|Returns a new host.
|-
 
|[[enet.host:check_events | host:check_events]]
 
|Checks for any queued [[enet.event | events]] and dispatches one if available.
 
|-
 
|[[enet.host:connect | host:connect]]
 
|Connects a host to a remote host. Returns [[enet.peer | peer]] object associated with remote host.
 
|-
 
|[[enet.host:flush | host:flush]]
 
|Sends any queued packets.
 
|-
 
|[[enet.host:broadcast | host:broadcast]]
 
|Queues a packet to be sent to all connected peers.
 
|-
 
|[[enet.host:channel_limit | host:channel_limit]]
 
|Sets the maximum number of channels allowed.
 
|-
 
|[[enet.host:bandwidth_limit | host:bandwidth_limit]]
 
|Sets the bandwidth limits of the host in bytes/sec.
 
|-
 
|[[enet.host:socket_get_address | host:socket_get_address]]
 
|Returns a [[string]] that describes the socket address of the given host.
 
|-
 
|[[enet.host:total_sent_data | host:total_sent_data]]
 
|Returns the [[number]] of bytes that were sent through the given host.
 
|-
 
|[[enet.host:total_received_data | host:total_received_data]]
 
|Returns the [[number]] of bytes that were received by the given host.
 
|-
 
|[[enet.host:service_time | host:service_time]]
 
|Returns the timestamp of the last call to [[enet.host:service | host:service()]] or [[enet.host:flush | host:flush()]].
 
|-
 
|[[enet.host:peer_count | host:peer_count]]
 
|Returns the number of peers that are allocated for the given host.
 
|-
 
|[[enet.host:get_peer | host:get_peer]]
 
|Returns the connected [[enet.peer | peer]] at the specified index (starting at 1).
 
|-
 
|[[enet.host:__gc | host:__gc]]
 
|Destroys the host, freeing any bound ports and addresses.
 
|-
 
|[[enet.peer:disconnect | peer:disconnect]]
 
|Requests a disconnection from the peer.
 
|-
 
|[[enet.peer:disconnect_now | peer:disconnect_now]]
 
|Force immediate disconnection from peer.
 
|-
 
|[[enet.peer:disconnect_later | peer:disconnect_later]]
 
|Request a disconnection from peer, but only after all queued outgoing packets are sent.
 
|-
 
|[[enet.peer:reset | peer:reset]]
 
|Forcefully disconnects peer. The peer is not notified of the disconnection.
 
|-
 
|[[enet.peer:ping | peer:ping]]
 
|Send a ping request to peer, updates [[enet.peer:round_trip_time | round_trip_time]]. This is called automatically at regular intervals.
 
|-
 
|[[enet.peer:receive | peer:receive]]
 
|Attempts to dequeue an incoming packet for this peer.
 
|-
 
|[[enet.peer:send | peer:send]]
 
|Queues a packet to be sent to peer.
 
|-
 
|[[enet.peer:throttle_configure | peer:throttle_configure]]
 
|Changes the probability at which unreliable packets should not be dropped.
 
 
|-
 
|-
|[[enet.peer:ping_interval | peer:ping_interval]]
+
|[[enet.linked_version| linked_version]]
|Specifies the interval in milliseconds that pings are sent to the other end of the connection (defaults to 500).
+
|Returns the included ENet's version string.
|-
 
|[[enet.peer:timeout | peer:timeout]]
 
|Returns or sets the parameters when a timeout is detected.
 
|-
 
|[[enet.peer:index | peer:index]]
 
|Returns the index of the peer.
 
|-
 
|[[enet.peer:state | peer:state]]
 
|Returns the state of the peer.
 
|-
 
|[[enet.peer:connect_id | peer:connect_id]]
 
|Returns the field ENetPeer::connectID that is assigned for each connection.
 
|-
 
|[[enet.peer:round_trip_time | peer:round_trip_time]]
 
|Returns or sets the current round trip time (i.e. ping).
 
|-
 
|[[enet.peer:last_round_trip_time | peer:last_round_trip_time]]
 
|Returns or sets the round trip time of the previous round trip time computation.
 
 
|}
 
|}
  
Line 119: Line 40:
 
<source lang="Lua">
 
<source lang="Lua">
 
-- server.lua
 
-- server.lua
require "enet"
+
local enet = require "enet"
 
local host = enet.host_create("localhost:6789")
 
local host = enet.host_create("localhost:6789")
local event = host:service(100)
+
while true do
while event do
+
  local event = host:service(100)
  if event.type == "receive" then
+
  while event do
    print("Got message: ", event.data, event.peer)
+
    if event.type == "receive" then
    event.peer:send( "pong" )
+
      print("Got message: ", event.data, event.peer)
  elseif event.type == "connect" then
+
      event.peer:send( "pong" )
    print(event.peer .. " connected.")
+
    elseif event.type == "connect" then
  elseif event.type == "disconnect" then
+
      print(event.peer, "connected.")
    print(event.peer .. " disconnected.")
+
    elseif event.type == "disconnect" then
 +
      print(event.peer, "disconnected.")
 +
    end
 +
    event = host:service()
 
   end
 
   end
  event = host:service()
 
 
end
 
end
 
</source>
 
</source>
Line 138: Line 61:
 
<source lang="Lua">
 
<source lang="Lua">
 
-- client.lua
 
-- client.lua
require "enet"
+
local enet = require "enet"
 
local host = enet.host_create()
 
local host = enet.host_create()
 
local server = host:connect("localhost:6789")
 
local server = host:connect("localhost:6789")
local event = host:service(100)
+
while true do
while event do
+
  local event = host:service(100)
  if event.type == "receive" then
+
  while event do
    print("Got message: ", event.data, event.peer)
+
    if event.type == "receive" then
    event.peer:send( "ping" )
+
      print("Got message: ", event.data, event.peer)
  elseif event.type == "connect" then
+
      event.peer:send( "ping" )
    print(event.peer .. " connected.")
+
    elseif event.type == "connect" then
    event.peer:send( "ping" )
+
      print(event.peer, "connected.")
  elseif event.type == "disconnect" then
+
      event.peer:send( "ping" )
    print(event.peer .. " disconnected.")
+
    elseif event.type == "disconnect" then
 +
      print(event.peer, "disconnected.")
 +
    end
 +
    event = host:service()
 
   end
 
   end
  event = host:service()
 
 
end
 
end
 
</source>
 
</source>
 +
 +
== See Also ==
 +
* [[parent::love]]
 +
* [[socket]]
 +
== Other Languages ==
 +
{{i18n|lua-enet}}
 +
 +
[[Category:Libraries]]
 +
{{#set:Description=Multiplayer networking module for games.}}
 +
{{#set:LOVE Version=0.9.0}}
 +
{{#set:Keyword=Networking}}

Revision as of 03:19, 25 March 2018

Available since LÖVE 0.9.0
This module is not supported in earlier versions.


O.png Official documentation for lua-enet is available here. ENet's features are listed on its homepage. The official documentation may have typos. The documentation on this wiki reflects Löve's implementation, meaning it should be safe to follow what's written here.  


lua-enet is simply some Lua bindings for ENet.

ENet's purpose is to provide a relatively thin, simple and robust network communication layer for games on top of UDP (User Datagram Protocol).The primary feature it provides is optional reliable, in-order delivery of packets.

ENet omits certain higher level networking features such as authentication, lobbying, server discovery, encryption, or other similar tasks that are particularly application specific so that the library remains flexible, portable, and easily embeddable.

Types

Type Description
host An ENet host for communicating with peers.
peer An ENet peer which data packets may be sent or received from.
event A simple table containing information on an event.

Functions

Function Description
host_create Returns a new host.
linked_version Returns the included ENet's version string.

Examples

server.lua

-- server.lua
local enet = require "enet"
local host = enet.host_create("localhost:6789")
while true do
  local event = host:service(100)
  while event do
    if event.type == "receive" then
      print("Got message: ", event.data, event.peer)
      event.peer:send( "pong" )
    elseif event.type == "connect" then
      print(event.peer, "connected.")
    elseif event.type == "disconnect" then
      print(event.peer, "disconnected.")
    end
    event = host:service()
  end
end

client.lua

-- client.lua
local enet = require "enet"
local host = enet.host_create()
local server = host:connect("localhost:6789")
while true do
  local event = host:service(100)
  while event do
    if event.type == "receive" then
      print("Got message: ", event.data, event.peer)
      event.peer:send( "ping" )
    elseif event.type == "connect" then
      print(event.peer, "connected.")
      event.peer:send( "ping" )
    elseif event.type == "disconnect" then
      print(event.peer, "disconnected.")
    end
    event = host:service()
  end
end

See Also

Other Languages