Changeset 575 for libcaca


Ignore:
Timestamp:
Mar 9, 2006, 8:27:14 PM (17 years ago)
Author:
Jean-Yves Lamoureux
Message:
  • Multiplexed sockets, it is now possible to have multiple clients at the same time watching for the same libcaca application. And no fork or threads involved. THAT'S high technology.
Location:
libcaca/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • libcaca/trunk/build-kernel

    r573 r575  
    3434
    3535# For further development: create floppy images using the kernel
    36 #gcc -traditional -c -o bootsect.o bootsect.S
    37 #ld -Ttext 0x0 -s --oformat binary bootsect.o -o cacafire.img
     36gcc -traditional -c -o bootsect.o /usr/src/linux/arch/i386/boot/bootsect.S
     37ld -Ttext 0x0 -s --oformat binary bootsect.o -o cacafire.img
    3838
  • libcaca/trunk/caca/driver_network.c

    r560 r575  
    2626#include <sys/socket.h>
    2727#include <arpa/inet.h>
     28#include <fcntl.h>
    2829#include <string.h>
    2930
     
    5051    int clilen;
    5152    char buffer[256];
     53
     54    int client_count;
     55    int *fd_list;
     56
    5257};
    5358
     
    7075{
    7176    int yes=1;
     77
    7278    printf("Initing network stack.\n");
    7379
    7480    kk->drv.p = malloc(sizeof(struct driver_private));
    75 
     81    if(kk->drv.p == NULL)
     82        return -1;
    7683
    7784    kk->drv.p->width = 80;
    7885    kk->drv.p->height = 24;
    7986    kk->drv.p->port = 7575; // 75 75 decimal ASCII -> KK   // FIXME, sadly
     87    kk->drv.p->client_count = 0;
     88    kk->drv.p->fd_list = NULL;
     89
    8090
    8191
     
    93103        return -1;
    94104    }
    95 
    96105
    97106    kk->drv.p->my_addr.sin_family = AF_INET;
     
    106115        return -1;
    107116    }
     117
     118    /* Non blocking socket */
     119    fcntl(kk->drv.p->sockfd, F_SETFL, O_NONBLOCK);
     120
     121
    108122    printf("listen\n");
    109123    if (listen(kk->drv.p->sockfd, BACKLOG) == -1) {
     
    112126    }
    113127
    114     printf("accept\n");
     128    printf("network ok.\n");
     129
     130    return 0;
     131}
     132
     133static int network_end_graphics(caca_t *kk)
     134{
     135    int i;
     136    for(i = 0; i < kk->drv.p->client_count; i++) {
     137        close(kk->drv.p->fd_list[i]);
     138    }
     139    return 0;
     140}
     141
     142static int network_set_window_title(caca_t *kk, char const *title)
     143{
     144    /* Not handled (yet)*/
     145    return 0;
     146}
     147
     148static unsigned int network_get_window_width(caca_t *kk)
     149{
     150    return kk->drv.p->width * 6;
     151}
     152
     153static unsigned int network_get_window_height(caca_t *kk)
     154{
     155    return kk->drv.p->height * 10;
     156}
     157
     158static void network_display(caca_t *kk)
     159{
     160    int size, i;
     161    char *to_send = cucul_get_ansi(kk->qq, 0, &size);;
     162 
    115163    kk->drv.p->clilen = sizeof(kk->drv.p->remote_addr);
    116164    kk->drv.p->new_fd = accept(kk->drv.p->sockfd, (struct sockaddr *) &kk->drv.p->remote_addr, &kk->drv.p->clilen);
    117     if (kk->drv.p->new_fd < 0) {
    118         perror("ERROR on accept");
    119         return -1;
    120     }
    121 
    122 
    123     printf("Got connexion from %d.%d.%d.%d\n",
    124            (unsigned int)((kk->drv.p->remote_addr.sin_addr.s_addr)&0x000000FF),
    125            (unsigned int)((kk->drv.p->remote_addr.sin_addr.s_addr)&0x0000FF00)>>8,
    126            (unsigned int)((kk->drv.p->remote_addr.sin_addr.s_addr)&0x00FF0000)>>16,
    127            (unsigned int)((kk->drv.p->remote_addr.sin_addr.s_addr)&0xFF000000)>>24);
     165    if(kk->drv.p->new_fd != -1)
     166        {
     167
     168            if(kk->drv.p->fd_list == NULL) {
     169                kk->drv.p->fd_list = malloc(sizeof(int));
     170                if(kk->drv.p->fd_list == NULL)
     171                    return;
     172                kk->drv.p->fd_list[kk->drv.p->client_count] = kk->drv.p->new_fd;
     173            } else {
     174                kk->drv.p->fd_list = realloc(kk->drv.p->fd_list, (kk->drv.p->client_count+1) * sizeof(int));
     175                kk->drv.p->fd_list[kk->drv.p->client_count] = kk->drv.p->new_fd;
     176            }
     177
     178            kk->drv.p->client_count++;
     179
     180        }
     181
     182    for(i = 0; i < kk->drv.p->client_count; i++) {
     183        if(kk->drv.p->fd_list[i] == -1)
     184           continue;
     185       
     186        /* FIXME, handle >255 sizes */
     187            codes[16] = (unsigned char) kk->drv.p->width&0xff;
     188            codes[18] = (unsigned char) kk->drv.p->height&0xff;
     189           
     190            /* Send basic telnet codes */
     191            if (send(kk->drv.p->fd_list[i], codes,sizeof(codes) , 0) == -1) {
     192                kk->drv.p->fd_list[i] = -1;
     193             }
     194           
     195            /* ANSI code for move(0,0)*/
     196            if (send(kk->drv.p->fd_list[i], "\033[1,1H", 6, 0) == -1) {
     197                kk->drv.p->fd_list[i] = -1;
     198            }
     199           
     200            if (send(kk->drv.p->fd_list[i], to_send, size, 0) == -1) {
     201                kk->drv.p->fd_list[i] = -1;
     202            }
     203        }
    128204   
    129     /* FIXME, handle >255 sizes */
    130     codes[16] = (unsigned char) kk->drv.p->width&0xff;
    131     codes[18] = (unsigned char) kk->drv.p->height&0xff;
    132 
    133     /* Send basic telnet codes */
    134     if (send(kk->drv.p->new_fd, codes,sizeof(codes) , 0) == -1) {
    135         perror("send");
    136         return -1;
    137     }
    138 
    139     printf("network ok.\n");
    140 
    141     return 0;
    142 }
    143 
    144 static int network_end_graphics(caca_t *kk)
    145 {
    146     printf("network end graphics\n");
    147     return 0;
    148 }
    149 
    150 static int network_set_window_title(caca_t *kk, char const *title)
    151 {
    152     printf("network_set_window_title(%s) not implemented yet.\n", title);
    153     return 0;
    154 }
    155 
    156 static unsigned int network_get_window_width(caca_t *kk)
    157 {
    158     return kk->drv.p->width * 6;
    159 }
    160 
    161 static unsigned int network_get_window_height(caca_t *kk)
    162 {
    163     return kk->drv.p->height * 10;
    164 }
    165 
    166 static void network_display(caca_t *kk)
    167 {
    168     int size;
    169     char *to_send = cucul_get_ansi(kk->qq, 0, &size);;
    170  
    171    
    172     /* ANSI code for move(0,0)*/
    173     if (send(kk->drv.p->new_fd, "\033[1,1H", 6, 0) == -1) {
    174         perror("send");
    175         return;
    176     }
    177    
    178     if (send(kk->drv.p->new_fd, to_send, size, 0) == -1) {
    179         perror("send");
    180         return;
    181     }
    182 
    183205}
    184206static void network_handle_resize(caca_t *kk)
    185207{
    186     printf("Resize\n");
    187  
     208    /* Not handled */
    188209}
    189210
    190211static unsigned int network_get_event(caca_t *kk)
    191212{
     213    /* Not handled */
    192214    return 0;
    193215}
     
    213235
    214236#endif // USE_NETWORK
     237
Note: See TracChangeset for help on using the changeset viewer.