Ignore:
Timestamp:
Jul 27, 2008, 4:33:30 AM (12 years ago)
Author:
Pascal Terjan
Message:
  • Add missing part in detach code (a lot of cleanup and fixes needed)
File:
1 edited

Legend:

Unmodified
Added
Removed
  • neercs/trunk/src/attach.c

    r2588 r2589  
    1616#include "neercs.h"
    1717
    18 char * build_socket_path(char *socket_dir, char *session_name)
     18char * build_socket_path(char *socket_dir, char *session_name, int client)
    1919{
    2020    char *path, *dir;
     
    2828        dir = "/tmp";
    2929    if(path)
    30         snprintf(path, PATH_MAX+1, "%s/neercs.%s.sock", dir, session_name);
     30        snprintf(path, PATH_MAX+1, "%s/neercs.%s%s.sock", dir, session_name, client?"-clt":"");
    3131    return path;
    3232}
    3333
    34 int create_socket(struct screen_list* screen_list)
     34int create_client_socket(struct screen_list* screen_list)
    3535{
    3636    int sock;
     
    4141    if(sock < 0)
    4242    {
    43         perror("create_socket:socket");
     43        perror("create_client_socket:socket");
    4444        return errno;
    4545    }
     
    4848
    4949    myaddr.sun_family = AF_UNIX;
    50     strncpy(myaddr.sun_path, screen_list->socket_path, sizeof(myaddr.sun_path) - 1);
     50    strncpy(myaddr.sun_path, screen_list->c_socket_path, sizeof(myaddr.sun_path) - 1);
     51
     52    unlink(screen_list->c_socket_path);
    5153
    5254    if(bind(sock, (struct sockaddr *)&myaddr, sizeof(struct sockaddr_un)) < 0)
    5355    {
    54         free(screen_list->socket_path);
    55         screen_list->socket_path = NULL;
     56        free(screen_list->c_socket_path);
     57        screen_list->c_socket_path = NULL;
     58        close(sock);
     59        perror("create_client_socket:bind");
     60        return errno;
     61    }
     62    fcntl(sock, F_SETFL, O_NONBLOCK);
     63
     64    debug("Client listening on %s (%d)", screen_list->c_socket_path, sock);
     65
     66    screen_list->c_socket = sock;
     67
     68    return 0;
     69}
     70
     71int create_server_socket(struct screen_list* screen_list)
     72{
     73    int sock;
     74    struct sockaddr_un myaddr;
     75
     76    sock = socket(AF_UNIX, SOCK_DGRAM, 0);
     77
     78    if(sock < 0)
     79    {
     80        perror("create_server_socket:socket");
     81        return errno;
     82    }
     83
     84    memset(&myaddr, 0, sizeof(struct sockaddr_un));
     85
     86    myaddr.sun_family = AF_UNIX;
     87    strncpy(myaddr.sun_path, screen_list->s_socket_path, sizeof(myaddr.sun_path) - 1);
     88
     89    unlink(screen_list->c_socket_path);
     90
     91    if(bind(sock, (struct sockaddr *)&myaddr, sizeof(struct sockaddr_un)) < 0)
     92    {
     93        free(screen_list->s_socket_path);
     94        screen_list->s_socket_path = NULL;
    5695        close(sock);
    5796        perror("create_socket:bind");
     
    6099    fcntl(sock, F_SETFL, O_NONBLOCK);
    61100
    62     debug("Listening on %s (%d)", screen_list->socket_path, sock);
    63 
    64     screen_list->socket = sock;
     101    debug("Client listening on %s (%d)", screen_list->s_socket_path, sock);
     102
     103    screen_list->s_socket = sock;
    65104
    66105    return 0;
     
    102141}
    103142
    104 char * connect_server(char *socket_path, struct screen_list* screen_list)
     143char * connect_server(struct screen_list* screen_list)
    105144{
    106145    int sock;
    107146    struct sockaddr_un addr;
    108 
    109     debug("Connecting to %s", socket_path);
     147    char *p, *s;
     148
     149    debug("Connecting to %s", screen_list->s_socket_path);
    110150
    111151    /* Open the socket */
    112152    if ((sock = socket(AF_UNIX, SOCK_DGRAM, 0)) < 0) {
    113         perror("request_attach:socket");
     153        perror("connect_server:socket");
    114154        return NULL;
    115155    }
     
    117157    memset(&addr,0,sizeof(addr));
    118158    addr.sun_family = AF_UNIX;
    119     strcpy(addr.sun_path,socket_path);
     159    strcpy(addr.sun_path,screen_list->s_socket_path);
    120160    if (connect(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0)
    121161    {
    122         printf("Failed to connect to %s: %s\n", socket_path, strerror(errno));
     162        fprintf(stderr, "Failed to connect to %s: %s\n", screen_list->s_socket_path, strerror(errno));
    123163        return NULL;
    124164    }
    125165    fcntl(sock, F_SETFL, O_NONBLOCK);
    126166
    127     screen_list->socket_path = socket_path;
    128     screen_list->socket = sock;
    129 
    130     return strdup(socket_path); /* FIXME */
     167    screen_list->s_socket = sock;
     168
     169    p = strrchr(screen_list->s_socket_path, '/');
     170    p+=8; /* skip neercs. */
     171    s = strdup(p);
     172    p = strrchr(s, '.');
     173    *p = '\0'; /* drop .sock */
     174    p = strdup(s);
     175    free(s);
     176    return p;
     177}
     178
     179int connect_client(struct screen_list* screen_list)
     180{
     181    int sock;
     182    struct sockaddr_un addr;
     183
     184    debug("Connecting to %s", screen_list->c_socket_path);
     185
     186    /* Open the socket */
     187    if ((sock = socket(AF_UNIX, SOCK_DGRAM, 0)) < 0) {
     188        perror("connect_client:socket");
     189        return 1;
     190    }
     191
     192    memset(&addr,0,sizeof(addr));
     193    addr.sun_family = AF_UNIX;
     194    strcpy(addr.sun_path,screen_list->c_socket_path);
     195    if (connect(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0)
     196    {
     197        fprintf(stderr, "Failed to connect to %s: %s\n", screen_list->c_socket_path, strerror(errno));
     198        return 1;
     199    }
     200    fcntl(sock, F_SETFL, O_NONBLOCK);
     201
     202    screen_list->c_socket = sock;
     203
     204    return 0;
    131205}
    132206
    133207int request_attach(struct screen_list* screen_list)
    134208{
    135     debug("Requesting attach");
    136 
    137     write(screen_list->socket, "ATTACH", strlen("ATTACH"));
    138 
    139     return 0;
    140 }
    141 
    142 int send_event(caca_event_t ev, int sock)
     209    char buf[32];
     210    int bytes;
     211
     212    bytes = snprintf(buf, sizeof(buf)-1, "ATTACH %10d%c%10d",
     213                     cucul_get_canvas_width(screen_list->cv),
     214                     ' ',
     215                     cucul_get_canvas_height(screen_list->cv));
     216    buf[bytes] = '\0';
     217    debug("Requesting attach: %s", buf);
     218    return write(screen_list->s_socket, buf, strlen(buf)) <= 0;
     219}
     220
     221int send_event(caca_event_t ev, struct screen_list* screen_list)
    143222{
    144223    enum caca_event_type t;
     
    153232        buf[bytes] = '\0';
    154233        debug("Sending key press to server: %s", buf);
    155         return write(sock, buf, strlen(buf)) <= 0;
     234        return write(screen_list->s_socket, buf, strlen(buf)) <= 0;
    156235    }
    157236    else if(t & CACA_EVENT_RESIZE)
    158         return write(sock, "RESIZE", strlen("RESIZE")) <= 0;
     237    {
     238        char buf[32];
     239        int bytes;
     240        bytes = snprintf(buf, sizeof(buf)-1, "RESIZE %10d%c%10d",
     241                         caca_get_event_resize_width(&ev),
     242                         ' ',
     243                         caca_get_event_resize_height(&ev));
     244        buf[bytes] = '\0';
     245        debug("Sending resize to server: %s", buf);
     246        return write(screen_list->s_socket, buf, strlen(buf)) <= 0;
     247    }
    159248    else if(t & CACA_EVENT_QUIT)
    160         return write(sock, "QUIT", strlen("QUIT")) <= 0;
    161 
    162     return 0;
    163 }
    164 
     249        return write(screen_list->s_socket, "QUIT", strlen("QUIT")) <= 0;
     250
     251    return 0;
     252}
     253
Note: See TracChangeset for help on using the changeset viewer.