Task synchronization problem with recv()

Hello,
I’ve asked this question on reddit but i couldn’t get an answer,
I’m trying to make a multi-threaded program for a esp32 based custom made eink display controller board.

The point of the program is to mirror the pc screen so i wanted to take advantage of the 2 cores on the esp32 to write the frames to the display while downloading them, more specifically, downloading chunks of 3300 bytes at a time and feeding them to the display while the next chunk was downloaded.

the function to download looks like this:

volatile int renderer_chunk_counter = 0;
volatile int downloader_chunk_counter = 0;
static void receive_chunks(const int sock)
{
    for (int h = 0; h < 75; h++)
    {
        int tot = 0;
        int len = 0;
        memcpy(buffer_to_feed_display, buffer_to_receive_from_pc, 3300); //

        do
        {
            len = recv(sock, buffer_to_receive_from_pc + tot, 3300, 0);
            tot += len;
        } while (tot < 3300);
    }
    downloader_chunk_counter++;
}

the one to write to the display is like this:

void IRAM_ATTR write_to_display()
{
    while (1)
    {
        renderer_chunk_counter = 0;
        for (int h = 0; h < 75; h++)
        {
            // wait for next chunk to be available
            while (renderer_chunk_counter == downloader_chunk_counter)
            {
            };
            write_chunk_to_display();
            renderer_chunk_counter++;
        }
    }
}

The problem is that it seems that the recv function adds a lot of latency to the tasks’ synchronization. So if i remove the recv function write_to_display() takes only 20ms to complete, but even adding just a recv function for a transfer of 4 bytes inside the for loop ,( which makes it get executed 75 times), write_to_display() can take anywhere from 600ms to 2 seconds to complete. So i was wondering if there is a way to eliminate this latency or it is unavoidable because of the way wifi works.

thanks

have you tried to set your socket to nodelay mode (TCP sockets only) with setsockopt()?
May be also play with TCP_KEEPALIVE ?

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.