Skip to content

ENet: Expose the check_events method to GDScript #106112

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion modules/enet/doc_classes/ENetConnection.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@
Limits the maximum allowed channels of future incoming connections.
</description>
</method>
<method name="check_events">
<return type="Array" />
<description>
Receive the next queued event from the host since the last call to [method service]. The returned [Array] will have 4 elements. An [enum EventType], the [ENetPacketPeer] which generated the event, the event associated data (if any), the event associated channel (if any). If the generated event is [constant EVENT_RECEIVE], the received packet will be queued to the associated [ENetPacketPeer].
For each call to [method service] within a frame/tick, call this method until queued events have been exhausted. This is typically used by the caller to handle all queued events without needing to repeatedly call [method service] within a frame/tick (which would potentially cause more events to be enqueued).
[b]Note:[/b] This method may be called on either end involved in the event (sending and receiving hosts).
</description>
</method>
<method name="compress">
<return type="void" />
<param index="0" name="mode" type="int" enum="ENetConnection.CompressionMode" />
Expand Down Expand Up @@ -178,7 +186,7 @@
[url=https://facebook.github.io/zstd/]Zstandard[/url] compression. Note that this algorithm is not very efficient on packets smaller than 4 KB. Therefore, it's recommended to use other compression algorithms in most cases.
</constant>
<constant name="EVENT_ERROR" value="-1" enum="EventType">
An error occurred during [method service]. You will likely need to [method destroy] the host and recreate it.
An error occurred during [method service] or [method check_events]. You will likely need to [method destroy] the host and recreate it.
</constant>
<constant name="EVENT_NONE" value="0" enum="EventType">
No event occurred within the specified time limit.
Expand Down
13 changes: 13 additions & 0 deletions modules/enet/enet_connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,18 @@ Array ENetConnection::_service(int p_timeout) {
return out;
}

Array ENetConnection::_check_events() {
Event event;
Ref<ENetPacketPeer> peer;
EventType event_type = EVENT_NONE;
check_events(event_type, event);
Array out = { event_type, event.peer, event.data, event.channel_id };
if (event.packet && event.peer.is_valid()) {
event.peer->_queue_packet(event.packet);
}
return out;
}

void ENetConnection::_broadcast(int p_channel, PackedByteArray p_packet, int p_flags) {
ERR_FAIL_NULL_MSG(host, "The ENetConnection instance isn't currently active.");
ERR_FAIL_COND_MSG(p_channel < 0 || p_channel > (int)host->channelLimit, "Invalid channel");
Expand Down Expand Up @@ -377,6 +389,7 @@ void ENetConnection::_bind_methods() {
ClassDB::bind_method(D_METHOD("destroy"), &ENetConnection::destroy);
ClassDB::bind_method(D_METHOD("connect_to_host", "address", "port", "channels", "data"), &ENetConnection::connect_to_host, DEFVAL(0), DEFVAL(0));
ClassDB::bind_method(D_METHOD("service", "timeout"), &ENetConnection::_service, DEFVAL(0));
ClassDB::bind_method(D_METHOD("check_events"), &ENetConnection::_check_events);
ClassDB::bind_method(D_METHOD("flush"), &ENetConnection::flush);
ClassDB::bind_method(D_METHOD("bandwidth_limit", "in_bandwidth", "out_bandwidth"), &ENetConnection::bandwidth_limit, DEFVAL(0), DEFVAL(0));
ClassDB::bind_method(D_METHOD("channel_limit", "limit"), &ENetConnection::channel_limit);
Expand Down
1 change: 1 addition & 0 deletions modules/enet/enet_connection.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ class ENetConnection : public RefCounted {
EventType _parse_event(const ENetEvent &p_event, Event &r_event);
Error _create(ENetAddress *p_address, int p_max_peers, int p_max_channels, int p_in_bandwidth, int p_out_bandwidth);
Array _service(int p_timeout = 0);
Array _check_events();
void _broadcast(int p_channel, PackedByteArray p_packet, int p_flags);
TypedArray<ENetPacketPeer> _get_peers();

Expand Down