2014-02-10 01:10:30 +00:00
/**************************************************************************/
/* packet_peer.cpp */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* 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. */
/**************************************************************************/
2018-01-04 23:50:27 +00:00
2014-02-10 01:10:30 +00:00
# include "packet_peer.h"
2020-11-07 22:33:38 +00:00
# include "core/config/project_settings.h"
2018-09-11 16:13:45 +00:00
# include "core/io/marshalls.h"
2014-02-10 01:10:30 +00:00
/* helpers / binders */
2020-01-18 18:52:59 +00:00
void PacketPeer : : set_encode_buffer_max_size ( int p_max_size ) {
ERR_FAIL_COND_MSG ( p_max_size < 1024 , " Max encode buffer must be at least 1024 bytes " ) ;
ERR_FAIL_COND_MSG ( p_max_size > 256 * 1024 * 1024 , " Max encode buffer cannot exceed 256 MiB " ) ;
encode_buffer_max_size = next_power_of_2 ( p_max_size ) ;
2022-02-01 18:19:13 +00:00
encode_buffer . clear ( ) ;
2020-01-18 18:52:59 +00:00
}
int PacketPeer : : get_encode_buffer_max_size ( ) const {
return encode_buffer_max_size ;
}
2020-02-17 21:06:54 +00:00
Error PacketPeer : : get_packet_buffer ( Vector < uint8_t > & r_buffer ) {
2014-02-10 01:10:30 +00:00
const uint8_t * buffer ;
int buffer_size ;
Error err = get_packet ( & buffer , buffer_size ) ;
2020-05-14 14:41:43 +00:00
if ( err ) {
2014-02-10 01:10:30 +00:00
return err ;
2020-05-14 14:41:43 +00:00
}
2014-02-10 01:10:30 +00:00
r_buffer . resize ( buffer_size ) ;
2020-05-14 14:41:43 +00:00
if ( buffer_size = = 0 ) {
2014-02-10 01:10:30 +00:00
return OK ;
2020-05-14 14:41:43 +00:00
}
2014-02-10 01:10:30 +00:00
2020-02-17 21:06:54 +00:00
uint8_t * w = r_buffer . ptrw ( ) ;
2020-05-14 14:41:43 +00:00
for ( int i = 0 ; i < buffer_size ; i + + ) {
2014-02-10 01:10:30 +00:00
w [ i ] = buffer [ i ] ;
2020-05-14 14:41:43 +00:00
}
2014-02-10 01:10:30 +00:00
return OK ;
}
2020-02-17 21:06:54 +00:00
Error PacketPeer : : put_packet_buffer ( const Vector < uint8_t > & p_buffer ) {
2014-02-10 01:10:30 +00:00
int len = p_buffer . size ( ) ;
2020-05-14 14:41:43 +00:00
if ( len = = 0 ) {
2014-02-10 01:10:30 +00:00
return OK ;
2020-05-14 14:41:43 +00:00
}
2014-02-10 01:10:30 +00:00
2020-02-17 21:06:54 +00:00
const uint8_t * r = p_buffer . ptr ( ) ;
2014-02-10 01:10:30 +00:00
return put_packet ( & r [ 0 ] , len ) ;
}
2019-03-26 15:52:42 +00:00
Error PacketPeer : : get_var ( Variant & r_variant , bool p_allow_objects ) {
2014-02-10 01:10:30 +00:00
const uint8_t * buffer ;
int buffer_size ;
Error err = get_packet ( & buffer , buffer_size ) ;
2020-05-14 14:41:43 +00:00
if ( err ) {
2014-02-10 01:10:30 +00:00
return err ;
2020-05-14 14:41:43 +00:00
}
2014-02-10 01:10:30 +00:00
2020-04-01 23:20:12 +00:00
return decode_variant ( r_variant , buffer , buffer_size , nullptr , p_allow_objects ) ;
2014-02-10 01:10:30 +00:00
}
2019-03-26 15:52:42 +00:00
Error PacketPeer : : put_var ( const Variant & p_packet , bool p_full_objects ) {
2014-02-10 01:10:30 +00:00
int len ;
2020-04-01 23:20:12 +00:00
Error err = encode_variant ( p_packet , nullptr , len , p_full_objects ) ; // compute len first
2020-05-14 14:41:43 +00:00
if ( err ) {
2014-02-10 01:10:30 +00:00
return err ;
2020-05-14 14:41:43 +00:00
}
2014-02-10 01:10:30 +00:00
2020-05-14 14:41:43 +00:00
if ( len = = 0 ) {
2014-02-10 01:10:30 +00:00
return OK ;
2020-05-14 14:41:43 +00:00
}
2014-02-10 01:10:30 +00:00
2020-01-18 18:52:59 +00:00
ERR_FAIL_COND_V_MSG ( len > encode_buffer_max_size , ERR_OUT_OF_MEMORY , " Failed to encode variant, encode size is bigger then encode_buffer_max_size. Consider raising it via 'set_encode_buffer_max_size'. " ) ;
if ( unlikely ( encode_buffer . size ( ) < len ) ) {
encode_buffer . resize ( 0 ) ; // Avoid realloc
encode_buffer . resize ( next_power_of_2 ( len ) ) ;
}
2020-02-17 21:06:54 +00:00
uint8_t * w = encode_buffer . ptrw ( ) ;
err = encode_variant ( p_packet , w , len , p_full_objects ) ;
2019-09-25 08:28:50 +00:00
ERR_FAIL_COND_V_MSG ( err ! = OK , err , " Error when trying to encode Variant. " ) ;
2014-02-10 01:10:30 +00:00
2020-02-17 21:06:54 +00:00
return put_packet ( w , len ) ;
2014-02-10 01:10:30 +00:00
}
2019-03-26 15:52:42 +00:00
Variant PacketPeer : : _bnd_get_var ( bool p_allow_objects ) {
2014-02-10 01:10:30 +00:00
Variant var ;
2019-07-23 07:14:31 +00:00
Error err = get_var ( var , p_allow_objects ) ;
2014-02-10 01:10:30 +00:00
2019-07-23 07:14:31 +00:00
ERR_FAIL_COND_V ( err ! = OK , Variant ( ) ) ;
2014-02-10 01:10:30 +00:00
return var ;
2019-07-23 07:14:31 +00:00
}
2014-02-10 01:10:30 +00:00
2020-02-17 21:06:54 +00:00
Error PacketPeer : : _put_packet ( const Vector < uint8_t > & p_buffer ) {
2015-06-06 12:44:38 +00:00
return put_packet_buffer ( p_buffer ) ;
}
2020-05-14 12:29:06 +00:00
2020-02-17 21:06:54 +00:00
Vector < uint8_t > PacketPeer : : _get_packet ( ) {
Vector < uint8_t > raw ;
2015-06-06 12:44:38 +00:00
last_get_error = get_packet_buffer ( raw ) ;
return raw ;
}
Error PacketPeer : : _get_packet_error ( ) const {
return last_get_error ;
}
2014-02-10 01:10:30 +00:00
void PacketPeer : : _bind_methods ( ) {
2019-03-26 15:52:42 +00:00
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 ) ) ;
2021-08-28 15:04:22 +00:00
2017-02-13 11:47:24 +00:00
ClassDB : : bind_method ( D_METHOD ( " get_packet " ) , & PacketPeer : : _get_packet ) ;
2017-08-09 11:19:41 +00:00
ClassDB : : bind_method ( D_METHOD ( " put_packet " , " buffer " ) , & PacketPeer : : _put_packet ) ;
ClassDB : : bind_method ( D_METHOD ( " get_packet_error " ) , & PacketPeer : : _get_packet_error ) ;
2017-02-13 11:47:24 +00:00
ClassDB : : bind_method ( D_METHOD ( " get_available_packet_count " ) , & PacketPeer : : get_available_packet_count ) ;
2017-08-14 14:58:10 +00:00
2020-01-18 18:52:59 +00:00
ClassDB : : bind_method ( D_METHOD ( " get_encode_buffer_max_size " ) , & PacketPeer : : get_encode_buffer_max_size ) ;
ClassDB : : bind_method ( D_METHOD ( " set_encode_buffer_max_size " , " max_size " ) , & PacketPeer : : set_encode_buffer_max_size ) ;
2018-01-11 22:35:12 +00:00
2020-01-18 18:52:59 +00:00
ADD_PROPERTY ( PropertyInfo ( Variant : : INT , " encode_buffer_max_size " ) , " set_encode_buffer_max_size " , " get_encode_buffer_max_size " ) ;
2020-05-19 13:46:49 +00:00
}
2014-02-10 01:10:30 +00:00
/***************/
2021-08-28 15:04:22 +00:00
Error PacketPeerExtension : : get_packet ( const uint8_t * * r_buffer , int & r_buffer_size ) {
2022-09-01 03:09:07 +00:00
Error err ;
2021-08-28 15:04:22 +00:00
if ( GDVIRTUAL_CALL ( _get_packet , r_buffer , & r_buffer_size , err ) ) {
2022-09-01 03:09:07 +00:00
return err ;
2021-08-28 15:04:22 +00:00
}
WARN_PRINT_ONCE ( " PacketPeerExtension::_get_packet_native is unimplemented! " ) ;
return FAILED ;
}
Error PacketPeerExtension : : put_packet ( const uint8_t * p_buffer , int p_buffer_size ) {
2022-09-01 03:09:07 +00:00
Error err ;
2021-08-28 15:04:22 +00:00
if ( GDVIRTUAL_CALL ( _put_packet , p_buffer , p_buffer_size , err ) ) {
2022-09-01 03:09:07 +00:00
return err ;
2021-08-28 15:04:22 +00:00
}
WARN_PRINT_ONCE ( " PacketPeerExtension::_put_packet_native is unimplemented! " ) ;
return FAILED ;
}
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 ) ;
}
/***************/
2014-02-10 01:10:30 +00:00
void PacketPeerStream : : _bind_methods ( ) {
2018-01-12 13:25:05 +00:00
ClassDB : : bind_method ( D_METHOD ( " set_stream_peer " , " peer " ) , & PacketPeerStream : : set_stream_peer ) ;
2018-01-11 22:35:12 +00:00
ClassDB : : bind_method ( D_METHOD ( " get_stream_peer " ) , & PacketPeerStream : : get_stream_peer ) ;
2017-08-08 23:55:22 +00:00
ClassDB : : bind_method ( D_METHOD ( " set_input_buffer_max_size " , " max_size_bytes " ) , & PacketPeerStream : : set_input_buffer_max_size ) ;
ClassDB : : bind_method ( D_METHOD ( " set_output_buffer_max_size " , " max_size_bytes " ) , & PacketPeerStream : : set_output_buffer_max_size ) ;
2017-08-18 13:59:31 +00:00
ClassDB : : bind_method ( D_METHOD ( " get_input_buffer_max_size " ) , & PacketPeerStream : : get_input_buffer_max_size ) ;
ClassDB : : bind_method ( D_METHOD ( " get_output_buffer_max_size " ) , & PacketPeerStream : : get_output_buffer_max_size ) ;
2018-01-11 22:35:12 +00:00
ADD_PROPERTY ( PropertyInfo ( Variant : : INT , " input_buffer_max_size " ) , " set_input_buffer_max_size " , " get_input_buffer_max_size " ) ;
ADD_PROPERTY ( PropertyInfo ( Variant : : INT , " output_buffer_max_size " ) , " set_output_buffer_max_size " , " get_output_buffer_max_size " ) ;
2021-06-17 23:10:18 +00:00
ADD_PROPERTY ( PropertyInfo ( Variant : : OBJECT , " stream_peer " , PROPERTY_HINT_RESOURCE_TYPE , " StreamPeer " , PROPERTY_USAGE_NONE ) , " set_stream_peer " , " get_stream_peer " ) ;
2014-02-10 01:10:30 +00:00
}
Error PacketPeerStream : : _poll_buffer ( ) const {
ERR_FAIL_COND_V ( peer . is_null ( ) , ERR_UNCONFIGURED ) ;
int read = 0 ;
2018-01-03 20:08:44 +00:00
ERR_FAIL_COND_V ( input_buffer . size ( ) < ring_buffer . space_left ( ) , ERR_UNAVAILABLE ) ;
2018-07-25 01:11:03 +00:00
Error err = peer - > get_partial_data ( input_buffer . ptrw ( ) , ring_buffer . space_left ( ) , read ) ;
2020-05-14 14:41:43 +00:00
if ( err ) {
2014-02-10 01:10:30 +00:00
return err ;
2020-05-14 14:41:43 +00:00
}
if ( read = = 0 ) {
2014-02-10 01:10:30 +00:00
return OK ;
2020-05-14 14:41:43 +00:00
}
2014-02-10 01:10:30 +00:00
2017-08-08 23:55:22 +00:00
int w = ring_buffer . write ( & input_buffer [ 0 ] , read ) ;
2014-02-10 01:10:30 +00:00
ERR_FAIL_COND_V ( w ! = read , ERR_BUG ) ;
return OK ;
}
int PacketPeerStream : : get_available_packet_count ( ) const {
_poll_buffer ( ) ;
uint32_t remaining = ring_buffer . data_left ( ) ;
int ofs = 0 ;
int count = 0 ;
while ( remaining > = 4 ) {
uint8_t lbuf [ 4 ] ;
ring_buffer . copy ( lbuf , ofs , 4 ) ;
uint32_t len = decode_uint32 ( lbuf ) ;
remaining - = 4 ;
2016-03-08 23:00:52 +00:00
ofs + = 4 ;
2020-05-14 14:41:43 +00:00
if ( len > remaining ) {
2014-02-10 01:10:30 +00:00
break ;
2020-05-14 14:41:43 +00:00
}
2016-03-08 23:00:52 +00:00
remaining - = len ;
2014-02-10 01:10:30 +00:00
ofs + = len ;
count + + ;
}
return count ;
}
2017-12-15 15:05:42 +00:00
Error PacketPeerStream : : get_packet ( const uint8_t * * r_buffer , int & r_buffer_size ) {
2014-02-10 01:10:30 +00:00
ERR_FAIL_COND_V ( peer . is_null ( ) , ERR_UNCONFIGURED ) ;
_poll_buffer ( ) ;
int remaining = ring_buffer . data_left ( ) ;
ERR_FAIL_COND_V ( remaining < 4 , ERR_UNAVAILABLE ) ;
uint8_t lbuf [ 4 ] ;
ring_buffer . copy ( lbuf , 0 , 4 ) ;
remaining - = 4 ;
2016-03-08 23:00:52 +00:00
uint32_t len = decode_uint32 ( lbuf ) ;
2014-02-10 01:10:30 +00:00
ERR_FAIL_COND_V ( remaining < ( int ) len , ERR_UNAVAILABLE ) ;
2019-02-21 19:57:39 +00:00
ERR_FAIL_COND_V ( input_buffer . size ( ) < ( int ) len , ERR_UNAVAILABLE ) ;
2014-02-10 01:10:30 +00:00
ring_buffer . read ( lbuf , 4 ) ; //get rid of first 4 bytes
2018-07-25 01:11:03 +00:00
ring_buffer . read ( input_buffer . ptrw ( ) , len ) ; // read packet
2014-02-10 01:10:30 +00:00
2017-08-08 23:55:22 +00:00
* r_buffer = & input_buffer [ 0 ] ;
2014-02-10 01:10:30 +00:00
r_buffer_size = len ;
return OK ;
}
Error PacketPeerStream : : put_packet ( const uint8_t * p_buffer , int p_buffer_size ) {
ERR_FAIL_COND_V ( peer . is_null ( ) , ERR_UNCONFIGURED ) ;
Error err = _poll_buffer ( ) ; //won't hurt to poll here too
2020-05-14 14:41:43 +00:00
if ( err ) {
2014-02-10 01:10:30 +00:00
return err ;
2020-05-14 14:41:43 +00:00
}
2014-02-10 01:10:30 +00:00
2020-05-14 14:41:43 +00:00
if ( p_buffer_size = = 0 ) {
2014-02-10 01:10:30 +00:00
return OK ;
2020-05-14 14:41:43 +00:00
}
2014-02-10 01:10:30 +00:00
ERR_FAIL_COND_V ( p_buffer_size < 0 , ERR_INVALID_PARAMETER ) ;
2017-08-08 23:55:22 +00:00
ERR_FAIL_COND_V ( p_buffer_size + 4 > output_buffer . size ( ) , ERR_INVALID_PARAMETER ) ;
2014-02-10 01:10:30 +00:00
2018-07-25 01:11:03 +00:00
encode_uint32 ( p_buffer_size , output_buffer . ptrw ( ) ) ;
uint8_t * dst = & output_buffer . write [ 4 ] ;
2020-05-14 14:41:43 +00:00
for ( int i = 0 ; i < p_buffer_size ; i + + ) {
2014-02-10 01:10:30 +00:00
dst [ i ] = p_buffer [ i ] ;
2020-05-14 14:41:43 +00:00
}
2014-02-10 01:10:30 +00:00
2017-08-08 23:55:22 +00:00
return peer - > put_data ( & output_buffer [ 0 ] , p_buffer_size + 4 ) ;
2014-02-10 01:10:30 +00:00
}
int PacketPeerStream : : get_max_packet_size ( ) const {
2017-08-08 23:55:22 +00:00
return output_buffer . size ( ) ;
2014-02-10 01:10:30 +00:00
}
void PacketPeerStream : : set_stream_peer ( const Ref < StreamPeer > & p_peer ) {
if ( p_peer . ptr ( ) ! = peer . ptr ( ) ) {
2020-12-08 21:51:48 +00:00
ring_buffer . advance_read ( ring_buffer . data_left ( ) ) ; // Reset the ring buffer.
2020-05-19 13:46:49 +00:00
}
2014-02-10 01:10:30 +00:00
peer = p_peer ;
}
2018-01-11 22:35:12 +00:00
Ref < StreamPeer > PacketPeerStream : : get_stream_peer ( ) const {
return peer ;
}
2014-02-10 01:10:30 +00:00
void PacketPeerStream : : set_input_buffer_max_size ( int p_max_size ) {
2019-11-20 15:22:16 +00:00
ERR_FAIL_COND_MSG ( p_max_size < 0 , " Max size of input buffer size cannot be smaller than 0. " ) ;
2024-09-12 17:03:59 +00:00
// WARNING: May lose packets.
2019-08-15 02:57:49 +00:00
ERR_FAIL_COND_MSG ( ring_buffer . data_left ( ) , " Buffer in use, resizing would cause loss of data. " ) ;
2019-12-22 14:23:35 +00:00
ring_buffer . resize ( nearest_shift ( next_power_of_2 ( p_max_size + 4 ) ) - 1 ) ;
2017-08-17 21:35:55 +00:00
input_buffer . resize ( next_power_of_2 ( p_max_size + 4 ) ) ;
2017-08-08 23:55:22 +00:00
}
2017-08-18 13:59:31 +00:00
int PacketPeerStream : : get_input_buffer_max_size ( ) const {
return input_buffer . size ( ) - 4 ;
}
2017-08-08 23:55:22 +00:00
void PacketPeerStream : : set_output_buffer_max_size ( int p_max_size ) {
2017-08-17 21:35:55 +00:00
output_buffer . resize ( next_power_of_2 ( p_max_size + 4 ) ) ;
2014-02-10 01:10:30 +00:00
}
2017-08-18 13:59:31 +00:00
int PacketPeerStream : : get_output_buffer_max_size ( ) const {
return output_buffer . size ( ) - 4 ;
}
2014-02-10 01:10:30 +00:00
PacketPeerStream : : PacketPeerStream ( ) {
2024-01-03 00:14:50 +00:00
int64_t rbsize = GLOBAL_GET ( " network/limits/packet_peer_stream/max_buffer_po2 " ) ;
2014-02-10 01:10:30 +00:00
ring_buffer . resize ( rbsize ) ;
2024-01-03 00:14:50 +00:00
input_buffer . resize ( int64_t ( 1 ) < < rbsize ) ;
output_buffer . resize ( int64_t ( 1 ) < < rbsize ) ;
2014-02-10 01:10:30 +00:00
}