[Net] Enable PacketPeer native extensions.

This commit is contained in:
Fabio Alessandrelli 2021-08-28 17:04:22 +02:00
parent 53306cb79a
commit b9e6cc8f4f
4 changed files with 71 additions and 0 deletions

View File

@ -138,6 +138,7 @@ Error PacketPeer::_get_packet_error() const {
void PacketPeer::_bind_methods() { void PacketPeer::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_var", "allow_objects"), &PacketPeer::_bnd_get_var, DEFVAL(false)); ClassDB::bind_method(D_METHOD("get_var", "allow_objects"), &PacketPeer::_bnd_get_var, DEFVAL(false));
ClassDB::bind_method(D_METHOD("put_var", "var", "full_objects"), &PacketPeer::put_var, DEFVAL(false)); ClassDB::bind_method(D_METHOD("put_var", "var", "full_objects"), &PacketPeer::put_var, DEFVAL(false));
ClassDB::bind_method(D_METHOD("get_packet"), &PacketPeer::_get_packet); ClassDB::bind_method(D_METHOD("get_packet"), &PacketPeer::_get_packet);
ClassDB::bind_method(D_METHOD("put_packet", "buffer"), &PacketPeer::_put_packet); ClassDB::bind_method(D_METHOD("put_packet", "buffer"), &PacketPeer::_put_packet);
ClassDB::bind_method(D_METHOD("get_packet_error"), &PacketPeer::_get_packet_error); ClassDB::bind_method(D_METHOD("get_packet_error"), &PacketPeer::_get_packet_error);
@ -151,6 +152,51 @@ void PacketPeer::_bind_methods() {
/***************/ /***************/
int PacketPeerExtension::get_available_packet_count() const {
int count;
if (GDVIRTUAL_CALL(_get_available_packet_count, count)) {
return count;
}
WARN_PRINT_ONCE("PacketPeerExtension::_get_available_packet_count is unimplemented!");
return -1;
}
Error PacketPeerExtension::get_packet(const uint8_t **r_buffer, int &r_buffer_size) {
int err;
if (GDVIRTUAL_CALL(_get_packet, r_buffer, &r_buffer_size, err)) {
return (Error)err;
}
WARN_PRINT_ONCE("PacketPeerExtension::_get_packet_native is unimplemented!");
return FAILED;
}
Error PacketPeerExtension::put_packet(const uint8_t *p_buffer, int p_buffer_size) {
int err;
if (GDVIRTUAL_CALL(_put_packet, p_buffer, p_buffer_size, err)) {
return (Error)err;
}
WARN_PRINT_ONCE("PacketPeerExtension::_put_packet_native is unimplemented!");
return FAILED;
}
int PacketPeerExtension::get_max_packet_size() const {
int size;
if (GDVIRTUAL_CALL(_get_max_packet_size, size)) {
return size;
}
WARN_PRINT_ONCE("PacketPeerExtension::_get_max_packet_size is unimplemented!");
return 0;
}
void PacketPeerExtension::_bind_methods() {
GDVIRTUAL_BIND(_get_packet, "r_buffer", "r_buffer_size");
GDVIRTUAL_BIND(_put_packet, "p_buffer", "p_buffer_size");
GDVIRTUAL_BIND(_get_available_packet_count);
GDVIRTUAL_BIND(_get_max_packet_size);
}
/***************/
void PacketPeerStream::_bind_methods() { void PacketPeerStream::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_stream_peer", "peer"), &PacketPeerStream::set_stream_peer); ClassDB::bind_method(D_METHOD("set_stream_peer", "peer"), &PacketPeerStream::set_stream_peer);
ClassDB::bind_method(D_METHOD("get_stream_peer"), &PacketPeerStream::get_stream_peer); ClassDB::bind_method(D_METHOD("get_stream_peer"), &PacketPeerStream::get_stream_peer);

View File

@ -35,6 +35,10 @@
#include "core/object/class_db.h" #include "core/object/class_db.h"
#include "core/templates/ring_buffer.h" #include "core/templates/ring_buffer.h"
#include "core/object/gdvirtual.gen.inc"
#include "core/object/script_language.h"
#include "core/variant/native_ptr.h"
class PacketPeer : public RefCounted { class PacketPeer : public RefCounted {
GDCLASS(PacketPeer, RefCounted); GDCLASS(PacketPeer, RefCounted);
@ -73,6 +77,25 @@ public:
~PacketPeer() {} ~PacketPeer() {}
}; };
class PacketPeerExtension : public PacketPeer {
GDCLASS(PacketPeerExtension, PacketPeer);
protected:
static void _bind_methods();
public:
virtual int get_available_packet_count() const override;
virtual Error get_packet(const uint8_t **r_buffer, int &r_buffer_size) override; ///< buffer is GONE after next get_packet
virtual Error put_packet(const uint8_t *p_buffer, int p_buffer_size) override;
virtual int get_max_packet_size() const override;
/* GDExtension */
GDVIRTUAL0RC(int, _get_available_packet_count);
GDVIRTUAL2R(int, _get_packet, GDNativeConstPtr<const uint8_t *>, GDNativePtr<int>);
GDVIRTUAL2R(int, _put_packet, GDNativeConstPtr<const uint8_t>, int);
GDVIRTUAL0RC(int, _get_max_packet_size);
};
class PacketPeerStream : public PacketPeer { class PacketPeerStream : public PacketPeer {
GDCLASS(PacketPeerStream, PacketPeer); GDCLASS(PacketPeerStream, PacketPeer);

View File

@ -174,6 +174,7 @@ void register_core_types() {
GDREGISTER_CLASS(TCPServer); GDREGISTER_CLASS(TCPServer);
GDREGISTER_VIRTUAL_CLASS(PacketPeer); GDREGISTER_VIRTUAL_CLASS(PacketPeer);
GDREGISTER_CLASS(PacketPeerExtension);
GDREGISTER_CLASS(PacketPeerStream); GDREGISTER_CLASS(PacketPeerStream);
GDREGISTER_CLASS(PacketPeerUDP); GDREGISTER_CLASS(PacketPeerUDP);
GDREGISTER_CLASS(UDPServer); GDREGISTER_CLASS(UDPServer);

View File

@ -117,6 +117,7 @@ GDVIRTUAL_NATIVE_PTR(char16_t)
GDVIRTUAL_NATIVE_PTR(char32_t) GDVIRTUAL_NATIVE_PTR(char32_t)
GDVIRTUAL_NATIVE_PTR(wchar_t) GDVIRTUAL_NATIVE_PTR(wchar_t)
GDVIRTUAL_NATIVE_PTR(uint8_t) GDVIRTUAL_NATIVE_PTR(uint8_t)
GDVIRTUAL_NATIVE_PTR(uint8_t *)
GDVIRTUAL_NATIVE_PTR(int8_t) GDVIRTUAL_NATIVE_PTR(int8_t)
GDVIRTUAL_NATIVE_PTR(uint16_t) GDVIRTUAL_NATIVE_PTR(uint16_t)
GDVIRTUAL_NATIVE_PTR(int16_t) GDVIRTUAL_NATIVE_PTR(int16_t)