diff options
Diffstat (limited to 'core/io/stream_peer.cpp')
-rw-r--r-- | core/io/stream_peer.cpp | 88 |
1 files changed, 34 insertions, 54 deletions
diff --git a/core/io/stream_peer.cpp b/core/io/stream_peer.cpp index 9bbe92096d..403f61bb24 100644 --- a/core/io/stream_peer.cpp +++ b/core/io/stream_peer.cpp @@ -33,16 +33,15 @@ #include "core/io/marshalls.h" Error StreamPeer::_put_data(const Vector<uint8_t> &p_data) { - int len = p_data.size(); - if (len == 0) + if (len == 0) { return OK; + } const uint8_t *r = p_data.ptr(); return put_data(&r[0], len); } Array StreamPeer::_put_partial_data(const Vector<uint8_t> &p_data) { - Array ret; int len = p_data.size(); @@ -65,13 +64,11 @@ Array StreamPeer::_put_partial_data(const Vector<uint8_t> &p_data) { } Array StreamPeer::_get_data(int p_bytes) { - Array ret; Vector<uint8_t> data; data.resize(p_bytes); if (data.size() != p_bytes) { - ret.push_back(ERR_OUT_OF_MEMORY); ret.push_back(Vector<uint8_t>()); return ret; @@ -86,13 +83,11 @@ Array StreamPeer::_get_data(int p_bytes) { } Array StreamPeer::_get_partial_data(int p_bytes) { - Array ret; Vector<uint8_t> data; data.resize(p_bytes); if (data.size() != p_bytes) { - ret.push_back(ERR_OUT_OF_MEMORY); ret.push_back(Vector<uint8_t>()); return ret; @@ -105,7 +100,6 @@ Array StreamPeer::_get_partial_data(int p_bytes) { if (err != OK) { data.resize(0); } else if (received != data.size()) { - data.resize(received); } @@ -115,12 +109,10 @@ Array StreamPeer::_get_partial_data(int p_bytes) { } void StreamPeer::set_big_endian(bool p_enable) { - big_endian = p_enable; } bool StreamPeer::is_big_endian_enabled() const { - return big_endian; } @@ -129,11 +121,10 @@ void StreamPeer::put_u8(uint8_t p_val) { } void StreamPeer::put_8(int8_t p_val) { - put_data((const uint8_t *)&p_val, 1); } -void StreamPeer::put_u16(uint16_t p_val) { +void StreamPeer::put_u16(uint16_t p_val) { if (big_endian) { p_val = BSWAP16(p_val); } @@ -141,8 +132,8 @@ void StreamPeer::put_u16(uint16_t p_val) { encode_uint16(p_val, buf); put_data(buf, 2); } -void StreamPeer::put_16(int16_t p_val) { +void StreamPeer::put_16(int16_t p_val) { if (big_endian) { p_val = BSWAP16(p_val); } @@ -150,8 +141,8 @@ void StreamPeer::put_16(int16_t p_val) { encode_uint16(p_val, buf); put_data(buf, 2); } -void StreamPeer::put_u32(uint32_t p_val) { +void StreamPeer::put_u32(uint32_t p_val) { if (big_endian) { p_val = BSWAP32(p_val); } @@ -159,8 +150,8 @@ void StreamPeer::put_u32(uint32_t p_val) { encode_uint32(p_val, buf); put_data(buf, 4); } -void StreamPeer::put_32(int32_t p_val) { +void StreamPeer::put_32(int32_t p_val) { if (big_endian) { p_val = BSWAP32(p_val); } @@ -168,8 +159,8 @@ void StreamPeer::put_32(int32_t p_val) { encode_uint32(p_val, buf); put_data(buf, 4); } -void StreamPeer::put_u64(uint64_t p_val) { +void StreamPeer::put_u64(uint64_t p_val) { if (big_endian) { p_val = BSWAP64(p_val); } @@ -177,8 +168,8 @@ void StreamPeer::put_u64(uint64_t p_val) { encode_uint64(p_val, buf); put_data(buf, 8); } -void StreamPeer::put_64(int64_t p_val) { +void StreamPeer::put_64(int64_t p_val) { if (big_endian) { p_val = BSWAP64(p_val); } @@ -186,8 +177,8 @@ void StreamPeer::put_64(int64_t p_val) { encode_uint64(p_val, buf); put_data(buf, 8); } -void StreamPeer::put_float(float p_val) { +void StreamPeer::put_float(float p_val) { uint8_t buf[4]; encode_float(p_val, buf); @@ -198,8 +189,8 @@ void StreamPeer::put_float(float p_val) { put_data(buf, 4); } -void StreamPeer::put_double(double p_val) { +void StreamPeer::put_double(double p_val) { uint8_t buf[8]; encode_double(p_val, buf); if (big_endian) { @@ -208,20 +199,20 @@ void StreamPeer::put_double(double p_val) { } put_data(buf, 8); } -void StreamPeer::put_string(const String &p_string) { +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()); } -void StreamPeer::put_utf8_string(const String &p_string) { +void StreamPeer::put_utf8_string(const String &p_string) { CharString cs = p_string.utf8(); put_u32(cs.length()); put_data((const uint8_t *)cs.get_data(), cs.length()); } -void StreamPeer::put_var(const Variant &p_variant, bool p_full_objects) { +void StreamPeer::put_var(const Variant &p_variant, bool p_full_objects) { int len = 0; Vector<uint8_t> buf; encode_variant(p_variant, nullptr, len, p_full_objects); @@ -232,19 +223,18 @@ void StreamPeer::put_var(const Variant &p_variant, bool p_full_objects) { } uint8_t StreamPeer::get_u8() { - uint8_t buf[1]; get_data(buf, 1); return buf[0]; } -int8_t StreamPeer::get_8() { +int8_t StreamPeer::get_8() { uint8_t buf[1]; get_data(buf, 1); return buf[0]; } -uint16_t StreamPeer::get_u16() { +uint16_t StreamPeer::get_u16() { uint8_t buf[2]; get_data(buf, 2); uint16_t r = decode_uint16(buf); @@ -253,8 +243,8 @@ uint16_t StreamPeer::get_u16() { } return r; } -int16_t StreamPeer::get_16() { +int16_t StreamPeer::get_16() { uint8_t buf[2]; get_data(buf, 2); uint16_t r = decode_uint16(buf); @@ -263,8 +253,8 @@ int16_t StreamPeer::get_16() { } return r; } -uint32_t StreamPeer::get_u32() { +uint32_t StreamPeer::get_u32() { uint8_t buf[4]; get_data(buf, 4); uint32_t r = decode_uint32(buf); @@ -273,8 +263,8 @@ uint32_t StreamPeer::get_u32() { } return r; } -int32_t StreamPeer::get_32() { +int32_t StreamPeer::get_32() { uint8_t buf[4]; get_data(buf, 4); uint32_t r = decode_uint32(buf); @@ -283,8 +273,8 @@ int32_t StreamPeer::get_32() { } return r; } -uint64_t StreamPeer::get_u64() { +uint64_t StreamPeer::get_u64() { uint8_t buf[8]; get_data(buf, 8); uint64_t r = decode_uint64(buf); @@ -293,8 +283,8 @@ uint64_t StreamPeer::get_u64() { } return r; } -int64_t StreamPeer::get_64() { +int64_t StreamPeer::get_64() { uint8_t buf[8]; get_data(buf, 8); uint64_t r = decode_uint64(buf); @@ -303,8 +293,8 @@ int64_t StreamPeer::get_64() { } return r; } -float StreamPeer::get_float() { +float StreamPeer::get_float() { uint8_t buf[4]; get_data(buf, 4); @@ -317,7 +307,6 @@ float StreamPeer::get_float() { } double StreamPeer::get_double() { - uint8_t buf[8]; get_data(buf, 8); @@ -328,10 +317,11 @@ double StreamPeer::get_double() { return decode_double(buf); } -String StreamPeer::get_string(int p_bytes) { - if (p_bytes < 0) +String StreamPeer::get_string(int p_bytes) { + if (p_bytes < 0) { p_bytes = get_u32(); + } ERR_FAIL_COND_V(p_bytes < 0, String()); Vector<char> buf; @@ -342,10 +332,11 @@ String StreamPeer::get_string(int p_bytes) { buf.write[p_bytes] = 0; return buf.ptr(); } -String StreamPeer::get_utf8_string(int p_bytes) { - if (p_bytes < 0) +String StreamPeer::get_utf8_string(int p_bytes) { + if (p_bytes < 0) { p_bytes = get_u32(); + } ERR_FAIL_COND_V(p_bytes < 0, String()); Vector<uint8_t> buf; @@ -358,8 +349,8 @@ String StreamPeer::get_utf8_string(int p_bytes) { ret.parse_utf8((const char *)buf.ptr(), buf.size()); return ret; } -Variant StreamPeer::get_var(bool p_allow_objects) { +Variant StreamPeer::get_var(bool p_allow_objects) { int len = get_32(); Vector<uint8_t> var; Error err = var.resize(len); @@ -375,7 +366,6 @@ Variant StreamPeer::get_var(bool p_allow_objects) { } void StreamPeer::_bind_methods() { - ClassDB::bind_method(D_METHOD("put_data", "data"), &StreamPeer::_put_data); ClassDB::bind_method(D_METHOD("put_partial_data", "data"), &StreamPeer::_put_partial_data); @@ -417,10 +407,10 @@ void StreamPeer::_bind_methods() { ADD_PROPERTY(PropertyInfo(Variant::BOOL, "big_endian"), "set_big_endian", "is_big_endian_enabled"); } + //////////////////////////////// void StreamPeerBuffer::_bind_methods() { - ClassDB::bind_method(D_METHOD("seek", "position"), &StreamPeerBuffer::seek); ClassDB::bind_method(D_METHOD("get_size"), &StreamPeerBuffer::get_size); ClassDB::bind_method(D_METHOD("get_position"), &StreamPeerBuffer::get_position); @@ -434,9 +424,9 @@ void StreamPeerBuffer::_bind_methods() { } Error StreamPeerBuffer::put_data(const uint8_t *p_data, int p_bytes) { - - if (p_bytes <= 0) + if (p_bytes <= 0) { return OK; + } if (pointer + p_bytes > data.size()) { data.resize(pointer + p_bytes); @@ -450,23 +440,21 @@ Error StreamPeerBuffer::put_data(const uint8_t *p_data, int p_bytes) { } 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) + 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) { @@ -487,50 +475,42 @@ Error StreamPeerBuffer::get_partial_data(uint8_t *p_buffer, int p_bytes, int &r_ } 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 { +int StreamPeerBuffer::get_size() const { return data.size(); } int StreamPeerBuffer::get_position() const { - return pointer; } void StreamPeerBuffer::resize(int p_size) { - data.resize(p_size); } void StreamPeerBuffer::set_data_array(const Vector<uint8_t> &p_data) { - data = p_data; pointer = 0; } Vector<uint8_t> StreamPeerBuffer::get_data_array() const { - return data; } void StreamPeerBuffer::clear() { - data.resize(0); pointer = 0; } Ref<StreamPeerBuffer> StreamPeerBuffer::duplicate() const { - Ref<StreamPeerBuffer> spb; spb.instance(); spb->data = data; |