source: libcaca/trunk/caca/driver_network.c @ 584

Last change on this file since 584 was 584, checked in by Sam Hocevar, 15 years ago
  • Handle incoming connections in network_get_event() as well as network_display() so that new clients immediately get the contents of the canvas and do not need to wait for a caca_display() call.
  • Property svn:keywords set to Id
File size: 6.2 KB
Line 
1/*
2 *  libcaca       ASCII-Art library
3 *  Copyright (c) 2002-2006 Sam Hocevar <sam@zoy.org>
4 *                All Rights Reserved
5 *
6 *  This library is free software; you can redistribute it and/or
7 *  modify it under the terms of the Do What The Fuck You Want To
8 *  Public License, Version 2, as published by Sam Hocevar. See
9 *  http://sam.zoy.org/wtfpl/COPYING for more details.
10 */
11
12/** \file driver_network.c
13 *  \version \$Id: driver_network.c 584 2006-03-10 13:06:40Z sam $
14 *  \author Jean-Yves Lamoureux <jylam@lnxscene.org>
15 *  \brief Network driver
16 *
17 *  This file contains the libcaca network input and output driver
18 */
19
20#include "config.h"
21
22#if defined(USE_NETWORK)
23#include <stdio.h>
24#include <stdlib.h>
25#include <sys/types.h>
26#include <sys/socket.h>
27#include <arpa/inet.h>
28#include <fcntl.h>
29#include <string.h>
30
31#if defined(HAVE_UNISTD_H)
32#   include <unistd.h>
33#endif
34#include <stdarg.h>
35
36#include "caca.h"
37#include "caca_internals.h"
38#include "cucul.h"
39#include "cucul_internals.h"
40
41static void manage_connections(caca_t *kk);
42static int send_data(caca_t *kk, int fd);
43
44
45struct driver_private
46{
47    unsigned int width, height;
48    unsigned int port;
49    int sockfd;
50    struct sockaddr_in my_addr;
51    struct sockaddr_in remote_addr;
52    socklen_t sin_size;
53    int clilen;
54
55    char *buffer;
56    int size;
57
58    int client_count;
59    int *fd_list;
60};
61
62
63
64#define BACKLOG 1337    /* Number of pending connections */
65
66
67
68/* Following vars are static */
69static char codes[] = {0xff, 0xfb, 0x01,  // WILL ECHO                                                             
70                       0xff, 0xfb, 0x03,  // WILL SUPPRESS GO AHEAD
71                       0xff, 253, 31,     // DO NAWS
72                       0xff, 254, 31,     // DON'T NAWS
73                       0xff, 31, 250, 0, 30, 0, 0xFF, // to be replaced
74                       0xff, 240};
75
76
77static int network_init_graphics(caca_t *kk)
78{
79    int yes=1;
80
81    printf("Initing network stack.\n");
82
83    kk->drv.p = malloc(sizeof(struct driver_private));
84    if(kk->drv.p == NULL)
85        return -1;
86
87    kk->drv.p->width = 80;
88    kk->drv.p->height = 24;
89    kk->drv.p->port = 7575; // 75 75 decimal ASCII -> KK   // FIXME, sadly
90    kk->drv.p->client_count = 0;
91    kk->drv.p->fd_list = NULL;
92
93
94
95    _cucul_set_size(kk->qq, kk->drv.p->width, kk->drv.p->height);
96
97
98    printf("socket\n");
99    if ((kk->drv.p->sockfd = socket(PF_INET, SOCK_STREAM, 0)) == -1) {
100        perror("socket");
101        return -1;
102    }
103    printf("setsockopt\n");
104    if (setsockopt(kk->drv.p->sockfd,SOL_SOCKET,SO_REUSEADDR,&yes,sizeof(int)) == -1) {
105        perror("setsockopt");
106        return -1;
107    }
108
109    kk->drv.p->my_addr.sin_family = AF_INET;
110    kk->drv.p-> my_addr.sin_port = htons(kk->drv.p->port);
111    kk->drv.p->my_addr.sin_addr.s_addr = INADDR_ANY;
112    memset(&(kk->drv.p->my_addr.sin_zero), '\0', 8);
113
114    printf("bind\n");
115    if (bind(kk->drv.p->sockfd, (struct sockaddr *)&kk->drv.p->my_addr, sizeof(struct sockaddr))
116                                                                   == -1) {
117        perror("bind");
118        return -1;
119    }
120
121    /* Non blocking socket */
122    fcntl(kk->drv.p->sockfd, F_SETFL, O_NONBLOCK);
123
124
125    printf("listen\n");
126    if (listen(kk->drv.p->sockfd, BACKLOG) == -1) {
127        perror("listen");
128        return -1;
129    }
130
131    printf("network ok.\n");
132
133    kk->drv.p->buffer = NULL;
134
135    return 0;
136}
137
138static int network_end_graphics(caca_t *kk)
139{
140    int i;
141
142    for(i = 0; i < kk->drv.p->client_count; i++) {
143        close(kk->drv.p->fd_list[i]);
144    }
145
146    return 0;
147}
148
149static int network_set_window_title(caca_t *kk, char const *title)
150{
151    /* Not handled (yet)*/
152    return 0;
153}
154
155static unsigned int network_get_window_width(caca_t *kk)
156{
157    return kk->drv.p->width * 6;
158}
159
160static unsigned int network_get_window_height(caca_t *kk)
161{
162    return kk->drv.p->height * 10;
163}
164
165static void network_display(caca_t *kk)
166{
167    int i;
168
169    kk->drv.p->buffer = cucul_get_ansi(kk->qq, 0, &kk->drv.p->size);;
170
171    for(i = 0; i < kk->drv.p->client_count; i++)
172    {
173        if(send_data(kk, kk->drv.p->fd_list[i]))
174            kk->drv.p->fd_list[i] = -1;
175    }
176
177    manage_connections(kk);
178}
179 
180static void network_handle_resize(caca_t *kk)
181{
182    /* Not handled */
183}
184
185static unsigned int network_get_event(caca_t *kk)
186{
187    manage_connections(kk);
188
189    /* Not handled */
190    return 0;
191}
192
193/*
194 * XXX: The following functions are local
195 */
196
197static void manage_connections(caca_t *kk)
198{
199    int fd;
200
201    kk->drv.p->clilen = sizeof(kk->drv.p->remote_addr);
202    fd = accept(kk->drv.p->sockfd, (struct sockaddr *) &kk->drv.p->remote_addr, &kk->drv.p->clilen);
203    if(fd != -1)
204    {
205        if(kk->drv.p->fd_list == NULL)
206        {
207            kk->drv.p->fd_list = malloc(sizeof(int));
208            if(kk->drv.p->fd_list == NULL)
209                return;
210        }
211        else
212        {
213            kk->drv.p->fd_list = realloc(kk->drv.p->fd_list, (kk->drv.p->client_count+1) * sizeof(int));
214        }
215
216        if(send_data(kk, fd) == 0)
217        {
218            kk->drv.p->fd_list[kk->drv.p->client_count] = fd;
219            kk->drv.p->client_count++;
220        }
221    }
222}
223
224static int send_data(caca_t *kk, int fd)
225{
226    /* No error, there's just nothing to send yet */
227    if(!kk->drv.p->buffer)
228        return 0;
229
230    if(fd < 0)
231        return -1;
232
233    /* FIXME, handle >255 sizes */
234    codes[16] = (unsigned char) kk->drv.p->width & 0xff;
235    codes[18] = (unsigned char) kk->drv.p->height & 0xff;
236           
237    /* Send basic telnet codes */
238    if (send(fd, codes,sizeof(codes) , 0) == -1)
239        return -1;
240   
241    /* ANSI code for move(0,0)*/
242    if (send(fd, "\033[1,1H", 6, 0) == -1)
243        return -1;
244   
245    if (send(fd, kk->drv.p->buffer, kk->drv.p->size, 0) == -1)
246        return -1;
247
248    return 0;
249}
250
251/*
252 * Driver initialisation
253 */
254
255void network_init_driver(caca_t *kk)
256{
257    kk->drv.driver = CACA_DRIVER_NETWORK;
258
259    kk->drv.init_graphics = network_init_graphics;
260    kk->drv.end_graphics = network_end_graphics;
261    kk->drv.set_window_title = network_set_window_title;
262    kk->drv.get_window_width = network_get_window_width;
263    kk->drv.get_window_height = network_get_window_height;
264    kk->drv.display = network_display;
265    kk->drv.handle_resize = network_handle_resize;
266    kk->drv.get_event = network_get_event;
267}
268
269#endif // USE_NETWORK
270
Note: See TracBrowser for help on using the repository browser.