/* BitStreamQueue : class to manage packet received by server If we received message with bad order, with this class we can manage (with limit storage) Copyright (C) 2019 AleaJactaEst This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . */ #include "bitstreamqueue.h" #include "core/os/os.h" void BitStreamQueue::_bind_methods() { ClassDB::bind_method(D_METHOD("clear"), &BitStreamQueue::clear); ClassDB::bind_method(D_METHOD("length"), &BitStreamQueue::length); ClassDB::bind_method(D_METHOD("put_msgbytes", "msgbytes"), &BitStreamQueue::put_msgbytes); ClassDB::bind_method(D_METHOD("get_msg", "pos"), &BitStreamQueue::get_msg); ClassDB::bind_method(D_METHOD("get_received_number", "pos"), &BitStreamQueue::get_received_number); ClassDB::bind_method(D_METHOD("erase", "pos"), &BitStreamQueue::erase); } BitStreamQueue::BitStreamQueue() { OS::get_singleton()->print("[%s:%d] new BitStreamQueue\n", __FILE__, __LINE__); for(int i = 0 ; i < SIZE_QUEUE_MESSAGE ; ++i ) this->_msgin[i].instance(); //this->_msgin.resize(SIZE_QUEUE_MESSAGE); this->clear(); } BitStreamQueue::~BitStreamQueue() { OS::get_singleton()->print("[%s:%d] delete BitStreamQueue\n", __FILE__, __LINE__); this->clear(); //for(int i = 0 ; i < SIZE_QUEUE_MESSAGE ; ++i ) this->_msgin[i].unref(); for(int i = 0 ; i < SIZE_QUEUE_MESSAGE ; ++i ) { //OS::get_singleton()->print("[%s:%d] delete BitStreamQueue %d\n", __FILE__, __LINE__, i); this->_msgin[i].unref(); if (this->_msgin[i] != nullptr) { OS::get_singleton()->print("[%s:%d] delete BitStreamQueue %d : KO\n", __FILE__, __LINE__, i); //memdelete(this->_msgin[i]); //this->_msgin[i] = nullptr; } else { OS::get_singleton()->print("[%s:%d] delete BitStreamQueue %d : ok\n", __FILE__, __LINE__, i); } } } void BitStreamQueue::clear() { for(int i = 0 ; i < SIZE_QUEUE_MESSAGE ; ++i ) { this->_received_number[i] = 0; //this->_msgin[i].nref(); this->_msgin[i]->clear(); } this->_size = 0; } int BitStreamQueue::length() { return this->_size; } int BitStreamQueue::put_msgbytes(PoolByteArray msgbytes) { int i = 0; int ii ; while((i < SIZE_QUEUE_MESSAGE)&&(this->_msgin[i]->size_data() != 0)) ++i; if(i >= SIZE_QUEUE_MESSAGE) // Out of memory { ERR_PRINTS("Network queue is full (" + itos(i) + " / " + itos(SIZE_QUEUE_MESSAGE) + ")"); throw "Out of memory"; } this->_msgin[i]->put_data(msgbytes); uint32_t current_received_number = this->_msgin[i]->get_uint32(); this->_received_number[i] = current_received_number; // Check we don't have other message with same _received_number (also remove it) ii = i + 1; while(ii < SIZE_QUEUE_MESSAGE) { if ( this->_received_number[ii] == current_received_number) this->erase(ii); ++ii; } this->_size ++; return i; } Ref BitStreamQueue::get_msg(int pos) { //Ref ret; if ( pos >= SIZE_QUEUE_MESSAGE ) { ERR_PRINT("Try to get data out of memory"); throw "Out of memory"; } //ret.instance(); //*ret = &(this->_msgin[pos]); //return ret; //return Ref(this->_msgin[pos]); //return & (this->_msgin[pos]); return this->_msgin[pos]; } uint32_t BitStreamQueue::get_received_number(int pos) { if ( pos >= SIZE_QUEUE_MESSAGE ) { ERR_PRINT("Try to get data out of memory"); throw "Out of memory"; } return this->_received_number[pos]; } void BitStreamQueue::erase(int pos) { if ( pos >= SIZE_QUEUE_MESSAGE ) { ERR_PRINT("Try to erase data out of memory"); return; } this->_received_number[pos] = 0; this->_msgin[pos]->clear(); this->_size -- ; }