lua-enet
Available since LÖVE 0.9.0 |
This module is not supported in earlier versions. |
Official documentation for lua-enet is available here. ENet's features are listed on its homepage. Be wary that there's misspellings and misleading examples in the official documentation. |
lua-enet is simply some lua bindings for ENet.
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.
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. |
host:service | Wait for events, send and receive any ready packets. |
host:check_events | Checks for any queued events and dispatches one if available. |
host:connect | Connects a host to a remote host. Returns peer object associated with remote host. |
host:flush | Sends any queued packets. |
host:broadcast | Queues a packet to be sent to all connected peers. |
host:channel_limit | Sets the maximum number of channels allowed. |
host:bandwidth_limit | Sets the bandwidth limits of the host in bytes/sec. |
host:socket_get_address | Returns a string that describes the socket address of the given host. |
host:total_sent_data | Returns the number of bytes that were sent through the given host. |
host:total_received_data | Returns the number of bytes that were received by the given host. |
host:service_time | Returns the timestamp of the last call to host:service() or host:flush(). |
host:peer_count | Returns the number of peers that are allocated for the given host. |
host:get_peer | Returns the connected peer at the specified index (starting at 1). |
host:__gc | Destroys the host, freeing any bound ports and addresses. |
peer:disconnect | Requests a disconnection from the peer. |
peer:disconnect_now | Force immediate disconnection from peer. |
peer:disconnect_later | Request a disconnection from peer, but only after all queued outgoing packets are sent. |
peer:reset | Forcefully disconnects peer. The peer is not notified of the disconnection. |
peer:ping | Send a ping request to peer, updates round_trip_time. This is called automatically at regular intervals. |
peer:receive | Attempts to dequeue an incoming packet for this peer. |
peer:send | Queues a packet to be sent to peer. |
peer:throttle_configure | Changes the probability at which unreliable packets should not be dropped. |
peer:ping_interval | Specifies the interval in milliseconds that pings are sent to the other end of the connection (defaults to 500). |
peer:timeout | Returns or sets the parameters when a timeout is detected. |
peer:index | Returns the index of the peer. |
peer:state | Returns the state of the peer. |
peer:connect_id | Returns the field ENetPeer::connectID that is assigned for each connection. |
peer:round_trip_time | Returns or sets the current round trip time (i.e. ping). |
peer:last_round_trip_time | Returns or sets the round trip time of the previous round trip time computation. |
Examples
server.lua
-- server.lua
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
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