diff options
author | Juan Linietsky <reduzio@gmail.com> | 2016-08-22 01:14:08 -0300 |
---|---|---|
committer | Juan Linietsky <reduzio@gmail.com> | 2016-08-22 01:19:24 -0300 |
commit | cbbcf727035c8b481889f605337a96a9e58ed970 (patch) | |
tree | 2981840c23b2c6e7458b18af14ddb70b66270c15 /core/io/stream_peer.cpp | |
parent | 61cb8fd76c78ce2d27b5738e84c773ae4ec284e1 (diff) |
-High Level protocol optimization (should be smaller)
-Ability to set compression to ENet packets (check API)
-Fixed small bug in StringDB that lead to duplicate empty strings
-Added a new class, StreamPeerBuffer, useful to create your own tightly packed data
Diffstat (limited to 'core/io/stream_peer.cpp')
-rw-r--r-- | core/io/stream_peer.cpp | 143 |
1 files changed, 137 insertions, 6 deletions
diff --git a/core/io/stream_peer.cpp b/core/io/stream_peer.cpp index 306e7d8c9d..176d55ffdd 100644 --- a/core/io/stream_peer.cpp +++ b/core/io/stream_peer.cpp @@ -222,6 +222,7 @@ void StreamPeer::put_double(double p_val){ void StreamPeer::put_utf8_string(const String& p_string) { CharString cs=p_string.utf8(); + put_u32(p_string.length()); put_data((const uint8_t*)cs.get_data(),cs.length()); } @@ -348,8 +349,10 @@ String StreamPeer::get_string(int p_bytes){ ERR_FAIL_COND_V(p_bytes<0,String()); Vector<char> buf; - buf.resize(p_bytes+1); - get_data((uint8_t*)&buf[0],p_bytes); + 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()); buf[p_bytes]=0; return buf.ptr(); @@ -359,8 +362,10 @@ String StreamPeer::get_utf8_string(int p_bytes){ ERR_FAIL_COND_V(p_bytes<0,String()); Vector<uint8_t> buf; - buf.resize(p_bytes); - get_data(buf.ptr(),p_bytes); + Error err = buf.resize(p_bytes); + ERR_FAIL_COND_V(err!=OK,String()); + err = get_data(buf.ptr(),p_bytes); + ERR_FAIL_COND_V(err!=OK,String()); String ret; ret.parse_utf8((const char*)buf.ptr(),buf.size()); @@ -371,8 +376,10 @@ Variant StreamPeer::get_var(){ int len = get_32(); Vector<uint8_t> var; - var.resize(len); - get_data(var.ptr(),len); + Error err = var.resize(len); + ERR_FAIL_COND_V(err!=OK,Variant()); + err = get_data(var.ptr(),len); + ERR_FAIL_COND_V(err!=OK,Variant()); Variant ret; decode_variant(ret,var.ptr(),len); @@ -420,3 +427,127 @@ 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"),&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<uint8_t>::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<uint8_t>::Read r = data.read(); + copymem(p_buffer,r.ptr(),r_received); +} + +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<uint8_t> & p_data){ + + data=p_data; + pointer=0; +} + +DVector<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; +} + + +StreamPeerBuffer::StreamPeerBuffer() { + + pointer=0; +} |