1/**************************************************************************/
2/* enet_packet_peer.h */
3/**************************************************************************/
4/* This file is part of: */
5/* GODOT ENGINE */
6/* https://godotengine.org */
7/**************************************************************************/
8/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10/* */
11/* Permission is hereby granted, free of charge, to any person obtaining */
12/* a copy of this software and associated documentation files (the */
13/* "Software"), to deal in the Software without restriction, including */
14/* without limitation the rights to use, copy, modify, merge, publish, */
15/* distribute, sublicense, and/or sell copies of the Software, and to */
16/* permit persons to whom the Software is furnished to do so, subject to */
17/* the following conditions: */
18/* */
19/* The above copyright notice and this permission notice shall be */
20/* included in all copies or substantial portions of the Software. */
21/* */
22/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29/**************************************************************************/
30
31#ifndef ENET_PACKET_PEER_H
32#define ENET_PACKET_PEER_H
33
34#include "core/io/packet_peer.h"
35
36#include <enet/enet.h>
37
38class ENetPacketPeer : public PacketPeer {
39 GDCLASS(ENetPacketPeer, PacketPeer);
40
41private:
42 ENetPeer *peer = nullptr;
43 List<ENetPacket *> packet_queue;
44 ENetPacket *last_packet = nullptr;
45
46 static void _bind_methods();
47 Error _send(int p_channel, PackedByteArray p_packet, int p_flags);
48
49protected:
50 friend class ENetConnection;
51 // Internally used by ENetConnection during service, destroy, etc.
52 void _on_disconnect();
53 void _queue_packet(ENetPacket *p_packet);
54
55public:
56 enum {
57 PACKET_THROTTLE_SCALE = ENET_PEER_PACKET_THROTTLE_SCALE,
58 PACKET_LOSS_SCALE = ENET_PEER_PACKET_LOSS_SCALE,
59 };
60
61 enum {
62 FLAG_RELIABLE = ENET_PACKET_FLAG_RELIABLE,
63 FLAG_UNSEQUENCED = ENET_PACKET_FLAG_UNSEQUENCED,
64 FLAG_UNRELIABLE_FRAGMENT = ENET_PACKET_FLAG_UNRELIABLE_FRAGMENT,
65 FLAG_ALLOWED = ENET_PACKET_FLAG_RELIABLE | ENET_PACKET_FLAG_UNSEQUENCED | ENET_PACKET_FLAG_UNRELIABLE_FRAGMENT,
66 };
67
68 enum PeerState {
69 STATE_DISCONNECTED = ENET_PEER_STATE_DISCONNECTED,
70 STATE_CONNECTING = ENET_PEER_STATE_CONNECTING,
71 STATE_ACKNOWLEDGING_CONNECT = ENET_PEER_STATE_ACKNOWLEDGING_CONNECT,
72 STATE_CONNECTION_PENDING = ENET_PEER_STATE_CONNECTION_PENDING,
73 STATE_CONNECTION_SUCCEEDED = ENET_PEER_STATE_CONNECTION_SUCCEEDED,
74 STATE_CONNECTED = ENET_PEER_STATE_CONNECTED,
75 STATE_DISCONNECT_LATER = ENET_PEER_STATE_DISCONNECT_LATER,
76 STATE_DISCONNECTING = ENET_PEER_STATE_DISCONNECTING,
77 STATE_ACKNOWLEDGING_DISCONNECT = ENET_PEER_STATE_ACKNOWLEDGING_DISCONNECT,
78 STATE_ZOMBIE = ENET_PEER_STATE_ZOMBIE,
79 };
80
81 enum PeerStatistic {
82 PEER_PACKET_LOSS,
83 PEER_PACKET_LOSS_VARIANCE,
84 PEER_PACKET_LOSS_EPOCH,
85 PEER_ROUND_TRIP_TIME,
86 PEER_ROUND_TRIP_TIME_VARIANCE,
87 PEER_LAST_ROUND_TRIP_TIME,
88 PEER_LAST_ROUND_TRIP_TIME_VARIANCE,
89 PEER_PACKET_THROTTLE,
90 PEER_PACKET_THROTTLE_LIMIT,
91 PEER_PACKET_THROTTLE_COUNTER,
92 PEER_PACKET_THROTTLE_EPOCH,
93 PEER_PACKET_THROTTLE_ACCELERATION,
94 PEER_PACKET_THROTTLE_DECELERATION,
95 PEER_PACKET_THROTTLE_INTERVAL,
96 };
97
98 int get_max_packet_size() const override;
99 int get_available_packet_count() const override;
100 Error get_packet(const uint8_t **r_buffer, int &r_buffer_size) override; ///< buffer is GONE after next get_packet
101 Error put_packet(const uint8_t *p_buffer, int p_buffer_size) override;
102
103 void peer_disconnect(int p_data = 0);
104 void peer_disconnect_later(int p_data = 0);
105 void peer_disconnect_now(int p_data = 0);
106
107 void ping();
108 void ping_interval(int p_interval);
109 void reset();
110 int send(uint8_t p_channel, ENetPacket *p_packet);
111 void throttle_configure(int interval, int acceleration, int deceleration);
112 void set_timeout(int p_timeout, int p_timeout_min, int p_timeout_max);
113 double get_statistic(PeerStatistic p_stat);
114 PeerState get_state() const;
115 int get_channels() const;
116
117 // Extras
118 IPAddress get_remote_address() const;
119 int get_remote_port() const;
120
121 // Used by ENetMultiplayer (TODO use meta? If only they where StringNames)
122 bool is_active() const;
123
124 ENetPacketPeer(ENetPeer *p_peer);
125 ~ENetPacketPeer();
126};
127
128VARIANT_ENUM_CAST(ENetPacketPeer::PeerState);
129VARIANT_ENUM_CAST(ENetPacketPeer::PeerStatistic);
130
131#endif // ENET_PACKET_PEER_H
132