| 
									
										
										
										
											2023-01-05 13:25:55 +01:00
										 |  |  | /**************************************************************************/ | 
					
						
							|  |  |  | /*  stream_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-05 00:50:27 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | #include "stream_peer.h"
 | 
					
						
							| 
									
										
										
										
											2018-09-11 18:13:45 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | #include "core/io/marshalls.h"
 | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-17 18:06:54 -03:00
										 |  |  | Error StreamPeer::_put_data(const Vector<uint8_t> &p_data) { | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	int len = p_data.size(); | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 	if (len == 0) { | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 		return OK; | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2020-02-17 18:06:54 -03:00
										 |  |  | 	const uint8_t *r = p_data.ptr(); | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	return put_data(&r[0], len); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-17 18:06:54 -03:00
										 |  |  | Array StreamPeer::_put_partial_data(const Vector<uint8_t> &p_data) { | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	Array ret; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	int len = p_data.size(); | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	if (len == 0) { | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 		ret.push_back(OK); | 
					
						
							|  |  |  | 		ret.push_back(0); | 
					
						
							|  |  |  | 		return ret; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-17 18:06:54 -03:00
										 |  |  | 	const uint8_t *r = p_data.ptr(); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	int sent; | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	Error err = put_partial_data(&r[0], len, sent); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	if (err != OK) { | 
					
						
							|  |  |  | 		sent = 0; | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	ret.push_back(err); | 
					
						
							|  |  |  | 	ret.push_back(sent); | 
					
						
							|  |  |  | 	return ret; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Array StreamPeer::_get_data(int p_bytes) { | 
					
						
							|  |  |  | 	Array ret; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-17 18:06:54 -03:00
										 |  |  | 	Vector<uint8_t> data; | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	data.resize(p_bytes); | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	if (data.size() != p_bytes) { | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 		ret.push_back(ERR_OUT_OF_MEMORY); | 
					
						
							| 
									
										
										
										
											2020-02-17 18:06:54 -03:00
										 |  |  | 		ret.push_back(Vector<uint8_t>()); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 		return ret; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-17 18:06:54 -03:00
										 |  |  | 	uint8_t *w = data.ptrw(); | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	Error err = get_data(&w[0], p_bytes); | 
					
						
							| 
									
										
										
										
											2020-02-17 18:06:54 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	ret.push_back(err); | 
					
						
							|  |  |  | 	ret.push_back(data); | 
					
						
							|  |  |  | 	return ret; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Array StreamPeer::_get_partial_data(int p_bytes) { | 
					
						
							|  |  |  | 	Array ret; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-17 18:06:54 -03:00
										 |  |  | 	Vector<uint8_t> data; | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	data.resize(p_bytes); | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	if (data.size() != p_bytes) { | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 		ret.push_back(ERR_OUT_OF_MEMORY); | 
					
						
							| 
									
										
										
										
											2020-02-17 18:06:54 -03:00
										 |  |  | 		ret.push_back(Vector<uint8_t>()); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 		return ret; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-17 18:06:54 -03:00
										 |  |  | 	uint8_t *w = data.ptrw(); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	int received; | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	Error err = get_partial_data(&w[0], p_bytes, received); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	if (err != OK) { | 
					
						
							| 
									
										
										
										
											2022-02-02 00:04:13 +05:45
										 |  |  | 		data.clear(); | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	} else if (received != data.size()) { | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 		data.resize(received); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	ret.push_back(err); | 
					
						
							|  |  |  | 	ret.push_back(data); | 
					
						
							|  |  |  | 	return ret; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-20 14:58:03 +02:00
										 |  |  | void StreamPeer::set_big_endian(bool p_big_endian) { | 
					
						
							|  |  |  | 	big_endian = p_big_endian; | 
					
						
							| 
									
										
										
										
											2015-12-13 12:53:29 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | bool StreamPeer::is_big_endian_enabled() const { | 
					
						
							|  |  |  | 	return big_endian; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void StreamPeer::put_u8(uint8_t p_val) { | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	put_data((const uint8_t *)&p_val, 1); | 
					
						
							| 
									
										
										
										
											2015-12-13 12:53:29 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | void StreamPeer::put_8(int8_t p_val) { | 
					
						
							|  |  |  | 	put_data((const uint8_t *)&p_val, 1); | 
					
						
							| 
									
										
										
										
											2015-12-13 12:53:29 -03:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2020-05-14 14:29:06 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | void StreamPeer::put_u16(uint16_t p_val) { | 
					
						
							| 
									
										
										
										
											2015-12-13 12:53:29 -03:00
										 |  |  | 	if (big_endian) { | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 		p_val = BSWAP16(p_val); | 
					
						
							| 
									
										
										
										
											2015-12-13 12:53:29 -03:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	uint8_t buf[2]; | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	encode_uint16(p_val, buf); | 
					
						
							|  |  |  | 	put_data(buf, 2); | 
					
						
							| 
									
										
										
										
											2015-12-13 12:53:29 -03:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2020-05-14 14:29:06 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | void StreamPeer::put_16(int16_t p_val) { | 
					
						
							| 
									
										
										
										
											2015-12-13 12:53:29 -03:00
										 |  |  | 	if (big_endian) { | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 		p_val = BSWAP16(p_val); | 
					
						
							| 
									
										
										
										
											2015-12-13 12:53:29 -03:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	uint8_t buf[2]; | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	encode_uint16(p_val, buf); | 
					
						
							|  |  |  | 	put_data(buf, 2); | 
					
						
							| 
									
										
										
										
											2015-12-13 12:53:29 -03:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2020-05-14 14:29:06 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | void StreamPeer::put_u32(uint32_t p_val) { | 
					
						
							| 
									
										
										
										
											2015-12-13 12:53:29 -03:00
										 |  |  | 	if (big_endian) { | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 		p_val = BSWAP32(p_val); | 
					
						
							| 
									
										
										
										
											2015-12-13 12:53:29 -03:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	uint8_t buf[4]; | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	encode_uint32(p_val, buf); | 
					
						
							|  |  |  | 	put_data(buf, 4); | 
					
						
							| 
									
										
										
										
											2015-12-13 12:53:29 -03:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2020-05-14 14:29:06 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | void StreamPeer::put_32(int32_t p_val) { | 
					
						
							| 
									
										
										
										
											2015-12-13 12:53:29 -03:00
										 |  |  | 	if (big_endian) { | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 		p_val = BSWAP32(p_val); | 
					
						
							| 
									
										
										
										
											2015-12-13 12:53:29 -03:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	uint8_t buf[4]; | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	encode_uint32(p_val, buf); | 
					
						
							|  |  |  | 	put_data(buf, 4); | 
					
						
							| 
									
										
										
										
											2015-12-13 12:53:29 -03:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2020-05-14 14:29:06 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | void StreamPeer::put_u64(uint64_t p_val) { | 
					
						
							| 
									
										
										
										
											2015-12-13 12:53:29 -03:00
										 |  |  | 	if (big_endian) { | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 		p_val = BSWAP64(p_val); | 
					
						
							| 
									
										
										
										
											2015-12-13 12:53:29 -03:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	uint8_t buf[8]; | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	encode_uint64(p_val, buf); | 
					
						
							|  |  |  | 	put_data(buf, 8); | 
					
						
							| 
									
										
										
										
											2015-12-13 12:53:29 -03:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2020-05-14 14:29:06 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | void StreamPeer::put_64(int64_t p_val) { | 
					
						
							| 
									
										
										
										
											2015-12-13 12:53:29 -03:00
										 |  |  | 	if (big_endian) { | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 		p_val = BSWAP64(p_val); | 
					
						
							| 
									
										
										
										
											2015-12-13 12:53:29 -03:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	uint8_t buf[8]; | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	encode_uint64(p_val, buf); | 
					
						
							|  |  |  | 	put_data(buf, 8); | 
					
						
							| 
									
										
										
										
											2015-12-13 12:53:29 -03:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2020-05-14 14:29:06 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | void StreamPeer::put_float(float p_val) { | 
					
						
							| 
									
										
										
										
											2015-12-13 12:53:29 -03:00
										 |  |  | 	uint8_t buf[4]; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	encode_float(p_val, buf); | 
					
						
							| 
									
										
										
										
											2015-12-13 12:53:29 -03:00
										 |  |  | 	if (big_endian) { | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 		uint32_t *p32 = (uint32_t *)buf; | 
					
						
							|  |  |  | 		*p32 = BSWAP32(*p32); | 
					
						
							| 
									
										
										
										
											2015-12-13 12:53:29 -03:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	put_data(buf, 4); | 
					
						
							| 
									
										
										
										
											2015-12-13 12:53:29 -03:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2020-05-14 14:29:06 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | void StreamPeer::put_double(double p_val) { | 
					
						
							| 
									
										
										
										
											2015-12-13 12:53:29 -03:00
										 |  |  | 	uint8_t buf[8]; | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	encode_double(p_val, buf); | 
					
						
							| 
									
										
										
										
											2015-12-13 12:53:29 -03:00
										 |  |  | 	if (big_endian) { | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 		uint64_t *p64 = (uint64_t *)buf; | 
					
						
							|  |  |  | 		*p64 = BSWAP64(*p64); | 
					
						
							| 
									
										
										
										
											2015-12-13 12:53:29 -03:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	put_data(buf, 8); | 
					
						
							| 
									
										
										
										
											2015-12-13 12:53:29 -03:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2020-05-14 14:29:06 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-22 05:08:07 +02:00
										 |  |  | void StreamPeer::put_string(const String &p_string) { | 
					
						
							|  |  |  | 	CharString cs = p_string.ascii(); | 
					
						
							|  |  |  | 	put_u32(cs.length()); | 
					
						
							|  |  |  | 	put_data((const uint8_t *)cs.get_data(), cs.length()); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2020-05-14 14:29:06 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | void StreamPeer::put_utf8_string(const String &p_string) { | 
					
						
							|  |  |  | 	CharString cs = p_string.utf8(); | 
					
						
							| 
									
										
										
										
											2017-08-14 13:13:21 +02:00
										 |  |  | 	put_u32(cs.length()); | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	put_data((const uint8_t *)cs.get_data(), cs.length()); | 
					
						
							| 
									
										
										
										
											2015-12-13 12:53:29 -03:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2020-05-14 14:29:06 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-26 16:52:42 +01:00
										 |  |  | void StreamPeer::put_var(const Variant &p_variant, bool p_full_objects) { | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	int len = 0; | 
					
						
							| 
									
										
										
										
											2015-12-13 12:53:29 -03:00
										 |  |  | 	Vector<uint8_t> buf; | 
					
						
							| 
									
										
										
										
											2020-04-02 01:20:12 +02:00
										 |  |  | 	encode_variant(p_variant, nullptr, len, p_full_objects); | 
					
						
							| 
									
										
										
										
											2015-12-13 12:53:29 -03:00
										 |  |  | 	buf.resize(len); | 
					
						
							|  |  |  | 	put_32(len); | 
					
						
							| 
									
										
										
										
											2019-03-26 16:52:42 +01:00
										 |  |  | 	encode_variant(p_variant, buf.ptrw(), len, p_full_objects); | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	put_data(buf.ptr(), buf.size()); | 
					
						
							| 
									
										
										
										
											2015-12-13 12:53:29 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | uint8_t StreamPeer::get_u8() { | 
					
						
							| 
									
										
										
										
											2015-12-13 12:53:29 -03:00
										 |  |  | 	uint8_t buf[1]; | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	get_data(buf, 1); | 
					
						
							| 
									
										
										
										
											2015-12-13 12:53:29 -03:00
										 |  |  | 	return buf[0]; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2020-05-14 14:29:06 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | int8_t StreamPeer::get_8() { | 
					
						
							| 
									
										
										
										
											2015-12-13 12:53:29 -03:00
										 |  |  | 	uint8_t buf[1]; | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	get_data(buf, 1); | 
					
						
							| 
									
										
										
										
											2015-12-13 12:53:29 -03:00
										 |  |  | 	return buf[0]; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2020-05-14 14:29:06 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | uint16_t StreamPeer::get_u16() { | 
					
						
							| 
									
										
										
										
											2015-12-13 12:53:29 -03:00
										 |  |  | 	uint8_t buf[2]; | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	get_data(buf, 2); | 
					
						
							| 
									
										
										
										
											2015-12-13 12:53:29 -03:00
										 |  |  | 	uint16_t r = decode_uint16(buf); | 
					
						
							|  |  |  | 	if (big_endian) { | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 		r = BSWAP16(r); | 
					
						
							| 
									
										
										
										
											2015-12-13 12:53:29 -03:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	return r; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2020-05-14 14:29:06 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | int16_t StreamPeer::get_16() { | 
					
						
							| 
									
										
										
										
											2015-12-13 12:53:29 -03:00
										 |  |  | 	uint8_t buf[2]; | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	get_data(buf, 2); | 
					
						
							| 
									
										
										
										
											2015-12-13 12:53:29 -03:00
										 |  |  | 	uint16_t r = decode_uint16(buf); | 
					
						
							|  |  |  | 	if (big_endian) { | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 		r = BSWAP16(r); | 
					
						
							| 
									
										
										
										
											2015-12-13 12:53:29 -03:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	return r; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2020-05-14 14:29:06 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | uint32_t StreamPeer::get_u32() { | 
					
						
							| 
									
										
										
										
											2015-12-13 12:53:29 -03:00
										 |  |  | 	uint8_t buf[4]; | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	get_data(buf, 4); | 
					
						
							| 
									
										
										
										
											2015-12-13 12:53:29 -03:00
										 |  |  | 	uint32_t r = decode_uint32(buf); | 
					
						
							|  |  |  | 	if (big_endian) { | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 		r = BSWAP32(r); | 
					
						
							| 
									
										
										
										
											2015-12-13 12:53:29 -03:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	return r; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2020-05-14 14:29:06 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | int32_t StreamPeer::get_32() { | 
					
						
							| 
									
										
										
										
											2015-12-13 12:53:29 -03:00
										 |  |  | 	uint8_t buf[4]; | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	get_data(buf, 4); | 
					
						
							| 
									
										
										
										
											2015-12-13 12:53:29 -03:00
										 |  |  | 	uint32_t r = decode_uint32(buf); | 
					
						
							|  |  |  | 	if (big_endian) { | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 		r = BSWAP32(r); | 
					
						
							| 
									
										
										
										
											2015-12-13 12:53:29 -03:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	return r; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2020-05-14 14:29:06 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | uint64_t StreamPeer::get_u64() { | 
					
						
							| 
									
										
										
										
											2015-12-13 12:53:29 -03:00
										 |  |  | 	uint8_t buf[8]; | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	get_data(buf, 8); | 
					
						
							| 
									
										
										
										
											2015-12-13 12:53:29 -03:00
										 |  |  | 	uint64_t r = decode_uint64(buf); | 
					
						
							|  |  |  | 	if (big_endian) { | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 		r = BSWAP64(r); | 
					
						
							| 
									
										
										
										
											2015-12-13 12:53:29 -03:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	return r; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2020-05-14 14:29:06 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | int64_t StreamPeer::get_64() { | 
					
						
							| 
									
										
										
										
											2015-12-13 12:53:29 -03:00
										 |  |  | 	uint8_t buf[8]; | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	get_data(buf, 8); | 
					
						
							| 
									
										
										
										
											2015-12-13 12:53:29 -03:00
										 |  |  | 	uint64_t r = decode_uint64(buf); | 
					
						
							|  |  |  | 	if (big_endian) { | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 		r = BSWAP64(r); | 
					
						
							| 
									
										
										
										
											2015-12-13 12:53:29 -03:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	return r; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2020-05-14 14:29:06 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | float StreamPeer::get_float() { | 
					
						
							| 
									
										
										
										
											2015-12-13 12:53:29 -03:00
										 |  |  | 	uint8_t buf[4]; | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	get_data(buf, 4); | 
					
						
							| 
									
										
										
										
											2015-12-13 12:53:29 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if (big_endian) { | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 		uint32_t *p32 = (uint32_t *)buf; | 
					
						
							|  |  |  | 		*p32 = BSWAP32(*p32); | 
					
						
							| 
									
										
										
										
											2015-12-13 12:53:29 -03:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return decode_float(buf); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-29 15:45:46 -08:00
										 |  |  | double StreamPeer::get_double() { | 
					
						
							| 
									
										
										
										
											2015-12-13 12:53:29 -03:00
										 |  |  | 	uint8_t buf[8]; | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	get_data(buf, 8); | 
					
						
							| 
									
										
										
										
											2015-12-13 12:53:29 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if (big_endian) { | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 		uint64_t *p64 = (uint64_t *)buf; | 
					
						
							|  |  |  | 		*p64 = BSWAP64(*p64); | 
					
						
							| 
									
										
										
										
											2015-12-13 12:53:29 -03:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return decode_double(buf); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2020-05-14 14:29:06 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | String StreamPeer::get_string(int p_bytes) { | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 	if (p_bytes < 0) { | 
					
						
							| 
									
										
										
										
											2018-09-22 05:08:07 +02:00
										 |  |  | 		p_bytes = get_u32(); | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	ERR_FAIL_COND_V(p_bytes < 0, String()); | 
					
						
							| 
									
										
										
										
											2015-12-13 12:53:29 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	Vector<char> buf; | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	Error err = buf.resize(p_bytes + 1); | 
					
						
							|  |  |  | 	ERR_FAIL_COND_V(err != OK, String()); | 
					
						
							|  |  |  | 	err = get_data((uint8_t *)&buf[0], p_bytes); | 
					
						
							|  |  |  | 	ERR_FAIL_COND_V(err != OK, String()); | 
					
						
							| 
									
										
										
										
											2018-07-25 03:11:03 +02:00
										 |  |  | 	buf.write[p_bytes] = 0; | 
					
						
							| 
									
										
										
										
											2015-12-13 12:53:29 -03:00
										 |  |  | 	return buf.ptr(); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2020-05-14 14:29:06 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | String StreamPeer::get_utf8_string(int p_bytes) { | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 	if (p_bytes < 0) { | 
					
						
							| 
									
										
										
										
											2018-09-22 05:08:07 +02:00
										 |  |  | 		p_bytes = get_u32(); | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	ERR_FAIL_COND_V(p_bytes < 0, String()); | 
					
						
							| 
									
										
										
										
											2015-12-13 12:53:29 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	Vector<uint8_t> buf; | 
					
						
							| 
									
										
										
										
											2016-08-22 01:14:08 -03:00
										 |  |  | 	Error err = buf.resize(p_bytes); | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	ERR_FAIL_COND_V(err != OK, String()); | 
					
						
							| 
									
										
										
										
											2017-11-25 00:07:54 -03:00
										 |  |  | 	err = get_data(buf.ptrw(), p_bytes); | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	ERR_FAIL_COND_V(err != OK, String()); | 
					
						
							| 
									
										
										
										
											2015-12-13 12:53:29 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	String ret; | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	ret.parse_utf8((const char *)buf.ptr(), buf.size()); | 
					
						
							| 
									
										
										
										
											2015-12-13 12:53:29 -03:00
										 |  |  | 	return ret; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2020-05-14 14:29:06 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-26 16:52:42 +01:00
										 |  |  | Variant StreamPeer::get_var(bool p_allow_objects) { | 
					
						
							| 
									
										
										
										
											2015-12-13 12:53:29 -03:00
										 |  |  | 	int len = get_32(); | 
					
						
							|  |  |  | 	Vector<uint8_t> var; | 
					
						
							| 
									
										
										
										
											2016-08-22 01:14:08 -03:00
										 |  |  | 	Error err = var.resize(len); | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	ERR_FAIL_COND_V(err != OK, Variant()); | 
					
						
							| 
									
										
										
										
											2017-11-25 00:07:54 -03:00
										 |  |  | 	err = get_data(var.ptrw(), len); | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	ERR_FAIL_COND_V(err != OK, Variant()); | 
					
						
							| 
									
										
										
										
											2015-12-13 12:53:29 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	Variant ret; | 
					
						
							| 
									
										
										
										
											2020-04-02 01:20:12 +02:00
										 |  |  | 	err = decode_variant(ret, var.ptr(), len, nullptr, p_allow_objects); | 
					
						
							| 
									
										
										
										
											2019-09-25 10:28:50 +02:00
										 |  |  | 	ERR_FAIL_COND_V_MSG(err != OK, Variant(), "Error when trying to decode Variant."); | 
					
						
							| 
									
										
										
										
											2019-07-20 08:09:57 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-13 12:53:29 -03:00
										 |  |  | 	return ret; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | void StreamPeer::_bind_methods() { | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	ClassDB::bind_method(D_METHOD("put_data", "data"), &StreamPeer::_put_data); | 
					
						
							|  |  |  | 	ClassDB::bind_method(D_METHOD("put_partial_data", "data"), &StreamPeer::_put_partial_data); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	ClassDB::bind_method(D_METHOD("get_data", "bytes"), &StreamPeer::_get_data); | 
					
						
							|  |  |  | 	ClassDB::bind_method(D_METHOD("get_partial_data", "bytes"), &StreamPeer::_get_partial_data); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	ClassDB::bind_method(D_METHOD("get_available_bytes"), &StreamPeer::get_available_bytes); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	ClassDB::bind_method(D_METHOD("set_big_endian", "enable"), &StreamPeer::set_big_endian); | 
					
						
							|  |  |  | 	ClassDB::bind_method(D_METHOD("is_big_endian_enabled"), &StreamPeer::is_big_endian_enabled); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-17 10:43:23 +01:00
										 |  |  | 	ClassDB::bind_method(D_METHOD("put_8", "value"), &StreamPeer::put_8); | 
					
						
							|  |  |  | 	ClassDB::bind_method(D_METHOD("put_u8", "value"), &StreamPeer::put_u8); | 
					
						
							|  |  |  | 	ClassDB::bind_method(D_METHOD("put_16", "value"), &StreamPeer::put_16); | 
					
						
							|  |  |  | 	ClassDB::bind_method(D_METHOD("put_u16", "value"), &StreamPeer::put_u16); | 
					
						
							|  |  |  | 	ClassDB::bind_method(D_METHOD("put_32", "value"), &StreamPeer::put_32); | 
					
						
							|  |  |  | 	ClassDB::bind_method(D_METHOD("put_u32", "value"), &StreamPeer::put_u32); | 
					
						
							|  |  |  | 	ClassDB::bind_method(D_METHOD("put_64", "value"), &StreamPeer::put_64); | 
					
						
							|  |  |  | 	ClassDB::bind_method(D_METHOD("put_u64", "value"), &StreamPeer::put_u64); | 
					
						
							|  |  |  | 	ClassDB::bind_method(D_METHOD("put_float", "value"), &StreamPeer::put_float); | 
					
						
							|  |  |  | 	ClassDB::bind_method(D_METHOD("put_double", "value"), &StreamPeer::put_double); | 
					
						
							| 
									
										
										
										
											2018-09-22 05:08:07 +02:00
										 |  |  | 	ClassDB::bind_method(D_METHOD("put_string", "value"), &StreamPeer::put_string); | 
					
						
							| 
									
										
										
										
											2018-01-17 10:43:23 +01:00
										 |  |  | 	ClassDB::bind_method(D_METHOD("put_utf8_string", "value"), &StreamPeer::put_utf8_string); | 
					
						
							| 
									
										
										
										
											2019-03-26 16:52:42 +01:00
										 |  |  | 	ClassDB::bind_method(D_METHOD("put_var", "value", "full_objects"), &StreamPeer::put_var, DEFVAL(false)); | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	ClassDB::bind_method(D_METHOD("get_8"), &StreamPeer::get_8); | 
					
						
							|  |  |  | 	ClassDB::bind_method(D_METHOD("get_u8"), &StreamPeer::get_u8); | 
					
						
							|  |  |  | 	ClassDB::bind_method(D_METHOD("get_16"), &StreamPeer::get_16); | 
					
						
							|  |  |  | 	ClassDB::bind_method(D_METHOD("get_u16"), &StreamPeer::get_u16); | 
					
						
							|  |  |  | 	ClassDB::bind_method(D_METHOD("get_32"), &StreamPeer::get_32); | 
					
						
							|  |  |  | 	ClassDB::bind_method(D_METHOD("get_u32"), &StreamPeer::get_u32); | 
					
						
							|  |  |  | 	ClassDB::bind_method(D_METHOD("get_64"), &StreamPeer::get_64); | 
					
						
							|  |  |  | 	ClassDB::bind_method(D_METHOD("get_u64"), &StreamPeer::get_u64); | 
					
						
							|  |  |  | 	ClassDB::bind_method(D_METHOD("get_float"), &StreamPeer::get_float); | 
					
						
							|  |  |  | 	ClassDB::bind_method(D_METHOD("get_double"), &StreamPeer::get_double); | 
					
						
							| 
									
										
										
										
											2018-09-22 05:08:07 +02:00
										 |  |  | 	ClassDB::bind_method(D_METHOD("get_string", "bytes"), &StreamPeer::get_string, DEFVAL(-1)); | 
					
						
							|  |  |  | 	ClassDB::bind_method(D_METHOD("get_utf8_string", "bytes"), &StreamPeer::get_utf8_string, DEFVAL(-1)); | 
					
						
							| 
									
										
										
										
											2019-03-26 16:52:42 +01:00
										 |  |  | 	ClassDB::bind_method(D_METHOD("get_var", "allow_objects"), &StreamPeer::get_var, DEFVAL(false)); | 
					
						
							| 
									
										
										
										
											2018-01-12 00:35:12 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	ADD_PROPERTY(PropertyInfo(Variant::BOOL, "big_endian"), "set_big_endian", "is_big_endian_enabled"); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2020-05-14 14:29:06 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-22 01:14:08 -03:00
										 |  |  | ////////////////////////////////
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-06 17:30:20 +02:00
										 |  |  | Error StreamPeerExtension::get_data(uint8_t *r_buffer, int p_bytes) { | 
					
						
							| 
									
										
										
										
											2022-09-01 05:09:07 +02:00
										 |  |  | 	Error err; | 
					
						
							| 
									
										
										
										
											2021-09-06 17:30:20 +02:00
										 |  |  | 	int received = 0; | 
					
						
							|  |  |  | 	if (GDVIRTUAL_CALL(_get_data, r_buffer, p_bytes, &received, err)) { | 
					
						
							| 
									
										
										
										
											2022-09-01 05:09:07 +02:00
										 |  |  | 		return err; | 
					
						
							| 
									
										
										
										
											2021-09-06 17:30:20 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	WARN_PRINT_ONCE("StreamPeerExtension::_get_data is unimplemented!"); | 
					
						
							|  |  |  | 	return FAILED; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Error StreamPeerExtension::get_partial_data(uint8_t *r_buffer, int p_bytes, int &r_received) { | 
					
						
							| 
									
										
										
										
											2022-09-01 05:09:07 +02:00
										 |  |  | 	Error err; | 
					
						
							| 
									
										
										
										
											2021-09-06 17:30:20 +02:00
										 |  |  | 	if (GDVIRTUAL_CALL(_get_partial_data, r_buffer, p_bytes, &r_received, err)) { | 
					
						
							| 
									
										
										
										
											2022-09-01 05:09:07 +02:00
										 |  |  | 		return err; | 
					
						
							| 
									
										
										
										
											2021-09-06 17:30:20 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	WARN_PRINT_ONCE("StreamPeerExtension::_get_partial_data is unimplemented!"); | 
					
						
							|  |  |  | 	return FAILED; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Error StreamPeerExtension::put_data(const uint8_t *p_data, int p_bytes) { | 
					
						
							| 
									
										
										
										
											2022-09-01 05:09:07 +02:00
										 |  |  | 	Error err; | 
					
						
							| 
									
										
										
										
											2021-09-06 17:30:20 +02:00
										 |  |  | 	int sent = 0; | 
					
						
							|  |  |  | 	if (GDVIRTUAL_CALL(_put_data, p_data, p_bytes, &sent, err)) { | 
					
						
							| 
									
										
										
										
											2022-09-01 05:09:07 +02:00
										 |  |  | 		return err; | 
					
						
							| 
									
										
										
										
											2021-09-06 17:30:20 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	WARN_PRINT_ONCE("StreamPeerExtension::_put_data is unimplemented!"); | 
					
						
							|  |  |  | 	return FAILED; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Error StreamPeerExtension::put_partial_data(const uint8_t *p_data, int p_bytes, int &r_sent) { | 
					
						
							| 
									
										
										
										
											2022-09-01 05:09:07 +02:00
										 |  |  | 	Error err; | 
					
						
							| 
									
										
										
										
											2021-09-06 17:30:20 +02:00
										 |  |  | 	if (GDVIRTUAL_CALL(_put_data, p_data, p_bytes, &r_sent, err)) { | 
					
						
							| 
									
										
										
										
											2022-09-01 05:09:07 +02:00
										 |  |  | 		return err; | 
					
						
							| 
									
										
										
										
											2021-09-06 17:30:20 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	WARN_PRINT_ONCE("StreamPeerExtension::_put_partial_data is unimplemented!"); | 
					
						
							|  |  |  | 	return FAILED; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void StreamPeerExtension::_bind_methods() { | 
					
						
							|  |  |  | 	GDVIRTUAL_BIND(_get_data, "r_buffer", "r_bytes", "r_received"); | 
					
						
							|  |  |  | 	GDVIRTUAL_BIND(_get_partial_data, "r_buffer", "r_bytes", "r_received"); | 
					
						
							|  |  |  | 	GDVIRTUAL_BIND(_put_data, "p_data", "p_bytes", "r_sent"); | 
					
						
							|  |  |  | 	GDVIRTUAL_BIND(_put_partial_data, "p_data", "p_bytes", "r_sent"); | 
					
						
							|  |  |  | 	GDVIRTUAL_BIND(_get_available_bytes); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ////////////////////////////////
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-22 01:14:08 -03:00
										 |  |  | void StreamPeerBuffer::_bind_methods() { | 
					
						
							| 
									
										
										
										
											2017-09-10 15:37:49 +02:00
										 |  |  | 	ClassDB::bind_method(D_METHOD("seek", "position"), &StreamPeerBuffer::seek); | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	ClassDB::bind_method(D_METHOD("get_size"), &StreamPeerBuffer::get_size); | 
					
						
							| 
									
										
										
										
											2017-09-10 15:37:49 +02:00
										 |  |  | 	ClassDB::bind_method(D_METHOD("get_position"), &StreamPeerBuffer::get_position); | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	ClassDB::bind_method(D_METHOD("resize", "size"), &StreamPeerBuffer::resize); | 
					
						
							|  |  |  | 	ClassDB::bind_method(D_METHOD("set_data_array", "data"), &StreamPeerBuffer::set_data_array); | 
					
						
							|  |  |  | 	ClassDB::bind_method(D_METHOD("get_data_array"), &StreamPeerBuffer::get_data_array); | 
					
						
							|  |  |  | 	ClassDB::bind_method(D_METHOD("clear"), &StreamPeerBuffer::clear); | 
					
						
							| 
									
										
										
										
											2017-08-09 13:19:41 +02:00
										 |  |  | 	ClassDB::bind_method(D_METHOD("duplicate"), &StreamPeerBuffer::duplicate); | 
					
						
							| 
									
										
										
										
											2018-01-12 00:35:12 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-17 18:06:54 -03:00
										 |  |  | 	ADD_PROPERTY(PropertyInfo(Variant::PACKED_BYTE_ARRAY, "data_array"), "set_data_array", "get_data_array"); | 
					
						
							| 
									
										
										
										
											2016-08-22 01:14:08 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | Error StreamPeerBuffer::put_data(const uint8_t *p_data, int p_bytes) { | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 	if (p_bytes <= 0) { | 
					
						
							| 
									
										
										
										
											2016-08-22 01:14:08 -03:00
										 |  |  | 		return OK; | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-08-22 01:14:08 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	if (pointer + p_bytes > data.size()) { | 
					
						
							|  |  |  | 		data.resize(pointer + p_bytes); | 
					
						
							| 
									
										
										
										
											2016-08-22 01:14:08 -03:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-17 18:06:54 -03:00
										 |  |  | 	uint8_t *w = data.ptrw(); | 
					
						
							| 
									
										
										
										
											2021-04-27 16:19:21 +02:00
										 |  |  | 	memcpy(&w[pointer], p_data, p_bytes); | 
					
						
							| 
									
										
										
										
											2016-08-22 01:14:08 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	pointer += p_bytes; | 
					
						
							| 
									
										
										
										
											2016-08-22 01:14:08 -03:00
										 |  |  | 	return OK; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | Error StreamPeerBuffer::put_partial_data(const uint8_t *p_data, int p_bytes, int &r_sent) { | 
					
						
							|  |  |  | 	r_sent = p_bytes; | 
					
						
							|  |  |  | 	return put_data(p_data, p_bytes); | 
					
						
							| 
									
										
										
										
											2016-08-22 01:14:08 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | Error StreamPeerBuffer::get_data(uint8_t *p_buffer, int p_bytes) { | 
					
						
							| 
									
										
										
										
											2016-08-22 01:14:08 -03:00
										 |  |  | 	int recv; | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	get_partial_data(p_buffer, p_bytes, recv); | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 	if (recv != p_bytes) { | 
					
						
							| 
									
										
										
										
											2016-08-22 01:14:08 -03:00
										 |  |  | 		return ERR_INVALID_PARAMETER; | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-08-22 01:14:08 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	return OK; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2017-09-07 21:48:50 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | Error StreamPeerBuffer::get_partial_data(uint8_t *p_buffer, int p_bytes, int &r_received) { | 
					
						
							|  |  |  | 	if (pointer + p_bytes > data.size()) { | 
					
						
							|  |  |  | 		r_received = data.size() - pointer; | 
					
						
							|  |  |  | 		if (r_received <= 0) { | 
					
						
							|  |  |  | 			r_received = 0; | 
					
						
							| 
									
										
										
										
											2016-08-22 01:14:08 -03:00
										 |  |  | 			return OK; //you got 0
 | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} else { | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 		r_received = p_bytes; | 
					
						
							| 
									
										
										
										
											2016-08-22 01:14:08 -03:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-17 18:06:54 -03:00
										 |  |  | 	const uint8_t *r = data.ptr(); | 
					
						
							| 
									
										
										
										
											2021-04-27 16:19:21 +02:00
										 |  |  | 	memcpy(p_buffer, r + pointer, r_received); | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-12 01:19:50 -07:00
										 |  |  | 	pointer += r_received; | 
					
						
							| 
									
										
										
										
											2017-02-23 02:28:09 -06:00
										 |  |  | 	// FIXME: return what? OK or ERR_*
 | 
					
						
							| 
									
										
										
										
											2017-09-07 21:48:50 +02:00
										 |  |  | 	// return OK for now so we don't maybe return garbage
 | 
					
						
							|  |  |  | 	return OK; | 
					
						
							| 
									
										
										
										
											2016-08-22 01:14:08 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int StreamPeerBuffer::get_available_bytes() const { | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	return data.size() - pointer; | 
					
						
							| 
									
										
										
										
											2016-08-22 01:14:08 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | void StreamPeerBuffer::seek(int p_pos) { | 
					
						
							| 
									
										
										
										
											2016-08-22 01:14:08 -03:00
										 |  |  | 	ERR_FAIL_COND(p_pos < 0); | 
					
						
							|  |  |  | 	ERR_FAIL_COND(p_pos > data.size()); | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	pointer = p_pos; | 
					
						
							| 
									
										
										
										
											2016-08-22 01:14:08 -03:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2020-05-14 14:29:06 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | int StreamPeerBuffer::get_size() const { | 
					
						
							| 
									
										
										
										
											2016-08-22 01:14:08 -03:00
										 |  |  | 	return data.size(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-10 15:37:49 +02:00
										 |  |  | int StreamPeerBuffer::get_position() const { | 
					
						
							| 
									
										
										
										
											2016-08-22 01:14:08 -03:00
										 |  |  | 	return pointer; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | void StreamPeerBuffer::resize(int p_size) { | 
					
						
							| 
									
										
										
										
											2016-08-22 01:14:08 -03:00
										 |  |  | 	data.resize(p_size); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-17 18:06:54 -03:00
										 |  |  | void StreamPeerBuffer::set_data_array(const Vector<uint8_t> &p_data) { | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	data = p_data; | 
					
						
							|  |  |  | 	pointer = 0; | 
					
						
							| 
									
										
										
										
											2016-08-22 01:14:08 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-17 18:06:54 -03:00
										 |  |  | Vector<uint8_t> StreamPeerBuffer::get_data_array() const { | 
					
						
							| 
									
										
										
										
											2016-08-22 01:14:08 -03:00
										 |  |  | 	return data; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void StreamPeerBuffer::clear() { | 
					
						
							| 
									
										
										
										
											2022-02-02 00:04:13 +05:45
										 |  |  | 	data.clear(); | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	pointer = 0; | 
					
						
							| 
									
										
										
										
											2016-08-22 01:14:08 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Ref<StreamPeerBuffer> StreamPeerBuffer::duplicate() const { | 
					
						
							|  |  |  | 	Ref<StreamPeerBuffer> spb; | 
					
						
							| 
									
										
										
										
											2021-06-17 16:03:09 -06:00
										 |  |  | 	spb.instantiate(); | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	spb->data = data; | 
					
						
							| 
									
										
										
										
											2016-08-22 22:30:22 +09:00
										 |  |  | 	return spb; | 
					
						
							| 
									
										
										
										
											2016-08-22 01:14:08 -03:00
										 |  |  | } |