Merge pull request #10295 from raymoo/portstreampeerbuffer

Port StreamPeerBuffer to 2.1
This commit is contained in:
Rémi Verschelde 2017-08-13 17:24:01 +02:00 committed by GitHub
commit 042cee8b88
3 changed files with 156 additions and 0 deletions

View File

@ -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<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() + 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<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;
return spb;
}
StreamPeerBuffer::StreamPeerBuffer() {
pointer = 0;
}

View File

@ -90,4 +90,39 @@ public:
StreamPeer() { big_endian = false; }
};
class StreamPeerBuffer : public StreamPeer {
OBJ_TYPE(StreamPeerBuffer, StreamPeer);
OBJ_CATEGORY("Networking");
DVector<uint8_t> 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<uint8_t> &p_data);
DVector<uint8_t> get_data_array() const;
void clear();
Ref<StreamPeerBuffer> duplicate() const;
StreamPeerBuffer();
};
#endif // STREAM_PEER_H

View File

@ -111,6 +111,7 @@ void register_core_types() {
ObjectTypeDB::register_type<Resource>();
ObjectTypeDB::register_type<FuncRef>();
ObjectTypeDB::register_virtual_type<StreamPeer>();
ObjectTypeDB::register_type<StreamPeerBuffer>();
ObjectTypeDB::register_create_type<StreamPeerTCP>();
ObjectTypeDB::register_create_type<TCP_Server>();
ObjectTypeDB::register_create_type<PacketPeerUDP>();