Network buffer management. This module simplifies the use of multiple data buffers for sockets. Each socket gets its own buffer control structure and multiple buffers (e.g. for ping-pong-buffering) organized as ring buffers.
The buffers can be allocated dynamically or defined statically. A define USE_STATIC_MEMORY
forces this module to select the desired memory usage.
Per multi buffered socket two structures must be created in the user application. This is the main data buffer
and the buffer control structure
Refer to NetTest.c example or Echo.c example
E.g.:
After allocating a socket with socket_open(), the buffer control structure must be assigned to this socket
Initially NBufferAcquireBuffer() must be called for this socket to get a new buffer assigned to this socket:
After receiving new data (net_recv() returns a value > 0 or a callback function is called with new data), the user can decide if this buffer can be processed immediately and re-used for the next incoming data or if the packet must be processed later. In the latter case the current buffer index for later processing can be stored to
and a new function call to
will allocate and attach the next buffer.
If a buffer is waiting for processing, can be detected in your application by testing
After processing the cached data, this buffer must be freed with
This function call maintains the process index pi and sets BUFFER_EMPTY, if process index points to receive index.
Error handling for UDP and for TCP is a bit different. For both protocols a flag OutOfBuffer must be set immediately when function NBufferAcquireBuffer() returns no buffer. For TCP additionally a zero window packet must be sent to avoid a buffer overrun when the peer sends the next packet
In the main network task (this is where your net_isq() is called frequently) check the OutOfBuffer condition and try to allocate a new buffer