|
Allocating Buffer Memory |
Top Previous Next |
The first step is to allocate memory for a single buffer required by the wln object. This buffer is used to form outgoing packets and is necessary for correct operation. You never have to deal with this buffer directly -- it is handled internally by the wln object.
Buffer memory 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. Request the size you need in pages using the wln.buffrq method.
The allocation method (sys.buffalloc) applies to all buffers previously specified, in one fell swoop:
Dim x As Byte x = wln.buffrq(6) ' request 6 pages for the wln buffer. Out will then contain how many can actually be allocated. ' .... Allocation requests for buffers of other objects .... sys.buffalloc 'perform 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.
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 for 10%.
Current wln buffer size in bytes can always be checked with the wln.buffsize read-only property.
Note that wln buffer size can't be changed when the Wi-Fi hardware is already booted.
How many pages should the wln buffer get?
The size of the wln buffer directly dictates the maximum size of network packets that the wln object will be able to send (this buffer has nothing to do with incoming packets). Up to 100 bytes of the buffer space are required for various packet headers, and the rest is available to packet payload. For example, if you have allocated 2 pages for the buffer, then the buffer size is 512 bytes. Hence, maximum payload size cannot exceed 412 bytes.
For TCP communications, the size of individual packets is not that critical. The beauty of TCP is that it can work with whatever buffer space is available. It is true that the bigger the buffer, the better TCP throughput is. In reality, you will stop feeling any improvement in TCP performance once your wln buffer size exceeds 2 or 3 pages.
The UDP is another matter entirely. If you want to be able to send the packets of a certain size, then you must make sure that you have created an adequate wln buffer. For example, the DHCP protocol is based on UDP packets. The size of UDP-DHCP packets can be as large as 1400 bytes. If you are planning to use DHCP on the wln interface, you will have to allocate the wln buffer that allows the UDP packets with the payload of at least 1400 bytes. Therefore, you will need at least 1500 bytes of buffer space. Round this to 256-byte pages, and we arrive at required buffer size of 6 pages.