adding step to store message received (use to synchronize)

This commit is contained in:
AleaJactaEst 2020-04-02 21:07:38 +02:00
parent 0e1db657d6
commit e40e7de052
5 changed files with 156 additions and 104 deletions

View file

@ -23,17 +23,17 @@ func test_write():
var bitset = BitSet.new()
print ("[bitset:test_put]")
bitset.resize(1024)
#print ("[bitset:test_put] " + bitset.show())
print ("[bitset:test_put] " + bitset.show())
assert( bitset.show() == "0000000000000000000000000000010000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000")
bitset.write(1, 1)
#print ("[bitset:test_put] " + bitset.show())
print ("[bitset:test_put] " + bitset.show())
assert( bitset.show() == "0000000000000000000000000000010000000000000000000000000000000000001000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000")
bitset.write(2, 1)
#print ("[bitset:test_put] " + bitset.show())
print ("[bitset:test_put] " + bitset.show())
assert( bitset.show() == "0000000000000000000000000000010000000000000000000000000000000000001000000000000000000000000000000000011000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000")
bitset.write(1, 0)
#print ("[bitset:test_put] " + bitset.show())
assert( bitset.show() == "0000000000000000000000000000010000000000000000000000000000000000001000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000")
print ("[bitset:test_put] " + bitset.show())
assert( bitset.show() == "0000000000000000000000000000010000000000000000000000000000000000001000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000")
func test_alloc():
#var BitSet = preload("res://bitset.gdns")

View file

