Ignore:
Timestamp:
Mar 6, 2010, 6:10:39 PM (12 years ago)
Author:
Sam Hocevar
Message:

Implement bidirectional communication in mini-neercs.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • neercs/trunk/src/mini-client.c

    r4379 r4380  
    1717#endif
    1818
     19#include <stdio.h> /* BUFSIZ */
     20#include <string.h> /* strncmp() */
     21
     22#include <caca.h>
     23
    1924#include "mini-neercs.h"
    2025#include "mini-socket.h"
    21 
    22 #include <caca.h>
    2326
    2427static caca_display_t *dp;
     
    2831void client_init(void)
    2932{
    30     int i, usec = 1000;
     33    int i, usec = 10000;
    3134
    3235    cv = caca_create_canvas(0, 0);
    3336    dp = caca_create_display(cv);
    3437    caca_set_display_title(dp, "Press Esc to quit");
     38
     39    insock = socket_open("/tmp/neercs.sock.client", 1);
    3540
    3641    for (i = 0; i < 10; i++)
     
    4348    }
    4449
    45     insock = socket_open("/tmp/neercs.sock.client", 1);
    46 
    4750    socket_puts(outsock, "CONNECT /tmp/neercs.sock.client");
    4851}
     
    5154{
    5255    caca_event_t ev;
     56    int ret;
     57
     58    /* Handle client sockets */
     59    ret = socket_select(insock, 1000);
     60    if (ret > 0)
     61    {
     62        char buf[BUFSIZ];
     63        ssize_t bytes = socket_read(insock, buf, BUFSIZ);
     64        if (bytes <= 0)
     65            return 1;
     66
     67        /* Parse message */
     68        if (!strncmp(buf, "OK", strlen("OK")))
     69        {
     70            printf("Connection established\n");
     71        }
     72    }
     73
     74    /* Handle libcaca events */
    5375    if(caca_get_event(dp, CACA_EVENT_KEY_PRESS, &ev, 1000)
    5476        && caca_get_event_key_ch(&ev) == CACA_KEY_ESCAPE)
    5577        return 0;
     78
    5679    return 1;
    5780}
     
    6184    caca_free_display(dp);
    6285    caca_free_canvas(cv);
     86    if (insock)
     87        socket_close(insock);
     88    if (outsock)
     89        socket_close(outsock);
    6390}
    6491
Note: See TracChangeset for help on using the changeset viewer.