NIVeriStandAdd-Ons / UDP-Data-Link-Custom-Device

6 stars 14 forks source link

Circular Buffer 2-D #67

Open djd5560 opened 3 years ago

djd5560 commented 3 years ago

Hello,

I've implemented a few different instances of buffers in other programming languages, and I've always understood circular buffers as a 2-D matrix where the rows are the length of the maximum message to be received, and then the column being the depth of the buffer (or vice versa). Once a message is read in the buffer (assumption of read each cycle if buffer is not empty), the next message moves up the queue and an empty message is moved to the back. Another means is by just overwriting each message as the buffer fills, instead of deleting and re-adding an empty column.

The only reason I mark this as a potential implementation, is because using the circular buffer, I'm occasionally seeing collided data (data seems to be from two different packets). When compared to wireshark, i'm not seeing corrupt packets, or invalid data. My thought was a split packet due to overflow, but the code description explicitly states a packet will not be split. My thought is to separate the packets entirely, and then we will not see the potential corruption.

ryanvallieres commented 2 years ago

Hey djd5560

So, your understanding of how circular buffers are implemented is kind of similar to what we're doing here. When you have a 2D array in C (effectively a double pointer), you would get this behavior. However, at the end of the day, it's all just sequentially allocated memory and can be accessed in a very similar way using a single pointer. Regardless, I chose to use a slightly different implementation for this. We instead allocate a fixed-length single dimension block of memory (a pointer) and then buffer data in there using the inbound data size information. This allows for both transmit and receive to have support for multiple message lengths without any unused blocks of memory. It effectively acts as a 2D array, with each row in the array having a flexible size, instead of a fixed size.

To your other point/issue - this has been a recurring problem for some users of late. I'm hoping to get some time to add support for better inbound/outbound port sharing.