From 1aedd48929a0762a0f33cc968f3d4fe916769f88 Mon Sep 17 00:00:00 2001 From: Raymoo Date: Sat, 12 Aug 2017 01:08:35 -0700 Subject: [PATCH] Port StreamPeerBuffer to 2.1 --- core/io/stream_peer.cpp | 120 +++++++++++++++++++++++++++++++++++ core/io/stream_peer.h | 35 ++++++++++ core/register_core_types.cpp | 1 + 3 files changed, 156 insertions(+) diff --git a/core/io/stream_peer.cpp b/core/io/stream_peer.cpp index 4f43d88d611..6f0dba71f4d 100644 --- a/core/io/stream_peer.cpp +++ b/core/io/stream_peer.cpp @@ -394,3 +394,123 @@ void StreamPeer::_bind_methods() { ObjectTypeDB::bind_method(_MD("get_utf8_string", "bytes"), &StreamPeer::get_utf8_string); ObjectTypeDB::bind_method(_MD("get_var:Variant"), &StreamPeer::get_var); } + +//////////////////////////////// + +void StreamPeerBuffer::_bind_methods() { + + ObjectTypeDB::bind_method(_MD("seek", "pos"), &StreamPeerBuffer::seek); + ObjectTypeDB::bind_method(_MD("get_size"), &StreamPeerBuffer::get_size); + ObjectTypeDB::bind_method(_MD("get_pos"), &StreamPeerBuffer::get_pos); + ObjectTypeDB::bind_method(_MD("resize", "size"), &StreamPeerBuffer::resize); + ObjectTypeDB::bind_method(_MD("set_data_array", "data"), &StreamPeerBuffer::set_data_array); + ObjectTypeDB::bind_method(_MD("get_data_array"), &StreamPeerBuffer::get_data_array); + ObjectTypeDB::bind_method(_MD("clear"), &StreamPeerBuffer::clear); + ObjectTypeDB::bind_method(_MD("duplicate"), &StreamPeerBuffer::duplicate); +} + +Error StreamPeerBuffer::put_data(const uint8_t *p_data, int p_bytes) { + + if (p_bytes <= 0) + return OK; + + if (pointer + p_bytes > data.size()) { + data.resize(pointer + p_bytes); + } + + DVector::Write w = data.write(); + copymem(&w[pointer], p_data, p_bytes); + + pointer += p_bytes; + return OK; +} + +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); +} + +Error StreamPeerBuffer::get_data(uint8_t *p_buffer, int p_bytes) { + + int recv; + get_partial_data(p_buffer, p_bytes, recv); + if (recv != p_bytes) + return ERR_INVALID_PARAMETER; + + return OK; +} +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; + return OK; //you got 0 + } + } else { + r_received = p_bytes; + } + + DVector::Read r = data.read(); + copymem(p_buffer, r.ptr() + pointer, r_received); + + pointer += r_received; + // FIXME: return what? OK or ERR_* +} + +int StreamPeerBuffer::get_available_bytes() const { + + return data.size() - pointer; +} + +void StreamPeerBuffer::seek(int p_pos) { + + ERR_FAIL_COND(p_pos < 0); + ERR_FAIL_COND(p_pos > data.size()); + pointer = p_pos; +} +int StreamPeerBuffer::get_size() const { + + return data.size(); +} + +int StreamPeerBuffer::get_pos() const { + + return pointer; +} + +void StreamPeerBuffer::resize(int p_size) { + + data.resize(p_size); +} + +void StreamPeerBuffer::set_data_array(const DVector &p_data) { + + data = p_data; + pointer = 0; +} + +DVector StreamPeerBuffer::get_data_array() const { + + return data; +} + +void StreamPeerBuffer::clear() { + + data.resize(0); + pointer = 0; +} + +Ref StreamPeerBuffer::duplicate() const { + + Ref spb; + spb.instance(); + spb->data = data; + return spb; +} + +StreamPeerBuffer::StreamPeerBuffer() { + + pointer = 0; +} diff --git a/core/io/stream_peer.h b/core/io/stream_peer.h index b9bdf934bf5..d090d23e62c 100644 --- a/core/io/stream_peer.h +++ b/core/io/stream_peer.h @@ -90,4 +90,39 @@ public: StreamPeer() { big_endian = false; } }; +class StreamPeerBuffer : public StreamPeer { + OBJ_TYPE(StreamPeerBuffer, StreamPeer); + OBJ_CATEGORY("Networking"); + + DVector data; + int pointer; + +protected: + static void _bind_methods(); + +public: + Error put_data(const uint8_t *p_data, int p_bytes); + Error put_partial_data(const uint8_t *p_data, int p_bytes, int &r_sent); + + + Error get_data(uint8_t *p_buffer, int p_bytes); + Error get_partial_data(uint8_t *p_buffer, int p_bytes, int &r_received); + + int get_available_bytes() const; + + void seek(int p_pos); + int get_size() const; + int get_pos() const; + void resize(int p_size); + + void set_data_array(const DVector &p_data); + DVector get_data_array() const; + + void clear(); + + Ref duplicate() const; + + StreamPeerBuffer(); +}; + #endif // STREAM_PEER_H diff --git a/core/register_core_types.cpp b/core/register_core_types.cpp index 2cb8b7c501f..73f7932298a 100644 --- a/core/register_core_types.cpp +++ b/core/register_core_types.cpp @@ -111,6 +111,7 @@ void register_core_types() { ObjectTypeDB::register_type(); ObjectTypeDB::register_type(); ObjectTypeDB::register_virtual_type(); + ObjectTypeDB::register_type(); ObjectTypeDB::register_create_type(); ObjectTypeDB::register_create_type(); ObjectTypeDB::register_create_type();