test-client-godot/modules/bitstreamqueue/bitstreamqueue.cpp
2020-03-03 22:45:30 +01:00

138 lines
No EOL
3.8 KiB
C++

/*
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 <http://www.gnu.org/licenses/>.
*/
#include "bitstreamqueue.h"
#include "core/os/os.h"
void BitStreamField::_bind_methods()
{
ClassDB::bind_method(D_METHOD("get_id"), &BitStreamField::get_id);
ClassDB::bind_method(D_METHOD("get_msgin"), &BitStreamField::get_msgin);
ClassDB::bind_method(D_METHOD("put_id", "id"), &BitStreamField::put_id);
ClassDB::bind_method(D_METHOD("put_msgin", "msgin"), &BitStreamField::put_msgin);
}
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", "index"), &BitStreamQueue::get_msg);
ClassDB::bind_method(D_METHOD("get_msg_withid", "id"), &BitStreamQueue::get_msg_withid);
ClassDB::bind_method(D_METHOD("put_bitstreamfield", "data"), &BitStreamQueue::put_bitstreamfield);
ClassDB::bind_method(D_METHOD("erase_index", "index"), &BitStreamQueue::erase_index);
ClassDB::bind_method(D_METHOD("erase_id", "id"), &BitStreamQueue::erase_id);
ClassDB::bind_method(D_METHOD("get_index", "id"), &BitStreamQueue::get_index);
ClassDB::bind_method(D_METHOD("is_index", "id"), &BitStreamQueue::is_index);
}
BitStreamQueue::BitStreamQueue()
{
OS::get_singleton()->print("[%s:%d] new BitStreamQueue\n", __FILE__, __LINE__);
this->clear();
}
BitStreamQueue::~BitStreamQueue()
{
OS::get_singleton()->print("[%s:%d] delete BitStreamQueue\n", __FILE__, __LINE__);
this->clear();
}
void BitStreamQueue::clear()
{
this->_msg.clear();
}
int BitStreamQueue::length()
{
return this->_msg.size();
}
int BitStreamQueue::get_index(uint32_t id)
{
for (int i = 0; i < this->_msg.size(); i++)
{
if (this->_msg[i]->get_id() == id)
{
return i;
};
};
return -1;
}
bool BitStreamQueue::is_index(uint32_t id)
{
for (int i = 0; i < this->_msg.size(); i++)
{
if (this->_msg[i]->get_id() == id)
{
return true;
};
};
return false;
}
int BitStreamQueue::put_bitstreamfield(Ref<BitStreamField> data)
{
int i = this->get_index(data->get_id());
if (i == -1)
{
this->_msg.push_back(data);
i = this->get_index(data->get_id());
}
return i;
}
Ref<BitStreamField> BitStreamQueue::get_msg_withid(uint32_t id)
{
for (int i = 0; i < this->_msg.size(); i++)
{
if (this->_msg[i]->get_id() == id)
{
return this->_msg[i];
};
};
return NULL;
}
Ref<BitStreamField> BitStreamQueue::get_msg(int index)
{
ERR_FAIL_INDEX_V(index, this->_msg.size(), NULL);
return this->_msg[index];
}
void BitStreamQueue::erase_index(int index)
{
ERR_FAIL_INDEX(index, this->_msg.size());
this->_msg.remove(index);
}
void BitStreamQueue::erase_id(uint32_t id)
{
for (int i = 0; i < this->_msg.size(); i++)
{
if (this->_msg[i]->get_id() == id) {
this->_msg.remove(i);
return;
};
};
}