Allocating Memory for Buffers
Each buffer has a certain size, i.e, a memory capacity. This capacity is allocated upon request from your program. When the device initially boots, no memory is allocated to buffers at all.
Memory for buffers is allocated in pages. A page is 256 bytes of memory. Allocating memory for a buffer is a two-step process: first you have to request for a specific allocation (a number of pages) and then you have to perform the actual allocation.
For the sock. object to be able to send and receive data, you have to give its TX and RX buffers some memory. This is done through the sock.txbuffrq and sock.rxbuffrq methods.
The allocation method (sys.buffalloc) applies to all buffers previously specified, in one fell swoop.
Hence:
dim in, out as byte
out = sock.txbuffrq(10) ' Requesting 10 pages for the TX buffer. Out will then contain how many can actually be allocated.
in = sock.rxbuffrq(7) ' Requesting 7 pages for the RX buffer. Will return number of pages which can actually be allocated.
' ... Allocation requests for other buffers...
sys.buffalloc ' Performs actual memory allocation, as per previous requests.
Actual memory allocation takes up to 100ms, so it is usually done just once, on boot, for all required buffers. If you do not require some buffer, you may choose not to allocate any memory to it. In effect, it will be disabled.
You may not always get the full amount of memory you have requested. Memory is not an infinite resource, and if you have already requested (and received) allocations for 95% of the memory for your platform, your next request will get up to 5% of memory, even if you requested 10%.
There is a small overhead for each buffer. Meaning, not 100% of the memory allocated to a buffer is actually available for use. A certain number of bytes in each buffer is reserved for variables needed to administer this buffer. This number is 17 for 16-bit platforms and 33 for 32-bit platforms. The platform type is specified in your platform documentation.
Thus, if we requested (and received) a buffer with 2 pages (256 * 2 = 512 bytes), then we can only store 512 - 16 or 512 - 32 bytes in this buffer, depending on the platform type.
If you are changing the size of any buffer for a socket using sys.buffalloc and this socket is not closed (sock.statesimple <> PL_SSTS_CLOSED), the socket will be automatically closed. Whatever connection you had in progress will be discarded. The socket will not be closed if its buffer sizes remain unchanged.