Ignore:
Timestamp:
Nov 9, 2006, 6:24:29 PM (14 years ago)
Author:
Sam Hocevar
Message:
  • Don't try to guess the file format in cacaserver, instead feed the data to cucul_import_memory() and try again later if it says "not enough data".
File:
1 edited

Legend:

Unmodified
Added
Removed
  • libcaca/trunk/src/cacaserver.c

    r1308 r1315  
    196196    for(;;)
    197197    {
    198         uint8_t *buf = server->input;
    199         uint32_t control_size, data_size;
    200         unsigned int size;
    201 
     198restart:
    202199        /* Manage new connections as this function will be called sometimes
    203200         * more often than display */
     
    205202
    206203        /* Read data from stdin */
    207         read(0, buf, 12);
    208 
    209         while(buf[0] != 0xca || buf[1] != 0xca
    210                || buf[2] != 'C' || buf[3] != 'V')
    211         {
    212             memmove(buf, buf + 1, 11);
    213             read(0, buf + 11, 1);
    214         }
    215 
    216         control_size = ((uint32_t)buf[4] << 24) | ((uint32_t)buf[5] << 16)
    217                | ((uint32_t)buf[6] << 8) | (uint32_t)buf[7];
    218         data_size = ((uint32_t)buf[8] << 24) | ((uint32_t)buf[9] << 16)
    219                | ((uint32_t)buf[10] << 8) | (uint32_t)buf[11];
    220 
    221         size = 4 + control_size + data_size;
    222         buf = server->input = realloc(server->input, size);
    223         read(0, buf + 12, size - 12);
    224 
    225         if(cucul_import_memory(server->canvas, buf, size, "caca") < 0)
    226             continue; /* Load error */
    227 
     204        if(server->read < 12)
     205        {
     206            read(0, server->input + server->read, 12 - server->read);
     207            server->read = 12;
     208        }
     209
     210        while(cucul_import_memory(server->canvas, server->input,
     211                                  server->read, "caca") < 0)
     212        {
     213            memmove(server->input, server->input + 1, server->read - 1);
     214            read(0, server->input + server->read - 1, 1);
     215        }
     216
     217        for(;;)
     218        {
     219            long int needed;
     220            ssize_t wanted;
     221
     222            needed = cucul_import_memory(server->canvas, server->input,
     223                                         server->read, "caca");
     224            if(needed < 0)
     225                goto restart;
     226
     227            if(needed > 0)
     228            {
     229                server->read -= needed;
     230                memmove(server->input, server->input + needed, server->read);
     231                break;
     232            }
     233
     234            server->input = realloc(server->input, server->read + 128);
     235            wanted = read(0, server->input + server->read, 128);
     236            if(wanted < 0)
     237                goto restart;
     238            server->read += wanted;
     239        }
     240           
    228241        /* Free the previous export buffer, if any */
    229242        if(server->buffer)
Note: See TracChangeset for help on using the changeset viewer.