From 42a17775314424ba24974d072bb5f74bc4ff9467 Mon Sep 17 00:00:00 2001 From: Fabio Alessandrelli Date: Mon, 14 Jun 2021 11:51:53 +0200 Subject: [Net] Implement lower level ENet wrappers. --- modules/enet/enet_packet_peer.h | 131 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 131 insertions(+) create mode 100644 modules/enet/enet_packet_peer.h (limited to 'modules/enet/enet_packet_peer.h') diff --git a/modules/enet/enet_packet_peer.h b/modules/enet/enet_packet_peer.h new file mode 100644 index 0000000000..9af004de2f --- /dev/null +++ b/modules/enet/enet_packet_peer.h @@ -0,0 +1,131 @@ +/*************************************************************************/ +/* enet_packet_peer.h */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/*************************************************************************/ + +#ifndef ENET_PACKET_PEER_H +#define ENET_PACKET_PEER_H + +#include "core/io/packet_peer.h" + +#include + +class ENetPacketPeer : public PacketPeer { + GDCLASS(ENetPacketPeer, PacketPeer); + +private: + ENetPeer *peer = nullptr; + List packet_queue; + ENetPacket *last_packet = nullptr; + + static void _bind_methods(); + Error _send(int p_channel, PackedByteArray p_packet, int p_flags); + +protected: + friend class ENetConnection; + // Internally used by ENetConnection during service, destroy, etc. + void _on_disconnect(); + void _queue_packet(ENetPacket *p_packet); + +public: + enum { + PACKET_THROTTLE_SCALE = ENET_PEER_PACKET_THROTTLE_SCALE, + PACKET_LOSS_SCALE = ENET_PEER_PACKET_LOSS_SCALE, + }; + + enum { + FLAG_RELIABLE = ENET_PACKET_FLAG_RELIABLE, + FLAG_UNSEQUENCED = ENET_PACKET_FLAG_UNSEQUENCED, + FLAG_UNRELIABLE_FRAGMENT = ENET_PACKET_FLAG_UNRELIABLE_FRAGMENT, + FLAG_ALLOWED = ENET_PACKET_FLAG_RELIABLE | ENET_PACKET_FLAG_UNSEQUENCED | ENET_PACKET_FLAG_UNRELIABLE_FRAGMENT, + }; + + enum PeerState { + STATE_DISCONNECTED = ENET_PEER_STATE_DISCONNECTED, + STATE_CONNECTING = ENET_PEER_STATE_CONNECTING, + STATE_ACKNOWLEDGING_CONNECT = ENET_PEER_STATE_ACKNOWLEDGING_CONNECT, + STATE_CONNECTION_PENDING = ENET_PEER_STATE_CONNECTION_PENDING, + STATE_CONNECTION_SUCCEEDED = ENET_PEER_STATE_CONNECTION_SUCCEEDED, + STATE_CONNECTED = ENET_PEER_STATE_CONNECTED, + STATE_DISCONNECT_LATER = ENET_PEER_STATE_DISCONNECT_LATER, + STATE_DISCONNECTING = ENET_PEER_STATE_DISCONNECTING, + STATE_ACKNOWLEDGING_DISCONNECT = ENET_PEER_STATE_ACKNOWLEDGING_DISCONNECT, + STATE_ZOMBIE = ENET_PEER_STATE_ZOMBIE, + }; + + enum PeerStatistic { + PEER_PACKET_LOSS, + PEER_PACKET_LOSS_VARIANCE, + PEER_PACKET_LOSS_EPOCH, + PEER_ROUND_TRIP_TIME, + PEER_ROUND_TRIP_TIME_VARIANCE, + PEER_LAST_ROUND_TRIP_TIME, + PEER_LAST_ROUND_TRIP_TIME_VARIANCE, + PEER_PACKET_THROTTLE, + PEER_PACKET_THROTTLE_LIMIT, + PEER_PACKET_THROTTLE_COUNTER, + PEER_PACKET_THROTTLE_EPOCH, + PEER_PACKET_THROTTLE_ACCELERATION, + PEER_PACKET_THROTTLE_DECELERATION, + PEER_PACKET_THROTTLE_INTERVAL, + }; + + int get_max_packet_size() const override; + int get_available_packet_count() const override; + Error get_packet(const uint8_t **r_buffer, int &r_buffer_size) override; ///< buffer is GONE after next get_packet + Error put_packet(const uint8_t *p_buffer, int p_buffer_size) override; + + void peer_disconnect(int p_data = 0); + void peer_disconnect_later(int p_data = 0); + void peer_disconnect_now(int p_data = 0); + + void ping(); + void ping_interval(int p_interval); + void reset(); + int send(uint8_t p_channel, ENetPacket *p_packet); + void throttle_configure(int interval, int acceleration, int deceleration); + void set_timeout(int p_timeout, int p_timeout_min, int p_timeout_max); + double get_statistic(PeerStatistic p_stat); + PeerState get_state() const; + int get_channels() const; + + // Extras + IPAddress get_remote_address() const; + int get_remote_port() const; + + // Used by ENetMultiplayer (TODO use meta? If only they where StringNames) + bool is_active() const; + + ENetPacketPeer(ENetPeer *p_peer); + ~ENetPacketPeer(); +}; + +VARIANT_ENUM_CAST(ENetPacketPeer::PeerState); +VARIANT_ENUM_CAST(ENetPacketPeer::PeerStatistic); + +#endif // ENET_PACKET_PEER_H -- cgit v1.2.3