@ -63,12 +63,13 @@ int BitSet::size()
{
return this->_size_byte;
}
#include "core/os/os.h"
void BitSet::resize(uint32_t num_bits)
{
uint32_t n_last_bits;
uint32_t new_size_byte = (num_bits + BITSET_SIZE - 1) / BITSET_SIZE;
OS::get_singleton()->print((String("INFO [") + String(__FILE__) + String(":") + String(uitos(__LINE__)) + String("] Resize") + String("\n")).utf8());
new_size_byte = (num_bits + BITSET_SIZE - 1) / BITSET_SIZE;
if (this->_size_byte != new_size_byte )
{
@ -92,17 +93,20 @@ void BitSet::resize(uint32_t num_bits)
void BitSet::clear()
{
OS::get_singleton()->print((String("INFO [") + String(__FILE__) + String(":") + String(uitos(__LINE__)) + String("] Clear") + String("\n")).utf8());
this->resize(1);
}
void BitSet::clear_data()
{
OS::get_singleton()->print((String("INFO [") + String(__FILE__) + String(":") + String(uitos(__LINE__)) + String("] clear_data") + String("\n")).utf8());
for(uint32_t i = 0;i < this->_size_byte; ++i)
this->_data[i] = 0;
}
void BitSet::set_bit(uint32_t bit_number)
{
OS::get_singleton()->print((String("INFO [") + String(__FILE__) + String(":") + String(uitos(__LINE__)) + String("] set_bit") + String("\n")).utf8());
if (bit_number >= this->_num_bits)
{
ERR_PRINTS("[BitSet::set_bit] Out of range (size:" + itos(this->_size_byte) + ", pos:" + itos(bit_number) + ")" );
@ -115,6 +119,7 @@ void BitSet::set_bit(uint32_t bit_number)
void BitSet::clear_bit(uint32_t bit_number)
{
OS::get_singleton()->print((String("INFO [") + String(__FILE__) + String(":") + String(uitos(__LINE__)) + String("] clear_bit") + String("\n")).utf8());
if (bit_number >= this->_num_bits)
{
ERR_PRINTS("[BitSet::clear_bit] Out of range (size:" + itos(this->_size_byte) + ", pos:" + itos(bit_number) + ")" );
@ -122,7 +127,7 @@ void BitSet::clear_bit(uint32_t bit_number)
}
uint32_t mask = bit_number & (BITSET_SIZE - 1);
mask = 1 << mask;
this->_data[bit_number>>5] &= mask;
this->_data[bit_number>>5] &= ~ mask;
}
void BitSet::write(uint32_t bit_number, bool value)

View file

@ -21,7 +21,8 @@
#include "core/error_macros.h"
#ifdef DEBUG_ENABLED
#include "core/os/os.h"
#define DBG_PRINT(m_string) OS::get_singleton()->print((String("[") + String(__FILE__) + String(":") + String(uitos(__LINE__)) + String("] ") + String(m_string) + String("\n")).utf8())
#define DBG_PRINT(m_string) OS::get_singleton()->print((String("DEBUG [") + String(__FILE__) + String(":") + String(uitos(__LINE__)) + String("] ") + String(m_string) + String("\n")).utf8())
#else
#define DBG_PRINT(m_string)
#endif
#define INF_PRINT(m_string) OS::get_singleton()->print((String("INFO [") + String(__FILE__) + String(":") + String(uitos(__LINE__)) + String("] ") + String(m_string) + String("\n")).utf8())

View file

@ -22,12 +22,52 @@
#include "state_connexion.h"
#include "network_connection_core.h"
#include "modules/debug/debug.h"
#include "modules/networkconnection/network_data.h"
// We need managed when number 'uint32_t' exceed the capacity (32bits => 4294967296)
// So, we split in two group.
// < 0...1073741824 > < 1073741825 ... 3221225472 > < 3221225472 ... 4294967295 >
// Group 1 : < 0...1073741824 > + < 3221225472 ... 4294967295 > => number value : 2147483648
// Group 2 : < 1073741825 ... 3221225472 > => number value : 2147483648
inline uint32_t diff_uint32_circulate(uint32_t a, uint32_t b)
{
// We need managed when number 'uint32_t' exceed the capacity (32bits => 4294967296)
//
// So, we split in two group.
// < 0...1073741824 > < 1073741825 ... 3221225472 > < 3221225472 ... 4294967295 >
// Group 1 : < 0...1073741824 > + < 3221225472 ... 4294967295 > => number value : 2147483648
// Group 2 : < 1073741825 ... 3221225472 > => number value : 2147483648
//
if ( a >= 1073741825 && a <= 3221225472 )
return a - b;
a += 1073741825;
b += 1073741825;
return a - b;
}
inline void calculate_ack_bit(NetworkData * _data, bool ackBool)
{
// bool ackBool = (!_SystemMode && (_ConnectionState == Connected || _ConnectionState == Synchronize));
uint32_t i;
uint32_t ackBit = (ackBool ? 1 : 0);
if ( _data->_current_received_number - _data->_last_received_number < 32)
{
_data->_ack_bit_mask <<= _data->_current_received_number - _data->_last_received_number;
_data->_ack_bit_mask |= _data->_last_ack_bit << (_data->_current_received_number - _data->_last_received_number - 1);
}
else
{
_data->_ack_bit_mask = (_data->_current_received_number - _data->_last_received_number == 32 && _data->_last_ack_bit != 0) ? 0x80000000 : 0x00000000;
}
_data->_last_ack_bit = ackBit;
for(i=_data->_last_received_number+1;i<_data->_current_received_number;++i)
_data->_long_ack_bit_field.clear_bit(i & 511);
_data->_long_ack_bit_field.write(_data->_current_received_number & 0x1ff, ackBool);
if ( diff_uint32_circulate(_data->_last_ack_in_long_ack, _data->_current_received_number) > 512 )
{
_data->_last_ack_in_long_ack = _data->_current_received_number - 511;
}
_data->_last_received_number = _data->_current_received_number;
}
/*
* StateConnectionBase
@ -370,6 +410,8 @@ void StateConnectionConnected::receive_message(int index)
// SHOW_USAGE_MEMORY
bool system_mode;
//INF_PRINT("Receive application message");
Ref<BitStreamField> field = this->_network->_queue.get_msg(index);
uint32_t current_received_number = field->get_id();
@ -417,6 +459,10 @@ void StateConnectionConnected::receive_message(int index)
{
DBG_PRINT("Receive application message (" + itos(current_received_number) + ") [" + uitos(this->_network->_queue.length()) + "]");
}
calculate_ack_bit(this->_data, system_mode);
//DBG_PRINT("Ack:" + this->_data->_long_ack_bit_field.show());
this->_network->_queue.erase_index(index);
// Check in buffer if we have next
if ( this->_network->_queue.is_index(index + 1) )