|
@@ -19,9 +19,12 @@ Before installing **lua-enet**, you must make sure you have
|
|
[ENet](http://enet.bespin.org/) installed. Consult your system's package
|
|
[ENet](http://enet.bespin.org/) installed. Consult your system's package
|
|
management.
|
|
management.
|
|
|
|
|
|
-If you've got [Lua Rocks](http://www.luarocks.org/) then installation is very easy:
|
|
|
|
|
|
+If you've got [Lua Rocks](http://www.luarocks.org/) then installation is very
|
|
|
|
+easy:
|
|
|
|
|
|
- $ luarocks install enet
|
|
|
|
|
|
+```bash
|
|
|
|
+$ luarocks install enet
|
|
|
|
+```
|
|
|
|
|
|
Otherwise you can download the source can from GitHub:
|
|
Otherwise you can download the source can from GitHub:
|
|
<https://github.com/leafo/lua-enet>
|
|
<https://github.com/leafo/lua-enet>
|
|
@@ -48,16 +51,18 @@ plain Lua table. The `type` entry holds the kind of event as a string, and the
|
|
Using this information, we can make a simple echo server:
|
|
Using this information, we can make a simple echo server:
|
|
|
|
|
|
|
|
|
|
- -- server.lua
|
|
|
|
- require "enet"
|
|
|
|
- local host = enet.host_create"localhost:6789"
|
|
|
|
- while true do
|
|
|
|
- local event = host:service(100)
|
|
|
|
- if event and event.type == "receive" then
|
|
|
|
- print("Got message: ", event.data, event.peer)
|
|
|
|
- event.peer:send(event.data)
|
|
|
|
- end
|
|
|
|
- end
|
|
|
|
|
|
+```lua
|
|
|
|
+-- server.lua
|
|
|
|
+require "enet"
|
|
|
|
+local host = enet.host_create"localhost:6789"
|
|
|
|
+while true do
|
|
|
|
+ local event = host:service(100)
|
|
|
|
+ if event and event.type == "receive" then
|
|
|
|
+ print("Got message: ", event.data, event.peer)
|
|
|
|
+ event.peer:send(event.data)
|
|
|
|
+ end
|
|
|
|
+end
|
|
|
|
+```
|
|
|
|
|
|
|
|
|
|
Data, whether sent or received, is always a Lua string. Primitive types can be
|
|
Data, whether sent or received, is always a Lua string. Primitive types can be
|
|
@@ -66,27 +71,30 @@ serialization library.
|
|
|
|
|
|
The client for our server can be written like so:
|
|
The client for our server can be written like so:
|
|
|
|
|
|
- -- client.lua
|
|
|
|
- require "enet"
|
|
|
|
- local host = enet.host_create()
|
|
|
|
- local server = host:connect("localhost:6789")
|
|
|
|
-
|
|
|
|
- local done = false
|
|
|
|
- while not done do
|
|
|
|
- local event = host:service(100)
|
|
|
|
- if event then
|
|
|
|
- if event.type == "connect" then
|
|
|
|
- print("Connected to", event.peer)
|
|
|
|
- event.peer:send("hello world")
|
|
|
|
- elseif event.type == "receive" then
|
|
|
|
- print("Got message: ", event.data, event.peer)
|
|
|
|
- done = true
|
|
|
|
- end
|
|
|
|
- end
|
|
|
|
|
|
+
|
|
|
|
+```lua
|
|
|
|
+-- client.lua
|
|
|
|
+require "enet"
|
|
|
|
+local host = enet.host_create()
|
|
|
|
+local server = host:connect("localhost:6789")
|
|
|
|
+
|
|
|
|
+local done = false
|
|
|
|
+while not done do
|
|
|
|
+ local event = host:service(100)
|
|
|
|
+ if event then
|
|
|
|
+ if event.type == "connect" then
|
|
|
|
+ print("Connected to", event.peer)
|
|
|
|
+ event.peer:send("hello world")
|
|
|
|
+ elseif event.type == "receive" then
|
|
|
|
+ print("Got message: ", event.data, event.peer)
|
|
|
|
+ done = true
|
|
end
|
|
end
|
|
|
|
+ end
|
|
|
|
+end
|
|
|
|
+```
|
|
|
|
|
|
- server:disconnect()
|
|
|
|
- host:flush()
|
|
|
|
|
|
+server:disconnect()
|
|
|
|
+host:flush()
|
|
|
|
|
|
Upon receiving the connect message we send `"hello world"` to the server, then
|
|
Upon receiving the connect message we send `"hello world"` to the server, then
|
|
wait for the response.
|
|
wait for the response.
|
|
@@ -96,10 +104,10 @@ and then tell the host to flush (unless `host:service` will be called again)
|
|
otherwise the server will have to wait for the client to disconnect from timeout.
|
|
otherwise the server will have to wait for the client to disconnect from timeout.
|
|
|
|
|
|
<a name="reference"></a>
|
|
<a name="reference"></a>
|
|
-<div class="api">
|
|
|
|
## Reference
|
|
## Reference
|
|
|
|
|
|
### `enet.host_create([bind_address, peer_count, channel_count, in_bandwidth, out_bandwidth])`
|
|
### `enet.host_create([bind_address, peer_count, channel_count, in_bandwidth, out_bandwidth])`
|
|
|
|
+
|
|
Returns a new host. All arguments are optional.
|
|
Returns a new host. All arguments are optional.
|
|
|
|
|
|
A `bind_address` of `nil` makes a host that can not be connected to (typically
|
|
A `bind_address` of `nil` makes a host that can not be connected to (typically
|
|
@@ -118,6 +126,7 @@ Parameters:
|
|
(unlimited)
|
|
(unlimited)
|
|
|
|
|
|
### `host:connect(address [, channel_count, data])`
|
|
### `host:connect(address [, channel_count, data])`
|
|
|
|
+
|
|
Connects a host to a remote host. Returns peer object associated with remote
|
|
Connects a host to a remote host. Returns peer object associated with remote
|
|
host. The actual connection will not take place until the next `host:service`
|
|
host. The actual connection will not take place until the next `host:service`
|
|
is done, in which a `"connect"` event will be generated.
|
|
is done, in which a `"connect"` event will be generated.
|
|
@@ -129,6 +138,7 @@ the channel count on the server. Defaults to `1`.
|
|
Defaults to `0`.
|
|
Defaults to `0`.
|
|
|
|
|
|
### `host:service([timeout])`
|
|
### `host:service([timeout])`
|
|
|
|
+
|
|
Wait for events, send and receive any ready packets. `timeout` is the max
|
|
Wait for events, send and receive any ready packets. `timeout` is the max
|
|
number of milliseconds to be waited for an event. By default `timeout` is `0`.
|
|
number of milliseconds to be waited for an event. By default `timeout` is `0`.
|
|
Returns `nil` on timeout if no events occurred.
|
|
Returns `nil` on timeout if no events occurred.
|
|
@@ -141,35 +151,44 @@ A `"receive"` event also has a `data` entry which is a Lua string containing the
|
|
data received.
|
|
data received.
|
|
|
|
|
|
### `host:check_events()`
|
|
### `host:check_events()`
|
|
|
|
+
|
|
Checks for any queued events and dispatches one if available. Returns the
|
|
Checks for any queued events and dispatches one if available. Returns the
|
|
associated event if something was dispatched, otherwise `nil`.
|
|
associated event if something was dispatched, otherwise `nil`.
|
|
|
|
|
|
### `host:compress_with_range_coder()`
|
|
### `host:compress_with_range_coder()`
|
|
|
|
+
|
|
Enables an adaptive order-2 PPM range coder for the transmitted data of
|
|
Enables an adaptive order-2 PPM range coder for the transmitted data of
|
|
all pers.
|
|
all pers.
|
|
|
|
|
|
### `host:flush()`
|
|
### `host:flush()`
|
|
|
|
+
|
|
Sends any queued packets. This is only required to send packets earlier than
|
|
Sends any queued packets. This is only required to send packets earlier than
|
|
the next call to `host:service`, or if `host:service` will not be called again.
|
|
the next call to `host:service`, or if `host:service` will not be called again.
|
|
|
|
|
|
### `host:broadcast(data [, channel, flag])`
|
|
### `host:broadcast(data [, channel, flag])`
|
|
|
|
+
|
|
Queues a packet to be sent to all connected peers. See
|
|
Queues a packet to be sent to all connected peers. See
|
|
[peer:send](#peersenddata__channel_flag) for arguments.
|
|
[peer:send](#peersenddata__channel_flag) for arguments.
|
|
|
|
|
|
### `host:channel_limit(limit)`
|
|
### `host:channel_limit(limit)`
|
|
|
|
+
|
|
Sets the maximum number of channels allowed. If it is `0` then the system
|
|
Sets the maximum number of channels allowed. If it is `0` then the system
|
|
maximum allowable value is used.
|
|
maximum allowable value is used.
|
|
|
|
|
|
### `host:bandwidth_limit(incoming, outgoing)`
|
|
### `host:bandwidth_limit(incoming, outgoing)`
|
|
|
|
+
|
|
Sets the bandwidth limits of the host in bytes/sec. Set to `0` for unlimited.
|
|
Sets the bandwidth limits of the host in bytes/sec. Set to `0` for unlimited.
|
|
|
|
|
|
### `host:total_sent_data()`
|
|
### `host:total_sent_data()`
|
|
|
|
+
|
|
Returns the number of bytes that were sent through the given host.
|
|
Returns the number of bytes that were sent through the given host.
|
|
|
|
|
|
### `host:total_received_data()`
|
|
### `host:total_received_data()`
|
|
|
|
+
|
|
Returns the number of bytes that were received by the given host.
|
|
Returns the number of bytes that were received by the given host.
|
|
|
|
|
|
### `host:service_time()`
|
|
### `host:service_time()`
|
|
|
|
+
|
|
Returns the timestamp of the last call to host:service() or host:flush().
|
|
Returns the timestamp of the last call to host:service() or host:flush().
|
|
|
|
|
|
### `host:peer_count()`
|
|
### `host:peer_count()`
|
|
@@ -177,50 +196,60 @@ Returns the number of peers that are allocated for the given host. This
|
|
represents the maximum number of possible connections.
|
|
represents the maximum number of possible connections.
|
|
|
|
|
|
### `host:get_peer(index)`
|
|
### `host:get_peer(index)`
|
|
|
|
+
|
|
Returns the connected peer at the specified index (starting at 1). ENet
|
|
Returns the connected peer at the specified index (starting at 1). ENet
|
|
stores all peers in an array of the corresponding host and re-uses unused
|
|
stores all peers in an array of the corresponding host and re-uses unused
|
|
peers for new connections. You can query the state of a peer using
|
|
peers for new connections. You can query the state of a peer using
|
|
[peer:state](#peerstate).
|
|
[peer:state](#peerstate).
|
|
|
|
|
|
### `peer:connect_id()`
|
|
### `peer:connect_id()`
|
|
|
|
+
|
|
Returns the field ENetPeer::connectID that is assigned for each
|
|
Returns the field ENetPeer::connectID that is assigned for each
|
|
connection.
|
|
connection.
|
|
|
|
|
|
### `peer:disconnect([data])`
|
|
### `peer:disconnect([data])`
|
|
|
|
+
|
|
Requests a disconnection from the peer. The message is sent on the next
|
|
Requests a disconnection from the peer. The message is sent on the next
|
|
`host:service` or `host:flush`.
|
|
`host:service` or `host:flush`.
|
|
|
|
|
|
`data` is optional integer value to be associated with the disconnect.
|
|
`data` is optional integer value to be associated with the disconnect.
|
|
|
|
|
|
### `peer:disconnect_now([data])`
|
|
### `peer:disconnect_now([data])`
|
|
|
|
+
|
|
Force immediate disconnection from peer. Foreign peer not guaranteed to receive
|
|
Force immediate disconnection from peer. Foreign peer not guaranteed to receive
|
|
disconnect notification.
|
|
disconnect notification.
|
|
|
|
|
|
`data` is optional integer value to be associated with the disconnect.
|
|
`data` is optional integer value to be associated with the disconnect.
|
|
|
|
|
|
### `peer:disconnect_later([data])`
|
|
### `peer:disconnect_later([data])`
|
|
|
|
+
|
|
Request a disconnection from peer, but only after all queued outgoing packets
|
|
Request a disconnection from peer, but only after all queued outgoing packets
|
|
are sent.
|
|
are sent.
|
|
|
|
|
|
`data` is optional integer value to be associated with the disconnect.
|
|
`data` is optional integer value to be associated with the disconnect.
|
|
|
|
|
|
### `peer:index()`
|
|
### `peer:index()`
|
|
|
|
+
|
|
Returns the index of the peer. All peers of an ENet host are kept in an
|
|
Returns the index of the peer. All peers of an ENet host are kept in an
|
|
array. This function finds and returns the index of the peer of its host
|
|
array. This function finds and returns the index of the peer of its host
|
|
structure.
|
|
structure.
|
|
|
|
|
|
### `peer:ping()`
|
|
### `peer:ping()`
|
|
|
|
+
|
|
Send a ping request to peer, updates `round_trip_time`. This is called
|
|
Send a ping request to peer, updates `round_trip_time`. This is called
|
|
automatically at regular intervals.
|
|
automatically at regular intervals.
|
|
|
|
|
|
### `peer:ping_interval(interval)`
|
|
### `peer:ping_interval(interval)`
|
|
|
|
+
|
|
Specifies the interval in milliseconds that pings are sent to the other
|
|
Specifies the interval in milliseconds that pings are sent to the other
|
|
end of the connection (defaults to 500).
|
|
end of the connection (defaults to 500).
|
|
|
|
|
|
### `peer:reset()`
|
|
### `peer:reset()`
|
|
|
|
+
|
|
Forcefully disconnects peer. The peer is not notified of the disconnection.
|
|
Forcefully disconnects peer. The peer is not notified of the disconnection.
|
|
|
|
|
|
### `peer:send(data [, channel, flag])`
|
|
### `peer:send(data [, channel, flag])`
|
|
|
|
+
|
|
Queues a packet to be sent to peer. `data` is the contents of the packet, it
|
|
Queues a packet to be sent to peer. `data` is the contents of the packet, it
|
|
must be a Lua string.
|
|
must be a Lua string.
|
|
|
|
|
|
@@ -234,6 +263,7 @@ arrive. Defaults to reliable.
|
|
|
|
|
|
<a name="peerstate"></a>
|
|
<a name="peerstate"></a>
|
|
### `peer:state()`
|
|
### `peer:state()`
|
|
|
|
+
|
|
Returns the state of the peer as a string. This can be any of the
|
|
Returns the state of the peer as a string. This can be any of the
|
|
following:
|
|
following:
|
|
|
|
|
|
@@ -250,11 +280,13 @@ following:
|
|
* "unknown"
|
|
* "unknown"
|
|
|
|
|
|
### `peer:receive()`
|
|
### `peer:receive()`
|
|
|
|
+
|
|
Attempts to dequeue an incoming packet for this peer.
|
|
Attempts to dequeue an incoming packet for this peer.
|
|
Returns `nil` if there are no packets waiting. Otherwise returns two values:
|
|
Returns `nil` if there are no packets waiting. Otherwise returns two values:
|
|
the string representing the packet data, and the channel the packet came from.
|
|
the string representing the packet data, and the channel the packet came from.
|
|
|
|
|
|
### `peer:round_trip_time([value])`
|
|
### `peer:round_trip_time([value])`
|
|
|
|
+
|
|
Returns or sets the current round trip time (i.e. ping). If value is nil
|
|
Returns or sets the current round trip time (i.e. ping). If value is nil
|
|
the current value of the peer is returned. Otherwise the value roundTripTime
|
|
the current value of the peer is returned. Otherwise the value roundTripTime
|
|
is set to the specified value and returned.
|
|
is set to the specified value and returned.
|
|
@@ -263,6 +295,7 @@ Enet performs some filtering on the round trip times and it takes some time
|
|
until the parameters are accurate.
|
|
until the parameters are accurate.
|
|
|
|
|
|
### `peer:last_round_trip_time([value])`
|
|
### `peer:last_round_trip_time([value])`
|
|
|
|
+
|
|
Returns or sets the round trip time of the previous round trip time
|
|
Returns or sets the round trip time of the previous round trip time
|
|
computation. If value is nil the current value of the peer is returned.
|
|
computation. If value is nil the current value of the peer is returned.
|
|
Otherwise the value lastRoundTripTime is set to the specified value and
|
|
Otherwise the value lastRoundTripTime is set to the specified value and
|
|
@@ -284,6 +317,7 @@ Parameters:
|
|
increases
|
|
increases
|
|
|
|
|
|
### `(limit, minimum, maximum) peer:timeout(limit, minimum, maximum)`
|
|
### `(limit, minimum, maximum) peer:timeout(limit, minimum, maximum)`
|
|
|
|
+
|
|
Returns or sets the parameters when a timeout is detected. This is happens
|
|
Returns or sets the parameters when a timeout is detected. This is happens
|
|
either after a fixed timeout or a variable timeout of time that takes the
|
|
either after a fixed timeout or a variable timeout of time that takes the
|
|
round trip time into account. The former is specified with the `maximum`
|
|
round trip time into account. The former is specified with the `maximum`
|
|
@@ -291,15 +325,13 @@ parameter.
|
|
|
|
|
|
Parameters:
|
|
Parameters:
|
|
|
|
|
|
- * `limit` a factor that is multiplied with a value that based on the
|
|
|
|
- average round trip time to compute the timeout limit
|
|
|
|
- * `minimum` timeout value in milliseconds that a reliable packet has to
|
|
|
|
- be acknowledged if the variable timeout limit was exceeded
|
|
|
|
- * `maximum` fixed timeout in milliseconds for which any packet has to be acknowledged
|
|
|
|
-
|
|
|
|
-See official enet documentation for detailed description.
|
|
|
|
|
|
+* `limit` a factor that is multiplied with a value that based on the
|
|
|
|
+ average round trip time to compute the timeout limit
|
|
|
|
+* `minimum` timeout value in milliseconds that a reliable packet has to
|
|
|
|
+ be acknowledged if the variable timeout limit was exceeded
|
|
|
|
+* `maximum` fixed timeout in milliseconds for which any packet has to be acknowledged
|
|
|
|
|
|
-</div>
|
|
|
|
|
|
+See official ENet documentation for detailed description.
|
|
|
|
|
|
<a name="license"></a>
|
|
<a name="license"></a>
|
|
## License (MIT)
|
|
## License (MIT)
|